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 | |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame^] | 428 | assert(mod); |
| 429 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 430 | for (i = 0; i < len; ++i) { |
| 431 | if (name[i] == ':') { |
| 432 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 433 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 434 | LY_CHECK_RET(!mod, NULL); |
| 435 | |
| 436 | name = &name[i + 1]; |
| 437 | len = len - i - 1; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 442 | if (mod->implemented) { |
| 443 | /* module is implemented so there is already the compiled schema */ |
| 444 | flist = mod->compiled->features; |
| 445 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 446 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 447 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 448 | LY_ARRAY_FOR(flist, u) { |
| 449 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 450 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 451 | return f; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return NULL; |
| 456 | } |
| 457 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 458 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 459 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 460 | */ |
| 461 | static LY_ERR |
| 462 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 463 | { |
| 464 | LY_ERR ret = LY_SUCCESS; |
| 465 | |
| 466 | if (!ext_p->compiled) { |
| 467 | lysc_update_path(ctx, NULL, "{extension}"); |
| 468 | lysc_update_path(ctx, NULL, ext_p->name); |
| 469 | |
| 470 | /* compile the extension definition */ |
| 471 | ext_p->compiled = calloc(1, sizeof **ext); |
| 472 | ext_p->compiled->refcount = 1; |
| 473 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 474 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 475 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 476 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 477 | |
| 478 | lysc_update_path(ctx, NULL, NULL); |
| 479 | lysc_update_path(ctx, NULL, NULL); |
| 480 | |
| 481 | /* find extension definition plugin */ |
| 482 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 483 | } |
| 484 | |
| 485 | *ext = lysc_ext_dup(ext_p->compiled); |
| 486 | |
| 487 | done: |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 492 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 493 | * |
| 494 | * @param[in] ctx Compilation context. |
| 495 | * @param[in] ext_p Parsed extension instance. |
| 496 | * @param[in,out] ext Prepared compiled extension instance. |
| 497 | * @param[in] parent Extension instance parent. |
| 498 | * @param[in] parent_type Extension instance parent type. |
| 499 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 500 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 501 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 502 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 503 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 504 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 505 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 506 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 507 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 508 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 509 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 510 | |
| 511 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 512 | ext->insubstmt = ext_p->insubstmt; |
| 513 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 514 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 515 | ext->parent = parent; |
| 516 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 517 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 518 | 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] | 519 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 520 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 521 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 522 | if (ext_p->yin) { |
| 523 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 524 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 525 | * YANG (import) prefix must be done here. */ |
| 526 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 527 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 528 | u = 0; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 529 | } else { |
| 530 | assert(ctx->mod_def->parsed); |
| 531 | LY_ARRAY_FOR(ctx->mod_def->parsed->imports, v) { |
| 532 | 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] | 533 | char *s; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 534 | 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] | 535 | ret = LY_EMEM, cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 536 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 537 | u = strlen(ctx->mod_def->parsed->imports[v].prefix) + 1; /* add semicolon */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | if (!prefixed_name) { |
| 543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 544 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 545 | goto cleanup; |
| 546 | } |
| 547 | } else { |
| 548 | prefixed_name = ext_p->name; |
| 549 | } |
| 550 | lysc_update_path(ctx, NULL, prefixed_name); |
| 551 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 552 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 553 | 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] | 554 | if (!ext_mod) { |
| 555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 556 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 557 | goto cleanup; |
| 558 | } else if (!ext_mod->parsed->extensions) { |
| 559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 560 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 561 | prefixed_name, ext_mod->name); |
| 562 | goto cleanup; |
| 563 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 564 | } |
| 565 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 566 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 567 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 568 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 569 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 570 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 571 | 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] | 572 | break; |
| 573 | } |
| 574 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 575 | if (!ext->def) { |
| 576 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 577 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 578 | goto cleanup; |
| 579 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 580 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 581 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 582 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 583 | * 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] | 584 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 585 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 586 | struct lysp_stmt *stmt = NULL; |
| 587 | |
| 588 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 589 | /* ... argument was the first XML child element */ |
| 590 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 591 | /* TODO check namespace of the statement */ |
| 592 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 593 | stmt = ext_p->child; |
| 594 | } |
| 595 | } |
| 596 | } else { |
| 597 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 598 | * with LYS_YIN_ATTR flag */ |
| 599 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 600 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 601 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 602 | break; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | if (!stmt) { |
| 607 | /* missing extension's argument */ |
| 608 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 609 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 610 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 611 | |
| 612 | } |
| 613 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 614 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 615 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 616 | if (prefixed_name != ext_p->name) { |
| 617 | lydict_remove(ctx->ctx, ext_p->name); |
| 618 | ext_p->name = prefixed_name; |
| 619 | if (!ext_p->argument && ext->argument) { |
| 620 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 621 | } |
| 622 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 623 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 624 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 625 | if (ext->argument) { |
| 626 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 627 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 628 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 629 | if (ext->argument) { |
| 630 | lysc_update_path(ctx, NULL, NULL); |
| 631 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 632 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 633 | ext_p->compiled = ext; |
| 634 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 635 | ret = LY_SUCCESS; |
| 636 | cleanup: |
| 637 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 638 | lydict_remove(ctx->ctx, prefixed_name); |
| 639 | } |
| 640 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 641 | lysc_update_path(ctx, NULL, NULL); |
| 642 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 643 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 644 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 648 | * @brief Compile information from the if-feature statement |
| 649 | * @param[in] ctx Compile context. |
| 650 | * @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] | 651 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 652 | * @return LY_ERR value. |
| 653 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 654 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 655 | 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] | 656 | { |
| 657 | const char *c = *value; |
| 658 | int r, rc = EXIT_FAILURE; |
| 659 | int i, j, last_not, checkversion = 0; |
| 660 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 661 | uint8_t op; |
| 662 | struct iff_stack stack = {0, 0, NULL}; |
| 663 | struct lysc_feature *f; |
| 664 | |
| 665 | assert(c); |
| 666 | |
| 667 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 668 | for (i = j = last_not = 0; c[i]; i++) { |
| 669 | if (c[i] == '(') { |
| 670 | j++; |
| 671 | checkversion = 1; |
| 672 | continue; |
| 673 | } else if (c[i] == ')') { |
| 674 | j--; |
| 675 | continue; |
| 676 | } else if (isspace(c[i])) { |
| 677 | checkversion = 1; |
| 678 | continue; |
| 679 | } |
| 680 | |
| 681 | 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] | 682 | int sp; |
| 683 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 684 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 686 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 687 | return LY_EVALID; |
| 688 | } else if (!isspace(c[i + r])) { |
| 689 | /* feature name starting with the not/and/or */ |
| 690 | last_not = 0; |
| 691 | f_size++; |
| 692 | } else if (c[i] == 'n') { /* not operation */ |
| 693 | if (last_not) { |
| 694 | /* double not */ |
| 695 | expr_size = expr_size - 2; |
| 696 | last_not = 0; |
| 697 | } else { |
| 698 | last_not = 1; |
| 699 | } |
| 700 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 701 | if (f_exp != f_size) { |
| 702 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 703 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 704 | return LY_EVALID; |
| 705 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 706 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 707 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 708 | /* not a not operation */ |
| 709 | last_not = 0; |
| 710 | } |
| 711 | i += r; |
| 712 | } else { |
| 713 | f_size++; |
| 714 | last_not = 0; |
| 715 | } |
| 716 | expr_size++; |
| 717 | |
| 718 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 719 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 720 | i--; |
| 721 | break; |
| 722 | } |
| 723 | i++; |
| 724 | } |
| 725 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 726 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 727 | /* not matching count of ( and ) */ |
| 728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 729 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 730 | return LY_EVALID; |
| 731 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 732 | if (f_exp != f_size) { |
| 733 | /* features do not match the needed arguments for the logical operations */ |
| 734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 735 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 736 | "the required number of operands for the operations.", *value); |
| 737 | return LY_EVALID; |
| 738 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 739 | |
| 740 | if (checkversion || expr_size > 1) { |
| 741 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 742 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 744 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 745 | return LY_EVALID; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /* allocate the memory */ |
| 750 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 751 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 752 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 753 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 754 | |
| 755 | stack.size = expr_size; |
| 756 | f_size--; expr_size--; /* used as indexes from now */ |
| 757 | |
| 758 | for (i--; i >= 0; i--) { |
| 759 | if (c[i] == ')') { |
| 760 | /* push it on stack */ |
| 761 | iff_stack_push(&stack, LYS_IFF_RP); |
| 762 | continue; |
| 763 | } else if (c[i] == '(') { |
| 764 | /* pop from the stack into result all operators until ) */ |
| 765 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 766 | iff_setop(iff->expr, op, expr_size--); |
| 767 | } |
| 768 | continue; |
| 769 | } else if (isspace(c[i])) { |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | /* end of operator or operand -> find beginning and get what is it */ |
| 774 | j = i + 1; |
| 775 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 776 | i--; |
| 777 | } |
| 778 | i++; /* go back by one step */ |
| 779 | |
| 780 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 781 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 782 | /* double not */ |
| 783 | iff_stack_pop(&stack); |
| 784 | } else { |
| 785 | /* not has the highest priority, so do not pop from the stack |
| 786 | * as in case of AND and OR */ |
| 787 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 788 | } |
| 789 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 790 | /* as for OR - pop from the stack all operators with the same or higher |
| 791 | * priority and store them to the result, then push the AND to the stack */ |
| 792 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 793 | op = iff_stack_pop(&stack); |
| 794 | iff_setop(iff->expr, op, expr_size--); |
| 795 | } |
| 796 | iff_stack_push(&stack, LYS_IFF_AND); |
| 797 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 798 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 799 | op = iff_stack_pop(&stack); |
| 800 | iff_setop(iff->expr, op, expr_size--); |
| 801 | } |
| 802 | iff_stack_push(&stack, LYS_IFF_OR); |
| 803 | } else { |
| 804 | /* feature name, length is j - i */ |
| 805 | |
| 806 | /* add it to the expression */ |
| 807 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 808 | |
| 809 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 810 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 811 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 812 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 813 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 814 | iff->features[f_size] = f; |
| 815 | LY_ARRAY_INCREMENT(iff->features); |
| 816 | f_size--; |
| 817 | } |
| 818 | } |
| 819 | while (stack.index) { |
| 820 | op = iff_stack_pop(&stack); |
| 821 | iff_setop(iff->expr, op, expr_size--); |
| 822 | } |
| 823 | |
| 824 | if (++expr_size || ++f_size) { |
| 825 | /* not all expected operators and operands found */ |
| 826 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 827 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 828 | rc = LY_EINT; |
| 829 | } else { |
| 830 | rc = LY_SUCCESS; |
| 831 | } |
| 832 | |
| 833 | error: |
| 834 | /* cleanup */ |
| 835 | iff_stack_clean(&stack); |
| 836 | |
| 837 | return rc; |
| 838 | } |
| 839 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 840 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 841 | * @brief Get the XPath context node for the given schema node. |
| 842 | * @param[in] start The schema node where the XPath expression appears. |
| 843 | * @return The context node to evaluate XPath expression in given schema node. |
| 844 | * @return NULL in case the context node is the root node. |
| 845 | */ |
| 846 | static struct lysc_node * |
| 847 | lysc_xpath_context(struct lysc_node *start) |
| 848 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 849 | 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] | 850 | start = start->parent); |
| 851 | return start; |
| 852 | } |
| 853 | |
| 854 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 855 | * @brief Compile information from the when statement |
| 856 | * @param[in] ctx Compile context. |
| 857 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 858 | * @param[in] flags Flags of the node with the "when" defiition. |
| 859 | * @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] | 860 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 861 | * @return LY_ERR value. |
| 862 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 863 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 864 | 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] | 865 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 866 | LY_ERR ret = LY_SUCCESS; |
| 867 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 868 | *when = calloc(1, sizeof **when); |
| 869 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 870 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 871 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 872 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 873 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 874 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 875 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 876 | 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] | 877 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 878 | |
| 879 | done: |
| 880 | return ret; |
| 881 | } |
| 882 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 883 | /** |
| 884 | * @brief Compile information from the must statement |
| 885 | * @param[in] ctx Compile context. |
| 886 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 887 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 888 | * @return LY_ERR value. |
| 889 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 890 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 891 | 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] | 892 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 893 | LY_ERR ret = LY_SUCCESS; |
| 894 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 895 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 896 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 897 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 898 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 899 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 900 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 901 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 902 | 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] | 903 | |
| 904 | done: |
| 905 | return ret; |
| 906 | } |
| 907 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 908 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 909 | * @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] | 910 | * @param[in] ctx Compile context. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 911 | * @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] | 912 | * @return LY_ERR value. |
| 913 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 914 | static LY_ERR |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 915 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 916 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 917 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 918 | LY_ERR ret = LY_SUCCESS; |
| 919 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 920 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 921 | * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added, |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 922 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 923 | if (!imp_p->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 924 | /* try to use filepath if present */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 925 | if (imp_p->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 926 | struct ly_in *in; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 927 | if (ly_in_new_filepath(imp_p->module->filepath, 0, &in)) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 928 | LOGINT(ctx->ctx); |
| 929 | } else { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 930 | 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] | 931 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 932 | if (mod != imp_p->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 933 | 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] | 934 | imp_p->module->filepath, imp_p->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 935 | mod = NULL; |
| 936 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 938 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 939 | } |
| 940 | if (!mod) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 941 | 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] | 942 | 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] | 943 | imp_p->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 944 | return LY_ENOTFOUND; |
| 945 | } |
| 946 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 949 | return ret; |
| 950 | } |
| 951 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 952 | LY_ERR |
| 953 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 954 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 955 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 956 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 957 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 958 | LY_ERR ret = LY_SUCCESS; |
| 959 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 960 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 961 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 962 | if (!ctx_sc) { |
| 963 | context.ctx = ctx; |
| 964 | context.mod = module; |
Radek Krejci | 120d854 | 2020-08-12 09:29:16 +0200 | [diff] [blame^] | 965 | context.mod_def = module; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 966 | context.path_len = 1; |
| 967 | context.path[0] = '/'; |
| 968 | ctx_sc = &context; |
| 969 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 970 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 971 | if (!identities_p) { |
| 972 | return LY_SUCCESS; |
| 973 | } |
| 974 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 975 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 979 | 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] | 980 | LY_ARRAY_FOR(identities_p, u) { |
| 981 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 982 | |
| 983 | LY_ARRAY_INCREMENT(*identities); |
| 984 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
| 985 | DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name); |
| 986 | DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc); |
| 987 | DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref); |
| 988 | (*identities)[offset + u].module = ctx_sc->mod; |
| 989 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 990 | lys_compile_iffeature, ret, done); |
| 991 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 992 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 993 | LYEXT_PAR_IDENT, ret, done); |
| 994 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 995 | |
| 996 | lysc_update_path(ctx_sc, NULL, NULL); |
| 997 | } |
| 998 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 999 | done: |
| 1000 | return ret; |
| 1001 | } |
| 1002 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1003 | /** |
| 1004 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1005 | * |
| 1006 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1007 | * |
| 1008 | * @param[in] ctx Compile context for logging. |
| 1009 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1010 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1011 | * @return LY_SUCCESS if everything is ok. |
| 1012 | * @return LY_EVALID if the identity is derived from itself. |
| 1013 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1014 | static LY_ERR |
| 1015 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1016 | { |
| 1017 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1018 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1019 | struct ly_set recursion = {0}; |
| 1020 | struct lysc_ident *drv; |
| 1021 | |
| 1022 | if (!derived) { |
| 1023 | return LY_SUCCESS; |
| 1024 | } |
| 1025 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1026 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1027 | if (ident == derived[u]) { |
| 1028 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1029 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1030 | goto cleanup; |
| 1031 | } |
| 1032 | ly_set_add(&recursion, derived[u], 0); |
| 1033 | } |
| 1034 | |
| 1035 | for (v = 0; v < recursion.count; ++v) { |
| 1036 | drv = recursion.objs[v]; |
| 1037 | if (!drv->derived) { |
| 1038 | continue; |
| 1039 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1040 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1041 | if (ident == drv->derived[u]) { |
| 1042 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1043 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1044 | goto cleanup; |
| 1045 | } |
| 1046 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1047 | } |
| 1048 | } |
| 1049 | ret = LY_SUCCESS; |
| 1050 | |
| 1051 | cleanup: |
| 1052 | ly_set_erase(&recursion, NULL); |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1056 | /** |
| 1057 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1058 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1059 | * 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] | 1060 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1061 | * to distinguish these two use cases. |
| 1062 | * |
| 1063 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1064 | * @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] | 1065 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1066 | * @param[in] bases Array of bases of identityref to fill in. |
| 1067 | * @return LY_ERR value. |
| 1068 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1069 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1070 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, |
| 1071 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1072 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1073 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1074 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1075 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1076 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1077 | |
| 1078 | assert(ident || bases); |
| 1079 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1080 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1081 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1082 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1083 | return LY_EVALID; |
| 1084 | } |
| 1085 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1086 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1087 | s = strchr(bases_p[u], ':'); |
| 1088 | if (s) { |
| 1089 | /* prefixed identity */ |
| 1090 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1091 | 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] | 1092 | } else { |
| 1093 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1094 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1095 | } |
| 1096 | if (!mod) { |
| 1097 | if (ident) { |
| 1098 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1099 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1100 | } else { |
| 1101 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1102 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1103 | } |
| 1104 | return LY_EVALID; |
| 1105 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1106 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1107 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1108 | if (mod->compiled) { |
| 1109 | identities = mod->compiled->identities; |
| 1110 | } else { |
| 1111 | identities = mod->dis_identities; |
| 1112 | } |
| 1113 | LY_ARRAY_FOR(identities, v) { |
| 1114 | if (!strcmp(name, identities[v].name)) { |
| 1115 | if (ident) { |
| 1116 | if (ident == &identities[v]) { |
| 1117 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1118 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1119 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1120 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1121 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1122 | /* we have match! store the backlink */ |
| 1123 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1124 | *idref = ident; |
| 1125 | } else { |
| 1126 | /* we have match! store the found identity */ |
| 1127 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1128 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1129 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1130 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | if (!idref || !(*idref)) { |
| 1134 | if (ident) { |
| 1135 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1136 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1137 | } else { |
| 1138 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1139 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1140 | } |
| 1141 | return LY_EVALID; |
| 1142 | } |
| 1143 | } |
| 1144 | return LY_SUCCESS; |
| 1145 | } |
| 1146 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1147 | /** |
| 1148 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1149 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1150 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1151 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1152 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1153 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1154 | static LY_ERR |
| 1155 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1156 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1157 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1158 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1159 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1160 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1161 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1162 | continue; |
| 1163 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1164 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1165 | 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] | 1166 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1167 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1168 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1169 | return LY_SUCCESS; |
| 1170 | } |
| 1171 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1172 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1173 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 1174 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1175 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1176 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1177 | struct lysc_ctx context = {0}; |
| 1178 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1179 | assert(ctx_sc || ctx); |
| 1180 | |
| 1181 | if (!ctx_sc) { |
| 1182 | context.ctx = ctx; |
| 1183 | context.mod = module; |
| 1184 | context.path_len = 1; |
| 1185 | context.path[0] = '/'; |
| 1186 | ctx_sc = &context; |
| 1187 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1188 | |
| 1189 | if (!features_p) { |
| 1190 | return LY_SUCCESS; |
| 1191 | } |
| 1192 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1193 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1196 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1197 | 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] | 1198 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1199 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1200 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1201 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1202 | 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] | 1203 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1204 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1205 | 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] | 1206 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1207 | (*features)[offset + u].module = ctx_sc->mod; |
| 1208 | |
| 1209 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1210 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1211 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1212 | |
| 1213 | return LY_SUCCESS; |
| 1214 | } |
| 1215 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1216 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1217 | * @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] | 1218 | * |
| 1219 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1220 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1221 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1222 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1223 | * being currently processed). |
| 1224 | * @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] | 1225 | * @return LY_SUCCESS if everything is ok. |
| 1226 | * @return LY_EVALID if the feature references indirectly itself. |
| 1227 | */ |
| 1228 | static LY_ERR |
| 1229 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1230 | { |
| 1231 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1232 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1233 | struct ly_set recursion = {0}; |
| 1234 | struct lysc_feature *drv; |
| 1235 | |
| 1236 | if (!depfeatures) { |
| 1237 | return LY_SUCCESS; |
| 1238 | } |
| 1239 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1240 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1241 | if (feature == depfeatures[u]) { |
| 1242 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1243 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1244 | goto cleanup; |
| 1245 | } |
| 1246 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1247 | } |
| 1248 | |
| 1249 | for (v = 0; v < recursion.count; ++v) { |
| 1250 | drv = recursion.objs[v]; |
| 1251 | if (!drv->depfeatures) { |
| 1252 | continue; |
| 1253 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1254 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1255 | if (feature == drv->depfeatures[u]) { |
| 1256 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1257 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1258 | goto cleanup; |
| 1259 | } |
| 1260 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1261 | } |
| 1262 | } |
| 1263 | ret = LY_SUCCESS; |
| 1264 | |
| 1265 | cleanup: |
| 1266 | ly_set_erase(&recursion, NULL); |
| 1267 | return ret; |
| 1268 | } |
| 1269 | |
| 1270 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1271 | * @brief Create pre-compiled features array. |
| 1272 | * |
| 1273 | * See lys_feature_precompile() for more details. |
| 1274 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1275 | * @param[in] ctx Compile context. |
| 1276 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1277 | * @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] | 1278 | * @return LY_ERR value. |
| 1279 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1280 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1281 | 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] | 1282 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1283 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1284 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1285 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1286 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1287 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1288 | /* find the preprecompiled feature */ |
| 1289 | LY_ARRAY_FOR(features, x) { |
| 1290 | if (strcmp(features[x].name, feature_p->name)) { |
| 1291 | continue; |
| 1292 | } |
| 1293 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1294 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1295 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1296 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1297 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1298 | 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] | 1299 | 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] | 1300 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1301 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1302 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1303 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1304 | /* check for circular dependency - direct reference first,... */ |
| 1305 | if (feature == feature->iffeatures[u].features[v]) { |
| 1306 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1307 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1308 | return LY_EVALID; |
| 1309 | } |
| 1310 | /* ... and indirect circular reference */ |
| 1311 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1312 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1313 | /* add itself into the dependants list */ |
| 1314 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1315 | *df = feature; |
| 1316 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1317 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1318 | } |
| 1319 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1320 | lysc_update_path(ctx, NULL, NULL); |
| 1321 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1322 | done: |
| 1323 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1324 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1325 | |
| 1326 | LOGINT(ctx->ctx); |
| 1327 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1328 | } |
| 1329 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1330 | /** |
| 1331 | * @brief Revert compiled list of features back to the precompiled state. |
| 1332 | * |
| 1333 | * 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] | 1334 | * 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] | 1335 | * |
| 1336 | * @param[in] ctx Compilation context. |
| 1337 | * @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] | 1338 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1339 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1340 | static void |
| 1341 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1342 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1343 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1344 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1345 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1346 | mod->dis_features = mod->compiled->features; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1347 | mod->compiled->features = NULL; |
| 1348 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1349 | /* 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] | 1350 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1351 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1352 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1353 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
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_FREE(mod->dis_features[u].iffeatures); |
| 1356 | mod->dis_features[u].iffeatures = NULL; |
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_FOR(mod->dis_features[u].exts, v) { |
| 1359 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1360 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1361 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1362 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1366 | /** |
| 1367 | * @brief Validate and normalize numeric value from a range definition. |
| 1368 | * @param[in] ctx Compile context. |
| 1369 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1370 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1371 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1372 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1373 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1374 | * @param[in] value String value of the range boundary. |
| 1375 | * @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. |
| 1376 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1377 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1378 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1379 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1380 | 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] | 1381 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1382 | size_t fraction = 0, size; |
| 1383 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1384 | *len = 0; |
| 1385 | |
| 1386 | assert(value); |
| 1387 | /* parse value */ |
| 1388 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1389 | return LY_EVALID; |
| 1390 | } |
| 1391 | |
| 1392 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1393 | ++(*len); |
| 1394 | } |
| 1395 | |
| 1396 | while (isdigit(value[*len])) { |
| 1397 | ++(*len); |
| 1398 | } |
| 1399 | |
| 1400 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1401 | if (basetype == LY_TYPE_DEC64) { |
| 1402 | goto decimal; |
| 1403 | } else { |
| 1404 | *valcopy = strndup(value, *len); |
| 1405 | return LY_SUCCESS; |
| 1406 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1407 | } |
| 1408 | fraction = *len; |
| 1409 | |
| 1410 | ++(*len); |
| 1411 | while (isdigit(value[*len])) { |
| 1412 | ++(*len); |
| 1413 | } |
| 1414 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1415 | if (basetype == LY_TYPE_DEC64) { |
| 1416 | decimal: |
| 1417 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1418 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1419 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1420 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1421 | *len, value, frdigits); |
| 1422 | return LY_EINVAL; |
| 1423 | } |
| 1424 | if (fraction) { |
| 1425 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1426 | } else { |
| 1427 | size = (*len) + frdigits + 1; |
| 1428 | } |
| 1429 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1430 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1431 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1432 | (*valcopy)[size - 1] = '\0'; |
| 1433 | if (fraction) { |
| 1434 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1435 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1436 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1437 | } else { |
| 1438 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1439 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1440 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1441 | } |
| 1442 | return LY_SUCCESS; |
| 1443 | } |
| 1444 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1445 | /** |
| 1446 | * @brief Check that values in range are in ascendant order. |
| 1447 | * @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] | 1448 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1449 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1450 | * @param[in] value Current value to check. |
| 1451 | * @param[in] prev_value The last seen value. |
| 1452 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1453 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1454 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1455 | 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] | 1456 | { |
| 1457 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1458 | 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] | 1459 | return LY_EEXIST; |
| 1460 | } |
| 1461 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1462 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1463 | return LY_EEXIST; |
| 1464 | } |
| 1465 | } |
| 1466 | return LY_SUCCESS; |
| 1467 | } |
| 1468 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1469 | /** |
| 1470 | * @brief Set min/max value of the range part. |
| 1471 | * @param[in] ctx Compile context. |
| 1472 | * @param[in] part Range part structure to fill. |
| 1473 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1474 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1475 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1476 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1477 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1478 | * @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] | 1479 | * @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] | 1480 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1481 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1482 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1483 | * frdigits value), LY_EMEM. |
| 1484 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1485 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1486 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, |
| 1487 | int first, int length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1488 | { |
| 1489 | LY_ERR ret = LY_SUCCESS; |
| 1490 | char *valcopy = NULL; |
| 1491 | size_t len; |
| 1492 | |
| 1493 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1494 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1495 | LY_CHECK_GOTO(ret, finalize); |
| 1496 | } |
| 1497 | if (!valcopy && base_range) { |
| 1498 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1499 | 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] | 1500 | } else { |
| 1501 | part->min_64 = base_range->parts[0].min_64; |
| 1502 | } |
| 1503 | if (!first) { |
| 1504 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1505 | } |
| 1506 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1510 | case LY_TYPE_INT8: /* range */ |
| 1511 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1512 | 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] | 1513 | } else if (max) { |
| 1514 | part->max_64 = INT64_C(127); |
| 1515 | } else { |
| 1516 | part->min_64 = INT64_C(-128); |
| 1517 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1518 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1519 | 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] | 1520 | } |
| 1521 | break; |
| 1522 | case LY_TYPE_INT16: /* range */ |
| 1523 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1524 | 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] | 1525 | } else if (max) { |
| 1526 | part->max_64 = INT64_C(32767); |
| 1527 | } else { |
| 1528 | part->min_64 = INT64_C(-32768); |
| 1529 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1530 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1531 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1532 | } |
| 1533 | break; |
| 1534 | case LY_TYPE_INT32: /* range */ |
| 1535 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1536 | 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] | 1537 | } else if (max) { |
| 1538 | part->max_64 = INT64_C(2147483647); |
| 1539 | } else { |
| 1540 | part->min_64 = INT64_C(-2147483648); |
| 1541 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1542 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1543 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1544 | } |
| 1545 | break; |
| 1546 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1547 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1548 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1549 | 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] | 1550 | max ? &part->max_64 : &part->min_64); |
| 1551 | } else if (max) { |
| 1552 | part->max_64 = INT64_C(9223372036854775807); |
| 1553 | } else { |
| 1554 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1555 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1556 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1557 | 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] | 1558 | } |
| 1559 | break; |
| 1560 | case LY_TYPE_UINT8: /* range */ |
| 1561 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1562 | 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] | 1563 | } else if (max) { |
| 1564 | part->max_u64 = UINT64_C(255); |
| 1565 | } else { |
| 1566 | part->min_u64 = UINT64_C(0); |
| 1567 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1568 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1569 | 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] | 1570 | } |
| 1571 | break; |
| 1572 | case LY_TYPE_UINT16: /* range */ |
| 1573 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1574 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1575 | } else if (max) { |
| 1576 | part->max_u64 = UINT64_C(65535); |
| 1577 | } else { |
| 1578 | part->min_u64 = UINT64_C(0); |
| 1579 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1580 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1581 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1582 | } |
| 1583 | break; |
| 1584 | case LY_TYPE_UINT32: /* range */ |
| 1585 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1586 | 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] | 1587 | } else if (max) { |
| 1588 | part->max_u64 = UINT64_C(4294967295); |
| 1589 | } else { |
| 1590 | part->min_u64 = UINT64_C(0); |
| 1591 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1592 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1593 | 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] | 1594 | } |
| 1595 | break; |
| 1596 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1597 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1598 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1599 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1600 | 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] | 1601 | } else if (max) { |
| 1602 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1603 | } else { |
| 1604 | part->min_u64 = UINT64_C(0); |
| 1605 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1606 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1607 | 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] | 1608 | } |
| 1609 | break; |
| 1610 | default: |
| 1611 | LOGINT(ctx->ctx); |
| 1612 | ret = LY_EINT; |
| 1613 | } |
| 1614 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1615 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1616 | if (ret == LY_EDENIED) { |
| 1617 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1618 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1619 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1620 | } else if (ret == LY_EVALID) { |
| 1621 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1622 | "Invalid %s restriction - invalid value \"%s\".", |
| 1623 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1624 | } else if (ret == LY_EEXIST) { |
| 1625 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1626 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1627 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1628 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1629 | } else if (!ret && value) { |
| 1630 | *value = *value + len; |
| 1631 | } |
| 1632 | free(valcopy); |
| 1633 | return ret; |
| 1634 | } |
| 1635 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1636 | /** |
| 1637 | * @brief Compile the parsed range restriction. |
| 1638 | * @param[in] ctx Compile context. |
| 1639 | * @param[in] range_p Parsed range structure to compile. |
| 1640 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1641 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1642 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1643 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1644 | * range restriction must be more restrictive than the base_range. |
| 1645 | * @param[in,out] range Pointer to the created current range structure. |
| 1646 | * @return LY_ERR value. |
| 1647 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1648 | static LY_ERR |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 1649 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, |
| 1650 | uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1651 | { |
| 1652 | LY_ERR ret = LY_EVALID; |
| 1653 | const char *expr; |
| 1654 | struct lysc_range_part *parts = NULL, *part; |
| 1655 | int range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1656 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1657 | |
| 1658 | assert(range); |
| 1659 | assert(range_p); |
| 1660 | |
| 1661 | expr = range_p->arg; |
| 1662 | while(1) { |
| 1663 | if (isspace(*expr)) { |
| 1664 | ++expr; |
| 1665 | } else if (*expr == '\0') { |
| 1666 | if (range_expected) { |
| 1667 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1668 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1669 | length_restr ? "length" : "range", range_p->arg); |
| 1670 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1671 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1673 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1674 | length_restr ? "length" : "range", range_p->arg); |
| 1675 | goto cleanup; |
| 1676 | } |
| 1677 | parts_done++; |
| 1678 | break; |
| 1679 | } else if (!strncmp(expr, "min", 3)) { |
| 1680 | if (parts) { |
| 1681 | /* min cannot be used elsewhere than in the first part */ |
| 1682 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1683 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1684 | expr - range_p->arg, range_p->arg); |
| 1685 | goto cleanup; |
| 1686 | } |
| 1687 | expr += 3; |
| 1688 | |
| 1689 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1690 | 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] | 1691 | part->max_64 = part->min_64; |
| 1692 | } else if (*expr == '|') { |
| 1693 | if (!parts || range_expected) { |
| 1694 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1695 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1696 | goto cleanup; |
| 1697 | } |
| 1698 | expr++; |
| 1699 | parts_done++; |
| 1700 | /* process next part of the expression */ |
| 1701 | } else if (!strncmp(expr, "..", 2)) { |
| 1702 | expr += 2; |
| 1703 | while (isspace(*expr)) { |
| 1704 | expr++; |
| 1705 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1706 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1707 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1708 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1709 | goto cleanup; |
| 1710 | } |
| 1711 | /* continue expecting the upper boundary */ |
| 1712 | range_expected = 1; |
| 1713 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1714 | /* number */ |
| 1715 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1716 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1717 | 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] | 1718 | range_expected = 0; |
| 1719 | } else { |
| 1720 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1721 | 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] | 1722 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1723 | part->max_64 = part->min_64; |
| 1724 | } |
| 1725 | |
| 1726 | /* continue with possible another expression part */ |
| 1727 | } else if (!strncmp(expr, "max", 3)) { |
| 1728 | expr += 3; |
| 1729 | while (isspace(*expr)) { |
| 1730 | expr++; |
| 1731 | } |
| 1732 | if (*expr != '\0') { |
| 1733 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1734 | length_restr ? "length" : "range", expr); |
| 1735 | goto cleanup; |
| 1736 | } |
| 1737 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1738 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1739 | 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] | 1740 | range_expected = 0; |
| 1741 | } else { |
| 1742 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1743 | 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] | 1744 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1745 | part->min_64 = part->max_64; |
| 1746 | } |
| 1747 | } else { |
| 1748 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1749 | length_restr ? "length" : "range", expr); |
| 1750 | goto cleanup; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | /* check with the previous range/length restriction */ |
| 1755 | if (base_range) { |
| 1756 | switch (basetype) { |
| 1757 | case LY_TYPE_BINARY: |
| 1758 | case LY_TYPE_UINT8: |
| 1759 | case LY_TYPE_UINT16: |
| 1760 | case LY_TYPE_UINT32: |
| 1761 | case LY_TYPE_UINT64: |
| 1762 | case LY_TYPE_STRING: |
| 1763 | uns = 1; |
| 1764 | break; |
| 1765 | case LY_TYPE_DEC64: |
| 1766 | case LY_TYPE_INT8: |
| 1767 | case LY_TYPE_INT16: |
| 1768 | case LY_TYPE_INT32: |
| 1769 | case LY_TYPE_INT64: |
| 1770 | uns = 0; |
| 1771 | break; |
| 1772 | default: |
| 1773 | LOGINT(ctx->ctx); |
| 1774 | ret = LY_EINT; |
| 1775 | goto cleanup; |
| 1776 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1777 | 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] | 1778 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1779 | goto baseerror; |
| 1780 | } |
| 1781 | /* current lower bound is not lower than the base */ |
| 1782 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1783 | /* base has single value */ |
| 1784 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1785 | /* both lower bounds are the same */ |
| 1786 | if (parts[u].min_64 != parts[u].max_64) { |
| 1787 | /* current continues with a range */ |
| 1788 | goto baseerror; |
| 1789 | } else { |
| 1790 | /* equal single values, move both forward */ |
| 1791 | ++v; |
| 1792 | continue; |
| 1793 | } |
| 1794 | } else { |
| 1795 | /* base is single value lower than current range, so the |
| 1796 | * value from base range is removed in the current, |
| 1797 | * move only base and repeat checking */ |
| 1798 | ++v; |
| 1799 | --u; |
| 1800 | continue; |
| 1801 | } |
| 1802 | } else { |
| 1803 | /* base is the range */ |
| 1804 | if (parts[u].min_64 == parts[u].max_64) { |
| 1805 | /* current is a single value */ |
| 1806 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1807 | /* current is behind the base range, so base range is omitted, |
| 1808 | * move the base and keep the current for further check */ |
| 1809 | ++v; |
| 1810 | --u; |
| 1811 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1812 | continue; |
| 1813 | } else { |
| 1814 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1815 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1816 | /* higher bound is higher than the current higher bound */ |
| 1817 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1818 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1819 | * continue with the same current, but move the base */ |
| 1820 | --u; |
| 1821 | ++v; |
| 1822 | continue; |
| 1823 | } |
| 1824 | /* current range starts within the base range but end behind it */ |
| 1825 | goto baseerror; |
| 1826 | } else { |
| 1827 | /* current range is smaller than the base, |
| 1828 | * move current, but stay with the base */ |
| 1829 | continue; |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | if (u != parts_done) { |
| 1835 | baseerror: |
| 1836 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1837 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1838 | length_restr ? "length" : "range", range_p->arg); |
| 1839 | goto cleanup; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | if (!(*range)) { |
| 1844 | *range = calloc(1, sizeof **range); |
| 1845 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1846 | } |
| 1847 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1848 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1849 | if (range_p->eapptag) { |
| 1850 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1851 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1852 | } |
| 1853 | if (range_p->emsg) { |
| 1854 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1855 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1856 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1857 | if (range_p->dsc) { |
| 1858 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1859 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1860 | } |
| 1861 | if (range_p->ref) { |
| 1862 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1863 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1864 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1865 | /* extensions are taken only from the last range by the caller */ |
| 1866 | |
| 1867 | (*range)->parts = parts; |
| 1868 | parts = NULL; |
| 1869 | ret = LY_SUCCESS; |
| 1870 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1871 | LY_ARRAY_FREE(parts); |
| 1872 | |
| 1873 | return ret; |
| 1874 | } |
| 1875 | |
| 1876 | /** |
| 1877 | * @brief Checks pattern syntax. |
| 1878 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1879 | * @param[in] ctx Context. |
| 1880 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1881 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1882 | * @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] | 1883 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1884 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1885 | LY_ERR |
| 1886 | 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] | 1887 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1888 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1889 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1890 | int err_code; |
| 1891 | const char *orig_ptr; |
| 1892 | PCRE2_SIZE err_offset; |
| 1893 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1894 | #define URANGE_LEN 19 |
| 1895 | char *ublock2urange[][2] = { |
| 1896 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1897 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1898 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1899 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1900 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1901 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1902 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1903 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1904 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1905 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1906 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1907 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1908 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1909 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1910 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1911 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1912 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1913 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1914 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1915 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1916 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1917 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1918 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1919 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1920 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1921 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1922 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1923 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1924 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1925 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1926 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1927 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1928 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1929 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1930 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1931 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1932 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1933 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1934 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1935 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1936 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1937 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1938 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1939 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1940 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1941 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1942 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1943 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1944 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1945 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1946 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1947 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1948 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1949 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1950 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1951 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1952 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1953 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1954 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1955 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1956 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1957 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1958 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1959 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1960 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1961 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1962 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1963 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1964 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1965 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1966 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1967 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1968 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1969 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1970 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1971 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1972 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1973 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1974 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1975 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1976 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1977 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1978 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1979 | {NULL, NULL} |
| 1980 | }; |
| 1981 | |
| 1982 | /* adjust the expression to a Perl equivalent |
| 1983 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1984 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1985 | /* allocate space for the transformed pattern */ |
| 1986 | size = strlen(pattern) + 1; |
| 1987 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1988 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1989 | perl_regex[0] = '\0'; |
| 1990 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1991 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1992 | brack = 0; |
| 1993 | idx = 0; |
| 1994 | orig_ptr = pattern; |
| 1995 | while (orig_ptr[0]) { |
| 1996 | switch (orig_ptr[0]) { |
| 1997 | case '$': |
| 1998 | case '^': |
| 1999 | if (!brack) { |
| 2000 | /* make space for the extra character */ |
| 2001 | ++size; |
| 2002 | perl_regex = ly_realloc(perl_regex, size); |
| 2003 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2004 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2005 | /* print escape slash */ |
| 2006 | perl_regex[idx] = '\\'; |
| 2007 | ++idx; |
| 2008 | } |
| 2009 | break; |
| 2010 | case '[': |
| 2011 | /* must not be escaped */ |
| 2012 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2013 | ++brack; |
| 2014 | } |
| 2015 | break; |
| 2016 | case ']': |
| 2017 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2018 | /* pattern was checked and compiled already */ |
| 2019 | assert(brack); |
| 2020 | --brack; |
| 2021 | } |
| 2022 | break; |
| 2023 | default: |
| 2024 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2025 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2026 | |
| 2027 | /* copy char */ |
| 2028 | perl_regex[idx] = orig_ptr[0]; |
| 2029 | |
| 2030 | ++idx; |
| 2031 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2032 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2033 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2034 | |
| 2035 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2036 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2037 | start = ptr - perl_regex; |
| 2038 | |
| 2039 | ptr = strchr(ptr, '}'); |
| 2040 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2041 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2042 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2043 | free(perl_regex); |
| 2044 | return LY_EVALID; |
| 2045 | } |
| 2046 | end = (ptr - perl_regex) + 1; |
| 2047 | |
| 2048 | /* need more space */ |
| 2049 | if (end - start < URANGE_LEN) { |
| 2050 | 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] | 2051 | 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] | 2052 | } |
| 2053 | |
| 2054 | /* find our range */ |
| 2055 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2056 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2057 | break; |
| 2058 | } |
| 2059 | } |
| 2060 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2061 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2062 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2063 | free(perl_regex); |
| 2064 | return LY_EVALID; |
| 2065 | } |
| 2066 | |
| 2067 | /* 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] | 2068 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 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 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2073 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2074 | } |
| 2075 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2076 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2077 | /* skip brackets */ |
| 2078 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2079 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2080 | } else { |
| 2081 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2082 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2087 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2088 | 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] | 2089 | &err_code, &err_offset, NULL); |
| 2090 | if (!code_local) { |
| 2091 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2092 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | 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] | 2094 | free(perl_regex); |
| 2095 | return LY_EVALID; |
| 2096 | } |
| 2097 | free(perl_regex); |
| 2098 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2099 | if (code) { |
| 2100 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2101 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2102 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | return LY_SUCCESS; |
| 2106 | |
| 2107 | #undef URANGE_LEN |
| 2108 | } |
| 2109 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2110 | /** |
| 2111 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2112 | * @param[in] ctx Compile context. |
| 2113 | * @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] | 2114 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2115 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2116 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2117 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2118 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2119 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2120 | 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] | 2121 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2122 | { |
| 2123 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2124 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2125 | LY_ERR ret = LY_SUCCESS; |
| 2126 | |
| 2127 | /* first, copy the patterns from the base type */ |
| 2128 | if (base_patterns) { |
| 2129 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2130 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2131 | } |
| 2132 | |
| 2133 | LY_ARRAY_FOR(patterns_p, u) { |
| 2134 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2135 | *pattern = calloc(1, sizeof **pattern); |
| 2136 | ++(*pattern)->refcount; |
| 2137 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2138 | 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] | 2139 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2140 | |
| 2141 | if (patterns_p[u].arg[0] == 0x15) { |
| 2142 | (*pattern)->inverted = 1; |
| 2143 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2144 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2145 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2146 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2147 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2148 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2149 | 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] | 2150 | } |
| 2151 | done: |
| 2152 | return ret; |
| 2153 | } |
| 2154 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2155 | /** |
| 2156 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2157 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2158 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2159 | 0 /* LY_TYPE_UNKNOWN */, |
| 2160 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2161 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2162 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2163 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2164 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2165 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2166 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2167 | 0 /* LY_TYPE_BOOL */, |
| 2168 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2169 | 0 /* LY_TYPE_EMPTY */, |
| 2170 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2171 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2172 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2173 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2174 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2175 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2176 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2177 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2178 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2179 | }; |
| 2180 | |
| 2181 | /** |
| 2182 | * @brief stringification of the YANG built-in data types |
| 2183 | */ |
| 2184 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2185 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2186 | "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] | 2187 | }; |
| 2188 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2189 | /** |
| 2190 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2191 | * @param[in] ctx Compile context. |
| 2192 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2193 | * @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] | 2194 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2195 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2196 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2197 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2198 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2199 | 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] | 2200 | 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] | 2201 | { |
| 2202 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2203 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2204 | int32_t value = 0; |
| 2205 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2206 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2207 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2208 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2209 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2210 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2211 | return LY_EVALID; |
| 2212 | } |
| 2213 | |
| 2214 | LY_ARRAY_FOR(enums_p, u) { |
| 2215 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2216 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2217 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2218 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2219 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2220 | if (base_enums) { |
| 2221 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2222 | LY_ARRAY_FOR(base_enums, v) { |
| 2223 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2224 | break; |
| 2225 | } |
| 2226 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2227 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2228 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2229 | "Invalid %s - derived type adds new item \"%s\".", |
| 2230 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2231 | return LY_EVALID; |
| 2232 | } |
| 2233 | match = v; |
| 2234 | } |
| 2235 | |
| 2236 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2237 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2238 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2239 | e->value = (int32_t)enums_p[u].value; |
| 2240 | if (!u || e->value >= value) { |
| 2241 | value = e->value + 1; |
| 2242 | } |
| 2243 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2244 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2245 | if (e->value == (*enums)[v].value) { |
| 2246 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2247 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2248 | e->value, e->name, (*enums)[v].name); |
| 2249 | return LY_EVALID; |
| 2250 | } |
| 2251 | } |
| 2252 | } else if (base_enums) { |
| 2253 | /* inherit the assigned value */ |
| 2254 | e->value = base_enums[match].value; |
| 2255 | if (!u || e->value >= value) { |
| 2256 | value = e->value + 1; |
| 2257 | } |
| 2258 | } else { |
| 2259 | /* assign value automatically */ |
| 2260 | if (u && value == INT32_MIN) { |
| 2261 | /* counter overflow */ |
| 2262 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2263 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2264 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2265 | return LY_EVALID; |
| 2266 | } |
| 2267 | e->value = value++; |
| 2268 | } |
| 2269 | } else { /* LY_TYPE_BITS */ |
| 2270 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2271 | e->value = (int32_t)enums_p[u].value; |
| 2272 | if (!u || (uint32_t)e->value >= position) { |
| 2273 | position = (uint32_t)e->value + 1; |
| 2274 | } |
| 2275 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2276 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2277 | if (e->value == (*enums)[v].value) { |
| 2278 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2279 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2280 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2281 | return LY_EVALID; |
| 2282 | } |
| 2283 | } |
| 2284 | } else if (base_enums) { |
| 2285 | /* inherit the assigned value */ |
| 2286 | e->value = base_enums[match].value; |
| 2287 | if (!u || (uint32_t)e->value >= position) { |
| 2288 | position = (uint32_t)e->value + 1; |
| 2289 | } |
| 2290 | } else { |
| 2291 | /* assign value automatically */ |
| 2292 | if (u && position == 0) { |
| 2293 | /* counter overflow */ |
| 2294 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2295 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2296 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2297 | return LY_EVALID; |
| 2298 | } |
| 2299 | e->value = position++; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | if (base_enums) { |
| 2304 | /* the assigned values must not change from the derived type */ |
| 2305 | if (e->value != base_enums[match].value) { |
| 2306 | if (basetype == LY_TYPE_ENUM) { |
| 2307 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2308 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2309 | e->name, base_enums[match].value, e->value); |
| 2310 | } else { |
| 2311 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2312 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2313 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2314 | } |
| 2315 | return LY_EVALID; |
| 2316 | } |
| 2317 | } |
| 2318 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2319 | 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] | 2320 | 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] | 2321 | |
| 2322 | if (basetype == LY_TYPE_BITS) { |
| 2323 | /* keep bits ordered by position */ |
| 2324 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2325 | if (v != u) { |
| 2326 | memcpy(&storage, e, sizeof *e); |
| 2327 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2328 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2329 | } |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | done: |
| 2334 | return ret; |
| 2335 | } |
| 2336 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2337 | /** |
| 2338 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2339 | * |
| 2340 | * path-arg = absolute-path / relative-path |
| 2341 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2342 | * relative-path = 1*(".." "/") descendant-path |
| 2343 | * |
| 2344 | * @param[in,out] path Path to parse. |
| 2345 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2346 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2347 | * @param[out] name Name of the token. |
| 2348 | * @param[out] nam_len Length of the name. |
| 2349 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2350 | * must not be changed between consecutive calls. -1 if the |
| 2351 | * path is absolute. |
| 2352 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2353 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2354 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2355 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2356 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2357 | int *parent_times, int *has_predicate) |
| 2358 | { |
| 2359 | int par_times = 0; |
| 2360 | |
| 2361 | assert(path && *path); |
| 2362 | assert(parent_times); |
| 2363 | assert(prefix); |
| 2364 | assert(prefix_len); |
| 2365 | assert(name); |
| 2366 | assert(name_len); |
| 2367 | assert(has_predicate); |
| 2368 | |
| 2369 | *prefix = NULL; |
| 2370 | *prefix_len = 0; |
| 2371 | *name = NULL; |
| 2372 | *name_len = 0; |
| 2373 | *has_predicate = 0; |
| 2374 | |
| 2375 | if (!*parent_times) { |
| 2376 | if (!strncmp(*path, "..", 2)) { |
| 2377 | *path += 2; |
| 2378 | ++par_times; |
| 2379 | while (!strncmp(*path, "/..", 3)) { |
| 2380 | *path += 3; |
| 2381 | ++par_times; |
| 2382 | } |
| 2383 | } |
| 2384 | if (par_times) { |
| 2385 | *parent_times = par_times; |
| 2386 | } else { |
| 2387 | *parent_times = -1; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | if (**path != '/') { |
| 2392 | return LY_EINVAL; |
| 2393 | } |
| 2394 | /* skip '/' */ |
| 2395 | ++(*path); |
| 2396 | |
| 2397 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2398 | 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] | 2399 | |
| 2400 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2401 | /* path continues by another token or this is the last token */ |
| 2402 | return LY_SUCCESS; |
| 2403 | } else if ((*path)[0] != '[') { |
| 2404 | /* unexpected character */ |
| 2405 | return LY_EINVAL; |
| 2406 | } else { |
| 2407 | /* predicate starting with [ */ |
| 2408 | *has_predicate = 1; |
| 2409 | return LY_SUCCESS; |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2414 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2415 | * |
| 2416 | * The set of features used for target must be a subset of features used for the leafref. |
| 2417 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2418 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2419 | * |
| 2420 | * @param[in] refnode The leafref node. |
| 2421 | * @param[in] target Tha target node of the leafref. |
| 2422 | * @return LY_SUCCESS or LY_EVALID; |
| 2423 | */ |
| 2424 | static LY_ERR |
| 2425 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2426 | { |
| 2427 | LY_ERR ret = LY_EVALID; |
| 2428 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2429 | LY_ARRAY_COUNT_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2430 | struct ly_set features = {0}; |
| 2431 | |
| 2432 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2433 | if (iter->iffeatures) { |
| 2434 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2435 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2436 | 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] | 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2443 | * So when adding features applicable to the target into the features set, we should not be |
| 2444 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2445 | * to the leafref itself. */ |
| 2446 | count = features.count; |
| 2447 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2448 | if (iter->iffeatures) { |
| 2449 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2450 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2451 | 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] | 2452 | /* new feature was added (or LY_EMEM) */ |
| 2453 | goto cleanup; |
| 2454 | } |
| 2455 | } |
| 2456 | } |
| 2457 | } |
| 2458 | } |
| 2459 | ret = LY_SUCCESS; |
| 2460 | |
| 2461 | cleanup: |
| 2462 | ly_set_erase(&features, NULL); |
| 2463 | return ret; |
| 2464 | } |
| 2465 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2466 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2467 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2468 | struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2469 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2470 | /** |
| 2471 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2472 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2473 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2474 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2475 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2476 | * @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] | 2477 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2478 | * @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] | 2479 | * @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] | 2480 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2481 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2482 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2483 | * @return LY_ERR value. |
| 2484 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2485 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2486 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2487 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2488 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2489 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2490 | { |
| 2491 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2492 | struct lysc_type_bin *bin; |
| 2493 | struct lysc_type_num *num; |
| 2494 | struct lysc_type_str *str; |
| 2495 | struct lysc_type_bits *bits; |
| 2496 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2497 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2498 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2499 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2500 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2501 | |
| 2502 | switch (basetype) { |
| 2503 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2504 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2505 | |
| 2506 | /* 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] | 2507 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2508 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2509 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2510 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2511 | 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] | 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | if (tpdfname) { |
| 2516 | type_p->compiled = *type; |
| 2517 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2518 | } |
| 2519 | break; |
| 2520 | case LY_TYPE_BITS: |
| 2521 | /* RFC 7950 9.7 - bits */ |
| 2522 | bits = (struct lysc_type_bits*)(*type); |
| 2523 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2524 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2525 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2526 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2527 | } |
| 2528 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2529 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2530 | /* 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] | 2531 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2532 | 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] | 2533 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2534 | 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] | 2535 | free(*type); |
| 2536 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2537 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2538 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | if (tpdfname) { |
| 2542 | type_p->compiled = *type; |
| 2543 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2544 | } |
| 2545 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2546 | case LY_TYPE_DEC64: |
| 2547 | dec = (struct lysc_type_dec*)(*type); |
| 2548 | |
| 2549 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2550 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2551 | if (!type_p->fraction_digits) { |
| 2552 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2553 | 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] | 2554 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2555 | 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] | 2556 | free(*type); |
| 2557 | *type = NULL; |
| 2558 | } |
| 2559 | return LY_EVALID; |
| 2560 | } |
| 2561 | } else if (type_p->fraction_digits) { |
| 2562 | /* 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] | 2563 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2564 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2565 | "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] | 2566 | tpdfname); |
| 2567 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2568 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2569 | "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] | 2570 | free(*type); |
| 2571 | *type = NULL; |
| 2572 | } |
| 2573 | return LY_EVALID; |
| 2574 | } |
| 2575 | dec->fraction_digits = type_p->fraction_digits; |
| 2576 | |
| 2577 | /* RFC 7950 9.2.4 - range */ |
| 2578 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2579 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2580 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2581 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2582 | 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] | 2583 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | if (tpdfname) { |
| 2587 | type_p->compiled = *type; |
| 2588 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2589 | } |
| 2590 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2591 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2592 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2593 | |
| 2594 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2595 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2596 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2597 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2598 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2599 | 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] | 2600 | } |
| 2601 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2602 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2603 | } |
| 2604 | |
| 2605 | /* RFC 7950 9.4.5 - pattern */ |
| 2606 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2607 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2608 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2609 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2610 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2611 | } |
| 2612 | |
| 2613 | if (tpdfname) { |
| 2614 | type_p->compiled = *type; |
| 2615 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2616 | } |
| 2617 | break; |
| 2618 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2619 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2620 | |
| 2621 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2622 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2623 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2624 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2625 | } |
| 2626 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2627 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2628 | /* 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] | 2629 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2630 | 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] | 2631 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2632 | 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] | 2633 | free(*type); |
| 2634 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2635 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2636 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | if (tpdfname) { |
| 2640 | type_p->compiled = *type; |
| 2641 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2642 | } |
| 2643 | break; |
| 2644 | case LY_TYPE_INT8: |
| 2645 | case LY_TYPE_UINT8: |
| 2646 | case LY_TYPE_INT16: |
| 2647 | case LY_TYPE_UINT16: |
| 2648 | case LY_TYPE_INT32: |
| 2649 | case LY_TYPE_UINT32: |
| 2650 | case LY_TYPE_INT64: |
| 2651 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2652 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2653 | |
| 2654 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2655 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2656 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2657 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2658 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2659 | 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] | 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | if (tpdfname) { |
| 2664 | type_p->compiled = *type; |
| 2665 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2666 | } |
| 2667 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2668 | case LY_TYPE_IDENT: |
| 2669 | idref = (struct lysc_type_identityref*)(*type); |
| 2670 | |
| 2671 | /* RFC 7950 9.10.2 - base */ |
| 2672 | if (type_p->bases) { |
| 2673 | if (base) { |
| 2674 | /* only the directly derived identityrefs can contain base specification */ |
| 2675 | if (tpdfname) { |
| 2676 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2677 | "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] | 2678 | tpdfname); |
| 2679 | } else { |
| 2680 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2681 | "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] | 2682 | free(*type); |
| 2683 | *type = NULL; |
| 2684 | } |
| 2685 | return LY_EVALID; |
| 2686 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2687 | 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] | 2688 | } |
| 2689 | |
| 2690 | if (!base && !type_p->flags) { |
| 2691 | /* type derived from identityref built-in type must contain at least one base */ |
| 2692 | if (tpdfname) { |
| 2693 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2694 | } else { |
| 2695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2696 | free(*type); |
| 2697 | *type = NULL; |
| 2698 | } |
| 2699 | return LY_EVALID; |
| 2700 | } |
| 2701 | |
| 2702 | if (tpdfname) { |
| 2703 | type_p->compiled = *type; |
| 2704 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2705 | } |
| 2706 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2707 | case LY_TYPE_LEAFREF: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2708 | lref = (struct lysc_type_leafref*)*type; |
| 2709 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2710 | /* RFC 7950 9.9.3 - require-instance */ |
| 2711 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2712 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2713 | if (tpdfname) { |
| 2714 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2715 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2716 | } else { |
| 2717 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2718 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2719 | free(*type); |
| 2720 | *type = NULL; |
| 2721 | } |
| 2722 | return LY_EVALID; |
| 2723 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2724 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2725 | } else if (base) { |
| 2726 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2727 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2728 | } else { |
| 2729 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2730 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2731 | } |
| 2732 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2733 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2734 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2735 | } else if (base) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2736 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path); |
| 2737 | lref->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2738 | } else if (tpdfname) { |
| 2739 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2740 | return LY_EVALID; |
| 2741 | } else { |
| 2742 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2743 | free(*type); |
| 2744 | *type = NULL; |
| 2745 | return LY_EVALID; |
| 2746 | } |
| 2747 | if (tpdfname) { |
| 2748 | type_p->compiled = *type; |
| 2749 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2750 | } |
| 2751 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2752 | case LY_TYPE_INST: |
| 2753 | /* RFC 7950 9.9.3 - require-instance */ |
| 2754 | if (type_p->flags & LYS_SET_REQINST) { |
| 2755 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2756 | } else { |
| 2757 | /* default is true */ |
| 2758 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2759 | } |
| 2760 | |
| 2761 | if (tpdfname) { |
| 2762 | type_p->compiled = *type; |
| 2763 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2764 | } |
| 2765 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2766 | case LY_TYPE_UNION: |
| 2767 | un = (struct lysc_type_union*)(*type); |
| 2768 | |
| 2769 | /* RFC 7950 7.4 - type */ |
| 2770 | if (type_p->types) { |
| 2771 | if (base) { |
| 2772 | /* only the directly derived union can contain types specification */ |
| 2773 | if (tpdfname) { |
| 2774 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2775 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2776 | tpdfname); |
| 2777 | } else { |
| 2778 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2779 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2780 | free(*type); |
| 2781 | *type = NULL; |
| 2782 | } |
| 2783 | return LY_EVALID; |
| 2784 | } |
| 2785 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2786 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2787 | 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] | 2788 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, |
| 2789 | &type_p->types[u], &un->types[u + additional], NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2790 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2791 | /* add space for additional types from the union subtype */ |
| 2792 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2793 | 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] | 2794 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2795 | |
| 2796 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2797 | 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] | 2798 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2799 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2800 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2801 | 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] | 2802 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2803 | |
| 2804 | lref->basetype = LY_TYPE_LEAFREF; |
| 2805 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path); |
| 2806 | lref->refcount = 1; |
| 2807 | lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2808 | 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] | 2809 | /* TODO extensions */ |
| 2810 | |
| 2811 | } else { |
| 2812 | un->types[u + additional] = un_aux->types[v]; |
| 2813 | ++un_aux->types[v]->refcount; |
| 2814 | } |
| 2815 | ++additional; |
| 2816 | LY_ARRAY_INCREMENT(un->types); |
| 2817 | } |
| 2818 | /* compensate u increment in main loop */ |
| 2819 | --additional; |
| 2820 | |
| 2821 | /* free the replaced union subtype */ |
| 2822 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2823 | } else { |
| 2824 | LY_ARRAY_INCREMENT(un->types); |
| 2825 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | if (!base && !type_p->flags) { |
| 2830 | /* type derived from union built-in type must contain at least one type */ |
| 2831 | if (tpdfname) { |
| 2832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2833 | } else { |
| 2834 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2835 | free(*type); |
| 2836 | *type = NULL; |
| 2837 | } |
| 2838 | return LY_EVALID; |
| 2839 | } |
| 2840 | |
| 2841 | if (tpdfname) { |
| 2842 | type_p->compiled = *type; |
| 2843 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2844 | } |
| 2845 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2846 | case LY_TYPE_BOOL: |
| 2847 | case LY_TYPE_EMPTY: |
| 2848 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2849 | break; |
| 2850 | } |
| 2851 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2852 | done: |
| 2853 | return ret; |
| 2854 | } |
| 2855 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2856 | /** |
| 2857 | * @brief Compile information about the leaf/leaf-list's type. |
| 2858 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2859 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2860 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2861 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2862 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2863 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2864 | * @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] | 2865 | * @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] | 2866 | * @return LY_ERR value. |
| 2867 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2868 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2869 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2870 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2871 | struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2872 | { |
| 2873 | LY_ERR ret = LY_SUCCESS; |
| 2874 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2875 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2876 | struct type_context { |
| 2877 | const struct lysp_tpdf *tpdf; |
| 2878 | struct lysp_node *node; |
| 2879 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2880 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2881 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2882 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2883 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2884 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2885 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2886 | |
| 2887 | (*type) = NULL; |
| 2888 | |
| 2889 | tctx = calloc(1, sizeof *tctx); |
| 2890 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2891 | 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] | 2892 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2893 | ret == LY_SUCCESS; |
| 2894 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2895 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2896 | if (basetype) { |
| 2897 | break; |
| 2898 | } |
| 2899 | |
| 2900 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2901 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2902 | 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] | 2903 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2904 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2905 | if (units && !*units) { |
| 2906 | /* inherit units */ |
| 2907 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2908 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2909 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2910 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2911 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2912 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2913 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2914 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2915 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2916 | break; |
| 2917 | } |
| 2918 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2919 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2920 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2921 | * 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] | 2922 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2923 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2924 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2925 | dummyloops = 1; |
| 2926 | goto preparenext; |
| 2927 | } else { |
| 2928 | tctx = NULL; |
| 2929 | break; |
| 2930 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2931 | } |
| 2932 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2933 | /* circular typedef reference detection */ |
| 2934 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2935 | /* local part */ |
| 2936 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2937 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2938 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2939 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2940 | free(tctx); |
| 2941 | ret = LY_EVALID; |
| 2942 | goto cleanup; |
| 2943 | } |
| 2944 | } |
| 2945 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2946 | /* global part for unions corner case */ |
| 2947 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 2948 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2949 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2950 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2951 | free(tctx); |
| 2952 | ret = LY_EVALID; |
| 2953 | goto cleanup; |
| 2954 | } |
| 2955 | } |
| 2956 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2957 | /* store information for the following processing */ |
| 2958 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2959 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2960 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2961 | /* prepare next loop */ |
| 2962 | tctx_prev = tctx; |
| 2963 | tctx = calloc(1, sizeof *tctx); |
| 2964 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2965 | } |
| 2966 | free(tctx); |
| 2967 | |
| 2968 | /* allocate type according to the basetype */ |
| 2969 | switch (basetype) { |
| 2970 | case LY_TYPE_BINARY: |
| 2971 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2972 | break; |
| 2973 | case LY_TYPE_BITS: |
| 2974 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2975 | break; |
| 2976 | case LY_TYPE_BOOL: |
| 2977 | case LY_TYPE_EMPTY: |
| 2978 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2979 | break; |
| 2980 | case LY_TYPE_DEC64: |
| 2981 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2982 | break; |
| 2983 | case LY_TYPE_ENUM: |
| 2984 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2985 | break; |
| 2986 | case LY_TYPE_IDENT: |
| 2987 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2988 | break; |
| 2989 | case LY_TYPE_INST: |
| 2990 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2991 | break; |
| 2992 | case LY_TYPE_LEAFREF: |
| 2993 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2994 | break; |
| 2995 | case LY_TYPE_STRING: |
| 2996 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2997 | break; |
| 2998 | case LY_TYPE_UNION: |
| 2999 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3000 | break; |
| 3001 | case LY_TYPE_INT8: |
| 3002 | case LY_TYPE_UINT8: |
| 3003 | case LY_TYPE_INT16: |
| 3004 | case LY_TYPE_UINT16: |
| 3005 | case LY_TYPE_INT32: |
| 3006 | case LY_TYPE_UINT32: |
| 3007 | case LY_TYPE_INT64: |
| 3008 | case LY_TYPE_UINT64: |
| 3009 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3010 | break; |
| 3011 | case LY_TYPE_UNKNOWN: |
| 3012 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3013 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3014 | ret = LY_EVALID; |
| 3015 | goto cleanup; |
| 3016 | } |
| 3017 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3018 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3019 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3020 | ly_data_type2str[basetype]); |
| 3021 | free(*type); |
| 3022 | (*type) = NULL; |
| 3023 | ret = LY_EVALID; |
| 3024 | goto cleanup; |
| 3025 | } |
| 3026 | |
| 3027 | /* get restrictions from the referred typedefs */ |
| 3028 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3029 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3030 | |
| 3031 | /* remember the typedef context for circular check */ |
| 3032 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3033 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3034 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3035 | base = tctx->tpdf->type.compiled; |
| 3036 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3037 | } 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] | 3038 | /* no change, just use the type information from the base */ |
| 3039 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3040 | ++base->refcount; |
| 3041 | continue; |
| 3042 | } |
| 3043 | |
| 3044 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3045 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3046 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3047 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3048 | ret = LY_EVALID; |
| 3049 | goto cleanup; |
| 3050 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3051 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3052 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3053 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3054 | ret = LY_EVALID; |
| 3055 | goto cleanup; |
| 3056 | } |
| 3057 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3058 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3059 | /* TODO user type plugins */ |
| 3060 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3061 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3062 | 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] | 3063 | 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] | 3064 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3065 | LY_CHECK_GOTO(ret, cleanup); |
| 3066 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3067 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3068 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3069 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3070 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3071 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3072 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3073 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3074 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3075 | /* TODO user type plugins */ |
| 3076 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3077 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3078 | 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] | 3079 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3080 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3081 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3082 | free(*type); |
| 3083 | (*type) = base; |
| 3084 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3085 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3086 | if (dflt && !(*type)->dflt) { |
| 3087 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3088 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3089 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3090 | (*type)->dflt->realtype = (*type); |
| 3091 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3092 | 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] | 3093 | 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] | 3094 | if (err) { |
| 3095 | ly_err_print(err); |
| 3096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3097 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3098 | ly_err_free(err); |
| 3099 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3100 | if (ret == LY_EINCOMPLETE) { |
| 3101 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3102 | 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] | 3103 | |
| 3104 | /* but in general result is so far ok */ |
| 3105 | ret = LY_SUCCESS; |
| 3106 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3107 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3108 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3109 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3110 | 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] | 3111 | |
| 3112 | cleanup: |
| 3113 | ly_set_erase(&tpdf_chain, free); |
| 3114 | return ret; |
| 3115 | } |
| 3116 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3117 | /** |
| 3118 | * @brief Compile status information of the given node. |
| 3119 | * |
| 3120 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3121 | * has the status correctly set during the compilation. |
| 3122 | * |
| 3123 | * @param[in] ctx Compile context |
| 3124 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3125 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3126 | * the compatibility with the parent's status value. |
| 3127 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3128 | * @return LY_ERR value. |
| 3129 | */ |
| 3130 | static LY_ERR |
| 3131 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3132 | { |
| 3133 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3134 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3135 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3136 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3137 | if ((parent_flags & 0x3) != 0x3) { |
| 3138 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3139 | * combination of bits (0x3) which marks the uses_status value */ |
| 3140 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3141 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3142 | } |
| 3143 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3144 | } else { |
| 3145 | (*node_flags) |= LYS_STATUS_CURR; |
| 3146 | } |
| 3147 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3148 | /* check status compatibility with the parent */ |
| 3149 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3150 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3151 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3152 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3153 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3154 | } else { /* LYS_STATUS_DEPRC */ |
| 3155 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3156 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3157 | } |
| 3158 | return LY_EVALID; |
| 3159 | } |
| 3160 | } |
| 3161 | return LY_SUCCESS; |
| 3162 | } |
| 3163 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3164 | /** |
| 3165 | * @brief Check uniqness of the node/action/notification name. |
| 3166 | * |
| 3167 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3168 | * structures, but they share the namespace so we need to check their name collisions. |
| 3169 | * |
| 3170 | * @param[in] ctx Compile context. |
| 3171 | * @param[in] children List (linked list) of data nodes to go through. |
| 3172 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3173 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3174 | * @param[in] name Name of the item to find in the given lists. |
| 3175 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3176 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3177 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3178 | */ |
| 3179 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3180 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
| 3181 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3182 | { |
| 3183 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3184 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3185 | |
| 3186 | LY_LIST_FOR(children, iter) { |
| 3187 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3188 | goto error; |
| 3189 | } |
| 3190 | } |
| 3191 | LY_ARRAY_FOR(actions, u) { |
| 3192 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3193 | goto error; |
| 3194 | } |
| 3195 | } |
| 3196 | LY_ARRAY_FOR(notifs, u) { |
| 3197 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3198 | goto error; |
| 3199 | } |
| 3200 | } |
| 3201 | return LY_SUCCESS; |
| 3202 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3203 | 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] | 3204 | return LY_EEXIST; |
| 3205 | } |
| 3206 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3207 | 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] | 3208 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3209 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3210 | * @brief Compile parsed RPC/action schema node information. |
| 3211 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3212 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3213 | * @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] | 3214 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3215 | * @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). |
| 3216 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3217 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3218 | */ |
| 3219 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3220 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3221 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3222 | { |
| 3223 | LY_ERR ret = LY_SUCCESS; |
| 3224 | struct lysp_node *child_p; |
| 3225 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3226 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3227 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3228 | lysc_update_path(ctx, parent, action_p->name); |
| 3229 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3230 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3231 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3232 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3233 | action_p->name, action)) { |
| 3234 | return LY_EVALID; |
| 3235 | } |
| 3236 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3237 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3238 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3239 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3240 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3241 | return LY_EVALID; |
| 3242 | } |
| 3243 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3244 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3245 | action->module = ctx->mod; |
| 3246 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3247 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3248 | action->sp = action_p; |
| 3249 | } |
| 3250 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3251 | |
| 3252 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3253 | * 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] | 3254 | 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] | 3255 | |
| 3256 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3257 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3258 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3259 | 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] | 3260 | 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] | 3261 | |
| 3262 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3263 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3264 | 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] | 3265 | 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] | 3266 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3267 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3268 | 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] | 3269 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3270 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3271 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3272 | |
| 3273 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3274 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3275 | 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] | 3276 | 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] | 3277 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3278 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3279 | 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] | 3280 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3281 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3282 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3283 | |
| 3284 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3285 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3286 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3287 | } |
| 3288 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3289 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3290 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3291 | return ret; |
| 3292 | } |
| 3293 | |
| 3294 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3295 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3296 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3297 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3298 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3299 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3300 | * @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] | 3301 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3302 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3303 | */ |
| 3304 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3305 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3306 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3307 | { |
| 3308 | LY_ERR ret = LY_SUCCESS; |
| 3309 | struct lysp_node *child_p; |
| 3310 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3311 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3312 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3313 | lysc_update_path(ctx, parent, notif_p->name); |
| 3314 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3315 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3316 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3317 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3318 | notif_p->name, notif)) { |
| 3319 | return LY_EVALID; |
| 3320 | } |
| 3321 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3322 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3323 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3324 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3325 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3326 | return LY_EVALID; |
| 3327 | } |
| 3328 | |
| 3329 | notif->nodetype = LYS_NOTIF; |
| 3330 | notif->module = ctx->mod; |
| 3331 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3332 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3333 | notif->sp = notif_p; |
| 3334 | } |
| 3335 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3336 | |
| 3337 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3338 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3339 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3340 | |
| 3341 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3342 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3343 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3344 | 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] | 3345 | 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] | 3346 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3347 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3348 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3349 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3350 | 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] | 3351 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3352 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3353 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3354 | 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] | 3355 | } |
| 3356 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3357 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3358 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3359 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3360 | return ret; |
| 3361 | } |
| 3362 | |
| 3363 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3364 | * @brief Compile parsed container node information. |
| 3365 | * @param[in] ctx Compile context |
| 3366 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3367 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3368 | * is enriched with the container-specific information. |
| 3369 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3370 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3371 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3372 | 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] | 3373 | { |
| 3374 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3375 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3376 | struct lysp_node *child_p; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3377 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3378 | LY_ERR ret = LY_SUCCESS; |
| 3379 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3380 | if (cont_p->presence) { |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3381 | /* explicit presence */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3382 | cont->flags |= LYS_PRESENCE; |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3383 | } else if (cont_p->musts) { |
| 3384 | /* container with a must condition */ |
| 3385 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it has a \"must\" condition.", cont_p->name); |
| 3386 | cont->flags |= LYS_PRESENCE; |
| 3387 | } else if (cont_p->parent) { |
| 3388 | if (cont_p->parent->nodetype == LYS_CHOICE) { |
| 3389 | /* container is an implicit case, so its existence decides the existence of the whole case */ |
| 3390 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it is an implicit case of choice \"%s\".", |
| 3391 | cont_p->name, cont_p->parent->name); |
| 3392 | cont->flags |= LYS_PRESENCE; |
| 3393 | } else if ((cont_p->parent->nodetype == LYS_CASE) |
| 3394 | && (((struct lysp_node_case *)cont_p->parent)->child == node_p) && !cont_p->next) { |
| 3395 | /* container is the only node in a case, so its existence decides the existence of the whole case */ |
| 3396 | LOGWRN(ctx->ctx, "Container \"%s\" changed to presence because it is the only data node of case \"%s\".", |
| 3397 | cont_p->name, cont_p->parent->name); |
| 3398 | cont->flags |= LYS_PRESENCE; |
| 3399 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3400 | } |
| 3401 | |
Michal Vasko | ba417ac | 2020-08-06 14:48:20 +0200 | [diff] [blame] | 3402 | /* more cases when the container has meaning but is kept NP for convenience: |
| 3403 | * - when condition |
| 3404 | * - direct child action/notification |
| 3405 | */ |
| 3406 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3407 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3408 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3409 | } |
| 3410 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3411 | 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] | 3412 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3413 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3414 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3415 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3416 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3417 | 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] | 3418 | |
| 3419 | done: |
| 3420 | return ret; |
| 3421 | } |
| 3422 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3423 | /* |
| 3424 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3425 | * @param[in] ctx Compile context. |
| 3426 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3427 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3428 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3429 | * @return LY_ERR value. |
| 3430 | */ |
| 3431 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3432 | 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] | 3433 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3434 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3435 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3436 | 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] | 3437 | leaf->units ? NULL : &leaf->units)); |
| 3438 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3439 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3440 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3441 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3442 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3443 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3444 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3445 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3446 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3447 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3448 | } |
| 3449 | } else { |
| 3450 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3451 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3452 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3453 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3454 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3455 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3456 | } |
| 3457 | } |
| 3458 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3459 | /* 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] | 3460 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3461 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3462 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3463 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3464 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3465 | /* 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] | 3466 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3467 | } |
| 3468 | } |
| 3469 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3470 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3471 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3472 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3473 | return LY_EVALID; |
| 3474 | } |
| 3475 | } |
| 3476 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3477 | return LY_SUCCESS; |
| 3478 | } |
| 3479 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3480 | /** |
| 3481 | * @brief Compile parsed leaf node information. |
| 3482 | * @param[in] ctx Compile context |
| 3483 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3484 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3485 | * is enriched with the leaf-specific information. |
| 3486 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3487 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3488 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3489 | 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] | 3490 | { |
| 3491 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3492 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 3493 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3494 | LY_ERR ret = LY_SUCCESS; |
| 3495 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3496 | 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] | 3497 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3498 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3499 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3500 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3501 | if (leaf_p->units) { |
| 3502 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3503 | leaf->flags |= LYS_SET_UNITS; |
| 3504 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3505 | |
| 3506 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3507 | leaf->dflt = (void*)leaf_p->dflt; |
| 3508 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3509 | if (ret) { |
| 3510 | leaf->dflt = NULL; |
| 3511 | return ret; |
| 3512 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3513 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3514 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3515 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3516 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3517 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3518 | leaf->dflt->realtype = leaf->type; |
| 3519 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3520 | 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] | 3521 | 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] | 3522 | leaf->dflt->realtype->refcount++; |
| 3523 | if (err) { |
| 3524 | ly_err_print(err); |
| 3525 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3526 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3527 | ly_err_free(err); |
| 3528 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3529 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3530 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3531 | 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] | 3532 | |
| 3533 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3534 | ret = LY_SUCCESS; |
| 3535 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3536 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3537 | leaf->flags |= LYS_SET_DFLT; |
| 3538 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3539 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3540 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3541 | done: |
| 3542 | return ret; |
| 3543 | } |
| 3544 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3545 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3546 | * @brief Compile parsed leaf-list node information. |
| 3547 | * @param[in] ctx Compile context |
| 3548 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3549 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3550 | * is enriched with the leaf-list-specific information. |
| 3551 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3552 | */ |
| 3553 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3554 | 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] | 3555 | { |
| 3556 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3557 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3558 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3559 | LY_ERR ret = LY_SUCCESS; |
| 3560 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3561 | 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] | 3562 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3563 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3564 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3565 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3566 | if (llist_p->units) { |
| 3567 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3568 | llist->flags |= LYS_SET_UNITS; |
| 3569 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3570 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3571 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3572 | llist->dflts = (void*)llist_p->dflts; |
| 3573 | 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] | 3574 | if (ret != LY_SUCCESS) { |
| 3575 | /* make sure the defaults are freed correctly */ |
| 3576 | if (llist_p->dflts) { |
| 3577 | llist->dflts = NULL; |
| 3578 | } |
| 3579 | return ret; |
| 3580 | } |
| 3581 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3582 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3583 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3584 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done); |
| 3585 | 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] | 3586 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3587 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3588 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3589 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3590 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3591 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3592 | llist->dflts[u]->realtype = llist->type; |
| 3593 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3594 | 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] | 3595 | 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] | 3596 | llist->dflts[u]->realtype->refcount++; |
| 3597 | if (err) { |
| 3598 | ly_err_print(err); |
| 3599 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3600 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3601 | ly_err_free(err); |
| 3602 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3603 | if (ret == LY_EINCOMPLETE) { |
| 3604 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3605 | 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] | 3606 | |
| 3607 | /* but in general result is so far ok */ |
| 3608 | ret = LY_SUCCESS; |
| 3609 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3610 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3611 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3612 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3613 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3614 | 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] | 3615 | /* configuration data values must be unique - so check the default values */ |
| 3616 | LY_ARRAY_FOR(llist->dflts, u) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3617 | for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3618 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3619 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3620 | 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] | 3621 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3622 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3623 | if (dynamic) { |
| 3624 | free((char*)val); |
| 3625 | } |
| 3626 | return LY_EVALID; |
| 3627 | } |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | /* 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] | 3633 | |
| 3634 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3635 | if (llist->min) { |
| 3636 | llist->flags |= LYS_MAND_TRUE; |
| 3637 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3638 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3639 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3640 | done: |
| 3641 | return ret; |
| 3642 | } |
| 3643 | |
| 3644 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3645 | * @brief Compile information about list's uniques. |
| 3646 | * @param[in] ctx Compile context. |
| 3647 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3648 | * @param[in] uniques Sized array list of unique statements. |
| 3649 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3650 | * @return LY_ERR value. |
| 3651 | */ |
| 3652 | static LY_ERR |
| 3653 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3654 | { |
| 3655 | LY_ERR ret = LY_SUCCESS; |
| 3656 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3657 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3658 | const char *keystr, *delim; |
| 3659 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3660 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3661 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3662 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3663 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3664 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3665 | config = -1; |
| 3666 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3667 | keystr = uniques[v]; |
| 3668 | while (keystr) { |
| 3669 | delim = strpbrk(keystr, " \t\n"); |
| 3670 | if (delim) { |
| 3671 | len = delim - keystr; |
| 3672 | while (isspace(*delim)) { |
| 3673 | ++delim; |
| 3674 | } |
| 3675 | } else { |
| 3676 | len = strlen(keystr); |
| 3677 | } |
| 3678 | |
| 3679 | /* unique node must be present */ |
| 3680 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3681 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3682 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3683 | if (ret != LY_SUCCESS) { |
| 3684 | if (ret == LY_EDENIED) { |
| 3685 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3686 | "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] | 3687 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3688 | } |
| 3689 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3690 | } else if (flags) { |
| 3691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3692 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3693 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3694 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3695 | } |
| 3696 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3697 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3698 | /* all referenced leafs must be of the same config type */ |
| 3699 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3700 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3701 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3702 | return LY_EVALID; |
| 3703 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3704 | config = 1; |
| 3705 | } else { /* LYS_CONFIG_R */ |
| 3706 | config = 0; |
| 3707 | } |
| 3708 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3709 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3710 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3711 | if (parent->nodetype == LYS_LIST) { |
| 3712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3713 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3714 | return LY_EVALID; |
| 3715 | } |
| 3716 | } |
| 3717 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3718 | /* check status */ |
| 3719 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3720 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3721 | |
| 3722 | /* mark leaf as unique */ |
| 3723 | (*key)->flags |= LYS_UNIQUE; |
| 3724 | |
| 3725 | /* next unique value in line */ |
| 3726 | keystr = delim; |
| 3727 | } |
| 3728 | /* next unique definition */ |
| 3729 | } |
| 3730 | |
| 3731 | return LY_SUCCESS; |
| 3732 | } |
| 3733 | |
| 3734 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3735 | * @brief Compile parsed list node information. |
| 3736 | * @param[in] ctx Compile context |
| 3737 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3738 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3739 | * is enriched with the list-specific information. |
| 3740 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3741 | */ |
| 3742 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3743 | 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] | 3744 | { |
| 3745 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3746 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3747 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3748 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3749 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3750 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3751 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3752 | LY_ERR ret = LY_SUCCESS; |
| 3753 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3754 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3755 | if (list->min) { |
| 3756 | list->flags |= LYS_MAND_TRUE; |
| 3757 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3758 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3759 | |
| 3760 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3761 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3762 | } |
| 3763 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3764 | 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] | 3765 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3766 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3767 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3768 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3769 | |
| 3770 | /* keys */ |
| 3771 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3772 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3773 | return LY_EVALID; |
| 3774 | } |
| 3775 | |
| 3776 | /* find all the keys (must be direct children) */ |
| 3777 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3778 | if (!keystr) { |
| 3779 | /* keyless list */ |
| 3780 | list->flags |= LYS_KEYLESS; |
| 3781 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3782 | while (keystr) { |
| 3783 | delim = strpbrk(keystr, " \t\n"); |
| 3784 | if (delim) { |
| 3785 | len = delim - keystr; |
| 3786 | while (isspace(*delim)) { |
| 3787 | ++delim; |
| 3788 | } |
| 3789 | } else { |
| 3790 | len = strlen(keystr); |
| 3791 | } |
| 3792 | |
| 3793 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3794 | 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] | 3795 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3796 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3797 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3798 | return LY_EVALID; |
| 3799 | } |
| 3800 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3801 | if (key->flags & LYS_KEY) { |
| 3802 | /* the node was already marked as a key */ |
| 3803 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3804 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3805 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3806 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3807 | |
| 3808 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3809 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3810 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3811 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3812 | return LY_EVALID; |
| 3813 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3814 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3815 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3816 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3817 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3818 | "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] | 3819 | return LY_EVALID; |
| 3820 | } |
| 3821 | } else { |
| 3822 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3823 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3824 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3825 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3826 | return LY_EVALID; |
| 3827 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3828 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3829 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3830 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3831 | return LY_EVALID; |
| 3832 | } |
| 3833 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3834 | |
| 3835 | /* check status */ |
| 3836 | 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] | 3837 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3838 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3839 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3840 | if (key->dflt) { |
| 3841 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3842 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3843 | free(key->dflt); |
| 3844 | key->dflt = NULL; |
| 3845 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3846 | } |
| 3847 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3848 | key->flags |= LYS_KEY; |
| 3849 | |
| 3850 | /* move it to the correct position */ |
| 3851 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3852 | /* fix links in closest previous siblings of the key */ |
| 3853 | if (key->next) { |
| 3854 | key->next->prev = key->prev; |
| 3855 | } else { |
| 3856 | /* last child */ |
| 3857 | list->child->prev = key->prev; |
| 3858 | } |
| 3859 | if (key->prev->next) { |
| 3860 | key->prev->next = key->next; |
| 3861 | } |
| 3862 | /* fix links in the key */ |
| 3863 | if (prev_key) { |
| 3864 | key->prev = (struct lysc_node*)prev_key; |
| 3865 | key->next = prev_key->next; |
| 3866 | } else { |
| 3867 | key->prev = list->child->prev; |
| 3868 | key->next = list->child; |
| 3869 | } |
| 3870 | /* fix links in closes future siblings of the key */ |
| 3871 | if (prev_key) { |
| 3872 | if (prev_key->next) { |
| 3873 | prev_key->next->prev = (struct lysc_node*)key; |
| 3874 | } else { |
| 3875 | list->child->prev = (struct lysc_node*)key; |
| 3876 | } |
| 3877 | prev_key->next = (struct lysc_node*)key; |
| 3878 | } else { |
| 3879 | list->child->prev = (struct lysc_node*)key; |
| 3880 | } |
| 3881 | /* fix links in parent */ |
| 3882 | if (!key->prev->next) { |
| 3883 | list->child = (struct lysc_node*)key; |
| 3884 | } |
| 3885 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3886 | |
| 3887 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3888 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3889 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3890 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /* uniques */ |
| 3894 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3895 | 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] | 3896 | } |
| 3897 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3898 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3899 | 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] | 3900 | |
| 3901 | done: |
| 3902 | return ret; |
| 3903 | } |
| 3904 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3905 | /** |
| 3906 | * @brief Do some checks and set the default choice's case. |
| 3907 | * |
| 3908 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3909 | * |
| 3910 | * @param[in] ctx Compile context. |
| 3911 | * @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, |
| 3912 | * not the reference to the imported module. |
| 3913 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3914 | * @return LY_ERR value. |
| 3915 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3916 | static LY_ERR |
| 3917 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3918 | { |
| 3919 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3920 | const char *prefix = NULL, *name; |
| 3921 | size_t prefix_len = 0; |
| 3922 | |
| 3923 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3924 | name = strchr(dflt, ':'); |
| 3925 | if (name) { |
| 3926 | prefix = dflt; |
| 3927 | prefix_len = name - prefix; |
| 3928 | ++name; |
| 3929 | } else { |
| 3930 | name = dflt; |
| 3931 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3932 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3933 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3934 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3935 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3936 | prefix_len, prefix); |
| 3937 | return LY_EVALID; |
| 3938 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3939 | 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] | 3940 | if (!ch->dflt) { |
| 3941 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3942 | "Default case \"%s\" not found.", dflt); |
| 3943 | return LY_EVALID; |
| 3944 | } |
| 3945 | /* no mandatory nodes directly under the default case */ |
| 3946 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3947 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3948 | break; |
| 3949 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3950 | if (iter->flags & LYS_MAND_TRUE) { |
| 3951 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3952 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3953 | return LY_EVALID; |
| 3954 | } |
| 3955 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3956 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3957 | return LY_SUCCESS; |
| 3958 | } |
| 3959 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3960 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3961 | 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] | 3962 | { |
| 3963 | struct lys_module *mod; |
| 3964 | const char *prefix = NULL, *name; |
| 3965 | size_t prefix_len = 0; |
| 3966 | struct lysc_node_case *cs; |
| 3967 | struct lysc_node *node; |
| 3968 | |
| 3969 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3970 | name = strchr(dflt, ':'); |
| 3971 | if (name) { |
| 3972 | prefix = dflt; |
| 3973 | prefix_len = name - prefix; |
| 3974 | ++name; |
| 3975 | } else { |
| 3976 | name = dflt; |
| 3977 | } |
| 3978 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3979 | if (prefix) { |
| 3980 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3981 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3982 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3983 | "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] | 3984 | return LY_EVALID; |
| 3985 | } |
| 3986 | } else { |
| 3987 | mod = ctx->mod; |
| 3988 | } |
| 3989 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3990 | 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] | 3991 | if (!cs) { |
| 3992 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3993 | "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] | 3994 | return LY_EVALID; |
| 3995 | } |
| 3996 | |
| 3997 | /* check that there is no mandatory node */ |
| 3998 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3999 | if (node->parent != (struct lysc_node*)cs) { |
| 4000 | break; |
| 4001 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4002 | if (node->flags & LYS_MAND_TRUE) { |
| 4003 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4004 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4005 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4006 | return LY_EVALID; |
| 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | /* set the default case in choice */ |
| 4011 | ch->dflt = cs; |
| 4012 | cs->flags |= LYS_SET_DFLT; |
| 4013 | |
| 4014 | return LY_SUCCESS; |
| 4015 | } |
| 4016 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4017 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4018 | * @brief Compile parsed choice node information. |
| 4019 | * @param[in] ctx Compile context |
| 4020 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4021 | * @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] | 4022 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4023 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4024 | */ |
| 4025 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4026 | 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] | 4027 | { |
| 4028 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4029 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4030 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4031 | LY_ERR ret = LY_SUCCESS; |
| 4032 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4033 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4034 | if (child_p->nodetype == LYS_CASE) { |
| 4035 | 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] | 4036 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4037 | } |
| 4038 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4039 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4044 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4045 | 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] | 4046 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4047 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4048 | return ret; |
| 4049 | } |
| 4050 | |
| 4051 | /** |
| 4052 | * @brief Compile parsed anydata or anyxml node information. |
| 4053 | * @param[in] ctx Compile context |
| 4054 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4055 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4056 | * is enriched with the any-specific information. |
| 4057 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4058 | */ |
| 4059 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4060 | 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] | 4061 | { |
| 4062 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4063 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4064 | unsigned int u; |
| 4065 | LY_ERR ret = LY_SUCCESS; |
| 4066 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4067 | 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] | 4068 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4069 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4070 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4071 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4072 | |
| 4073 | if (any->flags & LYS_CONFIG_W) { |
| 4074 | 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] | 4075 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4076 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4077 | done: |
| 4078 | return ret; |
| 4079 | } |
| 4080 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4081 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4082 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4083 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4084 | * |
| 4085 | * @param[in] ctx Compile context |
| 4086 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4087 | * the choice itself is expected instead of a specific case node. |
| 4088 | * @param[in] node Schema node to connect into the list. |
| 4089 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4090 | * 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] | 4091 | */ |
| 4092 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4093 | 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] | 4094 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4095 | struct lysc_node **children, *start, *end; |
| 4096 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4097 | |
| 4098 | if (node->nodetype == LYS_CASE) { |
| 4099 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4100 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4101 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4102 | } |
| 4103 | if (children) { |
| 4104 | if (!(*children)) { |
| 4105 | /* first child */ |
| 4106 | *children = node; |
| 4107 | } else if (*children != node) { |
| 4108 | /* by the condition in previous branch we cover the choice/case children |
| 4109 | * - the children list is shared by the choice and the the first case, in addition |
| 4110 | * the first child of each case must be referenced from the case node. So the node is |
| 4111 | * actually always already inserted in case it is the first children - so here such |
| 4112 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4113 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4114 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4115 | /* some augments are already connected and we are connecting new ones, |
| 4116 | * keep module name order and insert the node into the children list */ |
| 4117 | end = (*children); |
| 4118 | do { |
| 4119 | end = end->prev; |
| 4120 | mod = end->module; |
| 4121 | while (end->prev->module == mod) { |
| 4122 | end = end->prev; |
| 4123 | } |
| 4124 | } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module) |
| 4125 | && (strcmp(mod->name, node->module->name) > 0)); |
| 4126 | |
| 4127 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4128 | start = end->prev; |
| 4129 | start->next = node; |
| 4130 | node->next = end; |
| 4131 | end->prev = node; |
| 4132 | node->prev = start; |
| 4133 | } else { |
| 4134 | /* insert at the end of the parent's children list */ |
| 4135 | (*children)->prev->next = node; |
| 4136 | node->prev = (*children)->prev; |
| 4137 | (*children)->prev = node; |
| 4138 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4139 | |
| 4140 | /* check the name uniqueness */ |
| 4141 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4142 | lysc_node_notifs(parent), node->name, node)) { |
| 4143 | return LY_EEXIST; |
| 4144 | } |
| 4145 | } |
| 4146 | } |
| 4147 | return LY_SUCCESS; |
| 4148 | } |
| 4149 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4150 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4151 | * @brief Prepare the case structure in choice node for the new data node. |
| 4152 | * |
| 4153 | * 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 |
| 4154 | * created in the choice when the first child was processed. |
| 4155 | * |
| 4156 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4157 | * @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, |
| 4158 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4159 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4160 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4161 | * @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, |
| 4162 | * 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] | 4163 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4164 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4165 | 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] | 4166 | { |
| 4167 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4168 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4169 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4170 | unsigned int u; |
| 4171 | LY_ERR ret; |
| 4172 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4173 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4174 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4175 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4176 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4177 | return NULL; \ |
| 4178 | } \ |
| 4179 | } |
| 4180 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4181 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4182 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4183 | |
| 4184 | /* we have to add an implicit case node into the parent choice */ |
| 4185 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4186 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4187 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4188 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4189 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4190 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4191 | /* the case is already present since the child is not its first children */ |
| 4192 | return (struct lysc_node_case*)ch->cases->prev; |
| 4193 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4194 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4195 | |
| 4196 | /* explicit parent case is not present (this is its first child) */ |
| 4197 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4198 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4199 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4200 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4201 | cs->sp = node_p; |
| 4202 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4203 | /* 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] | 4204 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4205 | |
| 4206 | if (node_p->when) { |
| 4207 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4208 | 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] | 4209 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4210 | |
| 4211 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4212 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4213 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4214 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4215 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4216 | 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] | 4217 | } else { |
| 4218 | LOGINT(ctx->ctx); |
| 4219 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4220 | } |
| 4221 | cs->module = ctx->mod; |
| 4222 | cs->prev = (struct lysc_node*)cs; |
| 4223 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4224 | 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] | 4225 | cs->child = child; |
| 4226 | |
| 4227 | return cs; |
| 4228 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4229 | if (cs) { |
| 4230 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4231 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4232 | return NULL; |
| 4233 | |
| 4234 | #undef UNIQUE_CHECK |
| 4235 | } |
| 4236 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4237 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4238 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4239 | * |
| 4240 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4241 | * @param[in] node Target node where the config is supposed to be changed. |
| 4242 | * @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] | 4243 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4244 | * 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] | 4245 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4246 | * @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] | 4247 | * @return LY_ERR value. |
| 4248 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4249 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4250 | 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] | 4251 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4252 | { |
| 4253 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4254 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4255 | |
| 4256 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4257 | /* nothing to do */ |
| 4258 | return LY_SUCCESS; |
| 4259 | } |
| 4260 | |
| 4261 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4262 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4263 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4264 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4265 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4266 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4267 | return LY_EVALID; |
| 4268 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4269 | node->flags |= LYS_SET_CONFIG; |
| 4270 | } else { |
| 4271 | if (node->flags & LYS_SET_CONFIG) { |
| 4272 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4273 | /* setting config flags, but have node with explicit config true */ |
| 4274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4275 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4276 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4277 | return LY_EVALID; |
| 4278 | } |
| 4279 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4280 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4281 | return LY_SUCCESS; |
| 4282 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4283 | } |
| 4284 | node->flags &= ~LYS_CONFIG_MASK; |
| 4285 | node->flags |= config; |
| 4286 | |
| 4287 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4288 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4289 | 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] | 4290 | } |
| 4291 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4292 | return LY_SUCCESS; |
| 4293 | } |
| 4294 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4295 | /** |
| 4296 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4297 | * |
| 4298 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4299 | * the flag to such parents from a mandatory children. |
| 4300 | * |
| 4301 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4302 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4303 | * (mandatory children was removed). |
| 4304 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4305 | void |
| 4306 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4307 | { |
| 4308 | struct lysc_node *iter; |
| 4309 | |
| 4310 | if (add) { /* set flag */ |
| 4311 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4312 | parent = parent->parent) { |
| 4313 | parent->flags |= LYS_MAND_TRUE; |
| 4314 | } |
| 4315 | } else { /* unset flag */ |
| 4316 | 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] | 4317 | 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] | 4318 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4319 | /* there is another mandatory node */ |
| 4320 | return; |
| 4321 | } |
| 4322 | } |
| 4323 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4324 | parent->flags &= ~LYS_MAND_TRUE; |
| 4325 | } |
| 4326 | } |
| 4327 | } |
| 4328 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4329 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4330 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4331 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4332 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4333 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4334 | */ |
| 4335 | static void |
| 4336 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4337 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4338 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4339 | size_t len; |
| 4340 | |
| 4341 | len = strlen(aug_p->nodeid); |
| 4342 | LY_ARRAY_FOR(result, v) { |
| 4343 | if (strlen(result[v]->nodeid) <= len) { |
| 4344 | continue; |
| 4345 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4346 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4347 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4348 | 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] | 4349 | break; |
| 4350 | } |
| 4351 | } |
| 4352 | result[v] = aug_p; |
| 4353 | LY_ARRAY_INCREMENT(result); |
| 4354 | } |
| 4355 | |
| 4356 | /** |
| 4357 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4358 | * |
| 4359 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4360 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4361 | * |
| 4362 | * @param[in] ctx Compile context. |
| 4363 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4364 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4365 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4366 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4367 | * @return LY_ERR value. |
| 4368 | */ |
| 4369 | LY_ERR |
| 4370 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4371 | { |
| 4372 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4373 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4374 | |
| 4375 | assert(augments); |
| 4376 | |
| 4377 | /* get count of the augments in module and all its submodules */ |
| 4378 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4379 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4380 | } |
| 4381 | LY_ARRAY_FOR(inc_p, u) { |
| 4382 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4383 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | if (!count) { |
| 4388 | *augments = NULL; |
| 4389 | return LY_SUCCESS; |
| 4390 | } |
| 4391 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4392 | |
| 4393 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4394 | * together, so there can be even /z/y betwwen them. */ |
| 4395 | LY_ARRAY_FOR(aug_p, u) { |
| 4396 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4397 | } |
| 4398 | LY_ARRAY_FOR(inc_p, u) { |
| 4399 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4400 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4401 | } |
| 4402 | } |
| 4403 | |
| 4404 | *augments = result; |
| 4405 | return LY_SUCCESS; |
| 4406 | } |
| 4407 | |
| 4408 | /** |
| 4409 | * @brief Compile the parsed augment connecting it into its target. |
| 4410 | * |
| 4411 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4412 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4413 | * are already implemented and compiled. |
| 4414 | * |
| 4415 | * @param[in] ctx Compile context. |
| 4416 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4417 | * @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 |
| 4418 | * children in case of the augmenting uses data. |
| 4419 | * @return LY_SUCCESS on success. |
| 4420 | * @return LY_EVALID on failure. |
| 4421 | */ |
| 4422 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4423 | 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] | 4424 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4425 | LY_ERR ret = LY_SUCCESS, rc; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4426 | struct lysp_node *node_p, *case_node_p; |
| 4427 | struct lysc_node *target; /* target target of the augment */ |
| 4428 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4429 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4430 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4431 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4432 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4433 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4434 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4435 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4436 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4437 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4438 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4439 | 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] | 4440 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4441 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4442 | if (ret != LY_SUCCESS) { |
| 4443 | if (ret == LY_EDENIED) { |
| 4444 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4445 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4446 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4447 | } |
| 4448 | return LY_EVALID; |
| 4449 | } |
| 4450 | |
| 4451 | /* check for mandatory nodes |
| 4452 | * - new cases augmenting some choice can have mandatory nodes |
| 4453 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4454 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4455 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4456 | allow_mandatory = 1; |
| 4457 | } |
| 4458 | |
| 4459 | when_shared = NULL; |
| 4460 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4461 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4462 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4463 | && !((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] | 4464 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4465 | && !(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] | 4466 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4467 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4468 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4469 | return LY_EVALID; |
| 4470 | } |
| 4471 | |
| 4472 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4473 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4474 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4475 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4476 | } else { |
| 4477 | 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] | 4478 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4479 | } |
| 4480 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4481 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4482 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4483 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4484 | * 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] | 4485 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4486 | /* 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] | 4487 | 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] | 4488 | } else if (target->nodetype == LYS_CHOICE) { |
| 4489 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4490 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4491 | } else { |
| 4492 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4493 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4494 | if (!node) { |
| 4495 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4496 | break; |
| 4497 | } |
| 4498 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4499 | } |
| 4500 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4501 | 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] | 4502 | node->flags &= ~LYS_MAND_TRUE; |
| 4503 | lys_compile_mandatory_parents(target, 0); |
| 4504 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4505 | "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] | 4506 | return LY_EVALID; |
| 4507 | } |
| 4508 | |
| 4509 | /* pass augment's when to all the children */ |
| 4510 | if (aug_p->when) { |
| 4511 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4512 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4513 | 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] | 4514 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4515 | |
| 4516 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4517 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4518 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4519 | } |
| 4520 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4521 | when_shared = *when; |
| 4522 | } else { |
| 4523 | ++when_shared->refcount; |
| 4524 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4525 | |
| 4526 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4527 | /* 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] | 4528 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4529 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4530 | } |
| 4531 | } |
| 4532 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4533 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4534 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4535 | switch (target->nodetype) { |
| 4536 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4537 | 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] | 4538 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4539 | 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] | 4540 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4541 | break; |
| 4542 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4543 | 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] | 4544 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4545 | 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] | 4546 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4547 | break; |
| 4548 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4549 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4550 | if (aug_p->actions) { |
| 4551 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4552 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4553 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4554 | return LY_EVALID; |
| 4555 | } |
| 4556 | if (aug_p->notifs) { |
| 4557 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4558 | "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] | 4559 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4560 | return LY_EVALID; |
| 4561 | } |
| 4562 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4563 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4564 | /* add this module into the target module augmented_by, if not there already */ |
| 4565 | rc = LY_SUCCESS; |
| 4566 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4567 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4568 | rc = LY_EEXIST; |
| 4569 | break; |
| 4570 | } |
| 4571 | } |
| 4572 | if (!rc) { |
| 4573 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4574 | *aug_mod = ctx->mod; |
| 4575 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4576 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4577 | lysc_update_path(ctx, NULL, NULL); |
| 4578 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4579 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4580 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4581 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4582 | return ret; |
| 4583 | } |
| 4584 | |
| 4585 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4586 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4587 | * |
| 4588 | * @param[in] ctx Compile context. |
| 4589 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4590 | * @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] | 4591 | * @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] | 4592 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4593 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4594 | * 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] | 4595 | * @return LY_ERR value. |
| 4596 | */ |
| 4597 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4598 | 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] | 4599 | { |
| 4600 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4601 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4602 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4603 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4604 | return LY_EVALID; |
| 4605 | } |
| 4606 | |
| 4607 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4608 | /* check if node has default value */ |
| 4609 | if (node->nodetype & LYS_LEAF) { |
| 4610 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4611 | if (refine_flag) { |
| 4612 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4613 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4614 | return LY_EVALID; |
| 4615 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4616 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4617 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4618 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4619 | |
| 4620 | /* update the list of incomplete default values if needed */ |
| 4621 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4622 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4623 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4624 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4625 | free(leaf->dflt); |
| 4626 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4627 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4628 | } |
| 4629 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4630 | if (refine_flag) { |
| 4631 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4632 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4633 | return LY_EVALID; |
| 4634 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4635 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4636 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4637 | 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] | 4638 | return LY_EVALID; |
| 4639 | } |
| 4640 | |
| 4641 | node->flags &= ~LYS_MAND_FALSE; |
| 4642 | node->flags |= LYS_MAND_TRUE; |
| 4643 | lys_compile_mandatory_parents(node->parent, 1); |
| 4644 | } else { |
| 4645 | /* make mandatory false */ |
| 4646 | node->flags &= ~LYS_MAND_TRUE; |
| 4647 | node->flags |= LYS_MAND_FALSE; |
| 4648 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4649 | 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] | 4650 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4651 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4652 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4653 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4654 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4655 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4656 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | return LY_SUCCESS; |
| 4660 | } |
| 4661 | |
| 4662 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4663 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4664 | * If present, also apply uses's modificators. |
| 4665 | * |
| 4666 | * @param[in] ctx Compile context |
| 4667 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4668 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4669 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4670 | * the compile context. |
| 4671 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4672 | */ |
| 4673 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4674 | 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] | 4675 | { |
| 4676 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4677 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4678 | /* 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] | 4679 | struct lysc_node_container context_node_fake = |
| 4680 | {.nodetype = LYS_CONTAINER, |
| 4681 | .module = ctx->mod, |
| 4682 | .flags = parent ? parent->flags : 0, |
| 4683 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4684 | .prev = (struct lysc_node*)&context_node_fake, |
| 4685 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4686 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4687 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4688 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4689 | int found; |
| 4690 | const char *id, *name, *prefix; |
| 4691 | size_t prefix_len, name_len; |
| 4692 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4693 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4694 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4695 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4696 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4697 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4698 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4699 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4700 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4701 | struct lysc_notif **notifs = NULL; |
| 4702 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4703 | |
| 4704 | /* search for the grouping definition */ |
| 4705 | found = 0; |
| 4706 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4707 | 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] | 4708 | if (prefix) { |
| 4709 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4710 | if (!mod) { |
| 4711 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4712 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4713 | return LY_EVALID; |
| 4714 | } |
| 4715 | } else { |
| 4716 | mod = ctx->mod_def; |
| 4717 | } |
| 4718 | if (mod == ctx->mod_def) { |
| 4719 | 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] | 4720 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4721 | LY_ARRAY_FOR(grp, u) { |
| 4722 | if (!strcmp(grp[u].name, name)) { |
| 4723 | grp = &grp[u]; |
| 4724 | found = 1; |
| 4725 | break; |
| 4726 | } |
| 4727 | } |
| 4728 | } |
| 4729 | } |
| 4730 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4731 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4732 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4733 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4734 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4735 | if (!strcmp(grp[u].name, name)) { |
| 4736 | grp = &grp[u]; |
| 4737 | found = 1; |
| 4738 | } |
| 4739 | } |
| 4740 | } |
| 4741 | if (!found && mod->parsed->includes) { |
| 4742 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4743 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4744 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4745 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4746 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4747 | if (!strcmp(grp[v].name, name)) { |
| 4748 | grp = &grp[v]; |
| 4749 | found = 1; |
| 4750 | } |
| 4751 | } |
| 4752 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4753 | } |
| 4754 | } |
| 4755 | } |
| 4756 | if (!found) { |
| 4757 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4758 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4759 | return LY_EVALID; |
| 4760 | } |
| 4761 | |
| 4762 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4763 | grp_stack_count = ctx->groupings.count; |
| 4764 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4765 | if (grp_stack_count == ctx->groupings.count) { |
| 4766 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4767 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4768 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4769 | return LY_EVALID; |
| 4770 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4771 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4772 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4773 | grp->flags |= LYS_USED_GRP; |
| 4774 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4775 | |
| 4776 | /* switch context's mod_def */ |
| 4777 | mod_old = ctx->mod_def; |
| 4778 | ctx->mod_def = mod; |
| 4779 | |
| 4780 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4781 | 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] | 4782 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4783 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4784 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4785 | /* 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] | 4786 | 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] | 4787 | |
| 4788 | /* some preparation for applying refines */ |
| 4789 | if (grp->data == node_p) { |
| 4790 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4791 | if (parent) { |
| 4792 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4793 | } else if (ctx->mod->compiled->data) { |
| 4794 | child = ctx->mod->compiled->data; |
| 4795 | } else { |
| 4796 | child = NULL; |
| 4797 | } |
| 4798 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4799 | } |
| 4800 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4801 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4802 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4803 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4804 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4805 | /* 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] | 4806 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4807 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4808 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4809 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4810 | |
| 4811 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4812 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4813 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4816 | when_shared = *when; |
| 4817 | } else { |
| 4818 | ++when_shared->refcount; |
| 4819 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4820 | |
| 4821 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4822 | /* 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] | 4823 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4824 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4825 | } |
| 4826 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4827 | } |
| 4828 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4829 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4830 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4831 | /* 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] | 4832 | 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] | 4833 | } |
| 4834 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4835 | /* compile actions */ |
| 4836 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4837 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4838 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4839 | 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] | 4840 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4841 | /* 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] | 4842 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4843 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4844 | 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] | 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | /* compile notifications */ |
| 4849 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4850 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4851 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4852 | 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] | 4853 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4854 | /* 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] | 4855 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4856 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4857 | 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] | 4858 | } |
| 4859 | } |
| 4860 | |
| 4861 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4862 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4863 | 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] | 4864 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4865 | 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] | 4866 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4867 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4868 | /* reload previous context's mod_def */ |
| 4869 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4870 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4871 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4872 | /* apply refine */ |
| 4873 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4874 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4875 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4876 | 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] | 4877 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4878 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4879 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4880 | |
| 4881 | /* default value */ |
| 4882 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4883 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4884 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4885 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.", |
| 4886 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4887 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4888 | } |
| 4889 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4891 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4892 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4893 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4894 | } |
| 4895 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4896 | struct ly_err_item *err = NULL; |
| 4897 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4898 | if (leaf->dflt) { |
| 4899 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4900 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4901 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4902 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4903 | } else { |
| 4904 | /* prepare a new one */ |
| 4905 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4906 | leaf->dflt->realtype = leaf->type; |
| 4907 | } |
| 4908 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4909 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4910 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4911 | 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] | 4912 | 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] | 4913 | leaf->dflt->realtype->refcount++; |
| 4914 | if (err) { |
| 4915 | ly_err_print(err); |
| 4916 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4917 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4918 | ly_err_free(err); |
| 4919 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4920 | if (rc == LY_EINCOMPLETE) { |
| 4921 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4922 | 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] | 4923 | |
| 4924 | /* but in general result is so far ok */ |
| 4925 | rc = LY_SUCCESS; |
| 4926 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4927 | LY_CHECK_GOTO(rc, cleanup); |
| 4928 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4929 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4930 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4931 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4932 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4933 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4934 | "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] | 4935 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4936 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4937 | |
| 4938 | /* remove previous set of default values */ |
| 4939 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4940 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4941 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4942 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4943 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4944 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4945 | LY_ARRAY_FREE(llist->dflts); |
| 4946 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4947 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4948 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4949 | |
| 4950 | /* create the new set of the default values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4951 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup); |
| 4952 | 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] | 4953 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4954 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4955 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4956 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4957 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4958 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4959 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4960 | 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] | 4961 | 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] | 4962 | 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] | 4963 | llist->dflts[u]->realtype->refcount++; |
| 4964 | if (err) { |
| 4965 | ly_err_print(err); |
| 4966 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4967 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4968 | ly_err_free(err); |
| 4969 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4970 | if (rc == LY_EINCOMPLETE) { |
| 4971 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4972 | 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] | 4973 | |
| 4974 | /* but in general result is so far ok */ |
| 4975 | rc = LY_SUCCESS; |
| 4976 | } |
| 4977 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4978 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4979 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4980 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4981 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4982 | /* unset LYS_SET_DFLT from the current default case */ |
| 4983 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4984 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4985 | 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] | 4986 | } |
| 4987 | } |
| 4988 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4989 | /* description */ |
| 4990 | if (rfn->dsc) { |
| 4991 | FREE_STRING(ctx->ctx, node->dsc); |
| 4992 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4993 | } |
| 4994 | |
| 4995 | /* reference */ |
| 4996 | if (rfn->ref) { |
| 4997 | FREE_STRING(ctx->ctx, node->ref); |
| 4998 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4999 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5000 | |
| 5001 | /* config */ |
| 5002 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5003 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5004 | 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] | 5005 | } else { |
| 5006 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5007 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5008 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | /* mandatory */ |
| 5012 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5013 | 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] | 5014 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5015 | |
| 5016 | /* presence */ |
| 5017 | if (rfn->presence) { |
| 5018 | if (node->nodetype != LYS_CONTAINER) { |
| 5019 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5020 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5021 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5022 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5023 | } |
| 5024 | node->flags |= LYS_PRESENCE; |
| 5025 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5026 | |
| 5027 | /* must */ |
| 5028 | if (rfn->musts) { |
| 5029 | switch (node->nodetype) { |
| 5030 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5031 | 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] | 5032 | break; |
| 5033 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5034 | 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] | 5035 | break; |
| 5036 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5037 | 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] | 5038 | break; |
| 5039 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5040 | 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] | 5041 | break; |
| 5042 | case LYS_ANYXML: |
| 5043 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5044 | 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] | 5045 | break; |
| 5046 | default: |
| 5047 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5048 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5049 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5050 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5051 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5052 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5053 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5054 | |
| 5055 | /* min/max-elements */ |
| 5056 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5057 | switch (node->nodetype) { |
| 5058 | case LYS_LEAFLIST: |
| 5059 | if (rfn->flags & LYS_SET_MAX) { |
| 5060 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5061 | } |
| 5062 | if (rfn->flags & LYS_SET_MIN) { |
| 5063 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5064 | if (rfn->min) { |
| 5065 | node->flags |= LYS_MAND_TRUE; |
| 5066 | lys_compile_mandatory_parents(node->parent, 1); |
| 5067 | } else { |
| 5068 | node->flags &= ~LYS_MAND_TRUE; |
| 5069 | lys_compile_mandatory_parents(node->parent, 0); |
| 5070 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5071 | } |
| 5072 | break; |
| 5073 | case LYS_LIST: |
| 5074 | if (rfn->flags & LYS_SET_MAX) { |
| 5075 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5076 | } |
| 5077 | if (rfn->flags & LYS_SET_MIN) { |
| 5078 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5079 | if (rfn->min) { |
| 5080 | node->flags |= LYS_MAND_TRUE; |
| 5081 | lys_compile_mandatory_parents(node->parent, 1); |
| 5082 | } else { |
| 5083 | node->flags &= ~LYS_MAND_TRUE; |
| 5084 | lys_compile_mandatory_parents(node->parent, 0); |
| 5085 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5086 | } |
| 5087 | break; |
| 5088 | default: |
| 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 %s statement - %s cannot hold this statement.", |
| 5091 | (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] | 5092 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5093 | } |
| 5094 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5095 | |
| 5096 | /* if-feature */ |
| 5097 | if (rfn->iffeatures) { |
| 5098 | /* 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] | 5099 | 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] | 5100 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5101 | |
| 5102 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5103 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5104 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5105 | /* 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] | 5106 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5107 | node = (struct lysc_node*)refined.objs[i]; |
| 5108 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5109 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5110 | |
| 5111 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5112 | if ((node->flags & LYS_MAND_TRUE) && |
| 5113 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5114 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5115 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5116 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5117 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5121 | if (node->nodetype == LYS_LIST) { |
| 5122 | min = ((struct lysc_node_list*)node)->min; |
| 5123 | max = ((struct lysc_node_list*)node)->max; |
| 5124 | } else { |
| 5125 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5126 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5127 | } |
| 5128 | if (min > max) { |
| 5129 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5130 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5131 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5132 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | } |
| 5136 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5137 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5138 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5139 | |
| 5140 | cleanup: |
| 5141 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5142 | if (context_node_fake.child) { |
| 5143 | context_node_fake.child->prev = child; |
| 5144 | } |
| 5145 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5146 | child->parent = parent; |
| 5147 | } |
| 5148 | |
| 5149 | if (uses_p->augments || uses_p->refines) { |
| 5150 | /* 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] | 5151 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5152 | 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] | 5153 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5154 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5155 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5156 | 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] | 5157 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5158 | } |
| 5159 | } |
| 5160 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5161 | /* reload previous context's mod_def */ |
| 5162 | ctx->mod_def = mod_old; |
| 5163 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5164 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5165 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5166 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5167 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5168 | |
| 5169 | return ret; |
| 5170 | } |
| 5171 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5172 | static int |
| 5173 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5174 | { |
| 5175 | struct lysp_node *iter; |
| 5176 | int len = 0; |
| 5177 | |
| 5178 | *path = NULL; |
| 5179 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5180 | char *s = *path; |
| 5181 | char *id; |
| 5182 | |
| 5183 | switch (iter->nodetype) { |
| 5184 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5185 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5186 | break; |
| 5187 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5188 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5189 | break; |
| 5190 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5191 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5192 | break; |
| 5193 | default: |
| 5194 | id = strdup(iter->name); |
| 5195 | break; |
| 5196 | } |
| 5197 | |
| 5198 | if (!iter->parent) { |
| 5199 | /* print prefix */ |
| 5200 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5201 | } else { |
| 5202 | /* prefix is the same as in parent */ |
| 5203 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5204 | } |
| 5205 | free(s); |
| 5206 | free(id); |
| 5207 | } |
| 5208 | |
| 5209 | if (len < 0) { |
| 5210 | free(*path); |
| 5211 | *path = NULL; |
| 5212 | } else if (len == 0) { |
| 5213 | *path = strdup("/"); |
| 5214 | len = 1; |
| 5215 | } |
| 5216 | return len; |
| 5217 | } |
| 5218 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5219 | /** |
| 5220 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5221 | * |
| 5222 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5223 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5224 | */ |
| 5225 | static LY_ERR |
| 5226 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5227 | { |
| 5228 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5229 | char *path; |
| 5230 | int len; |
| 5231 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5232 | struct lysp_node_uses fake_uses = { |
| 5233 | .parent = node_p, |
| 5234 | .nodetype = LYS_USES, |
| 5235 | .flags = 0, .next = NULL, |
| 5236 | .name = grp->name, |
| 5237 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5238 | .refines = NULL, .augments = NULL |
| 5239 | }; |
| 5240 | struct lysc_node_container fake_container = { |
| 5241 | .nodetype = LYS_CONTAINER, |
| 5242 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5243 | .module = ctx->mod, |
| 5244 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5245 | .prev = (struct lysc_node*)&fake_container, |
| 5246 | .name = "fake", |
| 5247 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5248 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5249 | }; |
| 5250 | |
| 5251 | if (grp->parent) { |
| 5252 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5253 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5254 | |
| 5255 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5256 | if (len < 0) { |
| 5257 | LOGMEM(ctx->ctx); |
| 5258 | return LY_EMEM; |
| 5259 | } |
| 5260 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5261 | ctx->path_len = (uint16_t)len; |
| 5262 | free(path); |
| 5263 | |
| 5264 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5265 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5266 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5267 | lysc_update_path(ctx, NULL, NULL); |
| 5268 | lysc_update_path(ctx, NULL, NULL); |
| 5269 | |
| 5270 | ctx->path_len = 1; |
| 5271 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5272 | |
| 5273 | /* cleanup */ |
| 5274 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5275 | |
| 5276 | return ret; |
| 5277 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5278 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5279 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5280 | * @brief Compile parsed schema node information. |
| 5281 | * @param[in] ctx Compile context |
| 5282 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5283 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5284 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5285 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5286 | * @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). |
| 5287 | * 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] | 5288 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5289 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5290 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5291 | 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] | 5292 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5293 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5294 | struct lysc_node *node; |
| 5295 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5296 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5297 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5298 | 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] | 5299 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5300 | if (node_p->nodetype != LYS_USES) { |
| 5301 | lysc_update_path(ctx, parent, node_p->name); |
| 5302 | } else { |
| 5303 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5304 | lysc_update_path(ctx, NULL, node_p->name); |
| 5305 | } |
| 5306 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5307 | switch (node_p->nodetype) { |
| 5308 | case LYS_CONTAINER: |
| 5309 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5310 | node_compile_spec = lys_compile_node_container; |
| 5311 | break; |
| 5312 | case LYS_LEAF: |
| 5313 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5314 | node_compile_spec = lys_compile_node_leaf; |
| 5315 | break; |
| 5316 | case LYS_LIST: |
| 5317 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5318 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5319 | break; |
| 5320 | case LYS_LEAFLIST: |
| 5321 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5322 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5323 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5324 | case LYS_CHOICE: |
| 5325 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5326 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5327 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5328 | case LYS_ANYXML: |
| 5329 | case LYS_ANYDATA: |
| 5330 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5331 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5332 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5333 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5334 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5335 | lysc_update_path(ctx, NULL, NULL); |
| 5336 | lysc_update_path(ctx, NULL, NULL); |
| 5337 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5338 | default: |
| 5339 | LOGINT(ctx->ctx); |
| 5340 | return LY_EINT; |
| 5341 | } |
| 5342 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5343 | node->nodetype = node_p->nodetype; |
| 5344 | node->module = ctx->mod; |
| 5345 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5346 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5347 | |
| 5348 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5349 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5350 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5351 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5352 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5353 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5354 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5355 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5356 | node->flags |= LYS_CONFIG_R; |
| 5357 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5358 | /* config not explicitely set, inherit it from parent */ |
| 5359 | if (parent) { |
| 5360 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5361 | } else { |
| 5362 | /* default is config true */ |
| 5363 | node->flags |= LYS_CONFIG_W; |
| 5364 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5365 | } else { |
| 5366 | /* config set explicitely */ |
| 5367 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5368 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5369 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5370 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5371 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5372 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5373 | goto error; |
| 5374 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5375 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5376 | /* *list ordering */ |
| 5377 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5378 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5379 | 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] | 5380 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5381 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5382 | node->flags &= ~LYS_ORDBY_MASK; |
| 5383 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5384 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5385 | /* default ordering is system */ |
| 5386 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5387 | } |
| 5388 | } |
| 5389 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5390 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5391 | * 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] | 5392 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5393 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5394 | * 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] | 5395 | 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] | 5396 | } |
| 5397 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5398 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5399 | node->sp = node_p; |
| 5400 | } |
| 5401 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5402 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5403 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5404 | if (node_p->when) { |
| 5405 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5406 | 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] | 5407 | |
| 5408 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5409 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5410 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5411 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5412 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5413 | 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] | 5414 | |
| 5415 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5416 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5417 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5418 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5419 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5420 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5421 | if (node->flags & LYS_MAND_TRUE) { |
| 5422 | lys_compile_mandatory_parents(parent, 1); |
| 5423 | } |
| 5424 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5425 | lysc_update_path(ctx, NULL, NULL); |
| 5426 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5427 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5428 | if (parent) { |
| 5429 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5430 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5431 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5432 | } else { |
| 5433 | lysc_update_path(ctx, parent, node->name); |
| 5434 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5435 | 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] | 5436 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5437 | if (uses_status) { |
| 5438 | |
| 5439 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5440 | /* 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] | 5441 | * it directly gets the same status flags as the choice; |
| 5442 | * 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] | 5443 | 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] | 5444 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5445 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5446 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5447 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5448 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5449 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5450 | 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] | 5451 | |
| 5452 | if (parent->nodetype == LYS_CHOICE) { |
| 5453 | lysc_update_path(ctx, NULL, NULL); |
| 5454 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5455 | } else { |
| 5456 | /* top-level element */ |
| 5457 | if (!ctx->mod->compiled->data) { |
| 5458 | ctx->mod->compiled->data = node; |
| 5459 | } else { |
| 5460 | /* insert at the end of the module's top-level nodes list */ |
| 5461 | ctx->mod->compiled->data->prev->next = node; |
| 5462 | node->prev = ctx->mod->compiled->data->prev; |
| 5463 | ctx->mod->compiled->data->prev = node; |
| 5464 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5465 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5466 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5467 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5468 | return LY_EVALID; |
| 5469 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5470 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5471 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5472 | |
| 5473 | return LY_SUCCESS; |
| 5474 | |
| 5475 | error: |
| 5476 | lysc_node_free(ctx->ctx, node); |
| 5477 | return ret; |
| 5478 | } |
| 5479 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5480 | static void |
| 5481 | lysc_disconnect(struct lysc_node *node) |
| 5482 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5483 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5484 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5485 | int remove_cs = 0; |
| 5486 | |
| 5487 | parent = node->parent; |
| 5488 | |
| 5489 | /* parent's first child */ |
| 5490 | if (!parent) { |
| 5491 | return; |
| 5492 | } |
| 5493 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5494 | cs = (struct lysc_node_case*)node; |
| 5495 | } else if (parent->nodetype == LYS_CASE) { |
| 5496 | /* disconnecting some node in a case */ |
| 5497 | cs = (struct lysc_node_case*)parent; |
| 5498 | parent = cs->parent; |
| 5499 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5500 | if (child == node) { |
| 5501 | if (cs->child == child) { |
| 5502 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5503 | /* case with a single child -> remove also the case */ |
| 5504 | child->parent = NULL; |
| 5505 | remove_cs = 1; |
| 5506 | } else { |
| 5507 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5508 | } |
| 5509 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5510 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5511 | } |
| 5512 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5513 | if (!remove_cs) { |
| 5514 | cs = NULL; |
| 5515 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5516 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5517 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5518 | } |
| 5519 | |
| 5520 | if (cs) { |
| 5521 | if (remove_cs) { |
| 5522 | /* cs has only one child which is being also removed */ |
| 5523 | lysc_disconnect((struct lysc_node*)cs); |
| 5524 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5525 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5526 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5527 | /* default case removed */ |
| 5528 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5529 | } |
| 5530 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5531 | /* first case removed */ |
| 5532 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5533 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5534 | if (cs->child) { |
| 5535 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5536 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5537 | prev = cs->child->prev; |
| 5538 | } /* else all the children are under a single case */ |
| 5539 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5540 | if (child->parent != (struct lysc_node*)cs) { |
| 5541 | break; |
| 5542 | } |
| 5543 | lysc_node_free(node->module->ctx, child); |
| 5544 | } |
| 5545 | if (prev) { |
| 5546 | if (prev->next) { |
| 5547 | prev->next = child; |
| 5548 | } |
| 5549 | if (child) { |
| 5550 | child->prev = prev; |
| 5551 | } else { |
| 5552 | /* link from the first child under the cases */ |
| 5553 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5554 | } |
| 5555 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5556 | } |
| 5557 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5561 | if (node->prev->next) { |
| 5562 | node->prev->next = node->next; |
| 5563 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5564 | if (node->next) { |
| 5565 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5566 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5567 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5568 | if (child) { |
| 5569 | child->prev = node->prev; |
| 5570 | } |
| 5571 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5572 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5573 | } |
| 5574 | } |
| 5575 | |
| 5576 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5577 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5578 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5579 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5580 | struct ly_set devs_p = {0}; |
| 5581 | struct ly_set targets = {0}; |
| 5582 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5583 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5584 | struct lysc_action *rpcs; |
| 5585 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5586 | struct lysp_deviation *dev; |
| 5587 | struct lysp_deviate *d, **dp_new; |
| 5588 | struct lysp_deviate_add *d_add; |
| 5589 | struct lysp_deviate_del *d_del; |
| 5590 | struct lysp_deviate_rpl *d_rpl; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5591 | LY_ARRAY_COUNT_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5592 | struct lysc_deviation { |
| 5593 | const char *nodeid; |
| 5594 | struct lysc_node *target; /* target node of the deviation */ |
| 5595 | 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] | 5596 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5597 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5598 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5599 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5600 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5601 | const char *prefix, *name, *nodeid, *dflt; |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 5602 | struct lys_module *mod, **dev_mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5603 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5604 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5605 | |
| 5606 | /* get all deviations from the module and all its submodules ... */ |
| 5607 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5608 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5609 | } |
| 5610 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5611 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5612 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5613 | } |
| 5614 | } |
| 5615 | if (!devs_p.count) { |
| 5616 | /* nothing to do */ |
| 5617 | return LY_SUCCESS; |
| 5618 | } |
| 5619 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5620 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5621 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5622 | /* ... and group them by the target node */ |
| 5623 | devs = calloc(devs_p.count, sizeof *devs); |
| 5624 | for (u = 0; u < devs_p.count; ++u) { |
| 5625 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5626 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5627 | |
| 5628 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5629 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5630 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5631 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5632 | /* move the target pointer to input/output to make them different from the action and |
| 5633 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5634 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5635 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5636 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5637 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5638 | flags |= LYSC_OPT_INTERNAL; |
| 5639 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5640 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5641 | flags |= LYSC_OPT_INTERNAL; |
| 5642 | } |
| 5643 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5644 | /* insert into the set of targets with duplicity detection */ |
| 5645 | i = ly_set_add(&targets, target, 0); |
| 5646 | if (!devs[i]) { |
| 5647 | /* new record */ |
| 5648 | devs[i] = calloc(1, sizeof **devs); |
| 5649 | devs[i]->target = target; |
| 5650 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5651 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5652 | } |
| 5653 | /* add deviates into the deviation's list of deviates */ |
| 5654 | for (d = dev->deviates; d; d = d->next) { |
| 5655 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5656 | *dp_new = d; |
| 5657 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5658 | devs[i]->not_supported = 1; |
| 5659 | } |
| 5660 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5661 | |
| 5662 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | /* MACROS for deviates checking */ |
| 5666 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5667 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5668 | 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] | 5669 | goto cleanup; \ |
| 5670 | } |
| 5671 | |
| 5672 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5673 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5675 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5676 | goto cleanup; \ |
| 5677 | } |
| 5678 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5679 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5680 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5681 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5682 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5683 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5684 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5685 | goto cleanup; \ |
| 5686 | } |
| 5687 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5688 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5689 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5690 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5691 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5692 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5693 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5694 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5695 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5696 | goto cleanup; \ |
| 5697 | } |
| 5698 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5699 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5700 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 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 adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5703 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5704 | goto cleanup; \ |
| 5705 | } |
| 5706 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5707 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5708 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5709 | 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] | 5710 | goto cleanup; \ |
| 5711 | } |
| 5712 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5713 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5714 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5716 | "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] | 5717 | goto cleanup; \ |
| 5718 | } |
| 5719 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5720 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5721 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5722 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5723 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5724 | 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] | 5725 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5726 | if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5727 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5728 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5729 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5730 | goto cleanup; \ |
| 5731 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5732 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5733 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5734 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5735 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5736 | (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] | 5737 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5738 | if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5739 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5740 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | /* apply deviations */ |
| 5744 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5745 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5746 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5747 | struct ly_err_item *err = NULL; |
| 5748 | |
| 5749 | dflt = NULL; |
| 5750 | changed_type = 0; |
| 5751 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5752 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5753 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5754 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5755 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5756 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5757 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5758 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5759 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5760 | } |
| 5761 | } |
| 5762 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5763 | /* not-supported */ |
| 5764 | if (devs[u]->not_supported) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5765 | if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) { |
| 5766 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.", |
| 5767 | LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5768 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5769 | |
| 5770 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5771 | if (devs[u]->target->parent) { \ |
| 5772 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5773 | } else { \ |
| 5774 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5775 | } \ |
| 5776 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5777 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5778 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5779 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5780 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5781 | 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] | 5782 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5783 | } |
| 5784 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5785 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5786 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5787 | /* remove RPC's/action's input */ |
| 5788 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5789 | 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] | 5790 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5791 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5792 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5793 | /* remove RPC's/action's output */ |
| 5794 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5795 | 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] | 5796 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5797 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5798 | } else { |
| 5799 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5800 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5801 | } |
| 5802 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5803 | /* remove Notification */ |
| 5804 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5805 | } else { |
| 5806 | /* remove the target node */ |
| 5807 | lysc_disconnect(devs[u]->target); |
| 5808 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5809 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5810 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5811 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5812 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5813 | continue; |
| 5814 | } |
| 5815 | |
| 5816 | /* list of deviates (not-supported is not present in the list) */ |
| 5817 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5818 | d = devs[u]->deviates[v]; |
| 5819 | |
| 5820 | switch (d->mod) { |
| 5821 | case LYS_DEV_ADD: |
| 5822 | d_add = (struct lysp_deviate_add*)d; |
| 5823 | /* [units-stmt] */ |
| 5824 | if (d_add->units) { |
| 5825 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5826 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5827 | |
| 5828 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5829 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5830 | } |
| 5831 | |
| 5832 | /* *must-stmt */ |
| 5833 | if (d_add->musts) { |
| 5834 | switch (devs[u]->target->nodetype) { |
| 5835 | case LYS_CONTAINER: |
| 5836 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5837 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5838 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5839 | break; |
| 5840 | case LYS_LEAF: |
| 5841 | case LYS_LEAFLIST: |
| 5842 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5843 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5844 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5845 | break; |
| 5846 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5847 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5848 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5849 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5850 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5851 | case LYS_ACTION: |
| 5852 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5853 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5854 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5855 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 5856 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5857 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5858 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5859 | break; |
| 5860 | } |
| 5861 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5862 | default: |
| 5863 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5864 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5865 | goto cleanup; |
| 5866 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5867 | ly_set_add(&ctx->xpath, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5871 | if (d_add->uniques) { |
| 5872 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5873 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5874 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5875 | |
| 5876 | /* *default-stmt */ |
| 5877 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5878 | switch (devs[u]->target->nodetype) { |
| 5879 | case LYS_LEAF: |
| 5880 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5881 | 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] | 5882 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5883 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5884 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5885 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5886 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5887 | } else { |
| 5888 | /* prepare new default value storage */ |
| 5889 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5890 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5891 | dflt = d_add->dflts[0]; |
| 5892 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5893 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5894 | /* mark the new default values as leaf's own */ |
| 5895 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5896 | break; |
| 5897 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5898 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5899 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5900 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5901 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5902 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5903 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5904 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5905 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5906 | LY_ARRAY_FREE(llist->dflts); |
| 5907 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5908 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5909 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5910 | } |
| 5911 | /* add new default value(s) */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5912 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5913 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5914 | for (x = y = LY_ARRAY_COUNT(llist->dflts); |
| 5915 | x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5916 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5917 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5918 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5919 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5920 | llist->dflts[x]->realtype = llist->type; |
| 5921 | 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] | 5922 | 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] | 5923 | (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] | 5924 | llist->dflts[x]->realtype->refcount++; |
| 5925 | if (err) { |
| 5926 | ly_err_print(err); |
| 5927 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5928 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5929 | d_add->dflts[x - y], err->msg); |
| 5930 | ly_err_free(err); |
| 5931 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5932 | if (rc == LY_EINCOMPLETE) { |
| 5933 | /* postpone default compilation when the tree is complete */ |
| 5934 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5935 | |
| 5936 | /* but in general result is so far ok */ |
| 5937 | rc = LY_SUCCESS; |
| 5938 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5939 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5940 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5941 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5942 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5943 | break; |
| 5944 | case LYS_CHOICE: |
| 5945 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5946 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5947 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5948 | * 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] | 5949 | 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] | 5950 | goto cleanup; |
| 5951 | } |
| 5952 | break; |
| 5953 | default: |
| 5954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5955 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5956 | goto cleanup; |
| 5957 | } |
| 5958 | } |
| 5959 | |
| 5960 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5961 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5962 | 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] | 5963 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5964 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5965 | goto cleanup; |
| 5966 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5967 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5968 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5969 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5970 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5971 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5972 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5973 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5974 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5975 | goto cleanup; |
| 5976 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5977 | 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] | 5978 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5979 | |
| 5980 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5981 | if (d_add->flags & LYS_MAND_MASK) { |
| 5982 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5983 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5984 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5985 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5986 | goto cleanup; |
| 5987 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5988 | 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] | 5989 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5990 | |
| 5991 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5992 | if (d_add->flags & LYS_SET_MIN) { |
| 5993 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5994 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5995 | /* change value */ |
| 5996 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5997 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5998 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5999 | /* change value */ |
| 6000 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6001 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6002 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6003 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6004 | goto cleanup; |
| 6005 | } |
| 6006 | if (d_add->min) { |
| 6007 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6008 | } |
| 6009 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6010 | |
| 6011 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6012 | if (d_add->flags & LYS_SET_MAX) { |
| 6013 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6014 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6015 | /* change value */ |
| 6016 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6017 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6018 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6019 | /* change value */ |
| 6020 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6021 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6022 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6023 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6024 | goto cleanup; |
| 6025 | } |
| 6026 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6027 | |
| 6028 | break; |
| 6029 | case LYS_DEV_DELETE: |
| 6030 | d_del = (struct lysp_deviate_del*)d; |
| 6031 | |
| 6032 | /* [units-stmt] */ |
| 6033 | if (d_del->units) { |
| 6034 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6035 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6036 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6037 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6038 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6039 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6040 | goto cleanup; |
| 6041 | } |
| 6042 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6043 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6044 | } |
| 6045 | |
| 6046 | /* *must-stmt */ |
| 6047 | if (d_del->musts) { |
| 6048 | switch (devs[u]->target->nodetype) { |
| 6049 | case LYS_CONTAINER: |
| 6050 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6051 | 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] | 6052 | break; |
| 6053 | case LYS_LEAF: |
| 6054 | case LYS_LEAFLIST: |
| 6055 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6056 | 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] | 6057 | break; |
| 6058 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6059 | 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] | 6060 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6061 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6062 | case LYS_ACTION: |
| 6063 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6064 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6065 | break; |
| 6066 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6067 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6068 | break; |
| 6069 | } |
| 6070 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6071 | default: |
| 6072 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6073 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6074 | goto cleanup; |
| 6075 | } |
| 6076 | } |
| 6077 | |
| 6078 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6079 | if (d_del->uniques) { |
| 6080 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6081 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6082 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6083 | LY_ARRAY_FOR(list->uniques, z) { |
| 6084 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6085 | nodeid = strpbrk(name, " \t\n"); |
| 6086 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6087 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6088 | break; |
| 6089 | } |
| 6090 | while (isspace(*nodeid)) { |
| 6091 | ++nodeid; |
| 6092 | } |
| 6093 | } else { |
| 6094 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6095 | break; |
| 6096 | } |
| 6097 | } |
| 6098 | } |
| 6099 | if (!name) { |
| 6100 | /* complete match - remove the unique */ |
| 6101 | LY_ARRAY_DECREMENT(list->uniques); |
| 6102 | LY_ARRAY_FREE(list->uniques[z]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6103 | 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] | 6104 | --z; |
| 6105 | break; |
| 6106 | } |
| 6107 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6108 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6109 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6110 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6111 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6112 | goto cleanup; |
| 6113 | } |
| 6114 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6115 | if (!LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6116 | LY_ARRAY_FREE(list->uniques); |
| 6117 | list->uniques = NULL; |
| 6118 | } |
| 6119 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6120 | |
| 6121 | /* *default-stmt */ |
| 6122 | if (d_del->dflts) { |
| 6123 | switch (devs[u]->target->nodetype) { |
| 6124 | case LYS_LEAF: |
| 6125 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6126 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6127 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6128 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6129 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6130 | 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] | 6131 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6132 | if (i) { |
| 6133 | free((char*)dflt); |
| 6134 | } |
| 6135 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6136 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6137 | d_del->dflts[0], dflt); |
| 6138 | goto cleanup; |
| 6139 | } |
| 6140 | if (i) { |
| 6141 | free((char*)dflt); |
| 6142 | } |
| 6143 | dflt = NULL; |
| 6144 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6145 | /* update the list of incomplete default values if needed */ |
| 6146 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6147 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6148 | /* remove the default specification */ |
| 6149 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6150 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6151 | free(leaf->dflt); |
| 6152 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6153 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6154 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6155 | break; |
| 6156 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6157 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6158 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6159 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6160 | 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] | 6161 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6162 | if (i) { |
| 6163 | free((char*)dflt); |
| 6164 | } |
| 6165 | dflt = NULL; |
| 6166 | break; |
| 6167 | } |
| 6168 | if (i) { |
| 6169 | free((char*)dflt); |
| 6170 | } |
| 6171 | dflt = NULL; |
| 6172 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6173 | if (y == LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6174 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6175 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6176 | goto cleanup; |
| 6177 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6178 | |
| 6179 | /* update the list of incomplete default values if needed */ |
| 6180 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6181 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6182 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6183 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6184 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6185 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6186 | free(llist->dflts[y]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6187 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 6188 | 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] | 6189 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6190 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6191 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6192 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6193 | LY_ARRAY_FREE(llist->dflts); |
| 6194 | llist->dflts = NULL; |
| 6195 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6196 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6197 | break; |
| 6198 | case LYS_CHOICE: |
| 6199 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6200 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6201 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6202 | 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] | 6203 | if (prefix) { |
| 6204 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6205 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6206 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6207 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6208 | "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] | 6209 | goto cleanup; |
| 6210 | } |
| 6211 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6212 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6213 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6214 | "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] | 6215 | goto cleanup; |
| 6216 | } |
| 6217 | } |
| 6218 | /* else { |
| 6219 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6220 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6221 | * the module of the default case, just matching its name */ |
| 6222 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6223 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6224 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6225 | 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] | 6226 | goto cleanup; |
| 6227 | } |
| 6228 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6229 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6230 | break; |
| 6231 | default: |
| 6232 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6233 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6234 | goto cleanup; |
| 6235 | } |
| 6236 | } |
| 6237 | |
| 6238 | break; |
| 6239 | case LYS_DEV_REPLACE: |
| 6240 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6241 | |
| 6242 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6243 | if (d_rpl->type) { |
| 6244 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6245 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6246 | 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] | 6247 | |
| 6248 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6249 | /* the target has default from the previous type - remove it */ |
| 6250 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6251 | /* update the list of incomplete default values if needed */ |
| 6252 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6253 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6254 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6255 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6256 | free(leaf->dflt); |
| 6257 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6258 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6259 | } else { /* LYS_LEAFLIST */ |
| 6260 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6261 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6262 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6263 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6264 | free(llist->dflts[x]); |
| 6265 | } |
| 6266 | LY_ARRAY_FREE(llist->dflts); |
| 6267 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6268 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6269 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6270 | } |
| 6271 | } |
| 6272 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6273 | /* there is no default value, do not set changed_type after type compilation |
| 6274 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6275 | changed_type = -1; |
| 6276 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6277 | 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] | 6278 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6279 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6280 | |
| 6281 | /* [units-stmt] */ |
| 6282 | if (d_rpl->units) { |
| 6283 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6284 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6285 | units, "replacing", "units", d_rpl->units); |
| 6286 | |
| 6287 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6288 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6289 | } |
| 6290 | |
| 6291 | /* [default-stmt] */ |
| 6292 | if (d_rpl->dflt) { |
| 6293 | switch (devs[u]->target->nodetype) { |
| 6294 | case LYS_LEAF: |
| 6295 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6296 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6297 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6298 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6299 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6300 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6301 | dflt = d_rpl->dflt; |
| 6302 | /* 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] | 6303 | break; |
| 6304 | case LYS_CHOICE: |
| 6305 | 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] | 6306 | 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] | 6307 | goto cleanup; |
| 6308 | } |
| 6309 | break; |
| 6310 | default: |
| 6311 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6312 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6313 | goto cleanup; |
| 6314 | } |
| 6315 | } |
| 6316 | |
| 6317 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6318 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6319 | 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] | 6320 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6321 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6322 | goto cleanup; |
| 6323 | } |
| 6324 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6325 | 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] | 6326 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6327 | goto cleanup; |
| 6328 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6329 | 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] | 6330 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6331 | |
| 6332 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6333 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6334 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6335 | 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] | 6336 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6337 | goto cleanup; |
| 6338 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6339 | 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] | 6340 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6341 | |
| 6342 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6343 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6344 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6345 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6346 | /* change value */ |
| 6347 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6348 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6349 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6350 | /* change value */ |
| 6351 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6352 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6353 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6354 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6355 | goto cleanup; |
| 6356 | } |
| 6357 | if (d_rpl->min) { |
| 6358 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6359 | } |
| 6360 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6361 | |
| 6362 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6363 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6364 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6365 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6366 | /* change value */ |
| 6367 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6368 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6369 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6370 | /* change value */ |
| 6371 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6372 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6373 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6374 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6375 | goto cleanup; |
| 6376 | } |
| 6377 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6378 | |
| 6379 | break; |
| 6380 | default: |
| 6381 | LOGINT(ctx->ctx); |
| 6382 | goto cleanup; |
| 6383 | } |
| 6384 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6385 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6386 | /* final check when all deviations of a single target node are applied */ |
| 6387 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6388 | /* check min-max compatibility */ |
| 6389 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6390 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6391 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6392 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6393 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6394 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6395 | } else { |
| 6396 | min = max = 0; |
| 6397 | } |
| 6398 | if (min > max) { |
| 6399 | 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] | 6400 | "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] | 6401 | goto cleanup; |
| 6402 | } |
| 6403 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6404 | if (dflt) { |
| 6405 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6406 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6407 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6408 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6409 | 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] | 6410 | 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] | 6411 | leaf->dflt->realtype->refcount++; |
| 6412 | if (err) { |
| 6413 | ly_err_print(err); |
| 6414 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6415 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6416 | ly_err_free(err); |
| 6417 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6418 | if (rc == LY_EINCOMPLETE) { |
| 6419 | /* postpone default compilation when the tree is complete */ |
| 6420 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6421 | |
| 6422 | /* but in general result is so far ok */ |
| 6423 | rc = LY_SUCCESS; |
| 6424 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6425 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6426 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6427 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6428 | int dynamic; |
| 6429 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6430 | 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] | 6431 | |
| 6432 | /* update the list of incomplete default values if needed */ |
| 6433 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6434 | |
| 6435 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6436 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6437 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6438 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6439 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6440 | 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] | 6441 | 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] | 6442 | leaf->dflt->realtype->refcount++; |
| 6443 | if (err) { |
| 6444 | ly_err_print(err); |
| 6445 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6446 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6447 | ly_err_free(err); |
| 6448 | } |
| 6449 | if (dynamic) { |
| 6450 | free((void*)dflt); |
| 6451 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6452 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6453 | if (rc == LY_EINCOMPLETE) { |
| 6454 | /* postpone default compilation when the tree is complete */ |
| 6455 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6456 | |
| 6457 | /* but in general result is so far ok */ |
| 6458 | rc = LY_SUCCESS; |
| 6459 | } |
| 6460 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6461 | } else { /* LYS_LEAFLIST */ |
| 6462 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6463 | 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] | 6464 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6465 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6466 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6467 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6468 | 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] | 6469 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6470 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6471 | llist->dflts[x]->realtype->refcount++; |
| 6472 | if (err) { |
| 6473 | ly_err_print(err); |
| 6474 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6475 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6476 | dflt, err->msg); |
| 6477 | ly_err_free(err); |
| 6478 | } |
| 6479 | if (dynamic) { |
| 6480 | free((void*)dflt); |
| 6481 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6482 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6483 | if (rc == LY_EINCOMPLETE) { |
| 6484 | /* postpone default compilation when the tree is complete */ |
| 6485 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6486 | |
| 6487 | /* but in general result is so far ok */ |
| 6488 | rc = LY_SUCCESS; |
| 6489 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6490 | LY_CHECK_GOTO(rc, cleanup); |
| 6491 | } |
| 6492 | } |
| 6493 | } |
| 6494 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6495 | /* check mandatory - default compatibility */ |
| 6496 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6497 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6498 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6499 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6500 | "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] | 6501 | goto cleanup; |
| 6502 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6503 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6504 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6505 | 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] | 6506 | goto cleanup; |
| 6507 | } |
| 6508 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6509 | /* mandatory node under a default case */ |
| 6510 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6511 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6512 | 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] | 6513 | goto cleanup; |
| 6514 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6515 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6516 | /* add this module into the target module deviated_by, if not there already */ |
| 6517 | rc = LY_SUCCESS; |
| 6518 | LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) { |
| 6519 | if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) { |
| 6520 | rc = LY_EEXIST; |
| 6521 | break; |
| 6522 | } |
| 6523 | } |
| 6524 | if (!rc) { |
| 6525 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup); |
| 6526 | *dev_mod = mod_p->mod; |
| 6527 | } |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 6528 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6529 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6530 | } |
| 6531 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6532 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6533 | ret = LY_SUCCESS; |
| 6534 | |
| 6535 | cleanup: |
| 6536 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6537 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6538 | free(devs[u]); |
| 6539 | } |
| 6540 | free(devs); |
| 6541 | ly_set_erase(&targets, NULL); |
| 6542 | ly_set_erase(&devs_p, NULL); |
| 6543 | |
| 6544 | return ret; |
| 6545 | } |
| 6546 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6547 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6548 | * @brief Compile the given YANG submodule into the main module. |
| 6549 | * @param[in] ctx Compile context |
| 6550 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6551 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6552 | */ |
| 6553 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6554 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6555 | { |
| 6556 | unsigned int u; |
| 6557 | LY_ERR ret = LY_SUCCESS; |
| 6558 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6559 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6560 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6561 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6562 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6563 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6564 | /* features are compiled directly into the compiled module structure, |
| 6565 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6566 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6567 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6568 | LY_CHECK_GOTO(ret, error); |
| 6569 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6570 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6571 | if (!mainmod->mod->dis_identities) { |
| 6572 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6573 | LY_CHECK_GOTO(ret, error); |
| 6574 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6575 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6576 | /* data nodes */ |
| 6577 | LY_LIST_FOR(submod->data, node_p) { |
| 6578 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6579 | LY_CHECK_GOTO(ret, error); |
| 6580 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6581 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6582 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6583 | 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] | 6584 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6585 | error: |
| 6586 | return ret; |
| 6587 | } |
| 6588 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6589 | static void * |
| 6590 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6591 | { |
| 6592 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6593 | if (substmts[u].stmt == stmt) { |
| 6594 | return substmts[u].storage; |
| 6595 | } |
| 6596 | } |
| 6597 | return NULL; |
| 6598 | } |
| 6599 | |
| 6600 | LY_ERR |
| 6601 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6602 | { |
| 6603 | LY_ERR ret = LY_EVALID, r; |
| 6604 | unsigned int u; |
| 6605 | struct lysp_stmt *stmt; |
| 6606 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6607 | |
| 6608 | /* check for invalid substatements */ |
| 6609 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6610 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6611 | continue; |
| 6612 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6613 | for (u = 0; substmts[u].stmt; ++u) { |
| 6614 | if (substmts[u].stmt == stmt->kw) { |
| 6615 | break; |
| 6616 | } |
| 6617 | } |
| 6618 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6619 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6620 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6621 | goto cleanup; |
| 6622 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6623 | } |
| 6624 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6625 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6626 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6627 | /* keep order of the processing the same as the order in the defined substmts, |
| 6628 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6629 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6630 | int stmt_present = 0; |
| 6631 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6632 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6633 | if (substmts[u].stmt != stmt->kw) { |
| 6634 | continue; |
| 6635 | } |
| 6636 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6637 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6638 | if (substmts[u].storage) { |
| 6639 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6640 | case LY_STMT_STATUS: |
| 6641 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6642 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6643 | break; |
| 6644 | case LY_STMT_UNITS: { |
| 6645 | const char **units; |
| 6646 | |
| 6647 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6648 | /* single item */ |
| 6649 | if (*((const char **)substmts[u].storage)) { |
| 6650 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6651 | goto cleanup; |
| 6652 | } |
| 6653 | units = (const char **)substmts[u].storage; |
| 6654 | } else { |
| 6655 | /* sized array */ |
| 6656 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6657 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6658 | } |
| 6659 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6660 | break; |
| 6661 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6662 | case LY_STMT_TYPE: { |
| 6663 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6664 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6665 | |
| 6666 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6667 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6668 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6670 | goto cleanup; |
| 6671 | } |
| 6672 | compiled = substmts[u].storage; |
| 6673 | } else { |
| 6674 | /* sized array */ |
| 6675 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6676 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6677 | compiled = (void*)type; |
| 6678 | } |
| 6679 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6680 | 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] | 6681 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6682 | 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] | 6683 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6684 | lysp_type_free(ctx->ctx, parsed); |
| 6685 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6686 | break; |
| 6687 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6688 | case LY_STMT_IF_FEATURE: { |
| 6689 | struct lysc_iffeature *iff = NULL; |
| 6690 | |
| 6691 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6692 | /* single item */ |
| 6693 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6694 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6695 | goto cleanup; |
| 6696 | } |
| 6697 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6698 | } else { |
| 6699 | /* sized array */ |
| 6700 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6701 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6702 | } |
| 6703 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6704 | break; |
| 6705 | } |
| 6706 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6707 | * 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] | 6708 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6709 | 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.", |
| 6710 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6711 | goto cleanup; |
| 6712 | } |
| 6713 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6714 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6715 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6716 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6717 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6718 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6719 | goto cleanup; |
| 6720 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6721 | } |
| 6722 | |
| 6723 | ret = LY_SUCCESS; |
| 6724 | |
| 6725 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6726 | return ret; |
| 6727 | } |
| 6728 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6729 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6730 | * @brief Check when for cyclic dependencies. |
| 6731 | * @param[in] set Set with all the referenced nodes. |
| 6732 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6733 | * @return LY_ERR value |
| 6734 | */ |
| 6735 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6736 | 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] | 6737 | { |
| 6738 | struct lyxp_set tmp_set; |
| 6739 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6740 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6741 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6742 | int idx; |
| 6743 | struct lysc_when *when; |
| 6744 | LY_ERR ret = LY_SUCCESS; |
| 6745 | |
| 6746 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6747 | |
| 6748 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6749 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6750 | xp_scnode = &set->val.scnodes[i]; |
| 6751 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6752 | if (xp_scnode->in_ctx != -1) { |
| 6753 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6754 | xp_scnode->in_ctx = 1; |
| 6755 | } |
| 6756 | } |
| 6757 | |
| 6758 | for (i = 0; i < set->used; ++i) { |
| 6759 | xp_scnode = &set->val.scnodes[i]; |
| 6760 | if (xp_scnode->in_ctx != 1) { |
| 6761 | /* already checked */ |
| 6762 | continue; |
| 6763 | } |
| 6764 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6765 | 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] | 6766 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6767 | /* no when to check */ |
| 6768 | xp_scnode->in_ctx = 0; |
| 6769 | continue; |
| 6770 | } |
| 6771 | |
| 6772 | node = xp_scnode->scnode; |
| 6773 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6774 | LY_ARRAY_FOR(node->when, u) { |
| 6775 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 6776 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6777 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6778 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6779 | 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] | 6780 | goto cleanup; |
| 6781 | } |
| 6782 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6783 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6784 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6785 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6786 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6787 | 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] | 6788 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6789 | 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] | 6790 | ret = LY_EVALID; |
| 6791 | goto cleanup; |
| 6792 | } |
| 6793 | |
| 6794 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6795 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6796 | } else { |
| 6797 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6798 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6799 | } |
| 6800 | } |
| 6801 | |
| 6802 | /* merge this set into the global when set */ |
| 6803 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6804 | } |
| 6805 | |
| 6806 | /* check when of non-data parents as well */ |
| 6807 | node = node->parent; |
| 6808 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6809 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6810 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6811 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6812 | } |
| 6813 | |
| 6814 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6815 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6816 | return ret; |
| 6817 | } |
| 6818 | |
| 6819 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6820 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6821 | * @param[in] ctx Compile context. |
| 6822 | * @param[in] node Node to check. |
| 6823 | * @return LY_ERR value |
| 6824 | */ |
| 6825 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6826 | 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] | 6827 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6828 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6829 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6830 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6831 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6832 | struct lysc_when **when = NULL; |
| 6833 | struct lysc_must *musts = NULL; |
| 6834 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6835 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6836 | |
| 6837 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6838 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6839 | if (node->flags & LYS_CONFIG_R) { |
| 6840 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6841 | if (op) { |
| 6842 | /* we are actually in output */ |
| 6843 | opts = LYXP_SCNODE_OUTPUT; |
| 6844 | } |
| 6845 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6846 | |
| 6847 | switch (node->nodetype) { |
| 6848 | case LYS_CONTAINER: |
| 6849 | when = ((struct lysc_node_container *)node)->when; |
| 6850 | musts = ((struct lysc_node_container *)node)->musts; |
| 6851 | break; |
| 6852 | case LYS_CHOICE: |
| 6853 | when = ((struct lysc_node_choice *)node)->when; |
| 6854 | break; |
| 6855 | case LYS_LEAF: |
| 6856 | when = ((struct lysc_node_leaf *)node)->when; |
| 6857 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6858 | break; |
| 6859 | case LYS_LEAFLIST: |
| 6860 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6861 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6862 | break; |
| 6863 | case LYS_LIST: |
| 6864 | when = ((struct lysc_node_list *)node)->when; |
| 6865 | musts = ((struct lysc_node_list *)node)->musts; |
| 6866 | break; |
| 6867 | case LYS_ANYXML: |
| 6868 | case LYS_ANYDATA: |
| 6869 | when = ((struct lysc_node_anydata *)node)->when; |
| 6870 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6871 | break; |
| 6872 | case LYS_CASE: |
| 6873 | when = ((struct lysc_node_case *)node)->when; |
| 6874 | break; |
| 6875 | case LYS_NOTIF: |
| 6876 | musts = ((struct lysc_notif *)node)->musts; |
| 6877 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6878 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6879 | case LYS_ACTION: |
| 6880 | /* first process input musts */ |
| 6881 | musts = ((struct lysc_action *)node)->input.musts; |
| 6882 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6883 | default: |
| 6884 | /* nothing to check */ |
| 6885 | break; |
| 6886 | } |
| 6887 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6888 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6889 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6890 | 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] | 6891 | 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] | 6892 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6893 | 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] | 6894 | goto cleanup; |
| 6895 | } |
| 6896 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6897 | ctx->path[0] = '\0'; |
| 6898 | 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] | 6899 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6900 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6901 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6902 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6903 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6904 | /* 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] | 6905 | 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] | 6906 | schema->name); |
| 6907 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6908 | |
| 6909 | /* check dummy node accessing */ |
| 6910 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6911 | 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] | 6912 | ret = LY_EVALID; |
| 6913 | goto cleanup; |
| 6914 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6915 | } |
| 6916 | } |
| 6917 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6918 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6919 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6920 | LY_CHECK_GOTO(ret, cleanup); |
| 6921 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6922 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6923 | } |
| 6924 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6925 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6926 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6927 | LY_ARRAY_FOR(musts, u) { |
| 6928 | 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] | 6929 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6930 | 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] | 6931 | goto cleanup; |
| 6932 | } |
| 6933 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6934 | ctx->path[0] = '\0'; |
| 6935 | 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] | 6936 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6937 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6938 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6939 | /* 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] | 6940 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6941 | 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] | 6942 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6943 | } |
| 6944 | } |
| 6945 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6946 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6947 | } |
| 6948 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6949 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6950 | /* now check output musts */ |
| 6951 | input_done = 1; |
| 6952 | musts = ((struct lysc_action *)node)->output.musts; |
| 6953 | opts = LYXP_SCNODE_OUTPUT; |
| 6954 | goto check_musts; |
| 6955 | } |
| 6956 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6957 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6958 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6959 | return ret; |
| 6960 | } |
| 6961 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6962 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6963 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6964 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6965 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6966 | struct ly_path *p; |
| 6967 | struct lysc_type *type; |
| 6968 | |
| 6969 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6970 | |
| 6971 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6972 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6973 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
| 6974 | lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6975 | |
| 6976 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6977 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6978 | ly_path_free(node->module->ctx, p); |
| 6979 | |
| 6980 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6981 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6982 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6983 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6984 | return LY_EVALID; |
| 6985 | } |
| 6986 | |
| 6987 | /* check status */ |
| 6988 | ctx->path[0] = '\0'; |
| 6989 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6990 | ctx->path_len = strlen(ctx->path); |
| 6991 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6992 | return LY_EVALID; |
| 6993 | } |
| 6994 | ctx->path_len = 1; |
| 6995 | ctx->path[1] = '\0'; |
| 6996 | |
| 6997 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6998 | if (lref->require_instance) { |
| 6999 | for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent); |
| 7000 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7001 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 7002 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 7003 | return LY_EVALID; |
| 7004 | } |
| 7005 | } |
| 7006 | |
| 7007 | /* store the target's type and check for circular chain of leafrefs */ |
| 7008 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 7009 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 7010 | if (type == (struct lysc_type *)lref) { |
| 7011 | /* circular chain detected */ |
| 7012 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7013 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 7014 | return LY_EVALID; |
| 7015 | } |
| 7016 | } |
| 7017 | |
| 7018 | /* check if leafref and its target are under common if-features */ |
| 7019 | if (lys_compile_leafref_features_validate(node, target)) { |
| 7020 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 7021 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7022 | " features applicable to the leafref itself.", lref->path->expr); |
| 7023 | return LY_EVALID; |
| 7024 | } |
| 7025 | |
| 7026 | return LY_SUCCESS; |
| 7027 | } |
| 7028 | |
| 7029 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7030 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7031 | { |
| 7032 | struct lysc_ext_instance *ext; |
| 7033 | struct lysp_ext_instance *ext_p = NULL; |
| 7034 | struct lysp_stmt *stmt; |
| 7035 | const struct lys_module *ext_mod; |
| 7036 | LY_ERR ret = LY_SUCCESS; |
| 7037 | |
| 7038 | /* create the parsed extension instance manually */ |
| 7039 | ext_p = calloc(1, sizeof *ext_p); |
| 7040 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7041 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7042 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7043 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7044 | ext_p->insubstmt_index = 0; |
| 7045 | |
| 7046 | stmt = calloc(1, sizeof *ext_p->child); |
| 7047 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7048 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7049 | stmt->kw = LY_STMT_TYPE; |
| 7050 | ext_p->child = stmt; |
| 7051 | |
| 7052 | /* allocate new extension instance */ |
| 7053 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7054 | |
| 7055 | /* manually get extension definition module */ |
| 7056 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7057 | |
| 7058 | /* compile the extension instance */ |
| 7059 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7060 | |
| 7061 | cleanup: |
| 7062 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7063 | free(ext_p); |
| 7064 | return ret; |
| 7065 | } |
| 7066 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7067 | static LY_ERR |
| 7068 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7069 | { |
| 7070 | struct lysc_node *node; |
| 7071 | struct lysc_type *type, *typeiter; |
| 7072 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7073 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7074 | uint32_t i; |
| 7075 | |
| 7076 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7077 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7078 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7079 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7080 | node = ctx->leafrefs.objs[i]; |
| 7081 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7082 | type = ((struct lysc_node_leaf *)node)->type; |
| 7083 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7084 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7085 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7086 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7087 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7088 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7089 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7090 | } |
| 7091 | } |
| 7092 | } |
| 7093 | } |
| 7094 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7095 | /* store pointer to the real type */ |
| 7096 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7097 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7098 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7099 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7100 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7101 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 7102 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7103 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7104 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7105 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7106 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7107 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7108 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7109 | } |
| 7110 | } |
| 7111 | } |
| 7112 | } |
| 7113 | |
| 7114 | /* check xpath */ |
| 7115 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7116 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7117 | } |
| 7118 | |
| 7119 | /* finish incomplete default values compilation */ |
| 7120 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 7121 | struct ly_err_item *err = NULL; |
| 7122 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
| 7123 | LY_ERR ret; |
| 7124 | ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
| 7125 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7126 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7127 | if (err) { |
| 7128 | ly_err_print(err); |
| 7129 | ctx->path[0] = '\0'; |
| 7130 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7131 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7132 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7133 | ly_err_free(err); |
| 7134 | } |
| 7135 | LY_CHECK_RET(ret); |
| 7136 | } |
| 7137 | |
| 7138 | return LY_SUCCESS; |
| 7139 | } |
| 7140 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7141 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7142 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7143 | { |
| 7144 | struct lysc_ctx ctx = {0}; |
| 7145 | struct lysc_module *mod_c; |
| 7146 | struct lysp_module *sp; |
| 7147 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7148 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7149 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7150 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7151 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7152 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7153 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7154 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7155 | 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] | 7156 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7157 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7158 | /* just imported modules are not compiled */ |
| 7159 | return LY_SUCCESS; |
| 7160 | } |
| 7161 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7162 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7163 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7164 | ctx.ctx = (*mod)->ctx; |
| 7165 | ctx.mod = *mod; |
| 7166 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7167 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7168 | ctx.path_len = 1; |
| 7169 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7170 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7171 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7172 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7173 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7174 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7175 | LY_ARRAY_FOR(sp->imports, u) { |
| 7176 | LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error); |
| 7177 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7178 | LY_ARRAY_FOR(sp->includes, u) { |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 7179 | LY_CHECK_GOTO(ret = lys_compile_submodule(&ctx, &sp->includes[u]), error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7180 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7181 | |
| 7182 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7183 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7184 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7185 | mod_c->features = (*mod)->dis_features; |
| 7186 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7187 | } else { |
| 7188 | /* features are compiled directly into the compiled module structure, |
| 7189 | * 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] | 7190 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7191 | LY_CHECK_GOTO(ret, error); |
| 7192 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7193 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7194 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7195 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7196 | * are present. */ |
| 7197 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7198 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7199 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7200 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7201 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7202 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7203 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7204 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7205 | 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] | 7206 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7207 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7208 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7209 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7210 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7211 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7212 | /* identities, work similarly to features with the precompilation */ |
| 7213 | if ((*mod)->dis_identities) { |
| 7214 | mod_c->identities = (*mod)->dis_identities; |
| 7215 | (*mod)->dis_identities = NULL; |
| 7216 | } else { |
| 7217 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7218 | LY_CHECK_GOTO(ret, error); |
| 7219 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7220 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7221 | 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] | 7222 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7223 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7224 | LY_ARRAY_FOR(sp->includes, v) { |
| 7225 | if (sp->includes[v].submodule->identities) { |
| 7226 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7227 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7228 | LY_CHECK_GOTO(ret, error); |
| 7229 | lysc_update_path(&ctx, NULL, NULL); |
| 7230 | } |
| 7231 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7232 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7233 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7234 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7235 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7236 | 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] | 7237 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7238 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7239 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7240 | 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] | 7241 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7242 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7243 | 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] | 7244 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7245 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7246 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7247 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7248 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7249 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7250 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7251 | /* extension instances TODO cover extension instances from submodules */ |
| 7252 | 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] | 7253 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7254 | /* finish compilation for all unresolved items in the context */ |
| 7255 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7256 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7257 | /* validate non-instantiated groupings from the parsed schema, |
| 7258 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7259 | ctx.options |= LYSC_OPT_GROUPING; |
| 7260 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7261 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7262 | 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] | 7263 | } |
| 7264 | } |
| 7265 | LY_LIST_FOR(sp->data, node_p) { |
| 7266 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7267 | LY_ARRAY_FOR(grps, u) { |
| 7268 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7269 | 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] | 7270 | } |
| 7271 | } |
| 7272 | } |
| 7273 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7274 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7275 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7276 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7277 | } |
| 7278 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7279 | #if 0 |
| 7280 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7281 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7282 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7283 | * the anotation definitions available in the internal schema structure. */ |
| 7284 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7285 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7286 | lys_free(mod, NULL, 1, 1); |
| 7287 | return NULL; |
| 7288 | } |
| 7289 | } |
| 7290 | #endif |
| 7291 | |
| 7292 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7293 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7294 | 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] | 7295 | } |
| 7296 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7297 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7298 | ly_set_erase(&ctx.xpath, NULL); |
| 7299 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7300 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7301 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7302 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7303 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7304 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7305 | lysp_module_free((*mod)->parsed); |
| 7306 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7307 | } |
| 7308 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7309 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7310 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7311 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7312 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7313 | if (m->implemented == 2) { |
| 7314 | m->implemented = 1; |
| 7315 | } |
| 7316 | } |
| 7317 | } |
| 7318 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7319 | (*mod)->compiled = mod_c; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7320 | return LY_SUCCESS; |
| 7321 | |
| 7322 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7323 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7324 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7325 | ly_set_erase(&ctx.xpath, NULL); |
| 7326 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7327 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7328 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7329 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7330 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7331 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7332 | |
| 7333 | /* revert compilation of modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7334 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7335 | m = ctx.ctx->list.objs[i]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7336 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7337 | /* revert features list to the precompiled state */ |
| 7338 | lys_feature_precompile_revert(&ctx, m); |
| 7339 | /* mark module as imported-only / not-implemented */ |
| 7340 | m->implemented = 0; |
| 7341 | /* free the compiled version of the module */ |
| 7342 | lysc_module_free(m->compiled, NULL); |
| 7343 | m->compiled = NULL; |
| 7344 | } |
| 7345 | } |
| 7346 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7347 | /* remove the module itself from the context and free it */ |
| 7348 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7349 | lys_module_free(*mod, NULL); |
| 7350 | *mod = NULL; |
| 7351 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7352 | return ret; |
| 7353 | } |