Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "dict.h" |
| 29 | #include "log.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | #include "path.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | #include "parser.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 32 | #include "parser_schema.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 35 | #include "plugins_exts_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "set.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 39 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 40 | #include "tree_schema_internal.h" |
| 41 | #include "xpath.h" |
| 42 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 43 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
| 44 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
| 45 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 46 | /** |
| 47 | * @brief Duplicate string into dictionary |
| 48 | * @param[in] CTX libyang context of the dictionary. |
| 49 | * @param[in] ORIG String to duplicate. |
| 50 | * @param[out] DUP Where to store the result. |
| 51 | */ |
| 52 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 53 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 54 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 55 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 56 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 57 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 58 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 59 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 60 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 61 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 62 | } \ |
| 63 | } |
| 64 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 65 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 66 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 67 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 68 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 69 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 70 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 71 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 72 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 73 | } \ |
| 74 | } |
| 75 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 76 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 77 | if (EXTS_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 78 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 79 | for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 80 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 81 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 82 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 83 | } \ |
| 84 | } |
| 85 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 86 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 87 | if (ARRAY_P) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 88 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 89 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
| 90 | for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 91 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 92 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 93 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 94 | } \ |
| 95 | } |
| 96 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 97 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 98 | if (MEMBER_P) { \ |
| 99 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 100 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 101 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 102 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 103 | } |
| 104 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 105 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 106 | if (MEMBER_P) { \ |
| 107 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 108 | LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 109 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 110 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 111 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 112 | } |
| 113 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 114 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 115 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 116 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 117 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 118 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 119 | return LY_EVALID; \ |
| 120 | } \ |
| 121 | } \ |
| 122 | } |
| 123 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 124 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 125 | if (ARRAY) { \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 126 | for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 127 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 128 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 129 | return LY_EVALID; \ |
| 130 | } \ |
| 131 | } \ |
| 132 | } |
| 133 | |
| 134 | struct lysc_ext * |
| 135 | lysc_ext_dup(struct lysc_ext *orig) |
| 136 | { |
| 137 | ++orig->refcount; |
| 138 | return orig; |
| 139 | } |
| 140 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 141 | static struct lysc_ext_instance * |
| 142 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 143 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 144 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 145 | (void) ctx; |
| 146 | (void) orig; |
| 147 | return NULL; |
| 148 | } |
| 149 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 150 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 151 | * @brief Add record into the compile context's list of incomplete default values. |
| 152 | * @param[in] ctx Compile context with the incomplete default values list. |
| 153 | * @param[in] context_node Context schema node to store in the record. |
| 154 | * @param[in] dflt Incomplete default value to store in the record. |
| 155 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 156 | * @return LY_EMEM in case of memory allocation failure. |
| 157 | * @return LY_SUCCESS |
| 158 | */ |
| 159 | static LY_ERR |
| 160 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 161 | { |
| 162 | struct lysc_incomplete_dflt *r; |
| 163 | r = malloc(sizeof *r); |
| 164 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 165 | r->context_node = context_node; |
| 166 | r->dflt = dflt; |
| 167 | r->dflt_mod = dflt_mod; |
| 168 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 169 | |
| 170 | return LY_SUCCESS; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 175 | * @param[in] ctx Compile context with the incomplete default values list. |
| 176 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 177 | */ |
| 178 | static void |
| 179 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 180 | { |
| 181 | unsigned int u; |
| 182 | struct lysc_incomplete_dflt *r; |
| 183 | |
| 184 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 185 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 186 | if (r->dflt == dflt) { |
| 187 | free(ctx->dflts.objs[u]); |
| 188 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 189 | --ctx->dflts.count; |
| 190 | return; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 195 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 196 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 197 | { |
| 198 | int len; |
| 199 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 200 | |
| 201 | if (!name) { |
| 202 | /* removing last path segment */ |
| 203 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 204 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 205 | if (ctx->path[ctx->path_len] == '=') { |
| 206 | ctx->path[ctx->path_len++] = '}'; |
| 207 | } else { |
| 208 | /* not a top-level special tag, remove also preceiding '/' */ |
| 209 | goto remove_nodelevel; |
| 210 | } |
| 211 | } else { |
| 212 | remove_nodelevel: |
| 213 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 214 | if (ctx->path_len == 0) { |
| 215 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 216 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | /* set new terminating NULL-byte */ |
| 220 | ctx->path[ctx->path_len] = '\0'; |
| 221 | } else { |
| 222 | if (ctx->path_len > 1) { |
| 223 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 224 | /* extension of the special tag */ |
| 225 | nextlevel = 2; |
| 226 | --ctx->path_len; |
| 227 | } else { |
| 228 | /* there is already some path, so add next level */ |
| 229 | nextlevel = 1; |
| 230 | } |
| 231 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 232 | |
| 233 | if (nextlevel != 2) { |
| 234 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 235 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 236 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 237 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 238 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 239 | } |
| 240 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 241 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 242 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 243 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 244 | /* output truncated */ |
| 245 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 246 | } else { |
| 247 | ctx->path_len += len; |
| 248 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 253 | * @brief Duplicate the compiled pattern structure. |
| 254 | * |
| 255 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 256 | * |
| 257 | * @param[in] orig The pattern structure to duplicate. |
| 258 | * @return The duplicated structure to use. |
| 259 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 260 | static struct lysc_pattern* |
| 261 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 262 | { |
| 263 | ++orig->refcount; |
| 264 | return orig; |
| 265 | } |
| 266 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 267 | /** |
| 268 | * @brief Duplicate the array of compiled patterns. |
| 269 | * |
| 270 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 271 | * |
| 272 | * @param[in] ctx Libyang context for logging. |
| 273 | * @param[in] orig The patterns sized array to duplicate. |
| 274 | * @return New sized array as a copy of @p orig. |
| 275 | * @return NULL in case of memory allocation error. |
| 276 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 277 | static struct lysc_pattern** |
| 278 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 279 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 280 | struct lysc_pattern **dup = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 281 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 282 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 283 | assert(orig); |
| 284 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 285 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 286 | LY_ARRAY_FOR(orig, u) { |
| 287 | dup[u] = lysc_pattern_dup(orig[u]); |
| 288 | LY_ARRAY_INCREMENT(dup); |
| 289 | } |
| 290 | return dup; |
| 291 | } |
| 292 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 293 | /** |
| 294 | * @brief Duplicate compiled range structure. |
| 295 | * |
| 296 | * @param[in] ctx Libyang context for logging. |
| 297 | * @param[in] orig The range structure to be duplicated. |
| 298 | * @return New compiled range structure as a copy of @p orig. |
| 299 | * @return NULL in case of memory allocation error. |
| 300 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 301 | struct lysc_range* |
| 302 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 303 | { |
| 304 | struct lysc_range *dup; |
| 305 | LY_ERR ret; |
| 306 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 307 | assert(orig); |
| 308 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 309 | dup = calloc(1, sizeof *dup); |
| 310 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 311 | if (orig->parts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 312 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup); |
| 313 | LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts); |
| 314 | memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 315 | } |
| 316 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 317 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 318 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 319 | |
| 320 | return dup; |
| 321 | cleanup: |
| 322 | free(dup); |
| 323 | (void) ret; /* set but not used due to the return type */ |
| 324 | return NULL; |
| 325 | } |
| 326 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 327 | /** |
| 328 | * @brief Stack for processing if-feature expressions. |
| 329 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 330 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 331 | int size; /**< number of items in the stack */ |
| 332 | int index; /**< first empty item */ |
| 333 | uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 334 | }; |
| 335 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 336 | /** |
| 337 | * @brief Add @ref ifftokens into the stack. |
| 338 | * @param[in] stack The if-feature stack to use. |
| 339 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 340 | * @return LY_EMEM in case of memory allocation error |
| 341 | * @return LY_ESUCCESS if the value successfully stored. |
| 342 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 343 | static LY_ERR |
| 344 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 345 | { |
| 346 | if (stack->index == stack->size) { |
| 347 | stack->size += 4; |
| 348 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 349 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 350 | } |
| 351 | stack->stack[stack->index++] = value; |
| 352 | return LY_SUCCESS; |
| 353 | } |
| 354 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 355 | /** |
| 356 | * @brief Get (and remove) the last item form the stack. |
| 357 | * @param[in] stack The if-feature stack to use. |
| 358 | * @return The value from the top of the stack. |
| 359 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 360 | static uint8_t |
| 361 | iff_stack_pop(struct iff_stack *stack) |
| 362 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 363 | assert(stack && stack->index); |
| 364 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 365 | stack->index--; |
| 366 | return stack->stack[stack->index]; |
| 367 | } |
| 368 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 369 | /** |
| 370 | * @brief Clean up the stack. |
| 371 | * @param[in] stack The if-feature stack to use. |
| 372 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 373 | static void |
| 374 | iff_stack_clean(struct iff_stack *stack) |
| 375 | { |
| 376 | stack->size = 0; |
| 377 | free(stack->stack); |
| 378 | } |
| 379 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 380 | /** |
| 381 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 382 | * (libyang format of the if-feature expression). |
| 383 | * @param[in,out] list The 2bits array to modify. |
| 384 | * @param[in] op The operand (@ref ifftokens) to store. |
| 385 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 386 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 387 | static void |
| 388 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 389 | { |
| 390 | uint8_t *item; |
| 391 | uint8_t mask = 3; |
| 392 | |
| 393 | assert(pos >= 0); |
| 394 | assert(op <= 3); /* max 2 bits */ |
| 395 | |
| 396 | item = &list[pos / 4]; |
| 397 | mask = mask << 2 * (pos % 4); |
| 398 | *item = (*item) & ~mask; |
| 399 | *item = (*item) | (op << 2 * (pos % 4)); |
| 400 | } |
| 401 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 402 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 403 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 404 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 405 | /** |
| 406 | * @brief Find a feature of the given name and referenced in the given module. |
| 407 | * |
| 408 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 409 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 410 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 411 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 412 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 413 | * assigned till that time will be still valid. |
| 414 | * |
| 415 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 416 | * @param[in] name Name of the feature including possible prefix. |
| 417 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 418 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 419 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 420 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 421 | lys_feature_find(struct lys_module *mod, const char *name, size_t len) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 422 | { |
| 423 | size_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 424 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 425 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 426 | |
| 427 | for (i = 0; i < len; ++i) { |
| 428 | if (name[i] == ':') { |
| 429 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 430 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 431 | LY_CHECK_RET(!mod, NULL); |
| 432 | |
| 433 | name = &name[i + 1]; |
| 434 | len = len - i - 1; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 439 | if (mod->implemented) { |
| 440 | /* module is implemented so there is already the compiled schema */ |
| 441 | flist = mod->compiled->features; |
| 442 | } else { |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 443 | flist = mod->dis_features; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 444 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 445 | LY_ARRAY_FOR(flist, u) { |
| 446 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 447 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 448 | return f; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return NULL; |
| 453 | } |
| 454 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 455 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 456 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 457 | */ |
| 458 | static LY_ERR |
| 459 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 460 | { |
| 461 | LY_ERR ret = LY_SUCCESS; |
| 462 | |
| 463 | if (!ext_p->compiled) { |
| 464 | lysc_update_path(ctx, NULL, "{extension}"); |
| 465 | lysc_update_path(ctx, NULL, ext_p->name); |
| 466 | |
| 467 | /* compile the extension definition */ |
| 468 | ext_p->compiled = calloc(1, sizeof **ext); |
| 469 | ext_p->compiled->refcount = 1; |
| 470 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 471 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 472 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 473 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 474 | |
| 475 | lysc_update_path(ctx, NULL, NULL); |
| 476 | lysc_update_path(ctx, NULL, NULL); |
| 477 | |
| 478 | /* find extension definition plugin */ |
| 479 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 480 | } |
| 481 | |
| 482 | *ext = lysc_ext_dup(ext_p->compiled); |
| 483 | |
| 484 | done: |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 489 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 490 | * |
| 491 | * @param[in] ctx Compilation context. |
| 492 | * @param[in] ext_p Parsed extension instance. |
| 493 | * @param[in,out] ext Prepared compiled extension instance. |
| 494 | * @param[in] parent Extension instance parent. |
| 495 | * @param[in] parent_type Extension instance parent type. |
| 496 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 497 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 498 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 499 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 500 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 501 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 502 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 503 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 504 | size_t u; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 505 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 506 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 507 | |
| 508 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 509 | ext->insubstmt = ext_p->insubstmt; |
| 510 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 511 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 512 | ext->parent = parent; |
| 513 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 514 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 515 | lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}"); |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 516 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 517 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 518 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 519 | if (ext_p->yin) { |
| 520 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 521 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 522 | * YANG (import) prefix must be done here. */ |
| 523 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 524 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 525 | u = 0; |
| 526 | } else if (ctx->mod_def->compiled) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 527 | LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) { |
| 528 | if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) { |
| 529 | char *s; |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 530 | LY_CHECK_ERR_GOTO(asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]) == -1, |
| 531 | ret = LY_EMEM, cleanup); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 532 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
| 533 | u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */ |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | if (!prefixed_name) { |
| 539 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 540 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 541 | goto cleanup; |
| 542 | } |
| 543 | } else { |
| 544 | prefixed_name = ext_p->name; |
| 545 | } |
| 546 | lysc_update_path(ctx, NULL, prefixed_name); |
| 547 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 548 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 549 | ext_mod = u ? lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1) : ctx->mod_def; |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 550 | if (!ext_mod) { |
| 551 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 552 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 553 | goto cleanup; |
| 554 | } else if (!ext_mod->parsed->extensions) { |
| 555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 556 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 557 | prefixed_name, ext_mod->name); |
| 558 | goto cleanup; |
| 559 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 560 | } |
| 561 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 562 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 563 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 564 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 565 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 566 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 567 | LY_CHECK_RET(lys_compile_extension(ctx, ext_mod, &ext_mod->parsed->extensions[v], &ext->def)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 568 | break; |
| 569 | } |
| 570 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 571 | if (!ext->def) { |
| 572 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 573 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 574 | goto cleanup; |
| 575 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 576 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 577 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 578 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 579 | * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 580 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 581 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 582 | struct lysp_stmt *stmt = NULL; |
| 583 | |
| 584 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 585 | /* ... argument was the first XML child element */ |
| 586 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 587 | /* TODO check namespace of the statement */ |
| 588 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 589 | stmt = ext_p->child; |
| 590 | } |
| 591 | } |
| 592 | } else { |
| 593 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 594 | * with LYS_YIN_ATTR flag */ |
| 595 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 596 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 597 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | if (!stmt) { |
| 603 | /* missing extension's argument */ |
| 604 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 605 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 606 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 607 | |
| 608 | } |
| 609 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 610 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 611 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 612 | if (prefixed_name != ext_p->name) { |
| 613 | lydict_remove(ctx->ctx, ext_p->name); |
| 614 | ext_p->name = prefixed_name; |
| 615 | if (!ext_p->argument && ext->argument) { |
| 616 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 617 | } |
| 618 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 619 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 620 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 621 | if (ext->argument) { |
| 622 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 623 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 624 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 625 | if (ext->argument) { |
| 626 | lysc_update_path(ctx, NULL, NULL); |
| 627 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 628 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 629 | ext_p->compiled = ext; |
| 630 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 631 | ret = LY_SUCCESS; |
| 632 | cleanup: |
| 633 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 634 | lydict_remove(ctx->ctx, prefixed_name); |
| 635 | } |
| 636 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 637 | lysc_update_path(ctx, NULL, NULL); |
| 638 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 639 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 640 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 644 | * @brief Compile information from the if-feature statement |
| 645 | * @param[in] ctx Compile context. |
| 646 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 647 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 648 | * @return LY_ERR value. |
| 649 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 650 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 651 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 652 | { |
| 653 | const char *c = *value; |
| 654 | int r, rc = EXIT_FAILURE; |
| 655 | int i, j, last_not, checkversion = 0; |
| 656 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 657 | uint8_t op; |
| 658 | struct iff_stack stack = {0, 0, NULL}; |
| 659 | struct lysc_feature *f; |
| 660 | |
| 661 | assert(c); |
| 662 | |
| 663 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 664 | for (i = j = last_not = 0; c[i]; i++) { |
| 665 | if (c[i] == '(') { |
| 666 | j++; |
| 667 | checkversion = 1; |
| 668 | continue; |
| 669 | } else if (c[i] == ')') { |
| 670 | j--; |
| 671 | continue; |
| 672 | } else if (isspace(c[i])) { |
| 673 | checkversion = 1; |
| 674 | continue; |
| 675 | } |
| 676 | |
| 677 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 678 | int sp; |
| 679 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 680 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 682 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 683 | return LY_EVALID; |
| 684 | } else if (!isspace(c[i + r])) { |
| 685 | /* feature name starting with the not/and/or */ |
| 686 | last_not = 0; |
| 687 | f_size++; |
| 688 | } else if (c[i] == 'n') { /* not operation */ |
| 689 | if (last_not) { |
| 690 | /* double not */ |
| 691 | expr_size = expr_size - 2; |
| 692 | last_not = 0; |
| 693 | } else { |
| 694 | last_not = 1; |
| 695 | } |
| 696 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 697 | if (f_exp != f_size) { |
| 698 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 699 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 700 | return LY_EVALID; |
| 701 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 702 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 703 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 704 | /* not a not operation */ |
| 705 | last_not = 0; |
| 706 | } |
| 707 | i += r; |
| 708 | } else { |
| 709 | f_size++; |
| 710 | last_not = 0; |
| 711 | } |
| 712 | expr_size++; |
| 713 | |
| 714 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 715 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 716 | i--; |
| 717 | break; |
| 718 | } |
| 719 | i++; |
| 720 | } |
| 721 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 722 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 723 | /* not matching count of ( and ) */ |
| 724 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 725 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 726 | return LY_EVALID; |
| 727 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 728 | if (f_exp != f_size) { |
| 729 | /* features do not match the needed arguments for the logical operations */ |
| 730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 731 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 732 | "the required number of operands for the operations.", *value); |
| 733 | return LY_EVALID; |
| 734 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 735 | |
| 736 | if (checkversion || expr_size > 1) { |
| 737 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 738 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 739 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 740 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 741 | return LY_EVALID; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /* allocate the memory */ |
| 746 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 747 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 748 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 749 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 750 | |
| 751 | stack.size = expr_size; |
| 752 | f_size--; expr_size--; /* used as indexes from now */ |
| 753 | |
| 754 | for (i--; i >= 0; i--) { |
| 755 | if (c[i] == ')') { |
| 756 | /* push it on stack */ |
| 757 | iff_stack_push(&stack, LYS_IFF_RP); |
| 758 | continue; |
| 759 | } else if (c[i] == '(') { |
| 760 | /* pop from the stack into result all operators until ) */ |
| 761 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 762 | iff_setop(iff->expr, op, expr_size--); |
| 763 | } |
| 764 | continue; |
| 765 | } else if (isspace(c[i])) { |
| 766 | continue; |
| 767 | } |
| 768 | |
| 769 | /* end of operator or operand -> find beginning and get what is it */ |
| 770 | j = i + 1; |
| 771 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 772 | i--; |
| 773 | } |
| 774 | i++; /* go back by one step */ |
| 775 | |
| 776 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 777 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 778 | /* double not */ |
| 779 | iff_stack_pop(&stack); |
| 780 | } else { |
| 781 | /* not has the highest priority, so do not pop from the stack |
| 782 | * as in case of AND and OR */ |
| 783 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 784 | } |
| 785 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 786 | /* as for OR - pop from the stack all operators with the same or higher |
| 787 | * priority and store them to the result, then push the AND to the stack */ |
| 788 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 789 | op = iff_stack_pop(&stack); |
| 790 | iff_setop(iff->expr, op, expr_size--); |
| 791 | } |
| 792 | iff_stack_push(&stack, LYS_IFF_AND); |
| 793 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 794 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 795 | op = iff_stack_pop(&stack); |
| 796 | iff_setop(iff->expr, op, expr_size--); |
| 797 | } |
| 798 | iff_stack_push(&stack, LYS_IFF_OR); |
| 799 | } else { |
| 800 | /* feature name, length is j - i */ |
| 801 | |
| 802 | /* add it to the expression */ |
| 803 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 804 | |
| 805 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 806 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 807 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 808 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 809 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 810 | iff->features[f_size] = f; |
| 811 | LY_ARRAY_INCREMENT(iff->features); |
| 812 | f_size--; |
| 813 | } |
| 814 | } |
| 815 | while (stack.index) { |
| 816 | op = iff_stack_pop(&stack); |
| 817 | iff_setop(iff->expr, op, expr_size--); |
| 818 | } |
| 819 | |
| 820 | if (++expr_size || ++f_size) { |
| 821 | /* not all expected operators and operands found */ |
| 822 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 823 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 824 | rc = LY_EINT; |
| 825 | } else { |
| 826 | rc = LY_SUCCESS; |
| 827 | } |
| 828 | |
| 829 | error: |
| 830 | /* cleanup */ |
| 831 | iff_stack_clean(&stack); |
| 832 | |
| 833 | return rc; |
| 834 | } |
| 835 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 836 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 837 | * @brief Get the XPath context node for the given schema node. |
| 838 | * @param[in] start The schema node where the XPath expression appears. |
| 839 | * @return The context node to evaluate XPath expression in given schema node. |
| 840 | * @return NULL in case the context node is the root node. |
| 841 | */ |
| 842 | static struct lysc_node * |
| 843 | lysc_xpath_context(struct lysc_node *start) |
| 844 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 845 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 846 | start = start->parent); |
| 847 | return start; |
| 848 | } |
| 849 | |
| 850 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 851 | * @brief Compile information from the when statement |
| 852 | * @param[in] ctx Compile context. |
| 853 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 854 | * @param[in] flags Flags of the node with the "when" defiition. |
| 855 | * @param[in] node Node that inherited the "when" definition, must be connected to parents. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 856 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 857 | * @return LY_ERR value. |
| 858 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 859 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 860 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 861 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 862 | LY_ERR ret = LY_SUCCESS; |
| 863 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 864 | *when = calloc(1, sizeof **when); |
| 865 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 866 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 867 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 868 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 869 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 870 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 871 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 872 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 873 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 874 | |
| 875 | done: |
| 876 | return ret; |
| 877 | } |
| 878 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 879 | /** |
| 880 | * @brief Compile information from the must statement |
| 881 | * @param[in] ctx Compile context. |
| 882 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 883 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 884 | * @return LY_ERR value. |
| 885 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 886 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 887 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 888 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 889 | LY_ERR ret = LY_SUCCESS; |
| 890 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 891 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 892 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 893 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 894 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 895 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 896 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 897 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 898 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 899 | |
| 900 | done: |
| 901 | return ret; |
| 902 | } |
| 903 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 904 | /** |
| 905 | * @brief Compile information from the import statement |
| 906 | * @param[in] ctx Compile context. |
| 907 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 908 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 909 | * @return LY_ERR value. |
| 910 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 911 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 912 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 913 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 914 | const struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 915 | LY_ERR ret = LY_SUCCESS; |
| 916 | |
| 917 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 918 | COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 919 | imp->module = imp_p->module; |
| 920 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 921 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 922 | * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added, |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 923 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 924 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 925 | /* try to use filepath if present */ |
| 926 | if (imp->module->filepath) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 927 | struct ly_in *in; |
| 928 | if (ly_in_new_filepath(imp->module->filepath, 0, &in)) { |
| 929 | LOGINT(ctx->ctx); |
| 930 | } else { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 931 | LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], |
| 932 | ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 933 | if (mod != imp->module) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 934 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
| 935 | imp->module->filepath, imp->module->name); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 936 | mod = NULL; |
| 937 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 938 | } |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 939 | ly_in_free(in, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 940 | } |
| 941 | if (!mod) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 942 | if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, (struct lys_module **)&mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 943 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 944 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 945 | return LY_ENOTFOUND; |
| 946 | } |
| 947 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | done: |
| 951 | return ret; |
| 952 | } |
| 953 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 954 | LY_ERR |
| 955 | lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 956 | struct lysp_ident *identities_p, struct lysc_ident **identities) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 957 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 958 | LY_ARRAY_COUNT_TYPE offset = 0, u, v; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 959 | struct lysc_ctx context = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 960 | LY_ERR ret = LY_SUCCESS; |
| 961 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 962 | assert(ctx_sc || ctx); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 963 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 964 | if (!ctx_sc) { |
| 965 | context.ctx = ctx; |
| 966 | context.mod = module; |
| 967 | context.path_len = 1; |
| 968 | context.path[0] = '/'; |
| 969 | ctx_sc = &context; |
| 970 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 971 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 972 | if (!identities_p) { |
| 973 | return LY_SUCCESS; |
| 974 | } |
| 975 | if (*identities) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 976 | offset = LY_ARRAY_COUNT(*identities); |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | lysc_update_path(ctx_sc, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 980 | 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] | 981 | LY_ARRAY_FOR(identities_p, u) { |
| 982 | lysc_update_path(ctx_sc, NULL, identities_p[u].name); |
| 983 | |
| 984 | LY_ARRAY_INCREMENT(*identities); |
| 985 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name); |
| 986 | DUP_STRING(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name); |
| 987 | DUP_STRING(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc); |
| 988 | DUP_STRING(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref); |
| 989 | (*identities)[offset + u].module = ctx_sc->mod; |
| 990 | COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v, |
| 991 | lys_compile_iffeature, ret, done); |
| 992 | /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */ |
| 993 | COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u], |
| 994 | LYEXT_PAR_IDENT, ret, done); |
| 995 | (*identities)[offset + u].flags = identities_p[u].flags; |
| 996 | |
| 997 | lysc_update_path(ctx_sc, NULL, NULL); |
| 998 | } |
| 999 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1000 | done: |
| 1001 | return ret; |
| 1002 | } |
| 1003 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1004 | /** |
| 1005 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 1006 | * |
| 1007 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 1008 | * |
| 1009 | * @param[in] ctx Compile context for logging. |
| 1010 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 1011 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 1012 | * @return LY_SUCCESS if everything is ok. |
| 1013 | * @return LY_EVALID if the identity is derived from itself. |
| 1014 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1015 | static LY_ERR |
| 1016 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 1017 | { |
| 1018 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1019 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1020 | struct ly_set recursion = {0}; |
| 1021 | struct lysc_ident *drv; |
| 1022 | |
| 1023 | if (!derived) { |
| 1024 | return LY_SUCCESS; |
| 1025 | } |
| 1026 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1027 | for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1028 | if (ident == derived[u]) { |
| 1029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1030 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1031 | goto cleanup; |
| 1032 | } |
| 1033 | ly_set_add(&recursion, derived[u], 0); |
| 1034 | } |
| 1035 | |
| 1036 | for (v = 0; v < recursion.count; ++v) { |
| 1037 | drv = recursion.objs[v]; |
| 1038 | if (!drv->derived) { |
| 1039 | continue; |
| 1040 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1041 | for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1042 | if (ident == drv->derived[u]) { |
| 1043 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1044 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1045 | goto cleanup; |
| 1046 | } |
| 1047 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1048 | } |
| 1049 | } |
| 1050 | ret = LY_SUCCESS; |
| 1051 | |
| 1052 | cleanup: |
| 1053 | ly_set_erase(&recursion, NULL); |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1057 | /** |
| 1058 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1059 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1060 | * 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] | 1061 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1062 | * to distinguish these two use cases. |
| 1063 | * |
| 1064 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1065 | * @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] | 1066 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1067 | * @param[in] bases Array of bases of identityref to fill in. |
| 1068 | * @return LY_ERR value. |
| 1069 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1070 | static LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1071 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, |
| 1072 | struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1073 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1074 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1075 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1076 | struct lys_module *mod; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1077 | struct lysc_ident **idref, *identities; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1078 | |
| 1079 | assert(ident || bases); |
| 1080 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1081 | if (LY_ARRAY_COUNT(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1082 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1083 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1084 | return LY_EVALID; |
| 1085 | } |
| 1086 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1087 | LY_ARRAY_FOR(bases_p, u) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1088 | s = strchr(bases_p[u], ':'); |
| 1089 | if (s) { |
| 1090 | /* prefixed identity */ |
| 1091 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1092 | 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] | 1093 | } else { |
| 1094 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1095 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1096 | } |
| 1097 | if (!mod) { |
| 1098 | if (ident) { |
| 1099 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1100 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1101 | } else { |
| 1102 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1103 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1104 | } |
| 1105 | return LY_EVALID; |
| 1106 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1107 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1108 | idref = NULL; |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1109 | if (mod->compiled) { |
| 1110 | identities = mod->compiled->identities; |
| 1111 | } else { |
| 1112 | identities = mod->dis_identities; |
| 1113 | } |
| 1114 | LY_ARRAY_FOR(identities, v) { |
| 1115 | if (!strcmp(name, identities[v].name)) { |
| 1116 | if (ident) { |
| 1117 | if (ident == &identities[v]) { |
| 1118 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1119 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1120 | return LY_EVALID; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1121 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1122 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &identities[v], ident->derived)); |
| 1123 | /* we have match! store the backlink */ |
| 1124 | LY_ARRAY_NEW_RET(ctx->ctx, identities[v].derived, idref, LY_EMEM); |
| 1125 | *idref = ident; |
| 1126 | } else { |
| 1127 | /* we have match! store the found identity */ |
| 1128 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 1129 | *idref = &identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1130 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1131 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | if (!idref || !(*idref)) { |
| 1135 | if (ident) { |
| 1136 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1137 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1138 | } else { |
| 1139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1140 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1141 | } |
| 1142 | return LY_EVALID; |
| 1143 | } |
| 1144 | } |
| 1145 | return LY_SUCCESS; |
| 1146 | } |
| 1147 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1148 | /** |
| 1149 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1150 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1151 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1152 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1153 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1154 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1155 | static LY_ERR |
| 1156 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1157 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1158 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1159 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1160 | lysc_update_path(ctx, NULL, "{identity}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1161 | for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1162 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1163 | continue; |
| 1164 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1165 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1166 | 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] | 1167 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1168 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1169 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1170 | return LY_SUCCESS; |
| 1171 | } |
| 1172 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1173 | LY_ERR |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1174 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 1175 | struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1176 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1177 | LY_ARRAY_COUNT_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1178 | struct lysc_ctx context = {0}; |
| 1179 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1180 | assert(ctx_sc || ctx); |
| 1181 | |
| 1182 | if (!ctx_sc) { |
| 1183 | context.ctx = ctx; |
| 1184 | context.mod = module; |
| 1185 | context.path_len = 1; |
| 1186 | context.path[0] = '/'; |
| 1187 | ctx_sc = &context; |
| 1188 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1189 | |
| 1190 | if (!features_p) { |
| 1191 | return LY_SUCCESS; |
| 1192 | } |
| 1193 | if (*features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1194 | offset = LY_ARRAY_COUNT(*features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1195 | } |
| 1196 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1197 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1198 | 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] | 1199 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1200 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1201 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1202 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1203 | 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] | 1204 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1205 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1206 | 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] | 1207 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1208 | (*features)[offset + u].module = ctx_sc->mod; |
| 1209 | |
| 1210 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1211 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1212 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1213 | |
| 1214 | return LY_SUCCESS; |
| 1215 | } |
| 1216 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1217 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1218 | * @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] | 1219 | * |
| 1220 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1221 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1222 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1223 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1224 | * being currently processed). |
| 1225 | * @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] | 1226 | * @return LY_SUCCESS if everything is ok. |
| 1227 | * @return LY_EVALID if the feature references indirectly itself. |
| 1228 | */ |
| 1229 | static LY_ERR |
| 1230 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1231 | { |
| 1232 | LY_ERR ret = LY_EVALID; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1233 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1234 | struct ly_set recursion = {0}; |
| 1235 | struct lysc_feature *drv; |
| 1236 | |
| 1237 | if (!depfeatures) { |
| 1238 | return LY_SUCCESS; |
| 1239 | } |
| 1240 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1241 | for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1242 | if (feature == depfeatures[u]) { |
| 1243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1244 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1245 | goto cleanup; |
| 1246 | } |
| 1247 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1248 | } |
| 1249 | |
| 1250 | for (v = 0; v < recursion.count; ++v) { |
| 1251 | drv = recursion.objs[v]; |
| 1252 | if (!drv->depfeatures) { |
| 1253 | continue; |
| 1254 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1255 | for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1256 | if (feature == drv->depfeatures[u]) { |
| 1257 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1258 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1259 | goto cleanup; |
| 1260 | } |
| 1261 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1262 | } |
| 1263 | } |
| 1264 | ret = LY_SUCCESS; |
| 1265 | |
| 1266 | cleanup: |
| 1267 | ly_set_erase(&recursion, NULL); |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
| 1271 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1272 | * @brief Create pre-compiled features array. |
| 1273 | * |
| 1274 | * See lys_feature_precompile() for more details. |
| 1275 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1276 | * @param[in] ctx Compile context. |
| 1277 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1278 | * @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] | 1279 | * @return LY_ERR value. |
| 1280 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1281 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1282 | 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] | 1283 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1284 | LY_ARRAY_COUNT_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1285 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1286 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1287 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1288 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1289 | /* find the preprecompiled feature */ |
| 1290 | LY_ARRAY_FOR(features, x) { |
| 1291 | if (strcmp(features[x].name, feature_p->name)) { |
| 1292 | continue; |
| 1293 | } |
| 1294 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1295 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1296 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1297 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1298 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1299 | 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] | 1300 | 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] | 1301 | if (feature->iffeatures) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1302 | for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1303 | if (feature->iffeatures[u].features) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1304 | for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1305 | /* check for circular dependency - direct reference first,... */ |
| 1306 | if (feature == feature->iffeatures[u].features[v]) { |
| 1307 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1308 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1309 | return LY_EVALID; |
| 1310 | } |
| 1311 | /* ... and indirect circular reference */ |
| 1312 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1313 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1314 | /* add itself into the dependants list */ |
| 1315 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1316 | *df = feature; |
| 1317 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1318 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1319 | } |
| 1320 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1321 | lysc_update_path(ctx, NULL, NULL); |
| 1322 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1323 | done: |
| 1324 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1325 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1326 | |
| 1327 | LOGINT(ctx->ctx); |
| 1328 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1331 | /** |
| 1332 | * @brief Revert compiled list of features back to the precompiled state. |
| 1333 | * |
| 1334 | * 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] | 1335 | * 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] | 1336 | * |
| 1337 | * @param[in] ctx Compilation context. |
| 1338 | * @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] | 1339 | * and supposed to hold the dis_features list. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1340 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1341 | static void |
| 1342 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1343 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1344 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1345 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1346 | /* keep the dis_features list until the complete lys_module is freed */ |
| 1347 | mod->dis_features = mod->compiled->features; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1348 | mod->compiled->features = NULL; |
| 1349 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1350 | /* 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] | 1351 | * which may points into the data being freed here */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1352 | LY_ARRAY_FOR(mod->dis_features, u) { |
| 1353 | LY_ARRAY_FOR(mod->dis_features[u].iffeatures, v) { |
| 1354 | lysc_iffeature_free(ctx->ctx, &mod->dis_features[u].iffeatures[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1355 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1356 | LY_ARRAY_FREE(mod->dis_features[u].iffeatures); |
| 1357 | mod->dis_features[u].iffeatures = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1358 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1359 | LY_ARRAY_FOR(mod->dis_features[u].exts, v) { |
| 1360 | lysc_ext_instance_free(ctx->ctx, &(mod->dis_features[u].exts)[v]); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1361 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 1362 | LY_ARRAY_FREE(mod->dis_features[u].exts); |
| 1363 | mod->dis_features[u].exts = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1367 | /** |
| 1368 | * @brief Validate and normalize numeric value from a range definition. |
| 1369 | * @param[in] ctx Compile context. |
| 1370 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1371 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1372 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1373 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1374 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1375 | * @param[in] value String value of the range boundary. |
| 1376 | * @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. |
| 1377 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1378 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1379 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1380 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1381 | 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] | 1382 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1383 | size_t fraction = 0, size; |
| 1384 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1385 | *len = 0; |
| 1386 | |
| 1387 | assert(value); |
| 1388 | /* parse value */ |
| 1389 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1390 | return LY_EVALID; |
| 1391 | } |
| 1392 | |
| 1393 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1394 | ++(*len); |
| 1395 | } |
| 1396 | |
| 1397 | while (isdigit(value[*len])) { |
| 1398 | ++(*len); |
| 1399 | } |
| 1400 | |
| 1401 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1402 | if (basetype == LY_TYPE_DEC64) { |
| 1403 | goto decimal; |
| 1404 | } else { |
| 1405 | *valcopy = strndup(value, *len); |
| 1406 | return LY_SUCCESS; |
| 1407 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1408 | } |
| 1409 | fraction = *len; |
| 1410 | |
| 1411 | ++(*len); |
| 1412 | while (isdigit(value[*len])) { |
| 1413 | ++(*len); |
| 1414 | } |
| 1415 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1416 | if (basetype == LY_TYPE_DEC64) { |
| 1417 | decimal: |
| 1418 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1419 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1420 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1421 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1422 | *len, value, frdigits); |
| 1423 | return LY_EINVAL; |
| 1424 | } |
| 1425 | if (fraction) { |
| 1426 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1427 | } else { |
| 1428 | size = (*len) + frdigits + 1; |
| 1429 | } |
| 1430 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1431 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1432 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1433 | (*valcopy)[size - 1] = '\0'; |
| 1434 | if (fraction) { |
| 1435 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1436 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1437 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1438 | } else { |
| 1439 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1440 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1441 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1442 | } |
| 1443 | return LY_SUCCESS; |
| 1444 | } |
| 1445 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1446 | /** |
| 1447 | * @brief Check that values in range are in ascendant order. |
| 1448 | * @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] | 1449 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1450 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1451 | * @param[in] value Current value to check. |
| 1452 | * @param[in] prev_value The last seen value. |
| 1453 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1454 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1455 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1456 | 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] | 1457 | { |
| 1458 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1459 | 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] | 1460 | return LY_EEXIST; |
| 1461 | } |
| 1462 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1463 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1464 | return LY_EEXIST; |
| 1465 | } |
| 1466 | } |
| 1467 | return LY_SUCCESS; |
| 1468 | } |
| 1469 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1470 | /** |
| 1471 | * @brief Set min/max value of the range part. |
| 1472 | * @param[in] ctx Compile context. |
| 1473 | * @param[in] part Range part structure to fill. |
| 1474 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1475 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1476 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1477 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1478 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1479 | * @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] | 1480 | * @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] | 1481 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1482 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1483 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1484 | * frdigits value), LY_EMEM. |
| 1485 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1486 | static LY_ERR |
| 1487 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1488 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1489 | { |
| 1490 | LY_ERR ret = LY_SUCCESS; |
| 1491 | char *valcopy = NULL; |
| 1492 | size_t len; |
| 1493 | |
| 1494 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1495 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1496 | LY_CHECK_GOTO(ret, finalize); |
| 1497 | } |
| 1498 | if (!valcopy && base_range) { |
| 1499 | if (max) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1500 | 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] | 1501 | } else { |
| 1502 | part->min_64 = base_range->parts[0].min_64; |
| 1503 | } |
| 1504 | if (!first) { |
| 1505 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1506 | } |
| 1507 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1511 | case LY_TYPE_INT8: /* range */ |
| 1512 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1513 | 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] | 1514 | } else if (max) { |
| 1515 | part->max_64 = INT64_C(127); |
| 1516 | } else { |
| 1517 | part->min_64 = INT64_C(-128); |
| 1518 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1519 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1520 | 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] | 1521 | } |
| 1522 | break; |
| 1523 | case LY_TYPE_INT16: /* range */ |
| 1524 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1525 | 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] | 1526 | } else if (max) { |
| 1527 | part->max_64 = INT64_C(32767); |
| 1528 | } else { |
| 1529 | part->min_64 = INT64_C(-32768); |
| 1530 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1531 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1532 | 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] | 1533 | } |
| 1534 | break; |
| 1535 | case LY_TYPE_INT32: /* range */ |
| 1536 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1537 | 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] | 1538 | } else if (max) { |
| 1539 | part->max_64 = INT64_C(2147483647); |
| 1540 | } else { |
| 1541 | part->min_64 = INT64_C(-2147483648); |
| 1542 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1543 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1544 | 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] | 1545 | } |
| 1546 | break; |
| 1547 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1548 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1549 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1550 | 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] | 1551 | max ? &part->max_64 : &part->min_64); |
| 1552 | } else if (max) { |
| 1553 | part->max_64 = INT64_C(9223372036854775807); |
| 1554 | } else { |
| 1555 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1556 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1557 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1558 | 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] | 1559 | } |
| 1560 | break; |
| 1561 | case LY_TYPE_UINT8: /* range */ |
| 1562 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1563 | 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] | 1564 | } else if (max) { |
| 1565 | part->max_u64 = UINT64_C(255); |
| 1566 | } else { |
| 1567 | part->min_u64 = UINT64_C(0); |
| 1568 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1569 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1570 | 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] | 1571 | } |
| 1572 | break; |
| 1573 | case LY_TYPE_UINT16: /* range */ |
| 1574 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1575 | 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] | 1576 | } else if (max) { |
| 1577 | part->max_u64 = UINT64_C(65535); |
| 1578 | } else { |
| 1579 | part->min_u64 = UINT64_C(0); |
| 1580 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1581 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1582 | 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] | 1583 | } |
| 1584 | break; |
| 1585 | case LY_TYPE_UINT32: /* range */ |
| 1586 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1587 | 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] | 1588 | } else if (max) { |
| 1589 | part->max_u64 = UINT64_C(4294967295); |
| 1590 | } else { |
| 1591 | part->min_u64 = UINT64_C(0); |
| 1592 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1593 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1594 | 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] | 1595 | } |
| 1596 | break; |
| 1597 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1598 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1599 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1600 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1601 | 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] | 1602 | } else if (max) { |
| 1603 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1604 | } else { |
| 1605 | part->min_u64 = UINT64_C(0); |
| 1606 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1607 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1608 | 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] | 1609 | } |
| 1610 | break; |
| 1611 | default: |
| 1612 | LOGINT(ctx->ctx); |
| 1613 | ret = LY_EINT; |
| 1614 | } |
| 1615 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1616 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1617 | if (ret == LY_EDENIED) { |
| 1618 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1619 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1620 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1621 | } else if (ret == LY_EVALID) { |
| 1622 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1623 | "Invalid %s restriction - invalid value \"%s\".", |
| 1624 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1625 | } else if (ret == LY_EEXIST) { |
| 1626 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1627 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1628 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1629 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1630 | } else if (!ret && value) { |
| 1631 | *value = *value + len; |
| 1632 | } |
| 1633 | free(valcopy); |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1637 | /** |
| 1638 | * @brief Compile the parsed range restriction. |
| 1639 | * @param[in] ctx Compile context. |
| 1640 | * @param[in] range_p Parsed range structure to compile. |
| 1641 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1642 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1643 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1644 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1645 | * range restriction must be more restrictive than the base_range. |
| 1646 | * @param[in,out] range Pointer to the created current range structure. |
| 1647 | * @return LY_ERR value. |
| 1648 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1649 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1650 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1651 | struct lysc_range *base_range, struct lysc_range **range) |
| 1652 | { |
| 1653 | LY_ERR ret = LY_EVALID; |
| 1654 | const char *expr; |
| 1655 | struct lysc_range_part *parts = NULL, *part; |
| 1656 | int range_expected = 0, uns; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1657 | LY_ARRAY_COUNT_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1658 | |
| 1659 | assert(range); |
| 1660 | assert(range_p); |
| 1661 | |
| 1662 | expr = range_p->arg; |
| 1663 | while(1) { |
| 1664 | if (isspace(*expr)) { |
| 1665 | ++expr; |
| 1666 | } else if (*expr == '\0') { |
| 1667 | if (range_expected) { |
| 1668 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1669 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1670 | length_restr ? "length" : "range", range_p->arg); |
| 1671 | goto cleanup; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1672 | } else if (!parts || parts_done == LY_ARRAY_COUNT(parts)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1673 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1674 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1675 | length_restr ? "length" : "range", range_p->arg); |
| 1676 | goto cleanup; |
| 1677 | } |
| 1678 | parts_done++; |
| 1679 | break; |
| 1680 | } else if (!strncmp(expr, "min", 3)) { |
| 1681 | if (parts) { |
| 1682 | /* min cannot be used elsewhere than in the first part */ |
| 1683 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1684 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1685 | expr - range_p->arg, range_p->arg); |
| 1686 | goto cleanup; |
| 1687 | } |
| 1688 | expr += 3; |
| 1689 | |
| 1690 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1691 | 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] | 1692 | part->max_64 = part->min_64; |
| 1693 | } else if (*expr == '|') { |
| 1694 | if (!parts || range_expected) { |
| 1695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1696 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1697 | goto cleanup; |
| 1698 | } |
| 1699 | expr++; |
| 1700 | parts_done++; |
| 1701 | /* process next part of the expression */ |
| 1702 | } else if (!strncmp(expr, "..", 2)) { |
| 1703 | expr += 2; |
| 1704 | while (isspace(*expr)) { |
| 1705 | expr++; |
| 1706 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1707 | if (!parts || LY_ARRAY_COUNT(parts) == parts_done) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1708 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1709 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1710 | goto cleanup; |
| 1711 | } |
| 1712 | /* continue expecting the upper boundary */ |
| 1713 | range_expected = 1; |
| 1714 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1715 | /* number */ |
| 1716 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1717 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1718 | 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] | 1719 | range_expected = 0; |
| 1720 | } else { |
| 1721 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1722 | 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] | 1723 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1724 | part->max_64 = part->min_64; |
| 1725 | } |
| 1726 | |
| 1727 | /* continue with possible another expression part */ |
| 1728 | } else if (!strncmp(expr, "max", 3)) { |
| 1729 | expr += 3; |
| 1730 | while (isspace(*expr)) { |
| 1731 | expr++; |
| 1732 | } |
| 1733 | if (*expr != '\0') { |
| 1734 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1735 | length_restr ? "length" : "range", expr); |
| 1736 | goto cleanup; |
| 1737 | } |
| 1738 | if (range_expected) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1739 | part = &parts[LY_ARRAY_COUNT(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1740 | 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] | 1741 | range_expected = 0; |
| 1742 | } else { |
| 1743 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1744 | 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] | 1745 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1746 | part->min_64 = part->max_64; |
| 1747 | } |
| 1748 | } else { |
| 1749 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1750 | length_restr ? "length" : "range", expr); |
| 1751 | goto cleanup; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | /* check with the previous range/length restriction */ |
| 1756 | if (base_range) { |
| 1757 | switch (basetype) { |
| 1758 | case LY_TYPE_BINARY: |
| 1759 | case LY_TYPE_UINT8: |
| 1760 | case LY_TYPE_UINT16: |
| 1761 | case LY_TYPE_UINT32: |
| 1762 | case LY_TYPE_UINT64: |
| 1763 | case LY_TYPE_STRING: |
| 1764 | uns = 1; |
| 1765 | break; |
| 1766 | case LY_TYPE_DEC64: |
| 1767 | case LY_TYPE_INT8: |
| 1768 | case LY_TYPE_INT16: |
| 1769 | case LY_TYPE_INT32: |
| 1770 | case LY_TYPE_INT64: |
| 1771 | uns = 0; |
| 1772 | break; |
| 1773 | default: |
| 1774 | LOGINT(ctx->ctx); |
| 1775 | ret = LY_EINT; |
| 1776 | goto cleanup; |
| 1777 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1778 | 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] | 1779 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1780 | goto baseerror; |
| 1781 | } |
| 1782 | /* current lower bound is not lower than the base */ |
| 1783 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1784 | /* base has single value */ |
| 1785 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1786 | /* both lower bounds are the same */ |
| 1787 | if (parts[u].min_64 != parts[u].max_64) { |
| 1788 | /* current continues with a range */ |
| 1789 | goto baseerror; |
| 1790 | } else { |
| 1791 | /* equal single values, move both forward */ |
| 1792 | ++v; |
| 1793 | continue; |
| 1794 | } |
| 1795 | } else { |
| 1796 | /* base is single value lower than current range, so the |
| 1797 | * value from base range is removed in the current, |
| 1798 | * move only base and repeat checking */ |
| 1799 | ++v; |
| 1800 | --u; |
| 1801 | continue; |
| 1802 | } |
| 1803 | } else { |
| 1804 | /* base is the range */ |
| 1805 | if (parts[u].min_64 == parts[u].max_64) { |
| 1806 | /* current is a single value */ |
| 1807 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1808 | /* current is behind the base range, so base range is omitted, |
| 1809 | * move the base and keep the current for further check */ |
| 1810 | ++v; |
| 1811 | --u; |
| 1812 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1813 | continue; |
| 1814 | } else { |
| 1815 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1816 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1817 | /* higher bound is higher than the current higher bound */ |
| 1818 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1819 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1820 | * continue with the same current, but move the base */ |
| 1821 | --u; |
| 1822 | ++v; |
| 1823 | continue; |
| 1824 | } |
| 1825 | /* current range starts within the base range but end behind it */ |
| 1826 | goto baseerror; |
| 1827 | } else { |
| 1828 | /* current range is smaller than the base, |
| 1829 | * move current, but stay with the base */ |
| 1830 | continue; |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | if (u != parts_done) { |
| 1836 | baseerror: |
| 1837 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1838 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1839 | length_restr ? "length" : "range", range_p->arg); |
| 1840 | goto cleanup; |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | if (!(*range)) { |
| 1845 | *range = calloc(1, sizeof **range); |
| 1846 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1847 | } |
| 1848 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1849 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1850 | if (range_p->eapptag) { |
| 1851 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1852 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1853 | } |
| 1854 | if (range_p->emsg) { |
| 1855 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1856 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1857 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1858 | if (range_p->dsc) { |
| 1859 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1860 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1861 | } |
| 1862 | if (range_p->ref) { |
| 1863 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1864 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1865 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1866 | /* extensions are taken only from the last range by the caller */ |
| 1867 | |
| 1868 | (*range)->parts = parts; |
| 1869 | parts = NULL; |
| 1870 | ret = LY_SUCCESS; |
| 1871 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1872 | LY_ARRAY_FREE(parts); |
| 1873 | |
| 1874 | return ret; |
| 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * @brief Checks pattern syntax. |
| 1879 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1880 | * @param[in] ctx Context. |
| 1881 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1882 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1883 | * @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] | 1884 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1885 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1886 | LY_ERR |
| 1887 | 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] | 1888 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1889 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1890 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1891 | int err_code; |
| 1892 | const char *orig_ptr; |
| 1893 | PCRE2_SIZE err_offset; |
| 1894 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1895 | #define URANGE_LEN 19 |
| 1896 | char *ublock2urange[][2] = { |
| 1897 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1898 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1899 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1900 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1901 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1902 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1903 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1904 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1905 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1906 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1907 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1908 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1909 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1910 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1911 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1912 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1913 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1914 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1915 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1916 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1917 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1918 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1919 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1920 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1921 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1922 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1923 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1924 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1925 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1926 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1927 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1928 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1929 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1930 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1931 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1932 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1933 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1934 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1935 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1936 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1937 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1938 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1939 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1940 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1941 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1942 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1943 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1944 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1945 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1946 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1947 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1948 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1949 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1950 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1951 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1952 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1953 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1954 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1955 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1956 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1957 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1958 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1959 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1960 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1961 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1962 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1963 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1964 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1965 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1966 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1967 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1968 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1969 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1970 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1971 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1972 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1973 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1974 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1975 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1976 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1977 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1978 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1979 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1980 | {NULL, NULL} |
| 1981 | }; |
| 1982 | |
| 1983 | /* adjust the expression to a Perl equivalent |
| 1984 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1985 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1986 | /* allocate space for the transformed pattern */ |
| 1987 | size = strlen(pattern) + 1; |
| 1988 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1990 | perl_regex[0] = '\0'; |
| 1991 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1992 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1993 | brack = 0; |
| 1994 | idx = 0; |
| 1995 | orig_ptr = pattern; |
| 1996 | while (orig_ptr[0]) { |
| 1997 | switch (orig_ptr[0]) { |
| 1998 | case '$': |
| 1999 | case '^': |
| 2000 | if (!brack) { |
| 2001 | /* make space for the extra character */ |
| 2002 | ++size; |
| 2003 | perl_regex = ly_realloc(perl_regex, size); |
| 2004 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2005 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2006 | /* print escape slash */ |
| 2007 | perl_regex[idx] = '\\'; |
| 2008 | ++idx; |
| 2009 | } |
| 2010 | break; |
| 2011 | case '[': |
| 2012 | /* must not be escaped */ |
| 2013 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2014 | ++brack; |
| 2015 | } |
| 2016 | break; |
| 2017 | case ']': |
| 2018 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 2019 | /* pattern was checked and compiled already */ |
| 2020 | assert(brack); |
| 2021 | --brack; |
| 2022 | } |
| 2023 | break; |
| 2024 | default: |
| 2025 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2026 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2027 | |
| 2028 | /* copy char */ |
| 2029 | perl_regex[idx] = orig_ptr[0]; |
| 2030 | |
| 2031 | ++idx; |
| 2032 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2033 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2034 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2035 | |
| 2036 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2037 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2038 | start = ptr - perl_regex; |
| 2039 | |
| 2040 | ptr = strchr(ptr, '}'); |
| 2041 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2042 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2043 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2044 | free(perl_regex); |
| 2045 | return LY_EVALID; |
| 2046 | } |
| 2047 | end = (ptr - perl_regex) + 1; |
| 2048 | |
| 2049 | /* need more space */ |
| 2050 | if (end - start < URANGE_LEN) { |
| 2051 | 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] | 2052 | 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] | 2053 | } |
| 2054 | |
| 2055 | /* find our range */ |
| 2056 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2057 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2058 | break; |
| 2059 | } |
| 2060 | } |
| 2061 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2062 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2063 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2064 | free(perl_regex); |
| 2065 | return LY_EVALID; |
| 2066 | } |
| 2067 | |
| 2068 | /* 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] | 2069 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2070 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2071 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2072 | } |
| 2073 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2074 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2075 | } |
| 2076 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2077 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2078 | /* skip brackets */ |
| 2079 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2080 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2081 | } else { |
| 2082 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2083 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2088 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2089 | 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] | 2090 | &err_code, &err_offset, NULL); |
| 2091 | if (!code_local) { |
| 2092 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2093 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2094 | 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] | 2095 | free(perl_regex); |
| 2096 | return LY_EVALID; |
| 2097 | } |
| 2098 | free(perl_regex); |
| 2099 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2100 | if (code) { |
| 2101 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2102 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2103 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | return LY_SUCCESS; |
| 2107 | |
| 2108 | #undef URANGE_LEN |
| 2109 | } |
| 2110 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2111 | /** |
| 2112 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2113 | * @param[in] ctx Compile context. |
| 2114 | * @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] | 2115 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2116 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2117 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2118 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2119 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2120 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2121 | 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] | 2122 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2123 | { |
| 2124 | struct lysc_pattern **pattern; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2125 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2126 | LY_ERR ret = LY_SUCCESS; |
| 2127 | |
| 2128 | /* first, copy the patterns from the base type */ |
| 2129 | if (base_patterns) { |
| 2130 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2131 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2132 | } |
| 2133 | |
| 2134 | LY_ARRAY_FOR(patterns_p, u) { |
| 2135 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2136 | *pattern = calloc(1, sizeof **pattern); |
| 2137 | ++(*pattern)->refcount; |
| 2138 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2139 | 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] | 2140 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2141 | |
| 2142 | if (patterns_p[u].arg[0] == 0x15) { |
| 2143 | (*pattern)->inverted = 1; |
| 2144 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2145 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2146 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2147 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2148 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2149 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2150 | 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] | 2151 | } |
| 2152 | done: |
| 2153 | return ret; |
| 2154 | } |
| 2155 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2156 | /** |
| 2157 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2158 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2159 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2160 | 0 /* LY_TYPE_UNKNOWN */, |
| 2161 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2162 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2163 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2164 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2165 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2166 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2167 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2168 | 0 /* LY_TYPE_BOOL */, |
| 2169 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2170 | 0 /* LY_TYPE_EMPTY */, |
| 2171 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2172 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2173 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2174 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2175 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2176 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2177 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2178 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2179 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2180 | }; |
| 2181 | |
| 2182 | /** |
| 2183 | * @brief stringification of the YANG built-in data types |
| 2184 | */ |
| 2185 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2186 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2187 | "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] | 2188 | }; |
| 2189 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2190 | /** |
| 2191 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2192 | * @param[in] ctx Compile context. |
| 2193 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2194 | * @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] | 2195 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2196 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2197 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2198 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2199 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2200 | 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] | 2201 | 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] | 2202 | { |
| 2203 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2204 | LY_ARRAY_COUNT_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2205 | int32_t value = 0; |
| 2206 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2207 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2208 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2209 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2210 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2211 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2212 | return LY_EVALID; |
| 2213 | } |
| 2214 | |
| 2215 | LY_ARRAY_FOR(enums_p, u) { |
| 2216 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2217 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2218 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2219 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2220 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2221 | if (base_enums) { |
| 2222 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2223 | LY_ARRAY_FOR(base_enums, v) { |
| 2224 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2225 | break; |
| 2226 | } |
| 2227 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2228 | if (v == LY_ARRAY_COUNT(base_enums)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2229 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2230 | "Invalid %s - derived type adds new item \"%s\".", |
| 2231 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2232 | return LY_EVALID; |
| 2233 | } |
| 2234 | match = v; |
| 2235 | } |
| 2236 | |
| 2237 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2238 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2239 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2240 | e->value = (int32_t)enums_p[u].value; |
| 2241 | if (!u || e->value >= value) { |
| 2242 | value = e->value + 1; |
| 2243 | } |
| 2244 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2245 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2246 | if (e->value == (*enums)[v].value) { |
| 2247 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2248 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2249 | e->value, e->name, (*enums)[v].name); |
| 2250 | return LY_EVALID; |
| 2251 | } |
| 2252 | } |
| 2253 | } else if (base_enums) { |
| 2254 | /* inherit the assigned value */ |
| 2255 | e->value = base_enums[match].value; |
| 2256 | if (!u || e->value >= value) { |
| 2257 | value = e->value + 1; |
| 2258 | } |
| 2259 | } else { |
| 2260 | /* assign value automatically */ |
| 2261 | if (u && value == INT32_MIN) { |
| 2262 | /* counter overflow */ |
| 2263 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2264 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2265 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2266 | return LY_EVALID; |
| 2267 | } |
| 2268 | e->value = value++; |
| 2269 | } |
| 2270 | } else { /* LY_TYPE_BITS */ |
| 2271 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2272 | e->value = (int32_t)enums_p[u].value; |
| 2273 | if (!u || (uint32_t)e->value >= position) { |
| 2274 | position = (uint32_t)e->value + 1; |
| 2275 | } |
| 2276 | /* check collision with other values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2277 | for (v = 0; v < LY_ARRAY_COUNT(*enums) - 1; ++v) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2278 | if (e->value == (*enums)[v].value) { |
| 2279 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2280 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2281 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2282 | return LY_EVALID; |
| 2283 | } |
| 2284 | } |
| 2285 | } else if (base_enums) { |
| 2286 | /* inherit the assigned value */ |
| 2287 | e->value = base_enums[match].value; |
| 2288 | if (!u || (uint32_t)e->value >= position) { |
| 2289 | position = (uint32_t)e->value + 1; |
| 2290 | } |
| 2291 | } else { |
| 2292 | /* assign value automatically */ |
| 2293 | if (u && position == 0) { |
| 2294 | /* counter overflow */ |
| 2295 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2296 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2297 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2298 | return LY_EVALID; |
| 2299 | } |
| 2300 | e->value = position++; |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | if (base_enums) { |
| 2305 | /* the assigned values must not change from the derived type */ |
| 2306 | if (e->value != base_enums[match].value) { |
| 2307 | if (basetype == LY_TYPE_ENUM) { |
| 2308 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2309 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2310 | e->name, base_enums[match].value, e->value); |
| 2311 | } else { |
| 2312 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2313 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2314 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2315 | } |
| 2316 | return LY_EVALID; |
| 2317 | } |
| 2318 | } |
| 2319 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2320 | 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] | 2321 | 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] | 2322 | |
| 2323 | if (basetype == LY_TYPE_BITS) { |
| 2324 | /* keep bits ordered by position */ |
| 2325 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2326 | if (v != u) { |
| 2327 | memcpy(&storage, e, sizeof *e); |
| 2328 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2329 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2330 | } |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | done: |
| 2335 | return ret; |
| 2336 | } |
| 2337 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2338 | /** |
| 2339 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2340 | * |
| 2341 | * path-arg = absolute-path / relative-path |
| 2342 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2343 | * relative-path = 1*(".." "/") descendant-path |
| 2344 | * |
| 2345 | * @param[in,out] path Path to parse. |
| 2346 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2347 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2348 | * @param[out] name Name of the token. |
| 2349 | * @param[out] nam_len Length of the name. |
| 2350 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2351 | * must not be changed between consecutive calls. -1 if the |
| 2352 | * path is absolute. |
| 2353 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2354 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2355 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2356 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2357 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2358 | int *parent_times, int *has_predicate) |
| 2359 | { |
| 2360 | int par_times = 0; |
| 2361 | |
| 2362 | assert(path && *path); |
| 2363 | assert(parent_times); |
| 2364 | assert(prefix); |
| 2365 | assert(prefix_len); |
| 2366 | assert(name); |
| 2367 | assert(name_len); |
| 2368 | assert(has_predicate); |
| 2369 | |
| 2370 | *prefix = NULL; |
| 2371 | *prefix_len = 0; |
| 2372 | *name = NULL; |
| 2373 | *name_len = 0; |
| 2374 | *has_predicate = 0; |
| 2375 | |
| 2376 | if (!*parent_times) { |
| 2377 | if (!strncmp(*path, "..", 2)) { |
| 2378 | *path += 2; |
| 2379 | ++par_times; |
| 2380 | while (!strncmp(*path, "/..", 3)) { |
| 2381 | *path += 3; |
| 2382 | ++par_times; |
| 2383 | } |
| 2384 | } |
| 2385 | if (par_times) { |
| 2386 | *parent_times = par_times; |
| 2387 | } else { |
| 2388 | *parent_times = -1; |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | if (**path != '/') { |
| 2393 | return LY_EINVAL; |
| 2394 | } |
| 2395 | /* skip '/' */ |
| 2396 | ++(*path); |
| 2397 | |
| 2398 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2399 | 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] | 2400 | |
| 2401 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2402 | /* path continues by another token or this is the last token */ |
| 2403 | return LY_SUCCESS; |
| 2404 | } else if ((*path)[0] != '[') { |
| 2405 | /* unexpected character */ |
| 2406 | return LY_EINVAL; |
| 2407 | } else { |
| 2408 | /* predicate starting with [ */ |
| 2409 | *has_predicate = 1; |
| 2410 | return LY_SUCCESS; |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2415 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2416 | * |
| 2417 | * The set of features used for target must be a subset of features used for the leafref. |
| 2418 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2419 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2420 | * |
| 2421 | * @param[in] refnode The leafref node. |
| 2422 | * @param[in] target Tha target node of the leafref. |
| 2423 | * @return LY_SUCCESS or LY_EVALID; |
| 2424 | */ |
| 2425 | static LY_ERR |
| 2426 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2427 | { |
| 2428 | LY_ERR ret = LY_EVALID; |
| 2429 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2430 | LY_ARRAY_COUNT_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2431 | struct ly_set features = {0}; |
| 2432 | |
| 2433 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2434 | if (iter->iffeatures) { |
| 2435 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2436 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2437 | 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] | 2438 | } |
| 2439 | } |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2444 | * So when adding features applicable to the target into the features set, we should not be |
| 2445 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2446 | * to the leafref itself. */ |
| 2447 | count = features.count; |
| 2448 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2449 | if (iter->iffeatures) { |
| 2450 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2451 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2452 | 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] | 2453 | /* new feature was added (or LY_EMEM) */ |
| 2454 | goto cleanup; |
| 2455 | } |
| 2456 | } |
| 2457 | } |
| 2458 | } |
| 2459 | } |
| 2460 | ret = LY_SUCCESS; |
| 2461 | |
| 2462 | cleanup: |
| 2463 | ly_set_erase(&features, NULL); |
| 2464 | return ret; |
| 2465 | } |
| 2466 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2467 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2468 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2469 | struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2470 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2471 | /** |
| 2472 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2473 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2474 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2475 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2476 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2477 | * @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] | 2478 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2479 | * @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] | 2480 | * @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] | 2481 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2482 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2483 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2484 | * @return LY_ERR value. |
| 2485 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2486 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2487 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2488 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2489 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2490 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2491 | { |
| 2492 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2493 | struct lysc_type_bin *bin; |
| 2494 | struct lysc_type_num *num; |
| 2495 | struct lysc_type_str *str; |
| 2496 | struct lysc_type_bits *bits; |
| 2497 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2498 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2499 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2501 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2502 | |
| 2503 | switch (basetype) { |
| 2504 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2505 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2506 | |
| 2507 | /* 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] | 2508 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2509 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2510 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2511 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2512 | 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] | 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | if (tpdfname) { |
| 2517 | type_p->compiled = *type; |
| 2518 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2519 | } |
| 2520 | break; |
| 2521 | case LY_TYPE_BITS: |
| 2522 | /* RFC 7950 9.7 - bits */ |
| 2523 | bits = (struct lysc_type_bits*)(*type); |
| 2524 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2525 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2526 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2527 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2528 | } |
| 2529 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2530 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2531 | /* 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] | 2532 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2533 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2534 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2535 | 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] | 2536 | free(*type); |
| 2537 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2538 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2539 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | if (tpdfname) { |
| 2543 | type_p->compiled = *type; |
| 2544 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2545 | } |
| 2546 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2547 | case LY_TYPE_DEC64: |
| 2548 | dec = (struct lysc_type_dec*)(*type); |
| 2549 | |
| 2550 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2551 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2552 | if (!type_p->fraction_digits) { |
| 2553 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2554 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2555 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2556 | 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] | 2557 | free(*type); |
| 2558 | *type = NULL; |
| 2559 | } |
| 2560 | return LY_EVALID; |
| 2561 | } |
| 2562 | } else if (type_p->fraction_digits) { |
| 2563 | /* 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] | 2564 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2565 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2566 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2567 | tpdfname); |
| 2568 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2569 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2570 | "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] | 2571 | free(*type); |
| 2572 | *type = NULL; |
| 2573 | } |
| 2574 | return LY_EVALID; |
| 2575 | } |
| 2576 | dec->fraction_digits = type_p->fraction_digits; |
| 2577 | |
| 2578 | /* RFC 7950 9.2.4 - range */ |
| 2579 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2580 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2581 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2582 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2583 | 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] | 2584 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | if (tpdfname) { |
| 2588 | type_p->compiled = *type; |
| 2589 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2590 | } |
| 2591 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2592 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2593 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2594 | |
| 2595 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2596 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2597 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2598 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2599 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2600 | 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] | 2601 | } |
| 2602 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2603 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2604 | } |
| 2605 | |
| 2606 | /* RFC 7950 9.4.5 - pattern */ |
| 2607 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2608 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2609 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2610 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2611 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2612 | } |
| 2613 | |
| 2614 | if (tpdfname) { |
| 2615 | type_p->compiled = *type; |
| 2616 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2617 | } |
| 2618 | break; |
| 2619 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2620 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2621 | |
| 2622 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2623 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2624 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2625 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2626 | } |
| 2627 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2628 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2629 | /* 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] | 2630 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2631 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2632 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2633 | 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] | 2634 | free(*type); |
| 2635 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2636 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2637 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | if (tpdfname) { |
| 2641 | type_p->compiled = *type; |
| 2642 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2643 | } |
| 2644 | break; |
| 2645 | case LY_TYPE_INT8: |
| 2646 | case LY_TYPE_UINT8: |
| 2647 | case LY_TYPE_INT16: |
| 2648 | case LY_TYPE_UINT16: |
| 2649 | case LY_TYPE_INT32: |
| 2650 | case LY_TYPE_UINT32: |
| 2651 | case LY_TYPE_INT64: |
| 2652 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2653 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2654 | |
| 2655 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2656 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2657 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2658 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2659 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2660 | 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] | 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | if (tpdfname) { |
| 2665 | type_p->compiled = *type; |
| 2666 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2667 | } |
| 2668 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2669 | case LY_TYPE_IDENT: |
| 2670 | idref = (struct lysc_type_identityref*)(*type); |
| 2671 | |
| 2672 | /* RFC 7950 9.10.2 - base */ |
| 2673 | if (type_p->bases) { |
| 2674 | if (base) { |
| 2675 | /* only the directly derived identityrefs can contain base specification */ |
| 2676 | if (tpdfname) { |
| 2677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2678 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2679 | tpdfname); |
| 2680 | } else { |
| 2681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2682 | "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] | 2683 | free(*type); |
| 2684 | *type = NULL; |
| 2685 | } |
| 2686 | return LY_EVALID; |
| 2687 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2688 | 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] | 2689 | } |
| 2690 | |
| 2691 | if (!base && !type_p->flags) { |
| 2692 | /* type derived from identityref built-in type must contain at least one base */ |
| 2693 | if (tpdfname) { |
| 2694 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2695 | } else { |
| 2696 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2697 | free(*type); |
| 2698 | *type = NULL; |
| 2699 | } |
| 2700 | return LY_EVALID; |
| 2701 | } |
| 2702 | |
| 2703 | if (tpdfname) { |
| 2704 | type_p->compiled = *type; |
| 2705 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2706 | } |
| 2707 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2708 | case LY_TYPE_LEAFREF: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2709 | lref = (struct lysc_type_leafref*)*type; |
| 2710 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2711 | /* RFC 7950 9.9.3 - require-instance */ |
| 2712 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2713 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2714 | if (tpdfname) { |
| 2715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2716 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2717 | } else { |
| 2718 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2719 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2720 | free(*type); |
| 2721 | *type = NULL; |
| 2722 | } |
| 2723 | return LY_EVALID; |
| 2724 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2725 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2726 | } else if (base) { |
| 2727 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2728 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2729 | } else { |
| 2730 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2731 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2732 | } |
| 2733 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2734 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2735 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2736 | } else if (base) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2737 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path); |
| 2738 | lref->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2739 | } else if (tpdfname) { |
| 2740 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2741 | return LY_EVALID; |
| 2742 | } else { |
| 2743 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2744 | free(*type); |
| 2745 | *type = NULL; |
| 2746 | return LY_EVALID; |
| 2747 | } |
| 2748 | if (tpdfname) { |
| 2749 | type_p->compiled = *type; |
| 2750 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2751 | } |
| 2752 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2753 | case LY_TYPE_INST: |
| 2754 | /* RFC 7950 9.9.3 - require-instance */ |
| 2755 | if (type_p->flags & LYS_SET_REQINST) { |
| 2756 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2757 | } else { |
| 2758 | /* default is true */ |
| 2759 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2760 | } |
| 2761 | |
| 2762 | if (tpdfname) { |
| 2763 | type_p->compiled = *type; |
| 2764 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2765 | } |
| 2766 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2767 | case LY_TYPE_UNION: |
| 2768 | un = (struct lysc_type_union*)(*type); |
| 2769 | |
| 2770 | /* RFC 7950 7.4 - type */ |
| 2771 | if (type_p->types) { |
| 2772 | if (base) { |
| 2773 | /* only the directly derived union can contain types specification */ |
| 2774 | if (tpdfname) { |
| 2775 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2776 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2777 | tpdfname); |
| 2778 | } else { |
| 2779 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2780 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2781 | free(*type); |
| 2782 | *type = NULL; |
| 2783 | } |
| 2784 | return LY_EVALID; |
| 2785 | } |
| 2786 | /* compile the type */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2787 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_COUNT(type_p->types), LY_EVALID); |
| 2788 | 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] | 2789 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, |
| 2790 | &type_p->types[u], &un->types[u + additional], NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2791 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2792 | /* add space for additional types from the union subtype */ |
| 2793 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2794 | 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] | 2795 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2796 | |
| 2797 | /* copy subtypes of the subtype union */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 2798 | 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] | 2799 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2800 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2801 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2802 | 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] | 2803 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2804 | |
| 2805 | lref->basetype = LY_TYPE_LEAFREF; |
| 2806 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path); |
| 2807 | lref->refcount = 1; |
| 2808 | lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2809 | 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] | 2810 | /* TODO extensions */ |
| 2811 | |
| 2812 | } else { |
| 2813 | un->types[u + additional] = un_aux->types[v]; |
| 2814 | ++un_aux->types[v]->refcount; |
| 2815 | } |
| 2816 | ++additional; |
| 2817 | LY_ARRAY_INCREMENT(un->types); |
| 2818 | } |
| 2819 | /* compensate u increment in main loop */ |
| 2820 | --additional; |
| 2821 | |
| 2822 | /* free the replaced union subtype */ |
| 2823 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2824 | } else { |
| 2825 | LY_ARRAY_INCREMENT(un->types); |
| 2826 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | if (!base && !type_p->flags) { |
| 2831 | /* type derived from union built-in type must contain at least one type */ |
| 2832 | if (tpdfname) { |
| 2833 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2834 | } else { |
| 2835 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2836 | free(*type); |
| 2837 | *type = NULL; |
| 2838 | } |
| 2839 | return LY_EVALID; |
| 2840 | } |
| 2841 | |
| 2842 | if (tpdfname) { |
| 2843 | type_p->compiled = *type; |
| 2844 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2845 | } |
| 2846 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2847 | case LY_TYPE_BOOL: |
| 2848 | case LY_TYPE_EMPTY: |
| 2849 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2850 | break; |
| 2851 | } |
| 2852 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2853 | done: |
| 2854 | return ret; |
| 2855 | } |
| 2856 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2857 | /** |
| 2858 | * @brief Compile information about the leaf/leaf-list's type. |
| 2859 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2860 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2861 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2862 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2863 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2864 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2865 | * @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] | 2866 | * @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] | 2867 | * @return LY_ERR value. |
| 2868 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2869 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2870 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2871 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2872 | struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2873 | { |
| 2874 | LY_ERR ret = LY_SUCCESS; |
| 2875 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2876 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2877 | struct type_context { |
| 2878 | const struct lysp_tpdf *tpdf; |
| 2879 | struct lysp_node *node; |
| 2880 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2881 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2882 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2883 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2884 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2885 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2886 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2887 | |
| 2888 | (*type) = NULL; |
| 2889 | |
| 2890 | tctx = calloc(1, sizeof *tctx); |
| 2891 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2892 | 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] | 2893 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2894 | ret == LY_SUCCESS; |
| 2895 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2896 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2897 | if (basetype) { |
| 2898 | break; |
| 2899 | } |
| 2900 | |
| 2901 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2902 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2903 | 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] | 2904 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2905 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2906 | if (units && !*units) { |
| 2907 | /* inherit units */ |
| 2908 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2909 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2910 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2911 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2912 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2913 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2914 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2915 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2916 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2917 | break; |
| 2918 | } |
| 2919 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2920 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2921 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2922 | * 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] | 2923 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2924 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2925 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2926 | dummyloops = 1; |
| 2927 | goto preparenext; |
| 2928 | } else { |
| 2929 | tctx = NULL; |
| 2930 | break; |
| 2931 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2932 | } |
| 2933 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2934 | /* circular typedef reference detection */ |
| 2935 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2936 | /* local part */ |
| 2937 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2938 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2939 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2940 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2941 | free(tctx); |
| 2942 | ret = LY_EVALID; |
| 2943 | goto cleanup; |
| 2944 | } |
| 2945 | } |
| 2946 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2947 | /* global part for unions corner case */ |
| 2948 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 2949 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2950 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2951 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2952 | free(tctx); |
| 2953 | ret = LY_EVALID; |
| 2954 | goto cleanup; |
| 2955 | } |
| 2956 | } |
| 2957 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2958 | /* store information for the following processing */ |
| 2959 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2960 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2961 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2962 | /* prepare next loop */ |
| 2963 | tctx_prev = tctx; |
| 2964 | tctx = calloc(1, sizeof *tctx); |
| 2965 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2966 | } |
| 2967 | free(tctx); |
| 2968 | |
| 2969 | /* allocate type according to the basetype */ |
| 2970 | switch (basetype) { |
| 2971 | case LY_TYPE_BINARY: |
| 2972 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2973 | break; |
| 2974 | case LY_TYPE_BITS: |
| 2975 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2976 | break; |
| 2977 | case LY_TYPE_BOOL: |
| 2978 | case LY_TYPE_EMPTY: |
| 2979 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2980 | break; |
| 2981 | case LY_TYPE_DEC64: |
| 2982 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2983 | break; |
| 2984 | case LY_TYPE_ENUM: |
| 2985 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2986 | break; |
| 2987 | case LY_TYPE_IDENT: |
| 2988 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2989 | break; |
| 2990 | case LY_TYPE_INST: |
| 2991 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2992 | break; |
| 2993 | case LY_TYPE_LEAFREF: |
| 2994 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2995 | break; |
| 2996 | case LY_TYPE_STRING: |
| 2997 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2998 | break; |
| 2999 | case LY_TYPE_UNION: |
| 3000 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3001 | break; |
| 3002 | case LY_TYPE_INT8: |
| 3003 | case LY_TYPE_UINT8: |
| 3004 | case LY_TYPE_INT16: |
| 3005 | case LY_TYPE_UINT16: |
| 3006 | case LY_TYPE_INT32: |
| 3007 | case LY_TYPE_UINT32: |
| 3008 | case LY_TYPE_INT64: |
| 3009 | case LY_TYPE_UINT64: |
| 3010 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3011 | break; |
| 3012 | case LY_TYPE_UNKNOWN: |
| 3013 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3014 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3015 | ret = LY_EVALID; |
| 3016 | goto cleanup; |
| 3017 | } |
| 3018 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3019 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3020 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3021 | ly_data_type2str[basetype]); |
| 3022 | free(*type); |
| 3023 | (*type) = NULL; |
| 3024 | ret = LY_EVALID; |
| 3025 | goto cleanup; |
| 3026 | } |
| 3027 | |
| 3028 | /* get restrictions from the referred typedefs */ |
| 3029 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3030 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3031 | |
| 3032 | /* remember the typedef context for circular check */ |
| 3033 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3034 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3035 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3036 | base = tctx->tpdf->type.compiled; |
| 3037 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3038 | } 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] | 3039 | /* no change, just use the type information from the base */ |
| 3040 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3041 | ++base->refcount; |
| 3042 | continue; |
| 3043 | } |
| 3044 | |
| 3045 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3046 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3047 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3048 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3049 | ret = LY_EVALID; |
| 3050 | goto cleanup; |
| 3051 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3052 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3053 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3054 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3055 | ret = LY_EVALID; |
| 3056 | goto cleanup; |
| 3057 | } |
| 3058 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3059 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3060 | /* TODO user type plugins */ |
| 3061 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3062 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3063 | 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] | 3064 | 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] | 3065 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3066 | LY_CHECK_GOTO(ret, cleanup); |
| 3067 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3068 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3069 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3070 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3071 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3072 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3073 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3074 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3075 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3076 | /* TODO user type plugins */ |
| 3077 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3078 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3079 | 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] | 3080 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3081 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3082 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3083 | free(*type); |
| 3084 | (*type) = base; |
| 3085 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3086 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3087 | if (dflt && !(*type)->dflt) { |
| 3088 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3089 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3090 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3091 | (*type)->dflt->realtype = (*type); |
| 3092 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3093 | 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] | 3094 | 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] | 3095 | if (err) { |
| 3096 | ly_err_print(err); |
| 3097 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3098 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3099 | ly_err_free(err); |
| 3100 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3101 | if (ret == LY_EINCOMPLETE) { |
| 3102 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3103 | 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] | 3104 | |
| 3105 | /* but in general result is so far ok */ |
| 3106 | ret = LY_SUCCESS; |
| 3107 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3108 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3109 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3110 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3111 | 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] | 3112 | |
| 3113 | cleanup: |
| 3114 | ly_set_erase(&tpdf_chain, free); |
| 3115 | return ret; |
| 3116 | } |
| 3117 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3118 | /** |
| 3119 | * @brief Compile status information of the given node. |
| 3120 | * |
| 3121 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3122 | * has the status correctly set during the compilation. |
| 3123 | * |
| 3124 | * @param[in] ctx Compile context |
| 3125 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3126 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3127 | * the compatibility with the parent's status value. |
| 3128 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3129 | * @return LY_ERR value. |
| 3130 | */ |
| 3131 | static LY_ERR |
| 3132 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3133 | { |
| 3134 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3135 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3136 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3137 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3138 | if ((parent_flags & 0x3) != 0x3) { |
| 3139 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3140 | * combination of bits (0x3) which marks the uses_status value */ |
| 3141 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3142 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3143 | } |
| 3144 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3145 | } else { |
| 3146 | (*node_flags) |= LYS_STATUS_CURR; |
| 3147 | } |
| 3148 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3149 | /* check status compatibility with the parent */ |
| 3150 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3151 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3152 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3153 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3154 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3155 | } else { /* LYS_STATUS_DEPRC */ |
| 3156 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3157 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3158 | } |
| 3159 | return LY_EVALID; |
| 3160 | } |
| 3161 | } |
| 3162 | return LY_SUCCESS; |
| 3163 | } |
| 3164 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3165 | /** |
| 3166 | * @brief Check uniqness of the node/action/notification name. |
| 3167 | * |
| 3168 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3169 | * structures, but they share the namespace so we need to check their name collisions. |
| 3170 | * |
| 3171 | * @param[in] ctx Compile context. |
| 3172 | * @param[in] children List (linked list) of data nodes to go through. |
| 3173 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3174 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3175 | * @param[in] name Name of the item to find in the given lists. |
| 3176 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3177 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3178 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3179 | */ |
| 3180 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3181 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
| 3182 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3183 | { |
| 3184 | const struct lysc_node *iter; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3185 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3186 | |
| 3187 | LY_LIST_FOR(children, iter) { |
| 3188 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3189 | goto error; |
| 3190 | } |
| 3191 | } |
| 3192 | LY_ARRAY_FOR(actions, u) { |
| 3193 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3194 | goto error; |
| 3195 | } |
| 3196 | } |
| 3197 | LY_ARRAY_FOR(notifs, u) { |
| 3198 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3199 | goto error; |
| 3200 | } |
| 3201 | } |
| 3202 | return LY_SUCCESS; |
| 3203 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3204 | 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] | 3205 | return LY_EEXIST; |
| 3206 | } |
| 3207 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3208 | 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] | 3209 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3210 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3211 | * @brief Compile parsed RPC/action schema node information. |
| 3212 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3213 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3214 | * @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] | 3215 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3216 | * @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). |
| 3217 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3218 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3219 | */ |
| 3220 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3221 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3222 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3223 | { |
| 3224 | LY_ERR ret = LY_SUCCESS; |
| 3225 | struct lysp_node *child_p; |
| 3226 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3227 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3228 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3229 | lysc_update_path(ctx, parent, action_p->name); |
| 3230 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3231 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3232 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3233 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3234 | action_p->name, action)) { |
| 3235 | return LY_EVALID; |
| 3236 | } |
| 3237 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3238 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3239 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3240 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3241 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3242 | return LY_EVALID; |
| 3243 | } |
| 3244 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3245 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3246 | action->module = ctx->mod; |
| 3247 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3248 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3249 | action->sp = action_p; |
| 3250 | } |
| 3251 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3252 | |
| 3253 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3254 | * 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] | 3255 | 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] | 3256 | |
| 3257 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3258 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3259 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3260 | 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] | 3261 | 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] | 3262 | |
| 3263 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3264 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3265 | 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] | 3266 | 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] | 3267 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3268 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3269 | 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] | 3270 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3271 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3272 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3273 | |
| 3274 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3275 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3276 | 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] | 3277 | 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] | 3278 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3279 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3280 | 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] | 3281 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3282 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3283 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3284 | |
| 3285 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3286 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3287 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3288 | } |
| 3289 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3290 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3291 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3292 | return ret; |
| 3293 | } |
| 3294 | |
| 3295 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3296 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3297 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3298 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3299 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3300 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3301 | * @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] | 3302 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3303 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3304 | */ |
| 3305 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3306 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3307 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3308 | { |
| 3309 | LY_ERR ret = LY_SUCCESS; |
| 3310 | struct lysp_node *child_p; |
| 3311 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3312 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3313 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3314 | lysc_update_path(ctx, parent, notif_p->name); |
| 3315 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3316 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3317 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3318 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3319 | notif_p->name, notif)) { |
| 3320 | return LY_EVALID; |
| 3321 | } |
| 3322 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3323 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3324 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3325 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3326 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3327 | return LY_EVALID; |
| 3328 | } |
| 3329 | |
| 3330 | notif->nodetype = LYS_NOTIF; |
| 3331 | notif->module = ctx->mod; |
| 3332 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3333 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3334 | notif->sp = notif_p; |
| 3335 | } |
| 3336 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3337 | |
| 3338 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3339 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3340 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3341 | |
| 3342 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3343 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3344 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3345 | 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] | 3346 | 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] | 3347 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3348 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3349 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3350 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3351 | 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] | 3352 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3353 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3354 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3355 | 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] | 3356 | } |
| 3357 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3358 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3359 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3360 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3361 | return ret; |
| 3362 | } |
| 3363 | |
| 3364 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3365 | * @brief Compile parsed container node information. |
| 3366 | * @param[in] ctx Compile context |
| 3367 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3368 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3369 | * is enriched with the container-specific information. |
| 3370 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3371 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3372 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3373 | 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] | 3374 | { |
| 3375 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3376 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3377 | struct lysp_node *child_p; |
| 3378 | unsigned int u; |
| 3379 | LY_ERR ret = LY_SUCCESS; |
| 3380 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3381 | if (cont_p->presence) { |
| 3382 | cont->flags |= LYS_PRESENCE; |
| 3383 | } |
| 3384 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3385 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3386 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3387 | } |
| 3388 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3389 | 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] | 3390 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3391 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3392 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3393 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3394 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3395 | 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] | 3396 | |
| 3397 | done: |
| 3398 | return ret; |
| 3399 | } |
| 3400 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3401 | /* |
| 3402 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3403 | * @param[in] ctx Compile context. |
| 3404 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3405 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3406 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3407 | * @return LY_ERR value. |
| 3408 | */ |
| 3409 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3410 | 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] | 3411 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3412 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3413 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3414 | 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] | 3415 | leaf->units ? NULL : &leaf->units)); |
| 3416 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3417 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3418 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3419 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3420 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3421 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3422 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3423 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3424 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3425 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3426 | } |
| 3427 | } else { |
| 3428 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3429 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3430 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3431 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3432 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3433 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3434 | } |
| 3435 | } |
| 3436 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3437 | /* 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] | 3438 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3439 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3440 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3441 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3442 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3443 | /* 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] | 3444 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3445 | } |
| 3446 | } |
| 3447 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3448 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3449 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3450 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3451 | return LY_EVALID; |
| 3452 | } |
| 3453 | } |
| 3454 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3455 | return LY_SUCCESS; |
| 3456 | } |
| 3457 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3458 | /** |
| 3459 | * @brief Compile parsed leaf node information. |
| 3460 | * @param[in] ctx Compile context |
| 3461 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3462 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3463 | * is enriched with the leaf-specific information. |
| 3464 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3465 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3466 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3467 | 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] | 3468 | { |
| 3469 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3470 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3471 | unsigned int u; |
| 3472 | LY_ERR ret = LY_SUCCESS; |
| 3473 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3474 | 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] | 3475 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3476 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3477 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3478 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3479 | if (leaf_p->units) { |
| 3480 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3481 | leaf->flags |= LYS_SET_UNITS; |
| 3482 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3483 | |
| 3484 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3485 | leaf->dflt = (void*)leaf_p->dflt; |
| 3486 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3487 | if (ret) { |
| 3488 | leaf->dflt = NULL; |
| 3489 | return ret; |
| 3490 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3491 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3492 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3493 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3494 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3495 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3496 | leaf->dflt->realtype = leaf->type; |
| 3497 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3498 | 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] | 3499 | 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] | 3500 | leaf->dflt->realtype->refcount++; |
| 3501 | if (err) { |
| 3502 | ly_err_print(err); |
| 3503 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3504 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3505 | ly_err_free(err); |
| 3506 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3507 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3508 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3509 | 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] | 3510 | |
| 3511 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3512 | ret = LY_SUCCESS; |
| 3513 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3514 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3515 | leaf->flags |= LYS_SET_DFLT; |
| 3516 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3517 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3518 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3519 | done: |
| 3520 | return ret; |
| 3521 | } |
| 3522 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3523 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3524 | * @brief Compile parsed leaf-list node information. |
| 3525 | * @param[in] ctx Compile context |
| 3526 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3527 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3528 | * is enriched with the leaf-list-specific information. |
| 3529 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3530 | */ |
| 3531 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3532 | 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] | 3533 | { |
| 3534 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3535 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3536 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3537 | LY_ERR ret = LY_SUCCESS; |
| 3538 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3539 | 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] | 3540 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3541 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3542 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3543 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3544 | if (llist_p->units) { |
| 3545 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3546 | llist->flags |= LYS_SET_UNITS; |
| 3547 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3548 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3549 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3550 | llist->dflts = (void*)llist_p->dflts; |
| 3551 | 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] | 3552 | if (ret != LY_SUCCESS) { |
| 3553 | /* make sure the defaults are freed correctly */ |
| 3554 | if (llist_p->dflts) { |
| 3555 | llist->dflts = NULL; |
| 3556 | } |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3560 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3561 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3562 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(llist_p->dflts), ret, done); |
| 3563 | 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] | 3564 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3565 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3566 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3567 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3568 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3569 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3570 | llist->dflts[u]->realtype = llist->type; |
| 3571 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3572 | 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] | 3573 | 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] | 3574 | llist->dflts[u]->realtype->refcount++; |
| 3575 | if (err) { |
| 3576 | ly_err_print(err); |
| 3577 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3578 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3579 | ly_err_free(err); |
| 3580 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3581 | if (ret == LY_EINCOMPLETE) { |
| 3582 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3583 | 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] | 3584 | |
| 3585 | /* but in general result is so far ok */ |
| 3586 | ret = LY_SUCCESS; |
| 3587 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3588 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3589 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3590 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3591 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3592 | 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] | 3593 | /* configuration data values must be unique - so check the default values */ |
| 3594 | LY_ARRAY_FOR(llist->dflts, u) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3595 | for (v = u + 1; v < LY_ARRAY_COUNT(llist->dflts); ++v) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3596 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3597 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3598 | 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] | 3599 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3600 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3601 | if (dynamic) { |
| 3602 | free((char*)val); |
| 3603 | } |
| 3604 | return LY_EVALID; |
| 3605 | } |
| 3606 | } |
| 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | /* 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] | 3611 | |
| 3612 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3613 | if (llist->min) { |
| 3614 | llist->flags |= LYS_MAND_TRUE; |
| 3615 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3616 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3617 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3618 | done: |
| 3619 | return ret; |
| 3620 | } |
| 3621 | |
| 3622 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3623 | * @brief Compile information about list's uniques. |
| 3624 | * @param[in] ctx Compile context. |
| 3625 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3626 | * @param[in] uniques Sized array list of unique statements. |
| 3627 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3628 | * @return LY_ERR value. |
| 3629 | */ |
| 3630 | static LY_ERR |
| 3631 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3632 | { |
| 3633 | LY_ERR ret = LY_SUCCESS; |
| 3634 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3635 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3636 | const char *keystr, *delim; |
| 3637 | size_t len; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3638 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3639 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3640 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3641 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3642 | for (v = 0; v < LY_ARRAY_COUNT(uniques); ++v) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3643 | config = -1; |
| 3644 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3645 | keystr = uniques[v]; |
| 3646 | while (keystr) { |
| 3647 | delim = strpbrk(keystr, " \t\n"); |
| 3648 | if (delim) { |
| 3649 | len = delim - keystr; |
| 3650 | while (isspace(*delim)) { |
| 3651 | ++delim; |
| 3652 | } |
| 3653 | } else { |
| 3654 | len = strlen(keystr); |
| 3655 | } |
| 3656 | |
| 3657 | /* unique node must be present */ |
| 3658 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3659 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3660 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3661 | if (ret != LY_SUCCESS) { |
| 3662 | if (ret == LY_EDENIED) { |
| 3663 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3664 | "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] | 3665 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3666 | } |
| 3667 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3668 | } else if (flags) { |
| 3669 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3670 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3671 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3672 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3673 | } |
| 3674 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3675 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3676 | /* all referenced leafs must be of the same config type */ |
| 3677 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3678 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3679 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3680 | return LY_EVALID; |
| 3681 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3682 | config = 1; |
| 3683 | } else { /* LYS_CONFIG_R */ |
| 3684 | config = 0; |
| 3685 | } |
| 3686 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3687 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3688 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3689 | if (parent->nodetype == LYS_LIST) { |
| 3690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3691 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3692 | return LY_EVALID; |
| 3693 | } |
| 3694 | } |
| 3695 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3696 | /* check status */ |
| 3697 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3698 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3699 | |
| 3700 | /* mark leaf as unique */ |
| 3701 | (*key)->flags |= LYS_UNIQUE; |
| 3702 | |
| 3703 | /* next unique value in line */ |
| 3704 | keystr = delim; |
| 3705 | } |
| 3706 | /* next unique definition */ |
| 3707 | } |
| 3708 | |
| 3709 | return LY_SUCCESS; |
| 3710 | } |
| 3711 | |
| 3712 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3713 | * @brief Compile parsed list node information. |
| 3714 | * @param[in] ctx Compile context |
| 3715 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3716 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3717 | * is enriched with the list-specific information. |
| 3718 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3719 | */ |
| 3720 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3721 | 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] | 3722 | { |
| 3723 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3724 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3725 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3726 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3727 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3728 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3729 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3730 | LY_ERR ret = LY_SUCCESS; |
| 3731 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3732 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3733 | if (list->min) { |
| 3734 | list->flags |= LYS_MAND_TRUE; |
| 3735 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3736 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3737 | |
| 3738 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3739 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3740 | } |
| 3741 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3742 | 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] | 3743 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3744 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3745 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3746 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3747 | |
| 3748 | /* keys */ |
| 3749 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3750 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3751 | return LY_EVALID; |
| 3752 | } |
| 3753 | |
| 3754 | /* find all the keys (must be direct children) */ |
| 3755 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3756 | if (!keystr) { |
| 3757 | /* keyless list */ |
| 3758 | list->flags |= LYS_KEYLESS; |
| 3759 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3760 | while (keystr) { |
| 3761 | delim = strpbrk(keystr, " \t\n"); |
| 3762 | if (delim) { |
| 3763 | len = delim - keystr; |
| 3764 | while (isspace(*delim)) { |
| 3765 | ++delim; |
| 3766 | } |
| 3767 | } else { |
| 3768 | len = strlen(keystr); |
| 3769 | } |
| 3770 | |
| 3771 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3772 | 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] | 3773 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3774 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3775 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3776 | return LY_EVALID; |
| 3777 | } |
| 3778 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3779 | if (key->flags & LYS_KEY) { |
| 3780 | /* the node was already marked as a key */ |
| 3781 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3782 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3783 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3784 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3785 | |
| 3786 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3787 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3788 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3789 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3790 | return LY_EVALID; |
| 3791 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3792 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3793 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3794 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3795 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3796 | "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] | 3797 | return LY_EVALID; |
| 3798 | } |
| 3799 | } else { |
| 3800 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3801 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3802 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3803 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3804 | return LY_EVALID; |
| 3805 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3806 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3807 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3808 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3809 | return LY_EVALID; |
| 3810 | } |
| 3811 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3812 | |
| 3813 | /* check status */ |
| 3814 | 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] | 3815 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3816 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3817 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3818 | if (key->dflt) { |
| 3819 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3820 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3821 | free(key->dflt); |
| 3822 | key->dflt = NULL; |
| 3823 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3824 | } |
| 3825 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3826 | key->flags |= LYS_KEY; |
| 3827 | |
| 3828 | /* move it to the correct position */ |
| 3829 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3830 | /* fix links in closest previous siblings of the key */ |
| 3831 | if (key->next) { |
| 3832 | key->next->prev = key->prev; |
| 3833 | } else { |
| 3834 | /* last child */ |
| 3835 | list->child->prev = key->prev; |
| 3836 | } |
| 3837 | if (key->prev->next) { |
| 3838 | key->prev->next = key->next; |
| 3839 | } |
| 3840 | /* fix links in the key */ |
| 3841 | if (prev_key) { |
| 3842 | key->prev = (struct lysc_node*)prev_key; |
| 3843 | key->next = prev_key->next; |
| 3844 | } else { |
| 3845 | key->prev = list->child->prev; |
| 3846 | key->next = list->child; |
| 3847 | } |
| 3848 | /* fix links in closes future siblings of the key */ |
| 3849 | if (prev_key) { |
| 3850 | if (prev_key->next) { |
| 3851 | prev_key->next->prev = (struct lysc_node*)key; |
| 3852 | } else { |
| 3853 | list->child->prev = (struct lysc_node*)key; |
| 3854 | } |
| 3855 | prev_key->next = (struct lysc_node*)key; |
| 3856 | } else { |
| 3857 | list->child->prev = (struct lysc_node*)key; |
| 3858 | } |
| 3859 | /* fix links in parent */ |
| 3860 | if (!key->prev->next) { |
| 3861 | list->child = (struct lysc_node*)key; |
| 3862 | } |
| 3863 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3864 | |
| 3865 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3866 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3867 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3868 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | /* uniques */ |
| 3872 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3873 | 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] | 3874 | } |
| 3875 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3876 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3877 | 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] | 3878 | |
| 3879 | done: |
| 3880 | return ret; |
| 3881 | } |
| 3882 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3883 | /** |
| 3884 | * @brief Do some checks and set the default choice's case. |
| 3885 | * |
| 3886 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3887 | * |
| 3888 | * @param[in] ctx Compile context. |
| 3889 | * @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, |
| 3890 | * not the reference to the imported module. |
| 3891 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3892 | * @return LY_ERR value. |
| 3893 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3894 | static LY_ERR |
| 3895 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3896 | { |
| 3897 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3898 | const char *prefix = NULL, *name; |
| 3899 | size_t prefix_len = 0; |
| 3900 | |
| 3901 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3902 | name = strchr(dflt, ':'); |
| 3903 | if (name) { |
| 3904 | prefix = dflt; |
| 3905 | prefix_len = name - prefix; |
| 3906 | ++name; |
| 3907 | } else { |
| 3908 | name = dflt; |
| 3909 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3910 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3911 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3912 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3913 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3914 | prefix_len, prefix); |
| 3915 | return LY_EVALID; |
| 3916 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3917 | 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] | 3918 | if (!ch->dflt) { |
| 3919 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3920 | "Default case \"%s\" not found.", dflt); |
| 3921 | return LY_EVALID; |
| 3922 | } |
| 3923 | /* no mandatory nodes directly under the default case */ |
| 3924 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3925 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3926 | break; |
| 3927 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3928 | if (iter->flags & LYS_MAND_TRUE) { |
| 3929 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3930 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3931 | return LY_EVALID; |
| 3932 | } |
| 3933 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3934 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3935 | return LY_SUCCESS; |
| 3936 | } |
| 3937 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3938 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3939 | 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] | 3940 | { |
| 3941 | struct lys_module *mod; |
| 3942 | const char *prefix = NULL, *name; |
| 3943 | size_t prefix_len = 0; |
| 3944 | struct lysc_node_case *cs; |
| 3945 | struct lysc_node *node; |
| 3946 | |
| 3947 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3948 | name = strchr(dflt, ':'); |
| 3949 | if (name) { |
| 3950 | prefix = dflt; |
| 3951 | prefix_len = name - prefix; |
| 3952 | ++name; |
| 3953 | } else { |
| 3954 | name = dflt; |
| 3955 | } |
| 3956 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3957 | if (prefix) { |
| 3958 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3959 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3960 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3961 | "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] | 3962 | return LY_EVALID; |
| 3963 | } |
| 3964 | } else { |
| 3965 | mod = ctx->mod; |
| 3966 | } |
| 3967 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3968 | 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] | 3969 | if (!cs) { |
| 3970 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3971 | "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] | 3972 | return LY_EVALID; |
| 3973 | } |
| 3974 | |
| 3975 | /* check that there is no mandatory node */ |
| 3976 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3977 | if (node->parent != (struct lysc_node*)cs) { |
| 3978 | break; |
| 3979 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3980 | if (node->flags & LYS_MAND_TRUE) { |
| 3981 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3982 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3983 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3984 | return LY_EVALID; |
| 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | /* set the default case in choice */ |
| 3989 | ch->dflt = cs; |
| 3990 | cs->flags |= LYS_SET_DFLT; |
| 3991 | |
| 3992 | return LY_SUCCESS; |
| 3993 | } |
| 3994 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3995 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3996 | * @brief Compile parsed choice node information. |
| 3997 | * @param[in] ctx Compile context |
| 3998 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3999 | * @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] | 4000 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4001 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4002 | */ |
| 4003 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4004 | 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] | 4005 | { |
| 4006 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4007 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4008 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4009 | LY_ERR ret = LY_SUCCESS; |
| 4010 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4011 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4012 | if (child_p->nodetype == LYS_CASE) { |
| 4013 | 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] | 4014 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4015 | } |
| 4016 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4017 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4018 | } |
| 4019 | } |
| 4020 | |
| 4021 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4022 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4023 | 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] | 4024 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4025 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4026 | return ret; |
| 4027 | } |
| 4028 | |
| 4029 | /** |
| 4030 | * @brief Compile parsed anydata or anyxml node information. |
| 4031 | * @param[in] ctx Compile context |
| 4032 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4033 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4034 | * is enriched with the any-specific information. |
| 4035 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4036 | */ |
| 4037 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4038 | 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] | 4039 | { |
| 4040 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4041 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4042 | unsigned int u; |
| 4043 | LY_ERR ret = LY_SUCCESS; |
| 4044 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4045 | 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] | 4046 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4047 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4048 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4049 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4050 | |
| 4051 | if (any->flags & LYS_CONFIG_W) { |
| 4052 | 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] | 4053 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4054 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4055 | done: |
| 4056 | return ret; |
| 4057 | } |
| 4058 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4059 | /** |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4060 | * @brief Connect the node into the siblings list and check its name uniqueness. Also, |
| 4061 | * keep specific order of augments targetting the same node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4062 | * |
| 4063 | * @param[in] ctx Compile context |
| 4064 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4065 | * the choice itself is expected instead of a specific case node. |
| 4066 | * @param[in] node Schema node to connect into the list. |
| 4067 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4068 | * 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] | 4069 | */ |
| 4070 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4071 | 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] | 4072 | { |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4073 | struct lysc_node **children, *start, *end; |
| 4074 | const struct lys_module *mod; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4075 | |
| 4076 | if (node->nodetype == LYS_CASE) { |
| 4077 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4078 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4079 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4080 | } |
| 4081 | if (children) { |
| 4082 | if (!(*children)) { |
| 4083 | /* first child */ |
| 4084 | *children = node; |
| 4085 | } else if (*children != node) { |
| 4086 | /* by the condition in previous branch we cover the choice/case children |
| 4087 | * - the children list is shared by the choice and the the first case, in addition |
| 4088 | * the first child of each case must be referenced from the case node. So the node is |
| 4089 | * actually always already inserted in case it is the first children - so here such |
| 4090 | * a situation actually corresponds to the first branch */ |
Michal Vasko | 795b375 | 2020-07-13 15:24:27 +0200 | [diff] [blame] | 4091 | if (((*children)->prev->module != (*children)->module) && (node->module != (*children)->module) |
| 4092 | && (strcmp((*children)->prev->module->name, node->module->name) > 0)) { |
| 4093 | /* some augments are already connected and we are connecting new ones, |
| 4094 | * keep module name order and insert the node into the children list */ |
| 4095 | end = (*children); |
| 4096 | do { |
| 4097 | end = end->prev; |
| 4098 | mod = end->module; |
| 4099 | while (end->prev->module == mod) { |
| 4100 | end = end->prev; |
| 4101 | } |
| 4102 | } while ((end->prev->module != (*children)->module) && (end->prev->module != node->module) |
| 4103 | && (strcmp(mod->name, node->module->name) > 0)); |
| 4104 | |
| 4105 | /* we have the last existing node after our node, easily get the first before and connect it */ |
| 4106 | start = end->prev; |
| 4107 | start->next = node; |
| 4108 | node->next = end; |
| 4109 | end->prev = node; |
| 4110 | node->prev = start; |
| 4111 | } else { |
| 4112 | /* insert at the end of the parent's children list */ |
| 4113 | (*children)->prev->next = node; |
| 4114 | node->prev = (*children)->prev; |
| 4115 | (*children)->prev = node; |
| 4116 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4117 | |
| 4118 | /* check the name uniqueness */ |
| 4119 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4120 | lysc_node_notifs(parent), node->name, node)) { |
| 4121 | return LY_EEXIST; |
| 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | return LY_SUCCESS; |
| 4126 | } |
| 4127 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4128 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4129 | * @brief Prepare the case structure in choice node for the new data node. |
| 4130 | * |
| 4131 | * 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 |
| 4132 | * created in the choice when the first child was processed. |
| 4133 | * |
| 4134 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4135 | * @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, |
| 4136 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4137 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4138 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4139 | * @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, |
| 4140 | * 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] | 4141 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4142 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4143 | 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] | 4144 | { |
| 4145 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4146 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4147 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4148 | unsigned int u; |
| 4149 | LY_ERR ret; |
| 4150 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4151 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4152 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4153 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4154 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4155 | return NULL; \ |
| 4156 | } \ |
| 4157 | } |
| 4158 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4159 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4160 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4161 | |
| 4162 | /* we have to add an implicit case node into the parent choice */ |
| 4163 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4164 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4165 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4166 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4167 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4168 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4169 | /* the case is already present since the child is not its first children */ |
| 4170 | return (struct lysc_node_case*)ch->cases->prev; |
| 4171 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4172 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4173 | |
| 4174 | /* explicit parent case is not present (this is its first child) */ |
| 4175 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4176 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4177 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4178 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4179 | cs->sp = node_p; |
| 4180 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4181 | /* 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] | 4182 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4183 | |
| 4184 | if (node_p->when) { |
| 4185 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4186 | 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] | 4187 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4188 | |
| 4189 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4190 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4191 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4192 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4193 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4194 | 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] | 4195 | } else { |
| 4196 | LOGINT(ctx->ctx); |
| 4197 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4198 | } |
| 4199 | cs->module = ctx->mod; |
| 4200 | cs->prev = (struct lysc_node*)cs; |
| 4201 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4202 | 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] | 4203 | cs->child = child; |
| 4204 | |
| 4205 | return cs; |
| 4206 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4207 | if (cs) { |
| 4208 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4209 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4210 | return NULL; |
| 4211 | |
| 4212 | #undef UNIQUE_CHECK |
| 4213 | } |
| 4214 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4215 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4216 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4217 | * |
| 4218 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4219 | * @param[in] node Target node where the config is supposed to be changed. |
| 4220 | * @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] | 4221 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4222 | * 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] | 4223 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4224 | * @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] | 4225 | * @return LY_ERR value. |
| 4226 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4227 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4228 | 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] | 4229 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4230 | { |
| 4231 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4232 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4233 | |
| 4234 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4235 | /* nothing to do */ |
| 4236 | return LY_SUCCESS; |
| 4237 | } |
| 4238 | |
| 4239 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4240 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4241 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4242 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4243 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4244 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4245 | return LY_EVALID; |
| 4246 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4247 | node->flags |= LYS_SET_CONFIG; |
| 4248 | } else { |
| 4249 | if (node->flags & LYS_SET_CONFIG) { |
| 4250 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4251 | /* setting config flags, but have node with explicit config true */ |
| 4252 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4253 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4254 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4255 | return LY_EVALID; |
| 4256 | } |
| 4257 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4258 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4259 | return LY_SUCCESS; |
| 4260 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4261 | } |
| 4262 | node->flags &= ~LYS_CONFIG_MASK; |
| 4263 | node->flags |= config; |
| 4264 | |
| 4265 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4266 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4267 | 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] | 4268 | } |
| 4269 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4270 | return LY_SUCCESS; |
| 4271 | } |
| 4272 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4273 | /** |
| 4274 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4275 | * |
| 4276 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4277 | * the flag to such parents from a mandatory children. |
| 4278 | * |
| 4279 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4280 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4281 | * (mandatory children was removed). |
| 4282 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4283 | void |
| 4284 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4285 | { |
| 4286 | struct lysc_node *iter; |
| 4287 | |
| 4288 | if (add) { /* set flag */ |
| 4289 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4290 | parent = parent->parent) { |
| 4291 | parent->flags |= LYS_MAND_TRUE; |
| 4292 | } |
| 4293 | } else { /* unset flag */ |
| 4294 | 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] | 4295 | 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] | 4296 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4297 | /* there is another mandatory node */ |
| 4298 | return; |
| 4299 | } |
| 4300 | } |
| 4301 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4302 | parent->flags &= ~LYS_MAND_TRUE; |
| 4303 | } |
| 4304 | } |
| 4305 | } |
| 4306 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4307 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4308 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4309 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4310 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4311 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4312 | */ |
| 4313 | static void |
| 4314 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4315 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4316 | LY_ARRAY_COUNT_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4317 | size_t len; |
| 4318 | |
| 4319 | len = strlen(aug_p->nodeid); |
| 4320 | LY_ARRAY_FOR(result, v) { |
| 4321 | if (strlen(result[v]->nodeid) <= len) { |
| 4322 | continue; |
| 4323 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4324 | if (v < LY_ARRAY_COUNT(result)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4325 | /* move the rest of array */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4326 | 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] | 4327 | break; |
| 4328 | } |
| 4329 | } |
| 4330 | result[v] = aug_p; |
| 4331 | LY_ARRAY_INCREMENT(result); |
| 4332 | } |
| 4333 | |
| 4334 | /** |
| 4335 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4336 | * |
| 4337 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4338 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4339 | * |
| 4340 | * @param[in] ctx Compile context. |
| 4341 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4342 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4343 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4344 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4345 | * @return LY_ERR value. |
| 4346 | */ |
| 4347 | LY_ERR |
| 4348 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4349 | { |
| 4350 | struct lysp_augment **result = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4351 | LY_ARRAY_COUNT_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4352 | |
| 4353 | assert(augments); |
| 4354 | |
| 4355 | /* get count of the augments in module and all its submodules */ |
| 4356 | if (aug_p) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4357 | count += LY_ARRAY_COUNT(aug_p); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4358 | } |
| 4359 | LY_ARRAY_FOR(inc_p, u) { |
| 4360 | if (inc_p[u].submodule->augments) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4361 | count += LY_ARRAY_COUNT(inc_p[u].submodule->augments); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4362 | } |
| 4363 | } |
| 4364 | |
| 4365 | if (!count) { |
| 4366 | *augments = NULL; |
| 4367 | return LY_SUCCESS; |
| 4368 | } |
| 4369 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4370 | |
| 4371 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4372 | * together, so there can be even /z/y betwwen them. */ |
| 4373 | LY_ARRAY_FOR(aug_p, u) { |
| 4374 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4375 | } |
| 4376 | LY_ARRAY_FOR(inc_p, u) { |
| 4377 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4378 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4379 | } |
| 4380 | } |
| 4381 | |
| 4382 | *augments = result; |
| 4383 | return LY_SUCCESS; |
| 4384 | } |
| 4385 | |
| 4386 | /** |
| 4387 | * @brief Compile the parsed augment connecting it into its target. |
| 4388 | * |
| 4389 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4390 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4391 | * are already implemented and compiled. |
| 4392 | * |
| 4393 | * @param[in] ctx Compile context. |
| 4394 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4395 | * @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 |
| 4396 | * children in case of the augmenting uses data. |
| 4397 | * @return LY_SUCCESS on success. |
| 4398 | * @return LY_EVALID on failure. |
| 4399 | */ |
| 4400 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4401 | 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] | 4402 | { |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4403 | LY_ERR ret = LY_SUCCESS, rc; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4404 | struct lysp_node *node_p, *case_node_p; |
| 4405 | struct lysc_node *target; /* target target of the augment */ |
| 4406 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4407 | struct lysc_when **when, *when_shared; |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4408 | struct lys_module **aug_mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4409 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4410 | uint16_t flags = 0; |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4411 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4412 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4413 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4414 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4415 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4416 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4417 | 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] | 4418 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4419 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4420 | if (ret != LY_SUCCESS) { |
| 4421 | if (ret == LY_EDENIED) { |
| 4422 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4423 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4424 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4425 | } |
| 4426 | return LY_EVALID; |
| 4427 | } |
| 4428 | |
| 4429 | /* check for mandatory nodes |
| 4430 | * - new cases augmenting some choice can have mandatory nodes |
| 4431 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4432 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4433 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4434 | allow_mandatory = 1; |
| 4435 | } |
| 4436 | |
| 4437 | when_shared = NULL; |
| 4438 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4439 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4440 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4441 | && !((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] | 4442 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4443 | && !(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] | 4444 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4445 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4446 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4447 | return LY_EVALID; |
| 4448 | } |
| 4449 | |
| 4450 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4451 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4452 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4453 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4454 | } else { |
| 4455 | 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] | 4456 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4457 | } |
| 4458 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4459 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4460 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4461 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4462 | * 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] | 4463 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4464 | /* 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] | 4465 | 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] | 4466 | } else if (target->nodetype == LYS_CHOICE) { |
| 4467 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4468 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4469 | } else { |
| 4470 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4471 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4472 | if (!node) { |
| 4473 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4474 | break; |
| 4475 | } |
| 4476 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4477 | } |
| 4478 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4479 | 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] | 4480 | node->flags &= ~LYS_MAND_TRUE; |
| 4481 | lys_compile_mandatory_parents(target, 0); |
| 4482 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4483 | "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] | 4484 | return LY_EVALID; |
| 4485 | } |
| 4486 | |
| 4487 | /* pass augment's when to all the children */ |
| 4488 | if (aug_p->when) { |
| 4489 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4490 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4491 | 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] | 4492 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4493 | |
| 4494 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4495 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4496 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4499 | when_shared = *when; |
| 4500 | } else { |
| 4501 | ++when_shared->refcount; |
| 4502 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4503 | |
| 4504 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4505 | /* 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] | 4506 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4507 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4508 | } |
| 4509 | } |
| 4510 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4511 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4512 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4513 | switch (target->nodetype) { |
| 4514 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4515 | 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] | 4516 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4517 | 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] | 4518 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4519 | break; |
| 4520 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4521 | 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] | 4522 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4523 | 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] | 4524 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4525 | break; |
| 4526 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4527 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4528 | if (aug_p->actions) { |
| 4529 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4530 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4531 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4532 | return LY_EVALID; |
| 4533 | } |
| 4534 | if (aug_p->notifs) { |
| 4535 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4536 | "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] | 4537 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4538 | return LY_EVALID; |
| 4539 | } |
| 4540 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4541 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 4542 | /* add this module into the target module augmented_by, if not there already */ |
| 4543 | rc = LY_SUCCESS; |
| 4544 | LY_ARRAY_FOR(target->module->compiled->augmented_by, v) { |
| 4545 | if (target->module->compiled->augmented_by[v] == ctx->mod) { |
| 4546 | rc = LY_EEXIST; |
| 4547 | break; |
| 4548 | } |
| 4549 | } |
| 4550 | if (!rc) { |
| 4551 | LY_ARRAY_NEW_GOTO(ctx->ctx, target->module->compiled->augmented_by, aug_mod, ret, error); |
| 4552 | *aug_mod = ctx->mod; |
| 4553 | } |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4554 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4555 | lysc_update_path(ctx, NULL, NULL); |
| 4556 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | a3af598 | 2020-06-29 11:51:37 +0200 | [diff] [blame] | 4557 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4558 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4559 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4560 | return ret; |
| 4561 | } |
| 4562 | |
| 4563 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4564 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4565 | * |
| 4566 | * @param[in] ctx Compile context. |
| 4567 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4568 | * @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] | 4569 | * @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] | 4570 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4571 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4572 | * 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] | 4573 | * @return LY_ERR value. |
| 4574 | */ |
| 4575 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4576 | 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] | 4577 | { |
| 4578 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4579 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4580 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4581 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4582 | return LY_EVALID; |
| 4583 | } |
| 4584 | |
| 4585 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4586 | /* check if node has default value */ |
| 4587 | if (node->nodetype & LYS_LEAF) { |
| 4588 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4589 | if (refine_flag) { |
| 4590 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4591 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4592 | return LY_EVALID; |
| 4593 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4594 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4595 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4596 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4597 | |
| 4598 | /* update the list of incomplete default values if needed */ |
| 4599 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4600 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4601 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4602 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4603 | free(leaf->dflt); |
| 4604 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4605 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4606 | } |
| 4607 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4608 | if (refine_flag) { |
| 4609 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4610 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4611 | return LY_EVALID; |
| 4612 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4613 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4614 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4615 | 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] | 4616 | return LY_EVALID; |
| 4617 | } |
| 4618 | |
| 4619 | node->flags &= ~LYS_MAND_FALSE; |
| 4620 | node->flags |= LYS_MAND_TRUE; |
| 4621 | lys_compile_mandatory_parents(node->parent, 1); |
| 4622 | } else { |
| 4623 | /* make mandatory false */ |
| 4624 | node->flags &= ~LYS_MAND_TRUE; |
| 4625 | node->flags |= LYS_MAND_FALSE; |
| 4626 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4627 | 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] | 4628 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4629 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4630 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4631 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4632 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4633 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4634 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4635 | } |
| 4636 | } |
| 4637 | return LY_SUCCESS; |
| 4638 | } |
| 4639 | |
| 4640 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4641 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4642 | * If present, also apply uses's modificators. |
| 4643 | * |
| 4644 | * @param[in] ctx Compile context |
| 4645 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4646 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4647 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4648 | * the compile context. |
| 4649 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4650 | */ |
| 4651 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4652 | 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] | 4653 | { |
| 4654 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4655 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4656 | /* 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] | 4657 | struct lysc_node_container context_node_fake = |
| 4658 | {.nodetype = LYS_CONTAINER, |
| 4659 | .module = ctx->mod, |
| 4660 | .flags = parent ? parent->flags : 0, |
| 4661 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4662 | .prev = (struct lysc_node*)&context_node_fake, |
| 4663 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4664 | struct lysp_grp *grp = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4665 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4666 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4667 | int found; |
| 4668 | const char *id, *name, *prefix; |
| 4669 | size_t prefix_len, name_len; |
| 4670 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4671 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4672 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4673 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4674 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4675 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4676 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4677 | struct lysp_augment **augments = NULL; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4678 | LY_ARRAY_COUNT_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4679 | struct lysc_notif **notifs = NULL; |
| 4680 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4681 | |
| 4682 | /* search for the grouping definition */ |
| 4683 | found = 0; |
| 4684 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4685 | 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] | 4686 | if (prefix) { |
| 4687 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4688 | if (!mod) { |
| 4689 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4690 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4691 | return LY_EVALID; |
| 4692 | } |
| 4693 | } else { |
| 4694 | mod = ctx->mod_def; |
| 4695 | } |
| 4696 | if (mod == ctx->mod_def) { |
| 4697 | 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] | 4698 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4699 | LY_ARRAY_FOR(grp, u) { |
| 4700 | if (!strcmp(grp[u].name, name)) { |
| 4701 | grp = &grp[u]; |
| 4702 | found = 1; |
| 4703 | break; |
| 4704 | } |
| 4705 | } |
| 4706 | } |
| 4707 | } |
| 4708 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4709 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4710 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4711 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4712 | for (u = 0; !found && u < LY_ARRAY_COUNT(grp); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4713 | if (!strcmp(grp[u].name, name)) { |
| 4714 | grp = &grp[u]; |
| 4715 | found = 1; |
| 4716 | } |
| 4717 | } |
| 4718 | } |
| 4719 | if (!found && mod->parsed->includes) { |
| 4720 | /* ... and all the submodules */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4721 | for (u = 0; !found && u < LY_ARRAY_COUNT(mod->parsed->includes); ++u) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4722 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4723 | if (grp) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4724 | for (v = 0; !found && v < LY_ARRAY_COUNT(grp); ++v) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4725 | if (!strcmp(grp[v].name, name)) { |
| 4726 | grp = &grp[v]; |
| 4727 | found = 1; |
| 4728 | } |
| 4729 | } |
| 4730 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4731 | } |
| 4732 | } |
| 4733 | } |
| 4734 | if (!found) { |
| 4735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4736 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4737 | return LY_EVALID; |
| 4738 | } |
| 4739 | |
| 4740 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4741 | grp_stack_count = ctx->groupings.count; |
| 4742 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4743 | if (grp_stack_count == ctx->groupings.count) { |
| 4744 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4745 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4746 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4747 | return LY_EVALID; |
| 4748 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4749 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4750 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4751 | grp->flags |= LYS_USED_GRP; |
| 4752 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4753 | |
| 4754 | /* switch context's mod_def */ |
| 4755 | mod_old = ctx->mod_def; |
| 4756 | ctx->mod_def = mod; |
| 4757 | |
| 4758 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4759 | 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] | 4760 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4761 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4762 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4763 | /* 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] | 4764 | 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] | 4765 | |
| 4766 | /* some preparation for applying refines */ |
| 4767 | if (grp->data == node_p) { |
| 4768 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4769 | if (parent) { |
| 4770 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4771 | } else if (ctx->mod->compiled->data) { |
| 4772 | child = ctx->mod->compiled->data; |
| 4773 | } else { |
| 4774 | child = NULL; |
| 4775 | } |
| 4776 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4777 | } |
| 4778 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4779 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4780 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4781 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4782 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4783 | /* 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] | 4784 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4785 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4786 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4787 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4788 | |
| 4789 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4790 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4791 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4792 | } |
| 4793 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4794 | when_shared = *when; |
| 4795 | } else { |
| 4796 | ++when_shared->refcount; |
| 4797 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4798 | |
| 4799 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4800 | /* 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] | 4801 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4802 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4803 | } |
| 4804 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4805 | } |
| 4806 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4807 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4808 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4809 | /* 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] | 4810 | 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] | 4811 | } |
| 4812 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4813 | /* compile actions */ |
| 4814 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4815 | if (actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4816 | actions_index = *actions ? LY_ARRAY_COUNT(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4817 | 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] | 4818 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4819 | /* 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] | 4820 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_COUNT(*actions) - actions_index, ret, cleanup); |
| 4821 | LY_ARRAY_COUNT(context_node_fake.actions) = LY_ARRAY_COUNT(*actions) - actions_index; |
| 4822 | 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] | 4823 | } |
| 4824 | } |
| 4825 | |
| 4826 | /* compile notifications */ |
| 4827 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4828 | if (notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4829 | notifs_index = *notifs ? LY_ARRAY_COUNT(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4830 | 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] | 4831 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4832 | /* 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] | 4833 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_COUNT(*notifs) - notifs_index, ret, cleanup); |
| 4834 | LY_ARRAY_COUNT(context_node_fake.notifs) = LY_ARRAY_COUNT(*notifs) - notifs_index; |
| 4835 | 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] | 4836 | } |
| 4837 | } |
| 4838 | |
| 4839 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4840 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4841 | 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] | 4842 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4843 | 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] | 4844 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4845 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4846 | /* reload previous context's mod_def */ |
| 4847 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4848 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4849 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4850 | /* apply refine */ |
| 4851 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4852 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4853 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4854 | 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] | 4855 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4856 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4857 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4858 | |
| 4859 | /* default value */ |
| 4860 | if (rfn->dflts) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4861 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_COUNT(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4862 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4863 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_COUNT_TYPE" default values.", |
| 4864 | lys_nodetype2str(node->nodetype), LY_ARRAY_COUNT(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4865 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4866 | } |
| 4867 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4868 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4869 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4870 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4871 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4872 | } |
| 4873 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4874 | struct ly_err_item *err = NULL; |
| 4875 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4876 | if (leaf->dflt) { |
| 4877 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4878 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4879 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4880 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4881 | } else { |
| 4882 | /* prepare a new one */ |
| 4883 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4884 | leaf->dflt->realtype = leaf->type; |
| 4885 | } |
| 4886 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4887 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4888 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4889 | 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] | 4890 | 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] | 4891 | leaf->dflt->realtype->refcount++; |
| 4892 | if (err) { |
| 4893 | ly_err_print(err); |
| 4894 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4895 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4896 | ly_err_free(err); |
| 4897 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4898 | if (rc == LY_EINCOMPLETE) { |
| 4899 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4900 | 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] | 4901 | |
| 4902 | /* but in general result is so far ok */ |
| 4903 | rc = LY_SUCCESS; |
| 4904 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4905 | LY_CHECK_GOTO(rc, cleanup); |
| 4906 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4907 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4908 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4909 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4910 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4911 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4912 | "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] | 4913 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4914 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4915 | |
| 4916 | /* remove previous set of default values */ |
| 4917 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4918 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4919 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4920 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4921 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4922 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4923 | LY_ARRAY_FREE(llist->dflts); |
| 4924 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4925 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4926 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4927 | |
| 4928 | /* create the new set of the default values */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 4929 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(rfn->dflts), ret, cleanup); |
| 4930 | 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] | 4931 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4932 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4933 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4934 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4935 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4936 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4937 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4938 | 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] | 4939 | 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] | 4940 | 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] | 4941 | llist->dflts[u]->realtype->refcount++; |
| 4942 | if (err) { |
| 4943 | ly_err_print(err); |
| 4944 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4945 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4946 | ly_err_free(err); |
| 4947 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4948 | if (rc == LY_EINCOMPLETE) { |
| 4949 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4950 | 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] | 4951 | |
| 4952 | /* but in general result is so far ok */ |
| 4953 | rc = LY_SUCCESS; |
| 4954 | } |
| 4955 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4956 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4957 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4958 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4959 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4960 | /* unset LYS_SET_DFLT from the current default case */ |
| 4961 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4962 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4963 | 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] | 4964 | } |
| 4965 | } |
| 4966 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4967 | /* description */ |
| 4968 | if (rfn->dsc) { |
| 4969 | FREE_STRING(ctx->ctx, node->dsc); |
| 4970 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4971 | } |
| 4972 | |
| 4973 | /* reference */ |
| 4974 | if (rfn->ref) { |
| 4975 | FREE_STRING(ctx->ctx, node->ref); |
| 4976 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4977 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4978 | |
| 4979 | /* config */ |
| 4980 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4981 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4982 | 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] | 4983 | } else { |
| 4984 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4985 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4986 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | /* mandatory */ |
| 4990 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4991 | 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] | 4992 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4993 | |
| 4994 | /* presence */ |
| 4995 | if (rfn->presence) { |
| 4996 | if (node->nodetype != LYS_CONTAINER) { |
| 4997 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4998 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4999 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5000 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5001 | } |
| 5002 | node->flags |= LYS_PRESENCE; |
| 5003 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5004 | |
| 5005 | /* must */ |
| 5006 | if (rfn->musts) { |
| 5007 | switch (node->nodetype) { |
| 5008 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5009 | 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] | 5010 | break; |
| 5011 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5012 | 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] | 5013 | break; |
| 5014 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5015 | 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] | 5016 | break; |
| 5017 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5018 | 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] | 5019 | break; |
| 5020 | case LYS_ANYXML: |
| 5021 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5022 | 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] | 5023 | break; |
| 5024 | default: |
| 5025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5026 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5027 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5028 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5029 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5030 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5031 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5032 | |
| 5033 | /* min/max-elements */ |
| 5034 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5035 | switch (node->nodetype) { |
| 5036 | case LYS_LEAFLIST: |
| 5037 | if (rfn->flags & LYS_SET_MAX) { |
| 5038 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5039 | } |
| 5040 | if (rfn->flags & LYS_SET_MIN) { |
| 5041 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5042 | if (rfn->min) { |
| 5043 | node->flags |= LYS_MAND_TRUE; |
| 5044 | lys_compile_mandatory_parents(node->parent, 1); |
| 5045 | } else { |
| 5046 | node->flags &= ~LYS_MAND_TRUE; |
| 5047 | lys_compile_mandatory_parents(node->parent, 0); |
| 5048 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5049 | } |
| 5050 | break; |
| 5051 | case LYS_LIST: |
| 5052 | if (rfn->flags & LYS_SET_MAX) { |
| 5053 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5054 | } |
| 5055 | if (rfn->flags & LYS_SET_MIN) { |
| 5056 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5057 | if (rfn->min) { |
| 5058 | node->flags |= LYS_MAND_TRUE; |
| 5059 | lys_compile_mandatory_parents(node->parent, 1); |
| 5060 | } else { |
| 5061 | node->flags &= ~LYS_MAND_TRUE; |
| 5062 | lys_compile_mandatory_parents(node->parent, 0); |
| 5063 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5064 | } |
| 5065 | break; |
| 5066 | default: |
| 5067 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5068 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5069 | (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] | 5070 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5071 | } |
| 5072 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5073 | |
| 5074 | /* if-feature */ |
| 5075 | if (rfn->iffeatures) { |
| 5076 | /* 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] | 5077 | 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] | 5078 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5079 | |
| 5080 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5081 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5082 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5083 | /* 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] | 5084 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5085 | node = (struct lysc_node*)refined.objs[i]; |
| 5086 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5087 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5088 | |
| 5089 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5090 | if ((node->flags & LYS_MAND_TRUE) && |
| 5091 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5092 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5093 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5094 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5095 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5096 | } |
| 5097 | |
| 5098 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5099 | if (node->nodetype == LYS_LIST) { |
| 5100 | min = ((struct lysc_node_list*)node)->min; |
| 5101 | max = ((struct lysc_node_list*)node)->max; |
| 5102 | } else { |
| 5103 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5104 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5105 | } |
| 5106 | if (min > max) { |
| 5107 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5108 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5109 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5110 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5111 | } |
| 5112 | } |
| 5113 | } |
| 5114 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5115 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5116 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5117 | |
| 5118 | cleanup: |
| 5119 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5120 | if (context_node_fake.child) { |
| 5121 | context_node_fake.child->prev = child; |
| 5122 | } |
| 5123 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5124 | child->parent = parent; |
| 5125 | } |
| 5126 | |
| 5127 | if (uses_p->augments || uses_p->refines) { |
| 5128 | /* 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] | 5129 | if (context_node_fake.actions) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5130 | 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] | 5131 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5132 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5133 | if (context_node_fake.notifs) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5134 | 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] | 5135 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5136 | } |
| 5137 | } |
| 5138 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5139 | /* reload previous context's mod_def */ |
| 5140 | ctx->mod_def = mod_old; |
| 5141 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5142 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5143 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5144 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5145 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5146 | |
| 5147 | return ret; |
| 5148 | } |
| 5149 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5150 | static int |
| 5151 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5152 | { |
| 5153 | struct lysp_node *iter; |
| 5154 | int len = 0; |
| 5155 | |
| 5156 | *path = NULL; |
| 5157 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5158 | char *s = *path; |
| 5159 | char *id; |
| 5160 | |
| 5161 | switch (iter->nodetype) { |
| 5162 | case LYS_USES: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5163 | LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5164 | break; |
| 5165 | case LYS_GROUPING: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5166 | LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5167 | break; |
| 5168 | case LYS_AUGMENT: |
Radek Krejci | 200f106 | 2020-07-11 22:51:03 +0200 | [diff] [blame] | 5169 | LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5170 | break; |
| 5171 | default: |
| 5172 | id = strdup(iter->name); |
| 5173 | break; |
| 5174 | } |
| 5175 | |
| 5176 | if (!iter->parent) { |
| 5177 | /* print prefix */ |
| 5178 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5179 | } else { |
| 5180 | /* prefix is the same as in parent */ |
| 5181 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5182 | } |
| 5183 | free(s); |
| 5184 | free(id); |
| 5185 | } |
| 5186 | |
| 5187 | if (len < 0) { |
| 5188 | free(*path); |
| 5189 | *path = NULL; |
| 5190 | } else if (len == 0) { |
| 5191 | *path = strdup("/"); |
| 5192 | len = 1; |
| 5193 | } |
| 5194 | return len; |
| 5195 | } |
| 5196 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5197 | /** |
| 5198 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5199 | * |
| 5200 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5201 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5202 | */ |
| 5203 | static LY_ERR |
| 5204 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5205 | { |
| 5206 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5207 | char *path; |
| 5208 | int len; |
| 5209 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5210 | struct lysp_node_uses fake_uses = { |
| 5211 | .parent = node_p, |
| 5212 | .nodetype = LYS_USES, |
| 5213 | .flags = 0, .next = NULL, |
| 5214 | .name = grp->name, |
| 5215 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5216 | .refines = NULL, .augments = NULL |
| 5217 | }; |
| 5218 | struct lysc_node_container fake_container = { |
| 5219 | .nodetype = LYS_CONTAINER, |
| 5220 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5221 | .module = ctx->mod, |
| 5222 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5223 | .prev = (struct lysc_node*)&fake_container, |
| 5224 | .name = "fake", |
| 5225 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5226 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5227 | }; |
| 5228 | |
| 5229 | if (grp->parent) { |
| 5230 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5231 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5232 | |
| 5233 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5234 | if (len < 0) { |
| 5235 | LOGMEM(ctx->ctx); |
| 5236 | return LY_EMEM; |
| 5237 | } |
| 5238 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5239 | ctx->path_len = (uint16_t)len; |
| 5240 | free(path); |
| 5241 | |
| 5242 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5243 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5244 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5245 | lysc_update_path(ctx, NULL, NULL); |
| 5246 | lysc_update_path(ctx, NULL, NULL); |
| 5247 | |
| 5248 | ctx->path_len = 1; |
| 5249 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5250 | |
| 5251 | /* cleanup */ |
| 5252 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5253 | |
| 5254 | return ret; |
| 5255 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5256 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5257 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5258 | * @brief Compile parsed schema node information. |
| 5259 | * @param[in] ctx Compile context |
| 5260 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5261 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5262 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5263 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5264 | * @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). |
| 5265 | * 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] | 5266 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5267 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5268 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5269 | 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] | 5270 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5271 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5272 | struct lysc_node *node; |
| 5273 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5274 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5275 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5276 | 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] | 5277 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5278 | if (node_p->nodetype != LYS_USES) { |
| 5279 | lysc_update_path(ctx, parent, node_p->name); |
| 5280 | } else { |
| 5281 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5282 | lysc_update_path(ctx, NULL, node_p->name); |
| 5283 | } |
| 5284 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5285 | switch (node_p->nodetype) { |
| 5286 | case LYS_CONTAINER: |
| 5287 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5288 | node_compile_spec = lys_compile_node_container; |
| 5289 | break; |
| 5290 | case LYS_LEAF: |
| 5291 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5292 | node_compile_spec = lys_compile_node_leaf; |
| 5293 | break; |
| 5294 | case LYS_LIST: |
| 5295 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5296 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5297 | break; |
| 5298 | case LYS_LEAFLIST: |
| 5299 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5300 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5301 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5302 | case LYS_CHOICE: |
| 5303 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5304 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5305 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5306 | case LYS_ANYXML: |
| 5307 | case LYS_ANYDATA: |
| 5308 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5309 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5310 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5311 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5312 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5313 | lysc_update_path(ctx, NULL, NULL); |
| 5314 | lysc_update_path(ctx, NULL, NULL); |
| 5315 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5316 | default: |
| 5317 | LOGINT(ctx->ctx); |
| 5318 | return LY_EINT; |
| 5319 | } |
| 5320 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5321 | node->nodetype = node_p->nodetype; |
| 5322 | node->module = ctx->mod; |
| 5323 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5324 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5325 | |
| 5326 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5327 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5328 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5329 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5330 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5331 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5332 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5333 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5334 | node->flags |= LYS_CONFIG_R; |
| 5335 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5336 | /* config not explicitely set, inherit it from parent */ |
| 5337 | if (parent) { |
| 5338 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5339 | } else { |
| 5340 | /* default is config true */ |
| 5341 | node->flags |= LYS_CONFIG_W; |
| 5342 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5343 | } else { |
| 5344 | /* config set explicitely */ |
| 5345 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5346 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5347 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5348 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5349 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5350 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5351 | goto error; |
| 5352 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5353 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5354 | /* *list ordering */ |
| 5355 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5356 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5357 | 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] | 5358 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5359 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5360 | node->flags &= ~LYS_ORDBY_MASK; |
| 5361 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5362 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5363 | /* default ordering is system */ |
| 5364 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5365 | } |
| 5366 | } |
| 5367 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5368 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5369 | * 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] | 5370 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5371 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5372 | * 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] | 5373 | 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] | 5374 | } |
| 5375 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5376 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5377 | node->sp = node_p; |
| 5378 | } |
| 5379 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5380 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5381 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5382 | if (node_p->when) { |
| 5383 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5384 | 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] | 5385 | |
| 5386 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5387 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5388 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5389 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5390 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5391 | 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] | 5392 | |
| 5393 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5394 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5395 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5396 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5397 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5398 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5399 | if (node->flags & LYS_MAND_TRUE) { |
| 5400 | lys_compile_mandatory_parents(parent, 1); |
| 5401 | } |
| 5402 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5403 | lysc_update_path(ctx, NULL, NULL); |
| 5404 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5405 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5406 | if (parent) { |
| 5407 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5408 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5409 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5410 | } else { |
| 5411 | lysc_update_path(ctx, parent, node->name); |
| 5412 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5413 | 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] | 5414 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5415 | if (uses_status) { |
| 5416 | |
| 5417 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5418 | /* 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] | 5419 | * it directly gets the same status flags as the choice; |
| 5420 | * 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] | 5421 | 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] | 5422 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5423 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5424 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5425 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5426 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5427 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5428 | 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] | 5429 | |
| 5430 | if (parent->nodetype == LYS_CHOICE) { |
| 5431 | lysc_update_path(ctx, NULL, NULL); |
| 5432 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5433 | } else { |
| 5434 | /* top-level element */ |
| 5435 | if (!ctx->mod->compiled->data) { |
| 5436 | ctx->mod->compiled->data = node; |
| 5437 | } else { |
| 5438 | /* insert at the end of the module's top-level nodes list */ |
| 5439 | ctx->mod->compiled->data->prev->next = node; |
| 5440 | node->prev = ctx->mod->compiled->data->prev; |
| 5441 | ctx->mod->compiled->data->prev = node; |
| 5442 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5443 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5444 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5445 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5446 | return LY_EVALID; |
| 5447 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5448 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5449 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5450 | |
| 5451 | return LY_SUCCESS; |
| 5452 | |
| 5453 | error: |
| 5454 | lysc_node_free(ctx->ctx, node); |
| 5455 | return ret; |
| 5456 | } |
| 5457 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5458 | static void |
| 5459 | lysc_disconnect(struct lysc_node *node) |
| 5460 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5461 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5462 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5463 | int remove_cs = 0; |
| 5464 | |
| 5465 | parent = node->parent; |
| 5466 | |
| 5467 | /* parent's first child */ |
| 5468 | if (!parent) { |
| 5469 | return; |
| 5470 | } |
| 5471 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5472 | cs = (struct lysc_node_case*)node; |
| 5473 | } else if (parent->nodetype == LYS_CASE) { |
| 5474 | /* disconnecting some node in a case */ |
| 5475 | cs = (struct lysc_node_case*)parent; |
| 5476 | parent = cs->parent; |
| 5477 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5478 | if (child == node) { |
| 5479 | if (cs->child == child) { |
| 5480 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5481 | /* case with a single child -> remove also the case */ |
| 5482 | child->parent = NULL; |
| 5483 | remove_cs = 1; |
| 5484 | } else { |
| 5485 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5486 | } |
| 5487 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5488 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5489 | } |
| 5490 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5491 | if (!remove_cs) { |
| 5492 | cs = NULL; |
| 5493 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5494 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5495 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5496 | } |
| 5497 | |
| 5498 | if (cs) { |
| 5499 | if (remove_cs) { |
| 5500 | /* cs has only one child which is being also removed */ |
| 5501 | lysc_disconnect((struct lysc_node*)cs); |
| 5502 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5503 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5504 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5505 | /* default case removed */ |
| 5506 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5507 | } |
| 5508 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5509 | /* first case removed */ |
| 5510 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5511 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5512 | if (cs->child) { |
| 5513 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5514 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5515 | prev = cs->child->prev; |
| 5516 | } /* else all the children are under a single case */ |
| 5517 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5518 | if (child->parent != (struct lysc_node*)cs) { |
| 5519 | break; |
| 5520 | } |
| 5521 | lysc_node_free(node->module->ctx, child); |
| 5522 | } |
| 5523 | if (prev) { |
| 5524 | if (prev->next) { |
| 5525 | prev->next = child; |
| 5526 | } |
| 5527 | if (child) { |
| 5528 | child->prev = prev; |
| 5529 | } else { |
| 5530 | /* link from the first child under the cases */ |
| 5531 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5532 | } |
| 5533 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5534 | } |
| 5535 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5539 | if (node->prev->next) { |
| 5540 | node->prev->next = node->next; |
| 5541 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5542 | if (node->next) { |
| 5543 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5544 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5545 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5546 | if (child) { |
| 5547 | child->prev = node->prev; |
| 5548 | } |
| 5549 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5550 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5551 | } |
| 5552 | } |
| 5553 | |
| 5554 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5555 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5556 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5557 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5558 | struct ly_set devs_p = {0}; |
| 5559 | struct ly_set targets = {0}; |
| 5560 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5561 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5562 | struct lysc_action *rpcs; |
| 5563 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5564 | struct lysp_deviation *dev; |
| 5565 | struct lysp_deviate *d, **dp_new; |
| 5566 | struct lysp_deviate_add *d_add; |
| 5567 | struct lysp_deviate_del *d_del; |
| 5568 | struct lysp_deviate_rpl *d_rpl; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5569 | LY_ARRAY_COUNT_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5570 | struct lysc_deviation { |
| 5571 | const char *nodeid; |
| 5572 | struct lysc_node *target; /* target node of the deviation */ |
| 5573 | 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] | 5574 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5575 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5576 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5577 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5578 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5579 | const char *prefix, *name, *nodeid, *dflt; |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 5580 | struct lys_module *mod, **dev_mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5581 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5582 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5583 | |
| 5584 | /* get all deviations from the module and all its submodules ... */ |
| 5585 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5586 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5587 | } |
| 5588 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5589 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5590 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5591 | } |
| 5592 | } |
| 5593 | if (!devs_p.count) { |
| 5594 | /* nothing to do */ |
| 5595 | return LY_SUCCESS; |
| 5596 | } |
| 5597 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5598 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5599 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5600 | /* ... and group them by the target node */ |
| 5601 | devs = calloc(devs_p.count, sizeof *devs); |
| 5602 | for (u = 0; u < devs_p.count; ++u) { |
| 5603 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5604 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5605 | |
| 5606 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5607 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5608 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5609 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5610 | /* move the target pointer to input/output to make them different from the action and |
| 5611 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5612 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5613 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5614 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5615 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5616 | flags |= LYSC_OPT_INTERNAL; |
| 5617 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5618 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5619 | flags |= LYSC_OPT_INTERNAL; |
| 5620 | } |
| 5621 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5622 | /* insert into the set of targets with duplicity detection */ |
| 5623 | i = ly_set_add(&targets, target, 0); |
| 5624 | if (!devs[i]) { |
| 5625 | /* new record */ |
| 5626 | devs[i] = calloc(1, sizeof **devs); |
| 5627 | devs[i]->target = target; |
| 5628 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5629 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5630 | } |
| 5631 | /* add deviates into the deviation's list of deviates */ |
| 5632 | for (d = dev->deviates; d; d = d->next) { |
| 5633 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5634 | *dp_new = d; |
| 5635 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5636 | devs[i]->not_supported = 1; |
| 5637 | } |
| 5638 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5639 | |
| 5640 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5641 | } |
| 5642 | |
| 5643 | /* MACROS for deviates checking */ |
| 5644 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5645 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5646 | 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] | 5647 | goto cleanup; \ |
| 5648 | } |
| 5649 | |
| 5650 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5651 | if (LY_ARRAY_COUNT(ARRAY) > MAX) { \ |
| 5652 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_COUNT_TYPE") %s properties.", \ |
| 5653 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_COUNT(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5654 | goto cleanup; \ |
| 5655 | } |
| 5656 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5657 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5658 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5659 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5660 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5661 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5662 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5663 | goto cleanup; \ |
| 5664 | } |
| 5665 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5666 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5667 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5668 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5669 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5670 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5671 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5672 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5673 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5674 | goto cleanup; \ |
| 5675 | } |
| 5676 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5677 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5678 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5679 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5680 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5681 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5682 | goto cleanup; \ |
| 5683 | } |
| 5684 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5685 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5686 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5687 | 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] | 5688 | goto cleanup; \ |
| 5689 | } |
| 5690 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5691 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5692 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5693 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5694 | "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] | 5695 | goto cleanup; \ |
| 5696 | } |
| 5697 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5698 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5699 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5700 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5701 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5702 | 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] | 5703 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5704 | if (y == LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5705 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5706 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5707 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5708 | goto cleanup; \ |
| 5709 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5710 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5711 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5712 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5713 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5714 | (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] | 5715 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5716 | if (!LY_ARRAY_COUNT(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5717 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5718 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5719 | } |
| 5720 | |
| 5721 | /* apply deviations */ |
| 5722 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5723 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5724 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5725 | struct ly_err_item *err = NULL; |
| 5726 | |
| 5727 | dflt = NULL; |
| 5728 | changed_type = 0; |
| 5729 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5730 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5731 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5732 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5733 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5734 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5735 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5736 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5737 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5738 | } |
| 5739 | } |
| 5740 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5741 | /* not-supported */ |
| 5742 | if (devs[u]->not_supported) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5743 | if (LY_ARRAY_COUNT(devs[u]->deviates) > 1) { |
| 5744 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_COUNT_TYPE") deviates on node \"%s\" since the node is not-supported.", |
| 5745 | LY_ARRAY_COUNT(devs[u]->deviates), devs[u]->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5746 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5747 | |
| 5748 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5749 | if (devs[u]->target->parent) { \ |
| 5750 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5751 | } else { \ |
| 5752 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5753 | } \ |
| 5754 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5755 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5756 | } \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5757 | if (x < LY_ARRAY_COUNT(ARRAY)) { \ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5758 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5759 | 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] | 5760 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5761 | } |
| 5762 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5763 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5764 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5765 | /* remove RPC's/action's input */ |
| 5766 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5767 | 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] | 5768 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5769 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5770 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5771 | /* remove RPC's/action's output */ |
| 5772 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5773 | 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] | 5774 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5775 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5776 | } else { |
| 5777 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5778 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5779 | } |
| 5780 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5781 | /* remove Notification */ |
| 5782 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5783 | } else { |
| 5784 | /* remove the target node */ |
| 5785 | lysc_disconnect(devs[u]->target); |
| 5786 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5787 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5788 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5789 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5790 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5791 | continue; |
| 5792 | } |
| 5793 | |
| 5794 | /* list of deviates (not-supported is not present in the list) */ |
| 5795 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5796 | d = devs[u]->deviates[v]; |
| 5797 | |
| 5798 | switch (d->mod) { |
| 5799 | case LYS_DEV_ADD: |
| 5800 | d_add = (struct lysp_deviate_add*)d; |
| 5801 | /* [units-stmt] */ |
| 5802 | if (d_add->units) { |
| 5803 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5804 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5805 | |
| 5806 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5807 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5808 | } |
| 5809 | |
| 5810 | /* *must-stmt */ |
| 5811 | if (d_add->musts) { |
| 5812 | switch (devs[u]->target->nodetype) { |
| 5813 | case LYS_CONTAINER: |
| 5814 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5815 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5816 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5817 | break; |
| 5818 | case LYS_LEAF: |
| 5819 | case LYS_LEAFLIST: |
| 5820 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5821 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5822 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5823 | break; |
| 5824 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5825 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5826 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5827 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5828 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5829 | case LYS_ACTION: |
| 5830 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5831 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5832 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5833 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 5834 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5835 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5836 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5837 | break; |
| 5838 | } |
| 5839 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5840 | default: |
| 5841 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5842 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5843 | goto cleanup; |
| 5844 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5845 | ly_set_add(&ctx->xpath, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5849 | if (d_add->uniques) { |
| 5850 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5851 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5852 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5853 | |
| 5854 | /* *default-stmt */ |
| 5855 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5856 | switch (devs[u]->target->nodetype) { |
| 5857 | case LYS_LEAF: |
| 5858 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5859 | 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] | 5860 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5861 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5862 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5863 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5864 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5865 | } else { |
| 5866 | /* prepare new default value storage */ |
| 5867 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5868 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5869 | dflt = d_add->dflts[0]; |
| 5870 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5871 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5872 | /* mark the new default values as leaf's own */ |
| 5873 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5874 | break; |
| 5875 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5876 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5877 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5878 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5879 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5880 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5881 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5882 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5883 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5884 | LY_ARRAY_FREE(llist->dflts); |
| 5885 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5886 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5887 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5888 | } |
| 5889 | /* add new default value(s) */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 5890 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5891 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_COUNT(d_add->dflts), ret, cleanup); |
| 5892 | for (x = y = LY_ARRAY_COUNT(llist->dflts); |
| 5893 | x < LY_ARRAY_COUNT(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5894 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5895 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5896 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5897 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5898 | llist->dflts[x]->realtype = llist->type; |
| 5899 | 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] | 5900 | 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] | 5901 | (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] | 5902 | llist->dflts[x]->realtype->refcount++; |
| 5903 | if (err) { |
| 5904 | ly_err_print(err); |
| 5905 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5906 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5907 | d_add->dflts[x - y], err->msg); |
| 5908 | ly_err_free(err); |
| 5909 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5910 | if (rc == LY_EINCOMPLETE) { |
| 5911 | /* postpone default compilation when the tree is complete */ |
| 5912 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5913 | |
| 5914 | /* but in general result is so far ok */ |
| 5915 | rc = LY_SUCCESS; |
| 5916 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5917 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5918 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5919 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5920 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5921 | break; |
| 5922 | case LYS_CHOICE: |
| 5923 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5924 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5925 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5926 | * 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] | 5927 | 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] | 5928 | goto cleanup; |
| 5929 | } |
| 5930 | break; |
| 5931 | default: |
| 5932 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5933 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5934 | goto cleanup; |
| 5935 | } |
| 5936 | } |
| 5937 | |
| 5938 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5939 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5940 | 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] | 5941 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5942 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5943 | goto cleanup; |
| 5944 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5945 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5946 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5947 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5948 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5949 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5950 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5951 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5952 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5953 | goto cleanup; |
| 5954 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5955 | 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] | 5956 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5957 | |
| 5958 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5959 | if (d_add->flags & LYS_MAND_MASK) { |
| 5960 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5961 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5962 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5963 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5964 | goto cleanup; |
| 5965 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5966 | 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] | 5967 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5968 | |
| 5969 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5970 | if (d_add->flags & LYS_SET_MIN) { |
| 5971 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5972 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5973 | /* change value */ |
| 5974 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5975 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5976 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5977 | /* change value */ |
| 5978 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5979 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5980 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5981 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5982 | goto cleanup; |
| 5983 | } |
| 5984 | if (d_add->min) { |
| 5985 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5986 | } |
| 5987 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5988 | |
| 5989 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5990 | if (d_add->flags & LYS_SET_MAX) { |
| 5991 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5992 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5993 | /* change value */ |
| 5994 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5995 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5996 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5997 | /* change value */ |
| 5998 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5999 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6000 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6001 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6002 | goto cleanup; |
| 6003 | } |
| 6004 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6005 | |
| 6006 | break; |
| 6007 | case LYS_DEV_DELETE: |
| 6008 | d_del = (struct lysp_deviate_del*)d; |
| 6009 | |
| 6010 | /* [units-stmt] */ |
| 6011 | if (d_del->units) { |
| 6012 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6013 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6014 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6015 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6016 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6017 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6018 | goto cleanup; |
| 6019 | } |
| 6020 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6021 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6022 | } |
| 6023 | |
| 6024 | /* *must-stmt */ |
| 6025 | if (d_del->musts) { |
| 6026 | switch (devs[u]->target->nodetype) { |
| 6027 | case LYS_CONTAINER: |
| 6028 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6029 | 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] | 6030 | break; |
| 6031 | case LYS_LEAF: |
| 6032 | case LYS_LEAFLIST: |
| 6033 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6034 | 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] | 6035 | break; |
| 6036 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6037 | 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] | 6038 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6039 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6040 | case LYS_ACTION: |
| 6041 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6042 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6043 | break; |
| 6044 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6045 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6046 | break; |
| 6047 | } |
| 6048 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6049 | default: |
| 6050 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6051 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6052 | goto cleanup; |
| 6053 | } |
| 6054 | } |
| 6055 | |
| 6056 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6057 | if (d_del->uniques) { |
| 6058 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6059 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6060 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6061 | LY_ARRAY_FOR(list->uniques, z) { |
| 6062 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6063 | nodeid = strpbrk(name, " \t\n"); |
| 6064 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 6065 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6066 | break; |
| 6067 | } |
| 6068 | while (isspace(*nodeid)) { |
| 6069 | ++nodeid; |
| 6070 | } |
| 6071 | } else { |
| 6072 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6073 | break; |
| 6074 | } |
| 6075 | } |
| 6076 | } |
| 6077 | if (!name) { |
| 6078 | /* complete match - remove the unique */ |
| 6079 | LY_ARRAY_DECREMENT(list->uniques); |
| 6080 | LY_ARRAY_FREE(list->uniques[z]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6081 | 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] | 6082 | --z; |
| 6083 | break; |
| 6084 | } |
| 6085 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6086 | if (!list->uniques || z == LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6087 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6088 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6089 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6090 | goto cleanup; |
| 6091 | } |
| 6092 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6093 | if (!LY_ARRAY_COUNT(list->uniques)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6094 | LY_ARRAY_FREE(list->uniques); |
| 6095 | list->uniques = NULL; |
| 6096 | } |
| 6097 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6098 | |
| 6099 | /* *default-stmt */ |
| 6100 | if (d_del->dflts) { |
| 6101 | switch (devs[u]->target->nodetype) { |
| 6102 | case LYS_LEAF: |
| 6103 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6104 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6105 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6106 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6107 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6108 | 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] | 6109 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6110 | if (i) { |
| 6111 | free((char*)dflt); |
| 6112 | } |
| 6113 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6114 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6115 | d_del->dflts[0], dflt); |
| 6116 | goto cleanup; |
| 6117 | } |
| 6118 | if (i) { |
| 6119 | free((char*)dflt); |
| 6120 | } |
| 6121 | dflt = NULL; |
| 6122 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6123 | /* update the list of incomplete default values if needed */ |
| 6124 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6125 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6126 | /* remove the default specification */ |
| 6127 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6128 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6129 | free(leaf->dflt); |
| 6130 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6131 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6132 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6133 | break; |
| 6134 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6135 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6136 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6137 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6138 | 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] | 6139 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6140 | if (i) { |
| 6141 | free((char*)dflt); |
| 6142 | } |
| 6143 | dflt = NULL; |
| 6144 | break; |
| 6145 | } |
| 6146 | if (i) { |
| 6147 | free((char*)dflt); |
| 6148 | } |
| 6149 | dflt = NULL; |
| 6150 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6151 | if (y == LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6152 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6153 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6154 | goto cleanup; |
| 6155 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6156 | |
| 6157 | /* update the list of incomplete default values if needed */ |
| 6158 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6159 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6160 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6161 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6162 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6163 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6164 | free(llist->dflts[y]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6165 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_COUNT(llist->dflts) - y) * (sizeof *llist->dflts)); |
| 6166 | 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] | 6167 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6168 | if (!LY_ARRAY_COUNT(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6169 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6170 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6171 | LY_ARRAY_FREE(llist->dflts); |
| 6172 | llist->dflts = NULL; |
| 6173 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6174 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6175 | break; |
| 6176 | case LYS_CHOICE: |
| 6177 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6178 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6179 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6180 | 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] | 6181 | if (prefix) { |
| 6182 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6183 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6184 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6185 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6186 | "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] | 6187 | goto cleanup; |
| 6188 | } |
| 6189 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6190 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6191 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6192 | "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] | 6193 | goto cleanup; |
| 6194 | } |
| 6195 | } |
| 6196 | /* else { |
| 6197 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6198 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6199 | * the module of the default case, just matching its name */ |
| 6200 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6201 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6202 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6203 | 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] | 6204 | goto cleanup; |
| 6205 | } |
| 6206 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6207 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6208 | break; |
| 6209 | default: |
| 6210 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6211 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6212 | goto cleanup; |
| 6213 | } |
| 6214 | } |
| 6215 | |
| 6216 | break; |
| 6217 | case LYS_DEV_REPLACE: |
| 6218 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6219 | |
| 6220 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6221 | if (d_rpl->type) { |
| 6222 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6223 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6224 | 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] | 6225 | |
| 6226 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6227 | /* the target has default from the previous type - remove it */ |
| 6228 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6229 | /* update the list of incomplete default values if needed */ |
| 6230 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6231 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6232 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6233 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6234 | free(leaf->dflt); |
| 6235 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6236 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6237 | } else { /* LYS_LEAFLIST */ |
| 6238 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6239 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6240 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6241 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6242 | free(llist->dflts[x]); |
| 6243 | } |
| 6244 | LY_ARRAY_FREE(llist->dflts); |
| 6245 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6246 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6247 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6248 | } |
| 6249 | } |
| 6250 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6251 | /* there is no default value, do not set changed_type after type compilation |
| 6252 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6253 | changed_type = -1; |
| 6254 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6255 | 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] | 6256 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6257 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6258 | |
| 6259 | /* [units-stmt] */ |
| 6260 | if (d_rpl->units) { |
| 6261 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6262 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6263 | units, "replacing", "units", d_rpl->units); |
| 6264 | |
| 6265 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6266 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6267 | } |
| 6268 | |
| 6269 | /* [default-stmt] */ |
| 6270 | if (d_rpl->dflt) { |
| 6271 | switch (devs[u]->target->nodetype) { |
| 6272 | case LYS_LEAF: |
| 6273 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6274 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6275 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6276 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6277 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6278 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6279 | dflt = d_rpl->dflt; |
| 6280 | /* 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] | 6281 | break; |
| 6282 | case LYS_CHOICE: |
| 6283 | 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] | 6284 | 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] | 6285 | goto cleanup; |
| 6286 | } |
| 6287 | break; |
| 6288 | default: |
| 6289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6290 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6291 | goto cleanup; |
| 6292 | } |
| 6293 | } |
| 6294 | |
| 6295 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6296 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6297 | 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] | 6298 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6299 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6300 | goto cleanup; |
| 6301 | } |
| 6302 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6303 | 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] | 6304 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6305 | goto cleanup; |
| 6306 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6307 | 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] | 6308 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6309 | |
| 6310 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6311 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6312 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6313 | 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] | 6314 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6315 | goto cleanup; |
| 6316 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6317 | 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] | 6318 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6319 | |
| 6320 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6321 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6322 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6323 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6324 | /* change value */ |
| 6325 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6326 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6327 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6328 | /* change value */ |
| 6329 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6330 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6331 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6332 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6333 | goto cleanup; |
| 6334 | } |
| 6335 | if (d_rpl->min) { |
| 6336 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6337 | } |
| 6338 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6339 | |
| 6340 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6341 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6342 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6343 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6344 | /* change value */ |
| 6345 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6346 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6347 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6348 | /* change value */ |
| 6349 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6350 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6351 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6352 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6353 | goto cleanup; |
| 6354 | } |
| 6355 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6356 | |
| 6357 | break; |
| 6358 | default: |
| 6359 | LOGINT(ctx->ctx); |
| 6360 | goto cleanup; |
| 6361 | } |
| 6362 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6363 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6364 | /* final check when all deviations of a single target node are applied */ |
| 6365 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6366 | /* check min-max compatibility */ |
| 6367 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6368 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6369 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6370 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6371 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6372 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6373 | } else { |
| 6374 | min = max = 0; |
| 6375 | } |
| 6376 | if (min > max) { |
| 6377 | 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] | 6378 | "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] | 6379 | goto cleanup; |
| 6380 | } |
| 6381 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6382 | if (dflt) { |
| 6383 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6384 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6385 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6386 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6387 | 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] | 6388 | 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] | 6389 | leaf->dflt->realtype->refcount++; |
| 6390 | if (err) { |
| 6391 | ly_err_print(err); |
| 6392 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6393 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6394 | ly_err_free(err); |
| 6395 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6396 | if (rc == LY_EINCOMPLETE) { |
| 6397 | /* postpone default compilation when the tree is complete */ |
| 6398 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6399 | |
| 6400 | /* but in general result is so far ok */ |
| 6401 | rc = LY_SUCCESS; |
| 6402 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6403 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6404 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6405 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6406 | int dynamic; |
| 6407 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6408 | 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] | 6409 | |
| 6410 | /* update the list of incomplete default values if needed */ |
| 6411 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6412 | |
| 6413 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6414 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6415 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6416 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6417 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6418 | 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] | 6419 | 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] | 6420 | leaf->dflt->realtype->refcount++; |
| 6421 | if (err) { |
| 6422 | ly_err_print(err); |
| 6423 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6424 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6425 | ly_err_free(err); |
| 6426 | } |
| 6427 | if (dynamic) { |
| 6428 | free((void*)dflt); |
| 6429 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6430 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6431 | if (rc == LY_EINCOMPLETE) { |
| 6432 | /* postpone default compilation when the tree is complete */ |
| 6433 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6434 | |
| 6435 | /* but in general result is so far ok */ |
| 6436 | rc = LY_SUCCESS; |
| 6437 | } |
| 6438 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6439 | } else { /* LYS_LEAFLIST */ |
| 6440 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6441 | 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] | 6442 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6443 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6444 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6445 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6446 | 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] | 6447 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6448 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6449 | llist->dflts[x]->realtype->refcount++; |
| 6450 | if (err) { |
| 6451 | ly_err_print(err); |
| 6452 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6453 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6454 | dflt, err->msg); |
| 6455 | ly_err_free(err); |
| 6456 | } |
| 6457 | if (dynamic) { |
| 6458 | free((void*)dflt); |
| 6459 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6460 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6461 | if (rc == LY_EINCOMPLETE) { |
| 6462 | /* postpone default compilation when the tree is complete */ |
| 6463 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6464 | |
| 6465 | /* but in general result is so far ok */ |
| 6466 | rc = LY_SUCCESS; |
| 6467 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6468 | LY_CHECK_GOTO(rc, cleanup); |
| 6469 | } |
| 6470 | } |
| 6471 | } |
| 6472 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6473 | /* check mandatory - default compatibility */ |
| 6474 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6475 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6476 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6477 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6478 | "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] | 6479 | goto cleanup; |
| 6480 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6481 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6482 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6483 | 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] | 6484 | goto cleanup; |
| 6485 | } |
| 6486 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6487 | /* mandatory node under a default case */ |
| 6488 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6489 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6490 | 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] | 6491 | goto cleanup; |
| 6492 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6493 | |
Michal Vasko | e614320 | 2020-07-03 13:02:08 +0200 | [diff] [blame] | 6494 | /* add this module into the target module deviated_by, if not there already */ |
| 6495 | rc = LY_SUCCESS; |
| 6496 | LY_ARRAY_FOR(devs[u]->target->module->compiled->deviated_by, v) { |
| 6497 | if (devs[u]->target->module->compiled->deviated_by[v] == mod_p->mod) { |
| 6498 | rc = LY_EEXIST; |
| 6499 | break; |
| 6500 | } |
| 6501 | } |
| 6502 | if (!rc) { |
| 6503 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup); |
| 6504 | *dev_mod = mod_p->mod; |
| 6505 | } |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 6506 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6507 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6508 | } |
| 6509 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6510 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6511 | ret = LY_SUCCESS; |
| 6512 | |
| 6513 | cleanup: |
| 6514 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6515 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6516 | free(devs[u]); |
| 6517 | } |
| 6518 | free(devs); |
| 6519 | ly_set_erase(&targets, NULL); |
| 6520 | ly_set_erase(&devs_p, NULL); |
| 6521 | |
| 6522 | return ret; |
| 6523 | } |
| 6524 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6525 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6526 | * @brief Compile the given YANG submodule into the main module. |
| 6527 | * @param[in] ctx Compile context |
| 6528 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6529 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6530 | */ |
| 6531 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6532 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6533 | { |
| 6534 | unsigned int u; |
| 6535 | LY_ERR ret = LY_SUCCESS; |
| 6536 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6537 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6538 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6539 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6540 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6541 | if (!mainmod->mod->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6542 | /* features are compiled directly into the compiled module structure, |
| 6543 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6544 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6545 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6546 | LY_CHECK_GOTO(ret, error); |
| 6547 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6548 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 6549 | if (!mainmod->mod->dis_identities) { |
| 6550 | ret = lys_identity_precompile(ctx, NULL, NULL, submod->identities, &mainmod->identities); |
| 6551 | LY_CHECK_GOTO(ret, error); |
| 6552 | } |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6553 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6554 | /* data nodes */ |
| 6555 | LY_LIST_FOR(submod->data, node_p) { |
| 6556 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6557 | LY_CHECK_GOTO(ret, error); |
| 6558 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6559 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6560 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6561 | 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] | 6562 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6563 | error: |
| 6564 | return ret; |
| 6565 | } |
| 6566 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6567 | static void * |
| 6568 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6569 | { |
| 6570 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6571 | if (substmts[u].stmt == stmt) { |
| 6572 | return substmts[u].storage; |
| 6573 | } |
| 6574 | } |
| 6575 | return NULL; |
| 6576 | } |
| 6577 | |
| 6578 | LY_ERR |
| 6579 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6580 | { |
| 6581 | LY_ERR ret = LY_EVALID, r; |
| 6582 | unsigned int u; |
| 6583 | struct lysp_stmt *stmt; |
| 6584 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6585 | |
| 6586 | /* check for invalid substatements */ |
| 6587 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6588 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6589 | continue; |
| 6590 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6591 | for (u = 0; substmts[u].stmt; ++u) { |
| 6592 | if (substmts[u].stmt == stmt->kw) { |
| 6593 | break; |
| 6594 | } |
| 6595 | } |
| 6596 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6597 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6598 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6599 | goto cleanup; |
| 6600 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6601 | } |
| 6602 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6603 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6604 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6605 | /* keep order of the processing the same as the order in the defined substmts, |
| 6606 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6607 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6608 | int stmt_present = 0; |
| 6609 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6610 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6611 | if (substmts[u].stmt != stmt->kw) { |
| 6612 | continue; |
| 6613 | } |
| 6614 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6615 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6616 | if (substmts[u].storage) { |
| 6617 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6618 | case LY_STMT_STATUS: |
| 6619 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6620 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6621 | break; |
| 6622 | case LY_STMT_UNITS: { |
| 6623 | const char **units; |
| 6624 | |
| 6625 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6626 | /* single item */ |
| 6627 | if (*((const char **)substmts[u].storage)) { |
| 6628 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6629 | goto cleanup; |
| 6630 | } |
| 6631 | units = (const char **)substmts[u].storage; |
| 6632 | } else { |
| 6633 | /* sized array */ |
| 6634 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6635 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6636 | } |
| 6637 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6638 | break; |
| 6639 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6640 | case LY_STMT_TYPE: { |
| 6641 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6642 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6643 | |
| 6644 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6645 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6646 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6647 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6648 | goto cleanup; |
| 6649 | } |
| 6650 | compiled = substmts[u].storage; |
| 6651 | } else { |
| 6652 | /* sized array */ |
| 6653 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6654 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6655 | compiled = (void*)type; |
| 6656 | } |
| 6657 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6658 | 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] | 6659 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6660 | 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] | 6661 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6662 | lysp_type_free(ctx->ctx, parsed); |
| 6663 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6664 | break; |
| 6665 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6666 | case LY_STMT_IF_FEATURE: { |
| 6667 | struct lysc_iffeature *iff = NULL; |
| 6668 | |
| 6669 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6670 | /* single item */ |
| 6671 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6673 | goto cleanup; |
| 6674 | } |
| 6675 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6676 | } else { |
| 6677 | /* sized array */ |
| 6678 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6679 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6680 | } |
| 6681 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6682 | break; |
| 6683 | } |
| 6684 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6685 | * 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] | 6686 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6687 | 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.", |
| 6688 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6689 | goto cleanup; |
| 6690 | } |
| 6691 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6692 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6693 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6694 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6695 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6696 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6697 | goto cleanup; |
| 6698 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | ret = LY_SUCCESS; |
| 6702 | |
| 6703 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6704 | return ret; |
| 6705 | } |
| 6706 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6707 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6708 | * @brief Check when for cyclic dependencies. |
| 6709 | * @param[in] set Set with all the referenced nodes. |
| 6710 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6711 | * @return LY_ERR value |
| 6712 | */ |
| 6713 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6714 | 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] | 6715 | { |
| 6716 | struct lyxp_set tmp_set; |
| 6717 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6718 | uint32_t i, j; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6719 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6720 | int idx; |
| 6721 | struct lysc_when *when; |
| 6722 | LY_ERR ret = LY_SUCCESS; |
| 6723 | |
| 6724 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6725 | |
| 6726 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6727 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6728 | xp_scnode = &set->val.scnodes[i]; |
| 6729 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6730 | if (xp_scnode->in_ctx != -1) { |
| 6731 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6732 | xp_scnode->in_ctx = 1; |
| 6733 | } |
| 6734 | } |
| 6735 | |
| 6736 | for (i = 0; i < set->used; ++i) { |
| 6737 | xp_scnode = &set->val.scnodes[i]; |
| 6738 | if (xp_scnode->in_ctx != 1) { |
| 6739 | /* already checked */ |
| 6740 | continue; |
| 6741 | } |
| 6742 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6743 | 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] | 6744 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6745 | /* no when to check */ |
| 6746 | xp_scnode->in_ctx = 0; |
| 6747 | continue; |
| 6748 | } |
| 6749 | |
| 6750 | node = xp_scnode->scnode; |
| 6751 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6752 | LY_ARRAY_FOR(node->when, u) { |
| 6753 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 6754 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6755 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6756 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6757 | 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] | 6758 | goto cleanup; |
| 6759 | } |
| 6760 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6761 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6762 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6763 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6764 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6765 | 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] | 6766 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6767 | 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] | 6768 | ret = LY_EVALID; |
| 6769 | goto cleanup; |
| 6770 | } |
| 6771 | |
| 6772 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6773 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6774 | } else { |
| 6775 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6776 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6777 | } |
| 6778 | } |
| 6779 | |
| 6780 | /* merge this set into the global when set */ |
| 6781 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6782 | } |
| 6783 | |
| 6784 | /* check when of non-data parents as well */ |
| 6785 | node = node->parent; |
| 6786 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6787 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6788 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6789 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6790 | } |
| 6791 | |
| 6792 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6793 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6794 | return ret; |
| 6795 | } |
| 6796 | |
| 6797 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6798 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6799 | * @param[in] ctx Compile context. |
| 6800 | * @param[in] node Node to check. |
| 6801 | * @return LY_ERR value |
| 6802 | */ |
| 6803 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6804 | 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] | 6805 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6806 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6807 | uint32_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6808 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6809 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6810 | struct lysc_when **when = NULL; |
| 6811 | struct lysc_must *musts = NULL; |
| 6812 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame^] | 6813 | const struct lysc_node *op; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6814 | |
| 6815 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6816 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame^] | 6817 | if (node->flags & LYS_CONFIG_R) { |
| 6818 | for (op = node->parent; op && !(op->nodetype & (LYS_RPC | LYS_ACTION)); op = op->parent); |
| 6819 | if (op) { |
| 6820 | /* we are actually in output */ |
| 6821 | opts = LYXP_SCNODE_OUTPUT; |
| 6822 | } |
| 6823 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6824 | |
| 6825 | switch (node->nodetype) { |
| 6826 | case LYS_CONTAINER: |
| 6827 | when = ((struct lysc_node_container *)node)->when; |
| 6828 | musts = ((struct lysc_node_container *)node)->musts; |
| 6829 | break; |
| 6830 | case LYS_CHOICE: |
| 6831 | when = ((struct lysc_node_choice *)node)->when; |
| 6832 | break; |
| 6833 | case LYS_LEAF: |
| 6834 | when = ((struct lysc_node_leaf *)node)->when; |
| 6835 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6836 | break; |
| 6837 | case LYS_LEAFLIST: |
| 6838 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6839 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6840 | break; |
| 6841 | case LYS_LIST: |
| 6842 | when = ((struct lysc_node_list *)node)->when; |
| 6843 | musts = ((struct lysc_node_list *)node)->musts; |
| 6844 | break; |
| 6845 | case LYS_ANYXML: |
| 6846 | case LYS_ANYDATA: |
| 6847 | when = ((struct lysc_node_anydata *)node)->when; |
| 6848 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6849 | break; |
| 6850 | case LYS_CASE: |
| 6851 | when = ((struct lysc_node_case *)node)->when; |
| 6852 | break; |
| 6853 | case LYS_NOTIF: |
| 6854 | musts = ((struct lysc_notif *)node)->musts; |
| 6855 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6856 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6857 | case LYS_ACTION: |
| 6858 | /* first process input musts */ |
| 6859 | musts = ((struct lysc_action *)node)->input.musts; |
| 6860 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6861 | default: |
| 6862 | /* nothing to check */ |
| 6863 | break; |
| 6864 | } |
| 6865 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6866 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6867 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6868 | 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] | 6869 | 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] | 6870 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6871 | 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] | 6872 | goto cleanup; |
| 6873 | } |
| 6874 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6875 | ctx->path[0] = '\0'; |
| 6876 | 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] | 6877 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6878 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6879 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6880 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6881 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6882 | /* 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] | 6883 | 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] | 6884 | schema->name); |
| 6885 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6886 | |
| 6887 | /* check dummy node accessing */ |
| 6888 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6889 | 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] | 6890 | ret = LY_EVALID; |
| 6891 | goto cleanup; |
| 6892 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6893 | } |
| 6894 | } |
| 6895 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6896 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6897 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6898 | LY_CHECK_GOTO(ret, cleanup); |
| 6899 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6900 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6901 | } |
| 6902 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6903 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6904 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6905 | LY_ARRAY_FOR(musts, u) { |
| 6906 | 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] | 6907 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6908 | 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] | 6909 | goto cleanup; |
| 6910 | } |
| 6911 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6912 | ctx->path[0] = '\0'; |
| 6913 | 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] | 6914 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6915 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6916 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6917 | /* 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] | 6918 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6919 | 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] | 6920 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6921 | } |
| 6922 | } |
| 6923 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6924 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6925 | } |
| 6926 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6927 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6928 | /* now check output musts */ |
| 6929 | input_done = 1; |
| 6930 | musts = ((struct lysc_action *)node)->output.musts; |
| 6931 | opts = LYXP_SCNODE_OUTPUT; |
| 6932 | goto check_musts; |
| 6933 | } |
| 6934 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6935 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6936 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6937 | return ret; |
| 6938 | } |
| 6939 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6940 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6941 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6942 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame^] | 6943 | const struct lysc_node *target = NULL, *siter; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6944 | struct ly_path *p; |
| 6945 | struct lysc_type *type; |
| 6946 | |
| 6947 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6948 | |
| 6949 | /* try to find the target */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6950 | LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, |
| 6951 | lysc_is_output(node) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, |
| 6952 | lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6953 | |
| 6954 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 6955 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6956 | ly_path_free(node->module->ctx, p); |
| 6957 | |
| 6958 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6959 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6960 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6961 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6962 | return LY_EVALID; |
| 6963 | } |
| 6964 | |
| 6965 | /* check status */ |
| 6966 | ctx->path[0] = '\0'; |
| 6967 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6968 | ctx->path_len = strlen(ctx->path); |
| 6969 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6970 | return LY_EVALID; |
| 6971 | } |
| 6972 | ctx->path_len = 1; |
| 6973 | ctx->path[1] = '\0'; |
| 6974 | |
| 6975 | /* check config */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame^] | 6976 | if (lref->require_instance) { |
| 6977 | for (siter = node->parent; siter && !(siter->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); siter = siter->parent); |
| 6978 | if (!siter && (node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6979 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 6980 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 6981 | return LY_EVALID; |
| 6982 | } |
| 6983 | } |
| 6984 | |
| 6985 | /* store the target's type and check for circular chain of leafrefs */ |
| 6986 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 6987 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 6988 | if (type == (struct lysc_type *)lref) { |
| 6989 | /* circular chain detected */ |
| 6990 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6991 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 6992 | return LY_EVALID; |
| 6993 | } |
| 6994 | } |
| 6995 | |
| 6996 | /* check if leafref and its target are under common if-features */ |
| 6997 | if (lys_compile_leafref_features_validate(node, target)) { |
| 6998 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6999 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 7000 | " features applicable to the leafref itself.", lref->path->expr); |
| 7001 | return LY_EVALID; |
| 7002 | } |
| 7003 | |
| 7004 | return LY_SUCCESS; |
| 7005 | } |
| 7006 | |
| 7007 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7008 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 7009 | { |
| 7010 | struct lysc_ext_instance *ext; |
| 7011 | struct lysp_ext_instance *ext_p = NULL; |
| 7012 | struct lysp_stmt *stmt; |
| 7013 | const struct lys_module *ext_mod; |
| 7014 | LY_ERR ret = LY_SUCCESS; |
| 7015 | |
| 7016 | /* create the parsed extension instance manually */ |
| 7017 | ext_p = calloc(1, sizeof *ext_p); |
| 7018 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 7019 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 7020 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 7021 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 7022 | ext_p->insubstmt_index = 0; |
| 7023 | |
| 7024 | stmt = calloc(1, sizeof *ext_p->child); |
| 7025 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 7026 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 7027 | stmt->kw = LY_STMT_TYPE; |
| 7028 | ext_p->child = stmt; |
| 7029 | |
| 7030 | /* allocate new extension instance */ |
| 7031 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 7032 | |
| 7033 | /* manually get extension definition module */ |
| 7034 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 7035 | |
| 7036 | /* compile the extension instance */ |
| 7037 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 7038 | |
| 7039 | cleanup: |
| 7040 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 7041 | free(ext_p); |
| 7042 | return ret; |
| 7043 | } |
| 7044 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7045 | static LY_ERR |
| 7046 | lys_compile_unres(struct lysc_ctx *ctx) |
| 7047 | { |
| 7048 | struct lysc_node *node; |
| 7049 | struct lysc_type *type, *typeiter; |
| 7050 | struct lysc_type_leafref *lref; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7051 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7052 | uint32_t i; |
| 7053 | |
| 7054 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 7055 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 7056 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 7057 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7058 | node = ctx->leafrefs.objs[i]; |
| 7059 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 7060 | type = ((struct lysc_node_leaf *)node)->type; |
| 7061 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7062 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 7063 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7064 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 7065 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7066 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 7067 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 7068 | } |
| 7069 | } |
| 7070 | } |
| 7071 | } |
| 7072 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 7073 | /* store pointer to the real type */ |
| 7074 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 7075 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 7076 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 7077 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7078 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7079 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 7080 | } else if (type->basetype == LY_TYPE_UNION) { |
| 7081 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 7082 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 7083 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 7084 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 7085 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 7086 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 7087 | } |
| 7088 | } |
| 7089 | } |
| 7090 | } |
| 7091 | |
| 7092 | /* check xpath */ |
| 7093 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7094 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7095 | } |
| 7096 | |
| 7097 | /* finish incomplete default values compilation */ |
| 7098 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 7099 | struct ly_err_item *err = NULL; |
| 7100 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
| 7101 | LY_ERR ret; |
| 7102 | ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
| 7103 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7104 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7105 | if (err) { |
| 7106 | ly_err_print(err); |
| 7107 | ctx->path[0] = '\0'; |
| 7108 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7109 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7110 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7111 | ly_err_free(err); |
| 7112 | } |
| 7113 | LY_CHECK_RET(ret); |
| 7114 | } |
| 7115 | |
| 7116 | return LY_SUCCESS; |
| 7117 | } |
| 7118 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7119 | LY_ERR |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7120 | lys_compile(struct lys_module **mod, int options) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7121 | { |
| 7122 | struct lysc_ctx ctx = {0}; |
| 7123 | struct lysc_module *mod_c; |
| 7124 | struct lysp_module *sp; |
| 7125 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7126 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7127 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7128 | struct lys_module *m; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 7129 | LY_ARRAY_COUNT_TYPE u, v; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7130 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7131 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7132 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7133 | 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] | 7134 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7135 | if (!(*mod)->implemented) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7136 | /* just imported modules are not compiled */ |
| 7137 | return LY_SUCCESS; |
| 7138 | } |
| 7139 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7140 | sp = (*mod)->parsed; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7141 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7142 | ctx.ctx = (*mod)->ctx; |
| 7143 | ctx.mod = *mod; |
| 7144 | ctx.mod_def = *mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7145 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7146 | ctx.path_len = 1; |
| 7147 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7148 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7149 | (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 7150 | LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM); |
| 7151 | mod_c->mod = *mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7152 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7153 | COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7154 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7155 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7156 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7157 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7158 | |
| 7159 | /* features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7160 | if ((*mod)->dis_features) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7161 | /* there is already precompiled array of features */ |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7162 | mod_c->features = (*mod)->dis_features; |
| 7163 | (*mod)->dis_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7164 | } else { |
| 7165 | /* features are compiled directly into the compiled module structure, |
| 7166 | * 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] | 7167 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7168 | LY_CHECK_GOTO(ret, error); |
| 7169 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7170 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7171 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7172 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7173 | * are present. */ |
| 7174 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7175 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7176 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7177 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7178 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7179 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7180 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7181 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7182 | 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] | 7183 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7184 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7185 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7186 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7187 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7188 | |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7189 | /* identities, work similarly to features with the precompilation */ |
| 7190 | if ((*mod)->dis_identities) { |
| 7191 | mod_c->identities = (*mod)->dis_identities; |
| 7192 | (*mod)->dis_identities = NULL; |
| 7193 | } else { |
| 7194 | ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities); |
| 7195 | LY_CHECK_GOTO(ret, error); |
| 7196 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7197 | if (sp->identities) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7198 | 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] | 7199 | } |
Michal Vasko | 33ff942 | 2020-07-03 09:50:39 +0200 | [diff] [blame] | 7200 | lysc_update_path(&ctx, NULL, "{submodule}"); |
| 7201 | LY_ARRAY_FOR(sp->includes, v) { |
| 7202 | if (sp->includes[v].submodule->identities) { |
| 7203 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
| 7204 | ret = lys_compile_identities_derived(&ctx, sp->includes[v].submodule->identities, mod_c->identities); |
| 7205 | LY_CHECK_GOTO(ret, error); |
| 7206 | lysc_update_path(&ctx, NULL, NULL); |
| 7207 | } |
| 7208 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7209 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7210 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7211 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7212 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7213 | 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] | 7214 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7215 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7216 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7217 | 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] | 7218 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7219 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7220 | 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] | 7221 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7222 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7223 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7224 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7225 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7226 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7227 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7228 | /* extension instances TODO cover extension instances from submodules */ |
| 7229 | 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] | 7230 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7231 | /* finish compilation for all unresolved items in the context */ |
| 7232 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7233 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7234 | /* validate non-instantiated groupings from the parsed schema, |
| 7235 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7236 | ctx.options |= LYSC_OPT_GROUPING; |
| 7237 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7238 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7239 | 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] | 7240 | } |
| 7241 | } |
| 7242 | LY_LIST_FOR(sp->data, node_p) { |
| 7243 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7244 | LY_ARRAY_FOR(grps, u) { |
| 7245 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7246 | 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] | 7247 | } |
| 7248 | } |
| 7249 | } |
| 7250 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7251 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7252 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7253 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7254 | } |
| 7255 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7256 | #if 0 |
| 7257 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7258 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7259 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7260 | * the anotation definitions available in the internal schema structure. */ |
| 7261 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7262 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7263 | lys_free(mod, NULL, 1, 1); |
| 7264 | return NULL; |
| 7265 | } |
| 7266 | } |
| 7267 | #endif |
| 7268 | |
| 7269 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7270 | if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) { |
| 7271 | 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] | 7272 | } |
| 7273 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7274 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7275 | ly_set_erase(&ctx.xpath, NULL); |
| 7276 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7277 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7278 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7279 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7280 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7281 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7282 | lysp_module_free((*mod)->parsed); |
| 7283 | (*mod)->parsed = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7284 | } |
| 7285 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7286 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7287 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7288 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7289 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7290 | if (m->implemented == 2) { |
| 7291 | m->implemented = 1; |
| 7292 | } |
| 7293 | } |
| 7294 | } |
| 7295 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7296 | (*mod)->compiled = mod_c; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7297 | return LY_SUCCESS; |
| 7298 | |
| 7299 | error: |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7300 | lys_feature_precompile_revert(&ctx, *mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7301 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7302 | ly_set_erase(&ctx.xpath, NULL); |
| 7303 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7304 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7305 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7306 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7307 | lysc_module_free(mod_c, NULL); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7308 | (*mod)->compiled = NULL; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7309 | |
| 7310 | /* revert compilation of 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]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7313 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7314 | /* revert features list to the precompiled state */ |
| 7315 | lys_feature_precompile_revert(&ctx, m); |
| 7316 | /* mark module as imported-only / not-implemented */ |
| 7317 | m->implemented = 0; |
| 7318 | /* free the compiled version of the module */ |
| 7319 | lysc_module_free(m->compiled, NULL); |
| 7320 | m->compiled = NULL; |
| 7321 | } |
| 7322 | } |
| 7323 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7324 | /* remove the module itself from the context and free it */ |
| 7325 | ly_set_rm(&ctx.ctx->list, *mod, NULL); |
| 7326 | lys_module_free(*mod, NULL); |
| 7327 | *mod = NULL; |
| 7328 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7329 | return ret; |
| 7330 | } |