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" |
| 26 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include "dict.h" |
| 28 | #include "log.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 29 | #include "path.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 30 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 32 | #include "plugins_exts_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "set.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 35 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 37 | #include "tree_schema_internal.h" |
| 38 | #include "xpath.h" |
| 39 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 40 | static LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, |
| 41 | void *parent, LYEXT_PARENT parent_type, const struct lys_module *ext_mod); |
| 42 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 43 | /** |
| 44 | * @brief Duplicate string into dictionary |
| 45 | * @param[in] CTX libyang context of the dictionary. |
| 46 | * @param[in] ORIG String to duplicate. |
| 47 | * @param[out] DUP Where to store the result. |
| 48 | */ |
| 49 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 50 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 51 | #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] | 52 | if (ARRAY_P) { \ |
| 53 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 54 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 55 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 56 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 57 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 58 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 59 | } \ |
| 60 | } |
| 61 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 62 | #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] | 63 | if (ARRAY_P) { \ |
| 64 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 65 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 66 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 67 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 68 | 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] | 69 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 70 | } \ |
| 71 | } |
| 72 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 73 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 74 | if (EXTS_P) { \ |
| 75 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 76 | for (LY_ARRAY_SIZE_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 77 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 78 | 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] | 79 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 80 | } \ |
| 81 | } |
| 82 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 83 | #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] | 84 | if (ARRAY_P) { \ |
| 85 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 86 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 87 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 88 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 89 | 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] | 90 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 91 | } \ |
| 92 | } |
| 93 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 94 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 95 | if (MEMBER_P) { \ |
| 96 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 97 | 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] | 98 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 99 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 100 | } |
| 101 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 102 | #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] | 103 | if (MEMBER_P) { \ |
| 104 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 105 | LY_ARRAY_SIZE_TYPE __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 106 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 107 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 108 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 109 | } |
| 110 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 111 | #define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 112 | if (ARRAY) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 113 | for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 114 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 115 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 116 | return LY_EVALID; \ |
| 117 | } \ |
| 118 | } \ |
| 119 | } |
| 120 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 121 | #define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 122 | if (ARRAY) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 123 | for (LY_ARRAY_SIZE_TYPE u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 124 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \ |
| 125 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 126 | return LY_EVALID; \ |
| 127 | } \ |
| 128 | } \ |
| 129 | } |
| 130 | |
| 131 | struct lysc_ext * |
| 132 | lysc_ext_dup(struct lysc_ext *orig) |
| 133 | { |
| 134 | ++orig->refcount; |
| 135 | return orig; |
| 136 | } |
| 137 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 138 | static struct lysc_ext_instance * |
| 139 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 140 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 141 | /* TODO - extensions, increase refcount */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 142 | (void) ctx; |
| 143 | (void) orig; |
| 144 | return NULL; |
| 145 | } |
| 146 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 147 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 148 | * @brief Add record into the compile context's list of incomplete default values. |
| 149 | * @param[in] ctx Compile context with the incomplete default values list. |
| 150 | * @param[in] context_node Context schema node to store in the record. |
| 151 | * @param[in] dflt Incomplete default value to store in the record. |
| 152 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 153 | * @return LY_EMEM in case of memory allocation failure. |
| 154 | * @return LY_SUCCESS |
| 155 | */ |
| 156 | static LY_ERR |
| 157 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 158 | { |
| 159 | struct lysc_incomplete_dflt *r; |
| 160 | r = malloc(sizeof *r); |
| 161 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 162 | r->context_node = context_node; |
| 163 | r->dflt = dflt; |
| 164 | r->dflt_mod = dflt_mod; |
| 165 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 166 | |
| 167 | return LY_SUCCESS; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 172 | * @param[in] ctx Compile context with the incomplete default values list. |
| 173 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 174 | */ |
| 175 | static void |
| 176 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 177 | { |
| 178 | unsigned int u; |
| 179 | struct lysc_incomplete_dflt *r; |
| 180 | |
| 181 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 182 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 183 | if (r->dflt == dflt) { |
| 184 | free(ctx->dflts.objs[u]); |
| 185 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 186 | --ctx->dflts.count; |
| 187 | return; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 192 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 193 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 194 | { |
| 195 | int len; |
| 196 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 197 | |
| 198 | if (!name) { |
| 199 | /* removing last path segment */ |
| 200 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 201 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 202 | if (ctx->path[ctx->path_len] == '=') { |
| 203 | ctx->path[ctx->path_len++] = '}'; |
| 204 | } else { |
| 205 | /* not a top-level special tag, remove also preceiding '/' */ |
| 206 | goto remove_nodelevel; |
| 207 | } |
| 208 | } else { |
| 209 | remove_nodelevel: |
| 210 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 211 | if (ctx->path_len == 0) { |
| 212 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 213 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | /* set new terminating NULL-byte */ |
| 217 | ctx->path[ctx->path_len] = '\0'; |
| 218 | } else { |
| 219 | if (ctx->path_len > 1) { |
| 220 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 221 | /* extension of the special tag */ |
| 222 | nextlevel = 2; |
| 223 | --ctx->path_len; |
| 224 | } else { |
| 225 | /* there is already some path, so add next level */ |
| 226 | nextlevel = 1; |
| 227 | } |
| 228 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 229 | |
| 230 | if (nextlevel != 2) { |
| 231 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 232 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 233 | 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] | 234 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 235 | 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] | 236 | } |
| 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'}", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 239 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 240 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 241 | /* output truncated */ |
| 242 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 243 | } else { |
| 244 | ctx->path_len += len; |
| 245 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 250 | * @brief Duplicate the compiled pattern structure. |
| 251 | * |
| 252 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 253 | * |
| 254 | * @param[in] orig The pattern structure to duplicate. |
| 255 | * @return The duplicated structure to use. |
| 256 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 257 | static struct lysc_pattern* |
| 258 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 259 | { |
| 260 | ++orig->refcount; |
| 261 | return orig; |
| 262 | } |
| 263 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 264 | /** |
| 265 | * @brief Duplicate the array of compiled patterns. |
| 266 | * |
| 267 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 268 | * |
| 269 | * @param[in] ctx Libyang context for logging. |
| 270 | * @param[in] orig The patterns sized array to duplicate. |
| 271 | * @return New sized array as a copy of @p orig. |
| 272 | * @return NULL in case of memory allocation error. |
| 273 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 274 | static struct lysc_pattern** |
| 275 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 276 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 277 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 278 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 279 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 280 | assert(orig); |
| 281 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 282 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 283 | LY_ARRAY_FOR(orig, u) { |
| 284 | dup[u] = lysc_pattern_dup(orig[u]); |
| 285 | LY_ARRAY_INCREMENT(dup); |
| 286 | } |
| 287 | return dup; |
| 288 | } |
| 289 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 290 | /** |
| 291 | * @brief Duplicate compiled range structure. |
| 292 | * |
| 293 | * @param[in] ctx Libyang context for logging. |
| 294 | * @param[in] orig The range structure to be duplicated. |
| 295 | * @return New compiled range structure as a copy of @p orig. |
| 296 | * @return NULL in case of memory allocation error. |
| 297 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 298 | struct lysc_range* |
| 299 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 300 | { |
| 301 | struct lysc_range *dup; |
| 302 | LY_ERR ret; |
| 303 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 304 | assert(orig); |
| 305 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 306 | dup = calloc(1, sizeof *dup); |
| 307 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 308 | if (orig->parts) { |
| 309 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 310 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 311 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 312 | } |
| 313 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 314 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 315 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 316 | |
| 317 | return dup; |
| 318 | cleanup: |
| 319 | free(dup); |
| 320 | (void) ret; /* set but not used due to the return type */ |
| 321 | return NULL; |
| 322 | } |
| 323 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 324 | /** |
| 325 | * @brief Stack for processing if-feature expressions. |
| 326 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 327 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 328 | int size; /**< number of items in the stack */ |
| 329 | int index; /**< first empty item */ |
| 330 | 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] | 331 | }; |
| 332 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 333 | /** |
| 334 | * @brief Add @ref ifftokens into the stack. |
| 335 | * @param[in] stack The if-feature stack to use. |
| 336 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 337 | * @return LY_EMEM in case of memory allocation error |
| 338 | * @return LY_ESUCCESS if the value successfully stored. |
| 339 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 340 | static LY_ERR |
| 341 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 342 | { |
| 343 | if (stack->index == stack->size) { |
| 344 | stack->size += 4; |
| 345 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 346 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 347 | } |
| 348 | stack->stack[stack->index++] = value; |
| 349 | return LY_SUCCESS; |
| 350 | } |
| 351 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 352 | /** |
| 353 | * @brief Get (and remove) the last item form the stack. |
| 354 | * @param[in] stack The if-feature stack to use. |
| 355 | * @return The value from the top of the stack. |
| 356 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 357 | static uint8_t |
| 358 | iff_stack_pop(struct iff_stack *stack) |
| 359 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 360 | assert(stack && stack->index); |
| 361 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 362 | stack->index--; |
| 363 | return stack->stack[stack->index]; |
| 364 | } |
| 365 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 366 | /** |
| 367 | * @brief Clean up the stack. |
| 368 | * @param[in] stack The if-feature stack to use. |
| 369 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 370 | static void |
| 371 | iff_stack_clean(struct iff_stack *stack) |
| 372 | { |
| 373 | stack->size = 0; |
| 374 | free(stack->stack); |
| 375 | } |
| 376 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 377 | /** |
| 378 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 379 | * (libyang format of the if-feature expression). |
| 380 | * @param[in,out] list The 2bits array to modify. |
| 381 | * @param[in] op The operand (@ref ifftokens) to store. |
| 382 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 383 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 384 | static void |
| 385 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 386 | { |
| 387 | uint8_t *item; |
| 388 | uint8_t mask = 3; |
| 389 | |
| 390 | assert(pos >= 0); |
| 391 | assert(op <= 3); /* max 2 bits */ |
| 392 | |
| 393 | item = &list[pos / 4]; |
| 394 | mask = mask << 2 * (pos % 4); |
| 395 | *item = (*item) & ~mask; |
| 396 | *item = (*item) | (op << 2 * (pos % 4)); |
| 397 | } |
| 398 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 399 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 400 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 401 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 402 | /** |
| 403 | * @brief Find a feature of the given name and referenced in the given module. |
| 404 | * |
| 405 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 406 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 407 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 408 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 409 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 410 | * assigned till that time will be still valid. |
| 411 | * |
| 412 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 413 | * @param[in] name Name of the feature including possible prefix. |
| 414 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 415 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 416 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 417 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 418 | 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] | 419 | { |
| 420 | size_t i; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 421 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 422 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 423 | |
| 424 | for (i = 0; i < len; ++i) { |
| 425 | if (name[i] == ':') { |
| 426 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 427 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 428 | LY_CHECK_RET(!mod, NULL); |
| 429 | |
| 430 | name = &name[i + 1]; |
| 431 | len = len - i - 1; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 436 | if (mod->implemented) { |
| 437 | /* module is implemented so there is already the compiled schema */ |
| 438 | flist = mod->compiled->features; |
| 439 | } else { |
| 440 | flist = mod->off_features; |
| 441 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 442 | LY_ARRAY_FOR(flist, u) { |
| 443 | f = &flist[u]; |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 444 | if (!ly_strncmp(f->name, name, len)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 445 | return f; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return NULL; |
| 450 | } |
| 451 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 452 | /** |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 453 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 454 | */ |
| 455 | static LY_ERR |
| 456 | lys_compile_extension(struct lysc_ctx *ctx, const struct lys_module *ext_mod, struct lysp_ext *ext_p, struct lysc_ext **ext) |
| 457 | { |
| 458 | LY_ERR ret = LY_SUCCESS; |
| 459 | |
| 460 | if (!ext_p->compiled) { |
| 461 | lysc_update_path(ctx, NULL, "{extension}"); |
| 462 | lysc_update_path(ctx, NULL, ext_p->name); |
| 463 | |
| 464 | /* compile the extension definition */ |
| 465 | ext_p->compiled = calloc(1, sizeof **ext); |
| 466 | ext_p->compiled->refcount = 1; |
| 467 | DUP_STRING(ctx->ctx, ext_p->name, ext_p->compiled->name); |
| 468 | DUP_STRING(ctx->ctx, ext_p->argument, ext_p->compiled->argument); |
| 469 | ext_p->compiled->module = (struct lys_module *)ext_mod; |
| 470 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext_p->compiled->exts, *ext, LYEXT_PAR_EXT, ret, done); |
| 471 | |
| 472 | lysc_update_path(ctx, NULL, NULL); |
| 473 | lysc_update_path(ctx, NULL, NULL); |
| 474 | |
| 475 | /* find extension definition plugin */ |
| 476 | ext_p->compiled->plugin = lyext_get_plugin(ext_p->compiled); |
| 477 | } |
| 478 | |
| 479 | *ext = lysc_ext_dup(ext_p->compiled); |
| 480 | |
| 481 | done: |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | /** |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 486 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 487 | * |
| 488 | * @param[in] ctx Compilation context. |
| 489 | * @param[in] ext_p Parsed extension instance. |
| 490 | * @param[in,out] ext Prepared compiled extension instance. |
| 491 | * @param[in] parent Extension instance parent. |
| 492 | * @param[in] parent_type Extension instance parent type. |
| 493 | * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations. |
| 494 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 495 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 496 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, |
| 497 | LYEXT_PARENT parent_type, const struct lys_module *ext_mod) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 498 | { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 499 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 500 | const char *name; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 501 | size_t u; |
| 502 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 503 | const char *prefixed_name = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 504 | |
| 505 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 506 | ext->insubstmt = ext_p->insubstmt; |
| 507 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 508 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 509 | ext->parent = parent; |
| 510 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 511 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 512 | 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] | 513 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 514 | /* get module where the extension definition should be placed */ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 515 | for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u); |
| 516 | if (ext_p->yin) { |
| 517 | /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's |
| 518 | * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the |
| 519 | * YANG (import) prefix must be done here. */ |
| 520 | if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) { |
| 521 | prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0); |
| 522 | u = 0; |
| 523 | } else if (ctx->mod_def->compiled) { |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 524 | LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) { |
| 525 | if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) { |
| 526 | char *s; |
| 527 | asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]); |
| 528 | prefixed_name = lydict_insert_zc(ctx->ctx, s); |
| 529 | u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */ |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | if (!prefixed_name) { |
| 535 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 536 | "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name); |
| 537 | goto cleanup; |
| 538 | } |
| 539 | } else { |
| 540 | prefixed_name = ext_p->name; |
| 541 | } |
| 542 | lysc_update_path(ctx, NULL, prefixed_name); |
| 543 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 544 | if (!ext_mod) { |
Radek Krejci | 63f5551 | 2020-05-20 14:37:18 +0200 | [diff] [blame] | 545 | 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] | 546 | if (!ext_mod) { |
| 547 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 548 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name); |
| 549 | goto cleanup; |
| 550 | } else if (!ext_mod->parsed->extensions) { |
| 551 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 552 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 553 | prefixed_name, ext_mod->name); |
| 554 | goto cleanup; |
| 555 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 556 | } |
| 557 | name = &prefixed_name[u]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 558 | |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 559 | /* find the parsed extension definition there */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 560 | LY_ARRAY_FOR(ext_mod->parsed->extensions, v) { |
| 561 | if (!strcmp(name, ext_mod->parsed->extensions[v].name)) { |
Michal Vasko | 5fe75f1 | 2020-03-02 13:52:37 +0100 | [diff] [blame] | 562 | /* compile extension definition and assign it */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 563 | 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] | 564 | break; |
| 565 | } |
| 566 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 567 | if (!ext->def) { |
| 568 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 569 | "Extension definition of extension instance \"%s\" not found.", prefixed_name); |
| 570 | goto cleanup; |
| 571 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 572 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 573 | /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible |
| 574 | * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have |
| 575 | * 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] | 576 | if (ext_p->yin && ext->def->argument && !ext->argument) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 577 | /* Schema was parsed from YIN and an argument is expected, ... */ |
| 578 | struct lysp_stmt *stmt = NULL; |
| 579 | |
| 580 | if (ext->def->flags & LYS_YINELEM_TRUE) { |
| 581 | /* ... argument was the first XML child element */ |
| 582 | if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) { |
| 583 | /* TODO check namespace of the statement */ |
| 584 | if (!strcmp(ext_p->child->stmt, ext->def->argument)) { |
| 585 | stmt = ext_p->child; |
| 586 | } |
| 587 | } |
| 588 | } else { |
| 589 | /* ... argument was one of the XML attributes which are represented as child stmt |
| 590 | * with LYS_YIN_ATTR flag */ |
| 591 | for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) { |
| 592 | if (!strcmp(stmt->stmt, ext->def->argument)) { |
| 593 | /* this is the extension's argument */ |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 594 | break; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | if (!stmt) { |
| 599 | /* missing extension's argument */ |
| 600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 601 | "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument); |
| 602 | goto cleanup; |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 603 | |
| 604 | } |
| 605 | ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 606 | stmt->flags |= LYS_YIN_ARGUMENT; |
| 607 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 608 | if (prefixed_name != ext_p->name) { |
| 609 | lydict_remove(ctx->ctx, ext_p->name); |
| 610 | ext_p->name = prefixed_name; |
| 611 | if (!ext_p->argument && ext->argument) { |
| 612 | ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0); |
| 613 | } |
| 614 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 615 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 616 | if (ext->def->plugin && ext->def->plugin->compile) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 617 | if (ext->argument) { |
| 618 | lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument); |
| 619 | } |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 620 | LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 621 | if (ext->argument) { |
| 622 | lysc_update_path(ctx, NULL, NULL); |
| 623 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 624 | } |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 625 | ext_p->compiled = ext; |
| 626 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 627 | ret = LY_SUCCESS; |
| 628 | cleanup: |
| 629 | if (prefixed_name && prefixed_name != ext_p->name) { |
| 630 | lydict_remove(ctx->ctx, prefixed_name); |
| 631 | } |
| 632 | |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 633 | lysc_update_path(ctx, NULL, NULL); |
| 634 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 635 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 636 | return ret; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 640 | * @brief Compile information from the if-feature statement |
| 641 | * @param[in] ctx Compile context. |
| 642 | * @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] | 643 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 644 | * @return LY_ERR value. |
| 645 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 646 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 647 | 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] | 648 | { |
| 649 | const char *c = *value; |
| 650 | int r, rc = EXIT_FAILURE; |
| 651 | int i, j, last_not, checkversion = 0; |
| 652 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 653 | uint8_t op; |
| 654 | struct iff_stack stack = {0, 0, NULL}; |
| 655 | struct lysc_feature *f; |
| 656 | |
| 657 | assert(c); |
| 658 | |
| 659 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 660 | for (i = j = last_not = 0; c[i]; i++) { |
| 661 | if (c[i] == '(') { |
| 662 | j++; |
| 663 | checkversion = 1; |
| 664 | continue; |
| 665 | } else if (c[i] == ')') { |
| 666 | j--; |
| 667 | continue; |
| 668 | } else if (isspace(c[i])) { |
| 669 | checkversion = 1; |
| 670 | continue; |
| 671 | } |
| 672 | |
| 673 | 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] | 674 | int sp; |
| 675 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 676 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 677 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 678 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 679 | return LY_EVALID; |
| 680 | } else if (!isspace(c[i + r])) { |
| 681 | /* feature name starting with the not/and/or */ |
| 682 | last_not = 0; |
| 683 | f_size++; |
| 684 | } else if (c[i] == 'n') { /* not operation */ |
| 685 | if (last_not) { |
| 686 | /* double not */ |
| 687 | expr_size = expr_size - 2; |
| 688 | last_not = 0; |
| 689 | } else { |
| 690 | last_not = 1; |
| 691 | } |
| 692 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 693 | if (f_exp != f_size) { |
| 694 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 695 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 696 | return LY_EVALID; |
| 697 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 698 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 699 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 700 | /* not a not operation */ |
| 701 | last_not = 0; |
| 702 | } |
| 703 | i += r; |
| 704 | } else { |
| 705 | f_size++; |
| 706 | last_not = 0; |
| 707 | } |
| 708 | expr_size++; |
| 709 | |
| 710 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 711 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 712 | i--; |
| 713 | break; |
| 714 | } |
| 715 | i++; |
| 716 | } |
| 717 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 718 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 719 | /* not matching count of ( and ) */ |
| 720 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 721 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 722 | return LY_EVALID; |
| 723 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 724 | if (f_exp != f_size) { |
| 725 | /* features do not match the needed arguments for the logical operations */ |
| 726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 727 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 728 | "the required number of operands for the operations.", *value); |
| 729 | return LY_EVALID; |
| 730 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 731 | |
| 732 | if (checkversion || expr_size > 1) { |
| 733 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 734 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 735 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 736 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 737 | return LY_EVALID; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /* allocate the memory */ |
| 742 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 743 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 744 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 745 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 746 | |
| 747 | stack.size = expr_size; |
| 748 | f_size--; expr_size--; /* used as indexes from now */ |
| 749 | |
| 750 | for (i--; i >= 0; i--) { |
| 751 | if (c[i] == ')') { |
| 752 | /* push it on stack */ |
| 753 | iff_stack_push(&stack, LYS_IFF_RP); |
| 754 | continue; |
| 755 | } else if (c[i] == '(') { |
| 756 | /* pop from the stack into result all operators until ) */ |
| 757 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 758 | iff_setop(iff->expr, op, expr_size--); |
| 759 | } |
| 760 | continue; |
| 761 | } else if (isspace(c[i])) { |
| 762 | continue; |
| 763 | } |
| 764 | |
| 765 | /* end of operator or operand -> find beginning and get what is it */ |
| 766 | j = i + 1; |
| 767 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 768 | i--; |
| 769 | } |
| 770 | i++; /* go back by one step */ |
| 771 | |
| 772 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 773 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 774 | /* double not */ |
| 775 | iff_stack_pop(&stack); |
| 776 | } else { |
| 777 | /* not has the highest priority, so do not pop from the stack |
| 778 | * as in case of AND and OR */ |
| 779 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 780 | } |
| 781 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 782 | /* as for OR - pop from the stack all operators with the same or higher |
| 783 | * priority and store them to the result, then push the AND to the stack */ |
| 784 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 785 | op = iff_stack_pop(&stack); |
| 786 | iff_setop(iff->expr, op, expr_size--); |
| 787 | } |
| 788 | iff_stack_push(&stack, LYS_IFF_AND); |
| 789 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 790 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 791 | op = iff_stack_pop(&stack); |
| 792 | iff_setop(iff->expr, op, expr_size--); |
| 793 | } |
| 794 | iff_stack_push(&stack, LYS_IFF_OR); |
| 795 | } else { |
| 796 | /* feature name, length is j - i */ |
| 797 | |
| 798 | /* add it to the expression */ |
| 799 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 800 | |
| 801 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 802 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 803 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 804 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 805 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 806 | iff->features[f_size] = f; |
| 807 | LY_ARRAY_INCREMENT(iff->features); |
| 808 | f_size--; |
| 809 | } |
| 810 | } |
| 811 | while (stack.index) { |
| 812 | op = iff_stack_pop(&stack); |
| 813 | iff_setop(iff->expr, op, expr_size--); |
| 814 | } |
| 815 | |
| 816 | if (++expr_size || ++f_size) { |
| 817 | /* not all expected operators and operands found */ |
| 818 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 819 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 820 | rc = LY_EINT; |
| 821 | } else { |
| 822 | rc = LY_SUCCESS; |
| 823 | } |
| 824 | |
| 825 | error: |
| 826 | /* cleanup */ |
| 827 | iff_stack_clean(&stack); |
| 828 | |
| 829 | return rc; |
| 830 | } |
| 831 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 832 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 833 | * @brief Get the XPath context node for the given schema node. |
| 834 | * @param[in] start The schema node where the XPath expression appears. |
| 835 | * @return The context node to evaluate XPath expression in given schema node. |
| 836 | * @return NULL in case the context node is the root node. |
| 837 | */ |
| 838 | static struct lysc_node * |
| 839 | lysc_xpath_context(struct lysc_node *start) |
| 840 | { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 841 | 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] | 842 | start = start->parent); |
| 843 | return start; |
| 844 | } |
| 845 | |
| 846 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 847 | * @brief Compile information from the when statement |
| 848 | * @param[in] ctx Compile context. |
| 849 | * @param[in] when_p The parsed when statement structure. |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 850 | * @param[in] flags Flags of the node with the "when" defiition. |
| 851 | * @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] | 852 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 853 | * @return LY_ERR value. |
| 854 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 855 | static LY_ERR |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 856 | 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] | 857 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 858 | LY_ERR ret = LY_SUCCESS; |
| 859 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 860 | *when = calloc(1, sizeof **when); |
| 861 | (*when)->refcount = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 862 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1); |
Radek Krejci | a0f704a | 2019-09-09 16:12:23 +0200 | [diff] [blame] | 863 | (*when)->module = ctx->mod_def; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 864 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 865 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 866 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 867 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 868 | 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] | 869 | (*when)->flags = flags & LYS_STATUS_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 870 | |
| 871 | done: |
| 872 | return ret; |
| 873 | } |
| 874 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 875 | /** |
| 876 | * @brief Compile information from the must statement |
| 877 | * @param[in] ctx Compile context. |
| 878 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 879 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 880 | * @return LY_ERR value. |
| 881 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 882 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 883 | 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] | 884 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 885 | LY_ERR ret = LY_SUCCESS; |
| 886 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 887 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg, 0, 1); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 888 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 889 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 890 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 891 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 892 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 893 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 894 | 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] | 895 | |
| 896 | done: |
| 897 | return ret; |
| 898 | } |
| 899 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 900 | /** |
| 901 | * @brief Compile information from the import statement |
| 902 | * @param[in] ctx Compile context. |
| 903 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 904 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 905 | * @return LY_ERR value. |
| 906 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 907 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 908 | 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] | 909 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 910 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 911 | LY_ERR ret = LY_SUCCESS; |
| 912 | |
| 913 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 914 | 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] | 915 | imp->module = imp_p->module; |
| 916 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 917 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 918 | * 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] | 919 | * 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] | 920 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 921 | /* try to use filepath if present */ |
| 922 | if (imp->module->filepath) { |
| 923 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 924 | !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 925 | if (mod != imp->module) { |
| 926 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 927 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 928 | mod = NULL; |
| 929 | } |
| 930 | } |
| 931 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 932 | if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 933 | 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] | 934 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 935 | return LY_ENOTFOUND; |
| 936 | } |
| 937 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | done: |
| 941 | return ret; |
| 942 | } |
| 943 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 944 | /** |
| 945 | * @brief Compile information from the identity statement |
| 946 | * |
| 947 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 948 | * |
| 949 | * @param[in] ctx Compile context. |
| 950 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 951 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 952 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 953 | * @return LY_ERR value. |
| 954 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 955 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 956 | lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 957 | { |
| 958 | unsigned int u; |
| 959 | LY_ERR ret = LY_SUCCESS; |
| 960 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 961 | lysc_update_path(ctx, NULL, ident_p->name); |
| 962 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 963 | COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 964 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 965 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 966 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 967 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 968 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 969 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 970 | COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 971 | ident->flags = ident_p->flags; |
| 972 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 973 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 974 | done: |
| 975 | return ret; |
| 976 | } |
| 977 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 978 | /** |
| 979 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 980 | * |
| 981 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 982 | * |
| 983 | * @param[in] ctx Compile context for logging. |
| 984 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 985 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 986 | * @return LY_SUCCESS if everything is ok. |
| 987 | * @return LY_EVALID if the identity is derived from itself. |
| 988 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 989 | static LY_ERR |
| 990 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 991 | { |
| 992 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 993 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 994 | struct ly_set recursion = {0}; |
| 995 | struct lysc_ident *drv; |
| 996 | |
| 997 | if (!derived) { |
| 998 | return LY_SUCCESS; |
| 999 | } |
| 1000 | |
| 1001 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 1002 | if (ident == derived[u]) { |
| 1003 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1004 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1005 | goto cleanup; |
| 1006 | } |
| 1007 | ly_set_add(&recursion, derived[u], 0); |
| 1008 | } |
| 1009 | |
| 1010 | for (v = 0; v < recursion.count; ++v) { |
| 1011 | drv = recursion.objs[v]; |
| 1012 | if (!drv->derived) { |
| 1013 | continue; |
| 1014 | } |
| 1015 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 1016 | if (ident == drv->derived[u]) { |
| 1017 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1018 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 1019 | goto cleanup; |
| 1020 | } |
| 1021 | ly_set_add(&recursion, drv->derived[u], 0); |
| 1022 | } |
| 1023 | } |
| 1024 | ret = LY_SUCCESS; |
| 1025 | |
| 1026 | cleanup: |
| 1027 | ly_set_erase(&recursion, NULL); |
| 1028 | return ret; |
| 1029 | } |
| 1030 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1031 | /** |
| 1032 | * @brief Find and process the referenced base identities from another identity or identityref |
| 1033 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 1034 | * 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] | 1035 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 1036 | * to distinguish these two use cases. |
| 1037 | * |
| 1038 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1039 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 1040 | * @param[in] ident Referencing identity to work with. |
| 1041 | * @param[in] bases Array of bases of identityref to fill in. |
| 1042 | * @return LY_ERR value. |
| 1043 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1044 | static LY_ERR |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1045 | lys_compile_identity_bases(struct lysc_ctx *ctx, struct lys_module *context_module, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1046 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1047 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1048 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1049 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1050 | struct lysc_ident **idref; |
| 1051 | |
| 1052 | assert(ident || bases); |
| 1053 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1054 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1055 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1056 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 1057 | return LY_EVALID; |
| 1058 | } |
| 1059 | |
| 1060 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 1061 | s = strchr(bases_p[u], ':'); |
| 1062 | if (s) { |
| 1063 | /* prefixed identity */ |
| 1064 | name = &s[1]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1065 | 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] | 1066 | } else { |
| 1067 | name = bases_p[u]; |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1068 | mod = context_module; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1069 | } |
| 1070 | if (!mod) { |
| 1071 | if (ident) { |
| 1072 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1073 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1074 | } else { |
| 1075 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1076 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 1077 | } |
| 1078 | return LY_EVALID; |
| 1079 | } |
| 1080 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1081 | if (mod->compiled && mod->compiled->identities) { |
| 1082 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 1083 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1084 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1085 | if (ident == &mod->compiled->identities[v]) { |
| 1086 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1087 | "Identity \"%s\" is derived from itself.", ident->name); |
| 1088 | return LY_EVALID; |
| 1089 | } |
| 1090 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1091 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1092 | LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1093 | *idref = ident; |
| 1094 | } else { |
| 1095 | /* we have match! store the found identity */ |
| 1096 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1097 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1098 | } |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | if (!idref || !(*idref)) { |
| 1104 | if (ident) { |
| 1105 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1106 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1107 | } else { |
| 1108 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1109 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1110 | } |
| 1111 | return LY_EVALID; |
| 1112 | } |
| 1113 | } |
| 1114 | return LY_SUCCESS; |
| 1115 | } |
| 1116 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1117 | /** |
| 1118 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1119 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1120 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1121 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1122 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1123 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1124 | static LY_ERR |
| 1125 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1126 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1127 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1128 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1129 | for (u = 0; u < LY_ARRAY_SIZE(idents_p); ++u) { |
| 1130 | if (!idents_p[u].bases) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1131 | continue; |
| 1132 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1133 | lysc_update_path(ctx, NULL, idents[u].name); |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 1134 | 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] | 1135 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1136 | } |
| 1137 | return LY_SUCCESS; |
| 1138 | } |
| 1139 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1140 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1141 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1142 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1143 | LY_ARRAY_SIZE_TYPE offset = 0, u; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1144 | struct lysc_ctx context = {0}; |
| 1145 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1146 | assert(ctx_sc || ctx); |
| 1147 | |
| 1148 | if (!ctx_sc) { |
| 1149 | context.ctx = ctx; |
| 1150 | context.mod = module; |
| 1151 | context.path_len = 1; |
| 1152 | context.path[0] = '/'; |
| 1153 | ctx_sc = &context; |
| 1154 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1155 | |
| 1156 | if (!features_p) { |
| 1157 | return LY_SUCCESS; |
| 1158 | } |
| 1159 | if (*features) { |
| 1160 | offset = LY_ARRAY_SIZE(*features); |
| 1161 | } |
| 1162 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1163 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1164 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1165 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1166 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1167 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1168 | LY_ARRAY_INCREMENT(*features); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 1169 | 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] | 1170 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1171 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1172 | 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] | 1173 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1174 | (*features)[offset + u].module = ctx_sc->mod; |
| 1175 | |
| 1176 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1177 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1178 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1179 | |
| 1180 | return LY_SUCCESS; |
| 1181 | } |
| 1182 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1183 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1184 | * @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] | 1185 | * |
| 1186 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1187 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1188 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1189 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1190 | * being currently processed). |
| 1191 | * @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] | 1192 | * @return LY_SUCCESS if everything is ok. |
| 1193 | * @return LY_EVALID if the feature references indirectly itself. |
| 1194 | */ |
| 1195 | static LY_ERR |
| 1196 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1197 | { |
| 1198 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1199 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1200 | struct ly_set recursion = {0}; |
| 1201 | struct lysc_feature *drv; |
| 1202 | |
| 1203 | if (!depfeatures) { |
| 1204 | return LY_SUCCESS; |
| 1205 | } |
| 1206 | |
| 1207 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1208 | if (feature == depfeatures[u]) { |
| 1209 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1210 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1211 | goto cleanup; |
| 1212 | } |
| 1213 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1214 | } |
| 1215 | |
| 1216 | for (v = 0; v < recursion.count; ++v) { |
| 1217 | drv = recursion.objs[v]; |
| 1218 | if (!drv->depfeatures) { |
| 1219 | continue; |
| 1220 | } |
| 1221 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1222 | if (feature == drv->depfeatures[u]) { |
| 1223 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1224 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1225 | goto cleanup; |
| 1226 | } |
| 1227 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1228 | } |
| 1229 | } |
| 1230 | ret = LY_SUCCESS; |
| 1231 | |
| 1232 | cleanup: |
| 1233 | ly_set_erase(&recursion, NULL); |
| 1234 | return ret; |
| 1235 | } |
| 1236 | |
| 1237 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1238 | * @brief Create pre-compiled features array. |
| 1239 | * |
| 1240 | * See lys_feature_precompile() for more details. |
| 1241 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1242 | * @param[in] ctx Compile context. |
| 1243 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1244 | * @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] | 1245 | * @return LY_ERR value. |
| 1246 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1247 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1248 | 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] | 1249 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1250 | LY_ARRAY_SIZE_TYPE u, v, x; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1251 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1252 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1253 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1254 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1255 | /* find the preprecompiled feature */ |
| 1256 | LY_ARRAY_FOR(features, x) { |
| 1257 | if (strcmp(features[x].name, feature_p->name)) { |
| 1258 | continue; |
| 1259 | } |
| 1260 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1261 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1262 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1263 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1264 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1265 | 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] | 1266 | 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] | 1267 | if (feature->iffeatures) { |
| 1268 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1269 | if (feature->iffeatures[u].features) { |
| 1270 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1271 | /* check for circular dependency - direct reference first,... */ |
| 1272 | if (feature == feature->iffeatures[u].features[v]) { |
| 1273 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1274 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1275 | return LY_EVALID; |
| 1276 | } |
| 1277 | /* ... and indirect circular reference */ |
| 1278 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1279 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1280 | /* add itself into the dependants list */ |
| 1281 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1282 | *df = feature; |
| 1283 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1284 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1285 | } |
| 1286 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1287 | lysc_update_path(ctx, NULL, NULL); |
| 1288 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1289 | done: |
| 1290 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1291 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1292 | |
| 1293 | LOGINT(ctx->ctx); |
| 1294 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1295 | } |
| 1296 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1297 | /** |
| 1298 | * @brief Revert compiled list of features back to the precompiled state. |
| 1299 | * |
| 1300 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1301 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1302 | * |
| 1303 | * @param[in] ctx Compilation context. |
| 1304 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1305 | * and supposed to hold the off_features list. |
| 1306 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1307 | static void |
| 1308 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1309 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1310 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1311 | |
| 1312 | /* keep the off_features list until the complete lys_module is freed */ |
| 1313 | mod->off_features = mod->compiled->features; |
| 1314 | mod->compiled->features = NULL; |
| 1315 | |
| 1316 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1317 | * which may points into the data being freed here */ |
| 1318 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1319 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1320 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1321 | } |
| 1322 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1323 | mod->off_features[u].iffeatures = NULL; |
| 1324 | |
| 1325 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1326 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1327 | } |
| 1328 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1329 | mod->off_features[u].exts = NULL; |
| 1330 | } |
| 1331 | } |
| 1332 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1333 | /** |
| 1334 | * @brief Validate and normalize numeric value from a range definition. |
| 1335 | * @param[in] ctx Compile context. |
| 1336 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1337 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1338 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1339 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1340 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1341 | * @param[in] value String value of the range boundary. |
| 1342 | * @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. |
| 1343 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1344 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1345 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1346 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1347 | 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] | 1348 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1349 | size_t fraction = 0, size; |
| 1350 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1351 | *len = 0; |
| 1352 | |
| 1353 | assert(value); |
| 1354 | /* parse value */ |
| 1355 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1356 | return LY_EVALID; |
| 1357 | } |
| 1358 | |
| 1359 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1360 | ++(*len); |
| 1361 | } |
| 1362 | |
| 1363 | while (isdigit(value[*len])) { |
| 1364 | ++(*len); |
| 1365 | } |
| 1366 | |
| 1367 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1368 | if (basetype == LY_TYPE_DEC64) { |
| 1369 | goto decimal; |
| 1370 | } else { |
| 1371 | *valcopy = strndup(value, *len); |
| 1372 | return LY_SUCCESS; |
| 1373 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1374 | } |
| 1375 | fraction = *len; |
| 1376 | |
| 1377 | ++(*len); |
| 1378 | while (isdigit(value[*len])) { |
| 1379 | ++(*len); |
| 1380 | } |
| 1381 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1382 | if (basetype == LY_TYPE_DEC64) { |
| 1383 | decimal: |
| 1384 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1385 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1386 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1387 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1388 | *len, value, frdigits); |
| 1389 | return LY_EINVAL; |
| 1390 | } |
| 1391 | if (fraction) { |
| 1392 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1393 | } else { |
| 1394 | size = (*len) + frdigits + 1; |
| 1395 | } |
| 1396 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1397 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1398 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1399 | (*valcopy)[size - 1] = '\0'; |
| 1400 | if (fraction) { |
| 1401 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1402 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1403 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1404 | } else { |
| 1405 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1406 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1407 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1408 | } |
| 1409 | return LY_SUCCESS; |
| 1410 | } |
| 1411 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1412 | /** |
| 1413 | * @brief Check that values in range are in ascendant order. |
| 1414 | * @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] | 1415 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1416 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1417 | * @param[in] value Current value to check. |
| 1418 | * @param[in] prev_value The last seen value. |
| 1419 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1420 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1421 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1422 | 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] | 1423 | { |
| 1424 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1425 | 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] | 1426 | return LY_EEXIST; |
| 1427 | } |
| 1428 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1429 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1430 | return LY_EEXIST; |
| 1431 | } |
| 1432 | } |
| 1433 | return LY_SUCCESS; |
| 1434 | } |
| 1435 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1436 | /** |
| 1437 | * @brief Set min/max value of the range part. |
| 1438 | * @param[in] ctx Compile context. |
| 1439 | * @param[in] part Range part structure to fill. |
| 1440 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1441 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1442 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1443 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1444 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1445 | * @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] | 1446 | * @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] | 1447 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1448 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1449 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1450 | * frdigits value), LY_EMEM. |
| 1451 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1452 | static LY_ERR |
| 1453 | 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] | 1454 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1455 | { |
| 1456 | LY_ERR ret = LY_SUCCESS; |
| 1457 | char *valcopy = NULL; |
| 1458 | size_t len; |
| 1459 | |
| 1460 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1461 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1462 | LY_CHECK_GOTO(ret, finalize); |
| 1463 | } |
| 1464 | if (!valcopy && base_range) { |
| 1465 | if (max) { |
| 1466 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1467 | } else { |
| 1468 | part->min_64 = base_range->parts[0].min_64; |
| 1469 | } |
| 1470 | if (!first) { |
| 1471 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1472 | } |
| 1473 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1477 | case LY_TYPE_INT8: /* range */ |
| 1478 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1479 | 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] | 1480 | } else if (max) { |
| 1481 | part->max_64 = INT64_C(127); |
| 1482 | } else { |
| 1483 | part->min_64 = INT64_C(-128); |
| 1484 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1485 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1486 | 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] | 1487 | } |
| 1488 | break; |
| 1489 | case LY_TYPE_INT16: /* range */ |
| 1490 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1491 | 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] | 1492 | } else if (max) { |
| 1493 | part->max_64 = INT64_C(32767); |
| 1494 | } else { |
| 1495 | part->min_64 = INT64_C(-32768); |
| 1496 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1497 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1498 | 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] | 1499 | } |
| 1500 | break; |
| 1501 | case LY_TYPE_INT32: /* range */ |
| 1502 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1503 | 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] | 1504 | } else if (max) { |
| 1505 | part->max_64 = INT64_C(2147483647); |
| 1506 | } else { |
| 1507 | part->min_64 = INT64_C(-2147483648); |
| 1508 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1509 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1510 | 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] | 1511 | } |
| 1512 | break; |
| 1513 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1514 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1515 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1516 | 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] | 1517 | max ? &part->max_64 : &part->min_64); |
| 1518 | } else if (max) { |
| 1519 | part->max_64 = INT64_C(9223372036854775807); |
| 1520 | } else { |
| 1521 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1522 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1523 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1524 | 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] | 1525 | } |
| 1526 | break; |
| 1527 | case LY_TYPE_UINT8: /* range */ |
| 1528 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1529 | 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] | 1530 | } else if (max) { |
| 1531 | part->max_u64 = UINT64_C(255); |
| 1532 | } else { |
| 1533 | part->min_u64 = UINT64_C(0); |
| 1534 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1535 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1536 | 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] | 1537 | } |
| 1538 | break; |
| 1539 | case LY_TYPE_UINT16: /* range */ |
| 1540 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1541 | 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] | 1542 | } else if (max) { |
| 1543 | part->max_u64 = UINT64_C(65535); |
| 1544 | } else { |
| 1545 | part->min_u64 = UINT64_C(0); |
| 1546 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1547 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1548 | 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] | 1549 | } |
| 1550 | break; |
| 1551 | case LY_TYPE_UINT32: /* range */ |
| 1552 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1553 | 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] | 1554 | } else if (max) { |
| 1555 | part->max_u64 = UINT64_C(4294967295); |
| 1556 | } else { |
| 1557 | part->min_u64 = UINT64_C(0); |
| 1558 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1559 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1560 | 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] | 1561 | } |
| 1562 | break; |
| 1563 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1564 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1565 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1566 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1567 | 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] | 1568 | } else if (max) { |
| 1569 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1570 | } else { |
| 1571 | part->min_u64 = UINT64_C(0); |
| 1572 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1573 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1574 | 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] | 1575 | } |
| 1576 | break; |
| 1577 | default: |
| 1578 | LOGINT(ctx->ctx); |
| 1579 | ret = LY_EINT; |
| 1580 | } |
| 1581 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1582 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1583 | if (ret == LY_EDENIED) { |
| 1584 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1585 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1586 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1587 | } else if (ret == LY_EVALID) { |
| 1588 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1589 | "Invalid %s restriction - invalid value \"%s\".", |
| 1590 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1591 | } else if (ret == LY_EEXIST) { |
| 1592 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1593 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1594 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1595 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1596 | } else if (!ret && value) { |
| 1597 | *value = *value + len; |
| 1598 | } |
| 1599 | free(valcopy); |
| 1600 | return ret; |
| 1601 | } |
| 1602 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1603 | /** |
| 1604 | * @brief Compile the parsed range restriction. |
| 1605 | * @param[in] ctx Compile context. |
| 1606 | * @param[in] range_p Parsed range structure to compile. |
| 1607 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1608 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1609 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1610 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1611 | * range restriction must be more restrictive than the base_range. |
| 1612 | * @param[in,out] range Pointer to the created current range structure. |
| 1613 | * @return LY_ERR value. |
| 1614 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1615 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1616 | 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] | 1617 | struct lysc_range *base_range, struct lysc_range **range) |
| 1618 | { |
| 1619 | LY_ERR ret = LY_EVALID; |
| 1620 | const char *expr; |
| 1621 | struct lysc_range_part *parts = NULL, *part; |
| 1622 | int range_expected = 0, uns; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 1623 | LY_ARRAY_SIZE_TYPE parts_done = 0, u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1624 | |
| 1625 | assert(range); |
| 1626 | assert(range_p); |
| 1627 | |
| 1628 | expr = range_p->arg; |
| 1629 | while(1) { |
| 1630 | if (isspace(*expr)) { |
| 1631 | ++expr; |
| 1632 | } else if (*expr == '\0') { |
| 1633 | if (range_expected) { |
| 1634 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1635 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1636 | length_restr ? "length" : "range", range_p->arg); |
| 1637 | goto cleanup; |
| 1638 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1639 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1640 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1641 | length_restr ? "length" : "range", range_p->arg); |
| 1642 | goto cleanup; |
| 1643 | } |
| 1644 | parts_done++; |
| 1645 | break; |
| 1646 | } else if (!strncmp(expr, "min", 3)) { |
| 1647 | if (parts) { |
| 1648 | /* min cannot be used elsewhere than in the first part */ |
| 1649 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1650 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1651 | expr - range_p->arg, range_p->arg); |
| 1652 | goto cleanup; |
| 1653 | } |
| 1654 | expr += 3; |
| 1655 | |
| 1656 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1657 | 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] | 1658 | part->max_64 = part->min_64; |
| 1659 | } else if (*expr == '|') { |
| 1660 | if (!parts || range_expected) { |
| 1661 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1662 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1663 | goto cleanup; |
| 1664 | } |
| 1665 | expr++; |
| 1666 | parts_done++; |
| 1667 | /* process next part of the expression */ |
| 1668 | } else if (!strncmp(expr, "..", 2)) { |
| 1669 | expr += 2; |
| 1670 | while (isspace(*expr)) { |
| 1671 | expr++; |
| 1672 | } |
| 1673 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1675 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1676 | goto cleanup; |
| 1677 | } |
| 1678 | /* continue expecting the upper boundary */ |
| 1679 | range_expected = 1; |
| 1680 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1681 | /* number */ |
| 1682 | if (range_expected) { |
| 1683 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1684 | 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] | 1685 | range_expected = 0; |
| 1686 | } else { |
| 1687 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1688 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1689 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1690 | part->max_64 = part->min_64; |
| 1691 | } |
| 1692 | |
| 1693 | /* continue with possible another expression part */ |
| 1694 | } else if (!strncmp(expr, "max", 3)) { |
| 1695 | expr += 3; |
| 1696 | while (isspace(*expr)) { |
| 1697 | expr++; |
| 1698 | } |
| 1699 | if (*expr != '\0') { |
| 1700 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1701 | length_restr ? "length" : "range", expr); |
| 1702 | goto cleanup; |
| 1703 | } |
| 1704 | if (range_expected) { |
| 1705 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1706 | 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] | 1707 | range_expected = 0; |
| 1708 | } else { |
| 1709 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1710 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1711 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1712 | part->min_64 = part->max_64; |
| 1713 | } |
| 1714 | } else { |
| 1715 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1716 | length_restr ? "length" : "range", expr); |
| 1717 | goto cleanup; |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | /* check with the previous range/length restriction */ |
| 1722 | if (base_range) { |
| 1723 | switch (basetype) { |
| 1724 | case LY_TYPE_BINARY: |
| 1725 | case LY_TYPE_UINT8: |
| 1726 | case LY_TYPE_UINT16: |
| 1727 | case LY_TYPE_UINT32: |
| 1728 | case LY_TYPE_UINT64: |
| 1729 | case LY_TYPE_STRING: |
| 1730 | uns = 1; |
| 1731 | break; |
| 1732 | case LY_TYPE_DEC64: |
| 1733 | case LY_TYPE_INT8: |
| 1734 | case LY_TYPE_INT16: |
| 1735 | case LY_TYPE_INT32: |
| 1736 | case LY_TYPE_INT64: |
| 1737 | uns = 0; |
| 1738 | break; |
| 1739 | default: |
| 1740 | LOGINT(ctx->ctx); |
| 1741 | ret = LY_EINT; |
| 1742 | goto cleanup; |
| 1743 | } |
| 1744 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1745 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1746 | goto baseerror; |
| 1747 | } |
| 1748 | /* current lower bound is not lower than the base */ |
| 1749 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1750 | /* base has single value */ |
| 1751 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1752 | /* both lower bounds are the same */ |
| 1753 | if (parts[u].min_64 != parts[u].max_64) { |
| 1754 | /* current continues with a range */ |
| 1755 | goto baseerror; |
| 1756 | } else { |
| 1757 | /* equal single values, move both forward */ |
| 1758 | ++v; |
| 1759 | continue; |
| 1760 | } |
| 1761 | } else { |
| 1762 | /* base is single value lower than current range, so the |
| 1763 | * value from base range is removed in the current, |
| 1764 | * move only base and repeat checking */ |
| 1765 | ++v; |
| 1766 | --u; |
| 1767 | continue; |
| 1768 | } |
| 1769 | } else { |
| 1770 | /* base is the range */ |
| 1771 | if (parts[u].min_64 == parts[u].max_64) { |
| 1772 | /* current is a single value */ |
| 1773 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1774 | /* current is behind the base range, so base range is omitted, |
| 1775 | * move the base and keep the current for further check */ |
| 1776 | ++v; |
| 1777 | --u; |
| 1778 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1779 | continue; |
| 1780 | } else { |
| 1781 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1782 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1783 | /* higher bound is higher than the current higher bound */ |
| 1784 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1785 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1786 | * continue with the same current, but move the base */ |
| 1787 | --u; |
| 1788 | ++v; |
| 1789 | continue; |
| 1790 | } |
| 1791 | /* current range starts within the base range but end behind it */ |
| 1792 | goto baseerror; |
| 1793 | } else { |
| 1794 | /* current range is smaller than the base, |
| 1795 | * move current, but stay with the base */ |
| 1796 | continue; |
| 1797 | } |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | if (u != parts_done) { |
| 1802 | baseerror: |
| 1803 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1804 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1805 | length_restr ? "length" : "range", range_p->arg); |
| 1806 | goto cleanup; |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | if (!(*range)) { |
| 1811 | *range = calloc(1, sizeof **range); |
| 1812 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1813 | } |
| 1814 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1815 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1816 | if (range_p->eapptag) { |
| 1817 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1818 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1819 | } |
| 1820 | if (range_p->emsg) { |
| 1821 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1822 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1823 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1824 | if (range_p->dsc) { |
| 1825 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1826 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1827 | } |
| 1828 | if (range_p->ref) { |
| 1829 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1830 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1831 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1832 | /* extensions are taken only from the last range by the caller */ |
| 1833 | |
| 1834 | (*range)->parts = parts; |
| 1835 | parts = NULL; |
| 1836 | ret = LY_SUCCESS; |
| 1837 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1838 | LY_ARRAY_FREE(parts); |
| 1839 | |
| 1840 | return ret; |
| 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * @brief Checks pattern syntax. |
| 1845 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1846 | * @param[in] ctx Context. |
| 1847 | * @param[in] log_path Path for logging errors. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1848 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1849 | * @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] | 1850 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1851 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1852 | LY_ERR |
| 1853 | 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] | 1854 | { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1855 | int idx, idx2, start, end, size, brack; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1856 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1857 | int err_code; |
| 1858 | const char *orig_ptr; |
| 1859 | PCRE2_SIZE err_offset; |
| 1860 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1861 | #define URANGE_LEN 19 |
| 1862 | char *ublock2urange[][2] = { |
| 1863 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1864 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1865 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1866 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1867 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1868 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1869 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1870 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1871 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1872 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1873 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1874 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1875 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1876 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1877 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1878 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1879 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1880 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1881 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1882 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1883 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1884 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1885 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1886 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1887 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1888 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1889 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1890 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1891 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1892 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1893 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1894 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1895 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1896 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1897 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1898 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1899 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1900 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1901 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1902 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1903 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1904 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1905 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1906 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1907 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1908 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1909 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1910 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1911 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1912 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1913 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1914 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1915 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1916 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1917 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1918 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1919 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1920 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1921 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1922 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1923 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1924 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1925 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1926 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1927 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1928 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1929 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1930 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1931 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1932 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1933 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1934 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1935 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1936 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1937 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1938 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1939 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1940 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1941 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1942 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1943 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1944 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1945 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1946 | {NULL, NULL} |
| 1947 | }; |
| 1948 | |
| 1949 | /* adjust the expression to a Perl equivalent |
| 1950 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1951 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1952 | /* allocate space for the transformed pattern */ |
| 1953 | size = strlen(pattern) + 1; |
| 1954 | perl_regex = malloc(size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1955 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1956 | perl_regex[0] = '\0'; |
| 1957 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1958 | /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */ |
| 1959 | brack = 0; |
| 1960 | idx = 0; |
| 1961 | orig_ptr = pattern; |
| 1962 | while (orig_ptr[0]) { |
| 1963 | switch (orig_ptr[0]) { |
| 1964 | case '$': |
| 1965 | case '^': |
| 1966 | if (!brack) { |
| 1967 | /* make space for the extra character */ |
| 1968 | ++size; |
| 1969 | perl_regex = ly_realloc(perl_regex, size); |
| 1970 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1971 | |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1972 | /* print escape slash */ |
| 1973 | perl_regex[idx] = '\\'; |
| 1974 | ++idx; |
| 1975 | } |
| 1976 | break; |
| 1977 | case '[': |
| 1978 | /* must not be escaped */ |
| 1979 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 1980 | ++brack; |
| 1981 | } |
| 1982 | break; |
| 1983 | case ']': |
| 1984 | if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) { |
| 1985 | /* pattern was checked and compiled already */ |
| 1986 | assert(brack); |
| 1987 | --brack; |
| 1988 | } |
| 1989 | break; |
| 1990 | default: |
| 1991 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1992 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 1993 | |
| 1994 | /* copy char */ |
| 1995 | perl_regex[idx] = orig_ptr[0]; |
| 1996 | |
| 1997 | ++idx; |
| 1998 | ++orig_ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1999 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2000 | perl_regex[idx] = '\0'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2001 | |
| 2002 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 2003 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 2004 | start = ptr - perl_regex; |
| 2005 | |
| 2006 | ptr = strchr(ptr, '}'); |
| 2007 | if (!ptr) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2008 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2009 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 2010 | free(perl_regex); |
| 2011 | return LY_EVALID; |
| 2012 | } |
| 2013 | end = (ptr - perl_regex) + 1; |
| 2014 | |
| 2015 | /* need more space */ |
| 2016 | if (end - start < URANGE_LEN) { |
| 2017 | 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] | 2018 | 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] | 2019 | } |
| 2020 | |
| 2021 | /* find our range */ |
| 2022 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 2023 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 2024 | break; |
| 2025 | } |
| 2026 | } |
| 2027 | if (!ublock2urange[idx][0]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2029 | pattern, perl_regex + start + 5, "unknown block name"); |
| 2030 | free(perl_regex); |
| 2031 | return LY_EVALID; |
| 2032 | } |
| 2033 | |
| 2034 | /* 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] | 2035 | for (idx2 = 0, idx = 0; idx2 < start; ++idx2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2036 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2037 | ++idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2038 | } |
| 2039 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2040 | --idx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2041 | } |
| 2042 | } |
Michal Vasko | 40a0008 | 2020-05-27 15:20:01 +0200 | [diff] [blame] | 2043 | if (idx) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2044 | /* skip brackets */ |
| 2045 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 2046 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 2047 | } else { |
| 2048 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 2049 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 2054 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 2055 | 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] | 2056 | &err_code, &err_offset, NULL); |
| 2057 | if (!code_local) { |
| 2058 | PCRE2_UCHAR err_msg[256] = {0}; |
| 2059 | pcre2_get_error_message(err_code, err_msg, 256); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | 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] | 2061 | free(perl_regex); |
| 2062 | return LY_EVALID; |
| 2063 | } |
| 2064 | free(perl_regex); |
| 2065 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2066 | if (code) { |
| 2067 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2068 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2069 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | return LY_SUCCESS; |
| 2073 | |
| 2074 | #undef URANGE_LEN |
| 2075 | } |
| 2076 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2077 | /** |
| 2078 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 2079 | * @param[in] ctx Compile context. |
| 2080 | * @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] | 2081 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 2082 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 2083 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 2084 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 2085 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2086 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2087 | 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] | 2088 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 2089 | { |
| 2090 | struct lysc_pattern **pattern; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2091 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2092 | LY_ERR ret = LY_SUCCESS; |
| 2093 | |
| 2094 | /* first, copy the patterns from the base type */ |
| 2095 | if (base_patterns) { |
| 2096 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 2097 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 2098 | } |
| 2099 | |
| 2100 | LY_ARRAY_FOR(patterns_p, u) { |
| 2101 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 2102 | *pattern = calloc(1, sizeof **pattern); |
| 2103 | ++(*pattern)->refcount; |
| 2104 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | 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] | 2106 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2107 | |
| 2108 | if (patterns_p[u].arg[0] == 0x15) { |
| 2109 | (*pattern)->inverted = 1; |
| 2110 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 2111 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2112 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 2113 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2114 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 2115 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2116 | 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] | 2117 | } |
| 2118 | done: |
| 2119 | return ret; |
| 2120 | } |
| 2121 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2122 | /** |
| 2123 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 2124 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2125 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 2126 | 0 /* LY_TYPE_UNKNOWN */, |
| 2127 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2128 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 2129 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 2130 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 2131 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 2132 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2133 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 2134 | 0 /* LY_TYPE_BOOL */, |
| 2135 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2136 | 0 /* LY_TYPE_EMPTY */, |
| 2137 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2138 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2139 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2140 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2141 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2142 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2143 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2144 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2145 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2146 | }; |
| 2147 | |
| 2148 | /** |
| 2149 | * @brief stringification of the YANG built-in data types |
| 2150 | */ |
| 2151 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2152 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2153 | "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] | 2154 | }; |
| 2155 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2156 | /** |
| 2157 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2158 | * @param[in] ctx Compile context. |
| 2159 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2160 | * @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] | 2161 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2162 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2163 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2164 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2165 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2166 | 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] | 2167 | 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] | 2168 | { |
| 2169 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2170 | LY_ARRAY_SIZE_TYPE u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2171 | int32_t value = 0; |
| 2172 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2173 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2174 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2175 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2176 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2177 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2178 | return LY_EVALID; |
| 2179 | } |
| 2180 | |
| 2181 | LY_ARRAY_FOR(enums_p, u) { |
| 2182 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2183 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2184 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2185 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2186 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2187 | if (base_enums) { |
| 2188 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2189 | LY_ARRAY_FOR(base_enums, v) { |
| 2190 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2191 | break; |
| 2192 | } |
| 2193 | } |
| 2194 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2195 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2196 | "Invalid %s - derived type adds new item \"%s\".", |
| 2197 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2198 | return LY_EVALID; |
| 2199 | } |
| 2200 | match = v; |
| 2201 | } |
| 2202 | |
| 2203 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2204 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2205 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2206 | e->value = (int32_t)enums_p[u].value; |
| 2207 | if (!u || e->value >= value) { |
| 2208 | value = e->value + 1; |
| 2209 | } |
| 2210 | /* check collision with other values */ |
| 2211 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2212 | if (e->value == (*enums)[v].value) { |
| 2213 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2214 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2215 | e->value, e->name, (*enums)[v].name); |
| 2216 | return LY_EVALID; |
| 2217 | } |
| 2218 | } |
| 2219 | } else if (base_enums) { |
| 2220 | /* inherit the assigned value */ |
| 2221 | e->value = base_enums[match].value; |
| 2222 | if (!u || e->value >= value) { |
| 2223 | value = e->value + 1; |
| 2224 | } |
| 2225 | } else { |
| 2226 | /* assign value automatically */ |
| 2227 | if (u && value == INT32_MIN) { |
| 2228 | /* counter overflow */ |
| 2229 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2230 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2231 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2232 | return LY_EVALID; |
| 2233 | } |
| 2234 | e->value = value++; |
| 2235 | } |
| 2236 | } else { /* LY_TYPE_BITS */ |
| 2237 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2238 | e->value = (int32_t)enums_p[u].value; |
| 2239 | if (!u || (uint32_t)e->value >= position) { |
| 2240 | position = (uint32_t)e->value + 1; |
| 2241 | } |
| 2242 | /* check collision with other values */ |
| 2243 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2244 | if (e->value == (*enums)[v].value) { |
| 2245 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2246 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2247 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2248 | return LY_EVALID; |
| 2249 | } |
| 2250 | } |
| 2251 | } else if (base_enums) { |
| 2252 | /* inherit the assigned value */ |
| 2253 | e->value = base_enums[match].value; |
| 2254 | if (!u || (uint32_t)e->value >= position) { |
| 2255 | position = (uint32_t)e->value + 1; |
| 2256 | } |
| 2257 | } else { |
| 2258 | /* assign value automatically */ |
| 2259 | if (u && position == 0) { |
| 2260 | /* counter overflow */ |
| 2261 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2262 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2263 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2264 | return LY_EVALID; |
| 2265 | } |
| 2266 | e->value = position++; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | if (base_enums) { |
| 2271 | /* the assigned values must not change from the derived type */ |
| 2272 | if (e->value != base_enums[match].value) { |
| 2273 | if (basetype == LY_TYPE_ENUM) { |
| 2274 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2275 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2276 | e->name, base_enums[match].value, e->value); |
| 2277 | } else { |
| 2278 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2279 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2280 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2281 | } |
| 2282 | return LY_EVALID; |
| 2283 | } |
| 2284 | } |
| 2285 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2286 | 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] | 2287 | 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] | 2288 | |
| 2289 | if (basetype == LY_TYPE_BITS) { |
| 2290 | /* keep bits ordered by position */ |
| 2291 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2292 | if (v != u) { |
| 2293 | memcpy(&storage, e, sizeof *e); |
| 2294 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2295 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | done: |
| 2301 | return ret; |
| 2302 | } |
| 2303 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2304 | /** |
| 2305 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2306 | * |
| 2307 | * path-arg = absolute-path / relative-path |
| 2308 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2309 | * relative-path = 1*(".." "/") descendant-path |
| 2310 | * |
| 2311 | * @param[in,out] path Path to parse. |
| 2312 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2313 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2314 | * @param[out] name Name of the token. |
| 2315 | * @param[out] nam_len Length of the name. |
| 2316 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2317 | * must not be changed between consecutive calls. -1 if the |
| 2318 | * path is absolute. |
| 2319 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2320 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2321 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2322 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2323 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2324 | int *parent_times, int *has_predicate) |
| 2325 | { |
| 2326 | int par_times = 0; |
| 2327 | |
| 2328 | assert(path && *path); |
| 2329 | assert(parent_times); |
| 2330 | assert(prefix); |
| 2331 | assert(prefix_len); |
| 2332 | assert(name); |
| 2333 | assert(name_len); |
| 2334 | assert(has_predicate); |
| 2335 | |
| 2336 | *prefix = NULL; |
| 2337 | *prefix_len = 0; |
| 2338 | *name = NULL; |
| 2339 | *name_len = 0; |
| 2340 | *has_predicate = 0; |
| 2341 | |
| 2342 | if (!*parent_times) { |
| 2343 | if (!strncmp(*path, "..", 2)) { |
| 2344 | *path += 2; |
| 2345 | ++par_times; |
| 2346 | while (!strncmp(*path, "/..", 3)) { |
| 2347 | *path += 3; |
| 2348 | ++par_times; |
| 2349 | } |
| 2350 | } |
| 2351 | if (par_times) { |
| 2352 | *parent_times = par_times; |
| 2353 | } else { |
| 2354 | *parent_times = -1; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | if (**path != '/') { |
| 2359 | return LY_EINVAL; |
| 2360 | } |
| 2361 | /* skip '/' */ |
| 2362 | ++(*path); |
| 2363 | |
| 2364 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2365 | 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] | 2366 | |
| 2367 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2368 | /* path continues by another token or this is the last token */ |
| 2369 | return LY_SUCCESS; |
| 2370 | } else if ((*path)[0] != '[') { |
| 2371 | /* unexpected character */ |
| 2372 | return LY_EINVAL; |
| 2373 | } else { |
| 2374 | /* predicate starting with [ */ |
| 2375 | *has_predicate = 1; |
| 2376 | return LY_SUCCESS; |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2381 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2382 | * |
| 2383 | * The set of features used for target must be a subset of features used for the leafref. |
| 2384 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2385 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2386 | * |
| 2387 | * @param[in] refnode The leafref node. |
| 2388 | * @param[in] target Tha target node of the leafref. |
| 2389 | * @return LY_SUCCESS or LY_EVALID; |
| 2390 | */ |
| 2391 | static LY_ERR |
| 2392 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2393 | { |
| 2394 | LY_ERR ret = LY_EVALID; |
| 2395 | const struct lysc_node *iter; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2396 | LY_ARRAY_SIZE_TYPE u, v, count; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2397 | struct ly_set features = {0}; |
| 2398 | |
| 2399 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2400 | if (iter->iffeatures) { |
| 2401 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2402 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2403 | 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] | 2404 | } |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2410 | * So when adding features applicable to the target into the features set, we should not be |
| 2411 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2412 | * to the leafref itself. */ |
| 2413 | count = features.count; |
| 2414 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2415 | if (iter->iffeatures) { |
| 2416 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2417 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2418 | 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] | 2419 | /* new feature was added (or LY_EMEM) */ |
| 2420 | goto cleanup; |
| 2421 | } |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | ret = LY_SUCCESS; |
| 2427 | |
| 2428 | cleanup: |
| 2429 | ly_set_erase(&features, NULL); |
| 2430 | return ret; |
| 2431 | } |
| 2432 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2433 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2434 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2435 | struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2436 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2437 | /** |
| 2438 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2439 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2440 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2441 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2442 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2443 | * @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] | 2444 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2445 | * @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] | 2446 | * @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] | 2447 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2448 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2449 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2450 | * @return LY_ERR value. |
| 2451 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2452 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2453 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2454 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2455 | struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, |
| 2456 | struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2457 | { |
| 2458 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2459 | struct lysc_type_bin *bin; |
| 2460 | struct lysc_type_num *num; |
| 2461 | struct lysc_type_str *str; |
| 2462 | struct lysc_type_bits *bits; |
| 2463 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2464 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2465 | struct lysc_type_identityref *idref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2466 | struct lysc_type_leafref *lref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2467 | struct lysc_type_union *un, *un_aux; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2468 | |
| 2469 | switch (basetype) { |
| 2470 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2471 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2472 | |
| 2473 | /* 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] | 2474 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2475 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2476 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2477 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2478 | 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] | 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | if (tpdfname) { |
| 2483 | type_p->compiled = *type; |
| 2484 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2485 | } |
| 2486 | break; |
| 2487 | case LY_TYPE_BITS: |
| 2488 | /* RFC 7950 9.7 - bits */ |
| 2489 | bits = (struct lysc_type_bits*)(*type); |
| 2490 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2491 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2492 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2493 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2494 | } |
| 2495 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2496 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2497 | /* 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] | 2498 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2499 | 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] | 2500 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2501 | 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] | 2502 | free(*type); |
| 2503 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2504 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2505 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | if (tpdfname) { |
| 2509 | type_p->compiled = *type; |
| 2510 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2511 | } |
| 2512 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2513 | case LY_TYPE_DEC64: |
| 2514 | dec = (struct lysc_type_dec*)(*type); |
| 2515 | |
| 2516 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2517 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2518 | if (!type_p->fraction_digits) { |
| 2519 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2520 | 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] | 2521 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2522 | 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] | 2523 | free(*type); |
| 2524 | *type = NULL; |
| 2525 | } |
| 2526 | return LY_EVALID; |
| 2527 | } |
| 2528 | } else if (type_p->fraction_digits) { |
| 2529 | /* 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] | 2530 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2531 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2532 | "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] | 2533 | tpdfname); |
| 2534 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2535 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2536 | "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] | 2537 | free(*type); |
| 2538 | *type = NULL; |
| 2539 | } |
| 2540 | return LY_EVALID; |
| 2541 | } |
| 2542 | dec->fraction_digits = type_p->fraction_digits; |
| 2543 | |
| 2544 | /* RFC 7950 9.2.4 - range */ |
| 2545 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2546 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2547 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2548 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2549 | 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] | 2550 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | if (tpdfname) { |
| 2554 | type_p->compiled = *type; |
| 2555 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2556 | } |
| 2557 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2558 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2559 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2560 | |
| 2561 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2562 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2563 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2564 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2565 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2566 | 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] | 2567 | } |
| 2568 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2569 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2570 | } |
| 2571 | |
| 2572 | /* RFC 7950 9.4.5 - pattern */ |
| 2573 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2574 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2575 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2576 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2577 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2578 | } |
| 2579 | |
| 2580 | if (tpdfname) { |
| 2581 | type_p->compiled = *type; |
| 2582 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2583 | } |
| 2584 | break; |
| 2585 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2586 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2587 | |
| 2588 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2589 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2590 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2591 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2592 | } |
| 2593 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2594 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2595 | /* 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] | 2596 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2597 | 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] | 2598 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2599 | 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] | 2600 | free(*type); |
| 2601 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2602 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2603 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | if (tpdfname) { |
| 2607 | type_p->compiled = *type; |
| 2608 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2609 | } |
| 2610 | break; |
| 2611 | case LY_TYPE_INT8: |
| 2612 | case LY_TYPE_UINT8: |
| 2613 | case LY_TYPE_INT16: |
| 2614 | case LY_TYPE_UINT16: |
| 2615 | case LY_TYPE_INT32: |
| 2616 | case LY_TYPE_UINT32: |
| 2617 | case LY_TYPE_INT64: |
| 2618 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2619 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2620 | |
| 2621 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2622 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2623 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2624 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2625 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2626 | 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] | 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | if (tpdfname) { |
| 2631 | type_p->compiled = *type; |
| 2632 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2633 | } |
| 2634 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2635 | case LY_TYPE_IDENT: |
| 2636 | idref = (struct lysc_type_identityref*)(*type); |
| 2637 | |
| 2638 | /* RFC 7950 9.10.2 - base */ |
| 2639 | if (type_p->bases) { |
| 2640 | if (base) { |
| 2641 | /* only the directly derived identityrefs can contain base specification */ |
| 2642 | if (tpdfname) { |
| 2643 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2644 | "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] | 2645 | tpdfname); |
| 2646 | } else { |
| 2647 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2648 | "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] | 2649 | free(*type); |
| 2650 | *type = NULL; |
| 2651 | } |
| 2652 | return LY_EVALID; |
| 2653 | } |
Radek Krejci | 0a33b04 | 2020-05-27 10:05:06 +0200 | [diff] [blame] | 2654 | 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] | 2655 | } |
| 2656 | |
| 2657 | if (!base && !type_p->flags) { |
| 2658 | /* type derived from identityref built-in type must contain at least one base */ |
| 2659 | if (tpdfname) { |
| 2660 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2661 | } else { |
| 2662 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2663 | free(*type); |
| 2664 | *type = NULL; |
| 2665 | } |
| 2666 | return LY_EVALID; |
| 2667 | } |
| 2668 | |
| 2669 | if (tpdfname) { |
| 2670 | type_p->compiled = *type; |
| 2671 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2672 | } |
| 2673 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2674 | case LY_TYPE_LEAFREF: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2675 | lref = (struct lysc_type_leafref*)*type; |
| 2676 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2677 | /* RFC 7950 9.9.3 - require-instance */ |
| 2678 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2679 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2680 | if (tpdfname) { |
| 2681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2682 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2683 | } else { |
| 2684 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2685 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2686 | free(*type); |
| 2687 | *type = NULL; |
| 2688 | } |
| 2689 | return LY_EVALID; |
| 2690 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2691 | lref->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2692 | } else if (base) { |
| 2693 | /* inherit */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2694 | lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2695 | } else { |
| 2696 | /* default is true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2697 | lref->require_instance = 1; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2698 | } |
| 2699 | if (type_p->path) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2700 | lref->path = lyxp_expr_dup(ctx->ctx, type_p->path); |
| 2701 | lref->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2702 | } else if (base) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2703 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)base)->path); |
| 2704 | lref->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2705 | } else if (tpdfname) { |
| 2706 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2707 | return LY_EVALID; |
| 2708 | } else { |
| 2709 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2710 | free(*type); |
| 2711 | *type = NULL; |
| 2712 | return LY_EVALID; |
| 2713 | } |
| 2714 | if (tpdfname) { |
| 2715 | type_p->compiled = *type; |
| 2716 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2717 | } |
| 2718 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2719 | case LY_TYPE_INST: |
| 2720 | /* RFC 7950 9.9.3 - require-instance */ |
| 2721 | if (type_p->flags & LYS_SET_REQINST) { |
| 2722 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2723 | } else { |
| 2724 | /* default is true */ |
| 2725 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2726 | } |
| 2727 | |
| 2728 | if (tpdfname) { |
| 2729 | type_p->compiled = *type; |
| 2730 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2731 | } |
| 2732 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2733 | case LY_TYPE_UNION: |
| 2734 | un = (struct lysc_type_union*)(*type); |
| 2735 | |
| 2736 | /* RFC 7950 7.4 - type */ |
| 2737 | if (type_p->types) { |
| 2738 | if (base) { |
| 2739 | /* only the directly derived union can contain types specification */ |
| 2740 | if (tpdfname) { |
| 2741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2742 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2743 | tpdfname); |
| 2744 | } else { |
| 2745 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2746 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2747 | free(*type); |
| 2748 | *type = NULL; |
| 2749 | } |
| 2750 | return LY_EVALID; |
| 2751 | } |
| 2752 | /* compile the type */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2753 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2754 | for (LY_ARRAY_SIZE_TYPE u = 0, additional = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2755 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, |
| 2756 | &type_p->types[u], &un->types[u + additional], NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2757 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2758 | /* add space for additional types from the union subtype */ |
| 2759 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 2760 | LY_ARRAY_RESIZE_ERR_RET(ctx->ctx, un->types, (*((uint64_t*)(type_p->types) - 1)) + additional + LY_ARRAY_SIZE(un_aux->types) - 1, |
| 2761 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2762 | |
| 2763 | /* copy subtypes of the subtype union */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2764 | for (LY_ARRAY_SIZE_TYPE v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2765 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2766 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2767 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2768 | 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^] | 2769 | lref = (struct lysc_type_leafref *)un->types[u + additional]; |
| 2770 | |
| 2771 | lref->basetype = LY_TYPE_LEAFREF; |
| 2772 | lref->path = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path); |
| 2773 | lref->refcount = 1; |
| 2774 | lref->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 2775 | 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] | 2776 | /* TODO extensions */ |
| 2777 | |
| 2778 | } else { |
| 2779 | un->types[u + additional] = un_aux->types[v]; |
| 2780 | ++un_aux->types[v]->refcount; |
| 2781 | } |
| 2782 | ++additional; |
| 2783 | LY_ARRAY_INCREMENT(un->types); |
| 2784 | } |
| 2785 | /* compensate u increment in main loop */ |
| 2786 | --additional; |
| 2787 | |
| 2788 | /* free the replaced union subtype */ |
| 2789 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 2790 | } else { |
| 2791 | LY_ARRAY_INCREMENT(un->types); |
| 2792 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | if (!base && !type_p->flags) { |
| 2797 | /* type derived from union built-in type must contain at least one type */ |
| 2798 | if (tpdfname) { |
| 2799 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 2800 | } else { |
| 2801 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 2802 | free(*type); |
| 2803 | *type = NULL; |
| 2804 | } |
| 2805 | return LY_EVALID; |
| 2806 | } |
| 2807 | |
| 2808 | if (tpdfname) { |
| 2809 | type_p->compiled = *type; |
| 2810 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2811 | } |
| 2812 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2813 | case LY_TYPE_BOOL: |
| 2814 | case LY_TYPE_EMPTY: |
| 2815 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2816 | break; |
| 2817 | } |
| 2818 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2819 | done: |
| 2820 | return ret; |
| 2821 | } |
| 2822 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2823 | /** |
| 2824 | * @brief Compile information about the leaf/leaf-list's type. |
| 2825 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2826 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2827 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2828 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2829 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 2830 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2831 | * @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] | 2832 | * @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] | 2833 | * @return LY_ERR value. |
| 2834 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2835 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 2836 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, |
| 2837 | struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p, |
| 2838 | struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2839 | { |
| 2840 | LY_ERR ret = LY_SUCCESS; |
| 2841 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2842 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2843 | struct type_context { |
| 2844 | const struct lysp_tpdf *tpdf; |
| 2845 | struct lysp_node *node; |
| 2846 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2847 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2848 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2849 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2850 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2851 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2852 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2853 | |
| 2854 | (*type) = NULL; |
| 2855 | |
| 2856 | tctx = calloc(1, sizeof *tctx); |
| 2857 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2858 | 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] | 2859 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2860 | ret == LY_SUCCESS; |
| 2861 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2862 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2863 | if (basetype) { |
| 2864 | break; |
| 2865 | } |
| 2866 | |
| 2867 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2868 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 2869 | 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] | 2870 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2871 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2872 | if (units && !*units) { |
| 2873 | /* inherit units */ |
| 2874 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 2875 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2876 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2877 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2878 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 2879 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2880 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2881 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2882 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 2883 | break; |
| 2884 | } |
| 2885 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2886 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2887 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 2888 | * 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] | 2889 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2890 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2891 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2892 | dummyloops = 1; |
| 2893 | goto preparenext; |
| 2894 | } else { |
| 2895 | tctx = NULL; |
| 2896 | break; |
| 2897 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2898 | } |
| 2899 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2900 | /* circular typedef reference detection */ |
| 2901 | for (u = 0; u < tpdf_chain.count; u++) { |
| 2902 | /* local part */ |
| 2903 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 2904 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2905 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2906 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2907 | free(tctx); |
| 2908 | ret = LY_EVALID; |
| 2909 | goto cleanup; |
| 2910 | } |
| 2911 | } |
| 2912 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 2913 | /* global part for unions corner case */ |
| 2914 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 2915 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 2916 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2917 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 2918 | free(tctx); |
| 2919 | ret = LY_EVALID; |
| 2920 | goto cleanup; |
| 2921 | } |
| 2922 | } |
| 2923 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2924 | /* store information for the following processing */ |
| 2925 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2926 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2927 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2928 | /* prepare next loop */ |
| 2929 | tctx_prev = tctx; |
| 2930 | tctx = calloc(1, sizeof *tctx); |
| 2931 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2932 | } |
| 2933 | free(tctx); |
| 2934 | |
| 2935 | /* allocate type according to the basetype */ |
| 2936 | switch (basetype) { |
| 2937 | case LY_TYPE_BINARY: |
| 2938 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2939 | break; |
| 2940 | case LY_TYPE_BITS: |
| 2941 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2942 | break; |
| 2943 | case LY_TYPE_BOOL: |
| 2944 | case LY_TYPE_EMPTY: |
| 2945 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2946 | break; |
| 2947 | case LY_TYPE_DEC64: |
| 2948 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2949 | break; |
| 2950 | case LY_TYPE_ENUM: |
| 2951 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2952 | break; |
| 2953 | case LY_TYPE_IDENT: |
| 2954 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2955 | break; |
| 2956 | case LY_TYPE_INST: |
| 2957 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2958 | break; |
| 2959 | case LY_TYPE_LEAFREF: |
| 2960 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2961 | break; |
| 2962 | case LY_TYPE_STRING: |
| 2963 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2964 | break; |
| 2965 | case LY_TYPE_UNION: |
| 2966 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2967 | break; |
| 2968 | case LY_TYPE_INT8: |
| 2969 | case LY_TYPE_UINT8: |
| 2970 | case LY_TYPE_INT16: |
| 2971 | case LY_TYPE_UINT16: |
| 2972 | case LY_TYPE_INT32: |
| 2973 | case LY_TYPE_UINT32: |
| 2974 | case LY_TYPE_INT64: |
| 2975 | case LY_TYPE_UINT64: |
| 2976 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2977 | break; |
| 2978 | case LY_TYPE_UNKNOWN: |
| 2979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2980 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 2981 | ret = LY_EVALID; |
| 2982 | goto cleanup; |
| 2983 | } |
| 2984 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2985 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2986 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 2987 | ly_data_type2str[basetype]); |
| 2988 | free(*type); |
| 2989 | (*type) = NULL; |
| 2990 | ret = LY_EVALID; |
| 2991 | goto cleanup; |
| 2992 | } |
| 2993 | |
| 2994 | /* get restrictions from the referred typedefs */ |
| 2995 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 2996 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2997 | |
| 2998 | /* remember the typedef context for circular check */ |
| 2999 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3000 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3001 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3002 | base = tctx->tpdf->type.compiled; |
| 3003 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3004 | } 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] | 3005 | /* no change, just use the type information from the base */ |
| 3006 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3007 | ++base->refcount; |
| 3008 | continue; |
| 3009 | } |
| 3010 | |
| 3011 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3012 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3013 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3014 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3015 | ret = LY_EVALID; |
| 3016 | goto cleanup; |
| 3017 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3018 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3019 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3020 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3021 | ret = LY_EVALID; |
| 3022 | goto cleanup; |
| 3023 | } |
| 3024 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3025 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3026 | /* TODO user type plugins */ |
| 3027 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3028 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3029 | 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] | 3030 | 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] | 3031 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3032 | LY_CHECK_GOTO(ret, cleanup); |
| 3033 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3034 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3035 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3036 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3037 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3038 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3039 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3040 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3041 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3042 | /* TODO user type plugins */ |
| 3043 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3044 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3045 | 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] | 3046 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 90b148f | 2020-05-06 17:49:40 +0200 | [diff] [blame] | 3047 | } else if (basetype != LY_TYPE_BOOL && basetype != LY_TYPE_EMPTY) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3048 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3049 | free(*type); |
| 3050 | (*type) = base; |
| 3051 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3052 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3053 | if (dflt && !(*type)->dflt) { |
| 3054 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3055 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3056 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3057 | (*type)->dflt->realtype = (*type); |
| 3058 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3059 | 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] | 3060 | 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] | 3061 | if (err) { |
| 3062 | ly_err_print(err); |
| 3063 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3064 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3065 | ly_err_free(err); |
| 3066 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3067 | if (ret == LY_EINCOMPLETE) { |
| 3068 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3069 | 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] | 3070 | |
| 3071 | /* but in general result is so far ok */ |
| 3072 | ret = LY_SUCCESS; |
| 3073 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3074 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3075 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3076 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3077 | 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] | 3078 | |
| 3079 | cleanup: |
| 3080 | ly_set_erase(&tpdf_chain, free); |
| 3081 | return ret; |
| 3082 | } |
| 3083 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3084 | /** |
| 3085 | * @brief Compile status information of the given node. |
| 3086 | * |
| 3087 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3088 | * has the status correctly set during the compilation. |
| 3089 | * |
| 3090 | * @param[in] ctx Compile context |
| 3091 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3092 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3093 | * the compatibility with the parent's status value. |
| 3094 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3095 | * @return LY_ERR value. |
| 3096 | */ |
| 3097 | static LY_ERR |
| 3098 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3099 | { |
| 3100 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3101 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3102 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3103 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3104 | if ((parent_flags & 0x3) != 0x3) { |
| 3105 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3106 | * combination of bits (0x3) which marks the uses_status value */ |
| 3107 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3108 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3109 | } |
| 3110 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3111 | } else { |
| 3112 | (*node_flags) |= LYS_STATUS_CURR; |
| 3113 | } |
| 3114 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3115 | /* check status compatibility with the parent */ |
| 3116 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3117 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3118 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3119 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3120 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3121 | } else { /* LYS_STATUS_DEPRC */ |
| 3122 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3123 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3124 | } |
| 3125 | return LY_EVALID; |
| 3126 | } |
| 3127 | } |
| 3128 | return LY_SUCCESS; |
| 3129 | } |
| 3130 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3131 | /** |
| 3132 | * @brief Check uniqness of the node/action/notification name. |
| 3133 | * |
| 3134 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3135 | * structures, but they share the namespace so we need to check their name collisions. |
| 3136 | * |
| 3137 | * @param[in] ctx Compile context. |
| 3138 | * @param[in] children List (linked list) of data nodes to go through. |
| 3139 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3140 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3141 | * @param[in] name Name of the item to find in the given lists. |
| 3142 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3143 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3144 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3145 | */ |
| 3146 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 3147 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, const struct lysc_action *actions, |
| 3148 | const struct lysc_notif *notifs, const char *name, void *exclude) |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3149 | { |
| 3150 | const struct lysc_node *iter; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3151 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3152 | |
| 3153 | LY_LIST_FOR(children, iter) { |
| 3154 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3155 | goto error; |
| 3156 | } |
| 3157 | } |
| 3158 | LY_ARRAY_FOR(actions, u) { |
| 3159 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3160 | goto error; |
| 3161 | } |
| 3162 | } |
| 3163 | LY_ARRAY_FOR(notifs, u) { |
| 3164 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3165 | goto error; |
| 3166 | } |
| 3167 | } |
| 3168 | return LY_SUCCESS; |
| 3169 | error: |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3170 | 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] | 3171 | return LY_EEXIST; |
| 3172 | } |
| 3173 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3174 | 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] | 3175 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3176 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3177 | * @brief Compile parsed RPC/action schema node information. |
| 3178 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3179 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3180 | * @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] | 3181 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3182 | * @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). |
| 3183 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3184 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3185 | */ |
| 3186 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3187 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3188 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3189 | { |
| 3190 | LY_ERR ret = LY_SUCCESS; |
| 3191 | struct lysp_node *child_p; |
| 3192 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3193 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3194 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3195 | lysc_update_path(ctx, parent, action_p->name); |
| 3196 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3197 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3198 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3199 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3200 | action_p->name, action)) { |
| 3201 | return LY_EVALID; |
| 3202 | } |
| 3203 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3204 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3205 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3206 | "Action \"%s\" is placed inside %s.", action_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3207 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3208 | return LY_EVALID; |
| 3209 | } |
| 3210 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 3211 | action->nodetype = parent ? LYS_ACTION : LYS_RPC; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3212 | action->module = ctx->mod; |
| 3213 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3214 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3215 | action->sp = action_p; |
| 3216 | } |
| 3217 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3218 | |
| 3219 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3220 | * 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] | 3221 | 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] | 3222 | |
| 3223 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3224 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3225 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3226 | 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] | 3227 | 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] | 3228 | |
| 3229 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3230 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3231 | 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] | 3232 | 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] | 3233 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3234 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3235 | 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] | 3236 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3237 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3238 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3239 | |
| 3240 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3241 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3242 | 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] | 3243 | 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] | 3244 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3245 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3246 | 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] | 3247 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3248 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3249 | lysc_update_path(ctx, NULL, NULL); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3250 | |
| 3251 | if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3252 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 3253 | ly_set_add(&ctx->xpath, action, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3254 | } |
| 3255 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3256 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3257 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3258 | return ret; |
| 3259 | } |
| 3260 | |
| 3261 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3262 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3263 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3264 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3265 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3266 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3267 | * @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] | 3268 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3269 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3270 | */ |
| 3271 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3272 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3273 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3274 | { |
| 3275 | LY_ERR ret = LY_SUCCESS; |
| 3276 | struct lysp_node *child_p; |
| 3277 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3278 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3279 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3280 | lysc_update_path(ctx, parent, notif_p->name); |
| 3281 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3282 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3283 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3284 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3285 | notif_p->name, notif)) { |
| 3286 | return LY_EVALID; |
| 3287 | } |
| 3288 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3289 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3290 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3291 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3292 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3293 | return LY_EVALID; |
| 3294 | } |
| 3295 | |
| 3296 | notif->nodetype = LYS_NOTIF; |
| 3297 | notif->module = ctx->mod; |
| 3298 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3299 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3300 | notif->sp = notif_p; |
| 3301 | } |
| 3302 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3303 | |
| 3304 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3305 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3306 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3307 | |
| 3308 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3309 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3310 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3311 | 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] | 3312 | 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] | 3313 | if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3314 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 3315 | ly_set_add(&ctx->xpath, notif, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3316 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3317 | 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] | 3318 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3319 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3320 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3321 | 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] | 3322 | } |
| 3323 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3324 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3325 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3326 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3327 | return ret; |
| 3328 | } |
| 3329 | |
| 3330 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3331 | * @brief Compile parsed container node information. |
| 3332 | * @param[in] ctx Compile context |
| 3333 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3334 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3335 | * is enriched with the container-specific information. |
| 3336 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3337 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3338 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3339 | 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] | 3340 | { |
| 3341 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3342 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3343 | struct lysp_node *child_p; |
| 3344 | unsigned int u; |
| 3345 | LY_ERR ret = LY_SUCCESS; |
| 3346 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3347 | if (cont_p->presence) { |
| 3348 | cont->flags |= LYS_PRESENCE; |
| 3349 | } |
| 3350 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3351 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3352 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3353 | } |
| 3354 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3355 | 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] | 3356 | if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3357 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 3358 | ly_set_add(&ctx->xpath, cont, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3359 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3360 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3361 | 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] | 3362 | |
| 3363 | done: |
| 3364 | return ret; |
| 3365 | } |
| 3366 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3367 | /* |
| 3368 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3369 | * @param[in] ctx Compile context. |
| 3370 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3371 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3372 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3373 | * @return LY_ERR value. |
| 3374 | */ |
| 3375 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3376 | 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] | 3377 | { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3378 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3379 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3380 | 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] | 3381 | leaf->units ? NULL : &leaf->units)); |
| 3382 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3383 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3384 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3385 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3386 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3387 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3388 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3389 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3390 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3391 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3392 | } |
| 3393 | } else { |
| 3394 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3395 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3396 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3397 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3398 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3399 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3400 | } |
| 3401 | } |
| 3402 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3403 | /* 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^] | 3404 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3405 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3406 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3407 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3408 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3409 | /* 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^] | 3410 | ly_set_add(&ctx->leafrefs, leaf, 0); |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3411 | } |
| 3412 | } |
| 3413 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3414 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3415 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3416 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3417 | return LY_EVALID; |
| 3418 | } |
| 3419 | } |
| 3420 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3421 | return LY_SUCCESS; |
| 3422 | } |
| 3423 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3424 | /** |
| 3425 | * @brief Compile parsed leaf node information. |
| 3426 | * @param[in] ctx Compile context |
| 3427 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3428 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3429 | * is enriched with the leaf-specific information. |
| 3430 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3431 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3432 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3433 | 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] | 3434 | { |
| 3435 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3436 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3437 | unsigned int u; |
| 3438 | LY_ERR ret = LY_SUCCESS; |
| 3439 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3440 | 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] | 3441 | if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3442 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 3443 | ly_set_add(&ctx->xpath, leaf, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3444 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3445 | if (leaf_p->units) { |
| 3446 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3447 | leaf->flags |= LYS_SET_UNITS; |
| 3448 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3449 | |
| 3450 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3451 | leaf->dflt = (void*)leaf_p->dflt; |
| 3452 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
Radek Krejci | 812a5bf | 2019-09-09 09:18:05 +0200 | [diff] [blame] | 3453 | if (ret) { |
| 3454 | leaf->dflt = NULL; |
| 3455 | return ret; |
| 3456 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3457 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3458 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3459 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3460 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3461 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3462 | leaf->dflt->realtype = leaf->type; |
| 3463 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3464 | 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] | 3465 | 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] | 3466 | leaf->dflt->realtype->refcount++; |
| 3467 | if (err) { |
| 3468 | ly_err_print(err); |
| 3469 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3470 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3471 | ly_err_free(err); |
| 3472 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3473 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3474 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3475 | 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] | 3476 | |
| 3477 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3478 | ret = LY_SUCCESS; |
| 3479 | } |
Radek Krejci | b5bc93e | 2019-09-09 09:11:23 +0200 | [diff] [blame] | 3480 | LY_CHECK_RET(ret); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3481 | leaf->flags |= LYS_SET_DFLT; |
| 3482 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3483 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3484 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3485 | done: |
| 3486 | return ret; |
| 3487 | } |
| 3488 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3489 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3490 | * @brief Compile parsed leaf-list node information. |
| 3491 | * @param[in] ctx Compile context |
| 3492 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3493 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3494 | * is enriched with the leaf-list-specific information. |
| 3495 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3496 | */ |
| 3497 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3498 | 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] | 3499 | { |
| 3500 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3501 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3502 | LY_ARRAY_SIZE_TYPE u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3503 | LY_ERR ret = LY_SUCCESS; |
| 3504 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3505 | 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] | 3506 | if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3507 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 3508 | ly_set_add(&ctx->xpath, llist, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3509 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3510 | if (llist_p->units) { |
| 3511 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3512 | llist->flags |= LYS_SET_UNITS; |
| 3513 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3514 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3515 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3516 | llist->dflts = (void*)llist_p->dflts; |
| 3517 | 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] | 3518 | if (ret != LY_SUCCESS) { |
| 3519 | /* make sure the defaults are freed correctly */ |
| 3520 | if (llist_p->dflts) { |
| 3521 | llist->dflts = NULL; |
| 3522 | } |
| 3523 | return ret; |
| 3524 | } |
| 3525 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3526 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3527 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3528 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3529 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3530 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3531 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3532 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3533 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3534 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3535 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3536 | llist->dflts[u]->realtype = llist->type; |
| 3537 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3538 | 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] | 3539 | 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] | 3540 | llist->dflts[u]->realtype->refcount++; |
| 3541 | if (err) { |
| 3542 | ly_err_print(err); |
| 3543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3544 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3545 | ly_err_free(err); |
| 3546 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3547 | if (ret == LY_EINCOMPLETE) { |
| 3548 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3549 | 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] | 3550 | |
| 3551 | /* but in general result is so far ok */ |
| 3552 | ret = LY_SUCCESS; |
| 3553 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3554 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3555 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3556 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3557 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3558 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3559 | /* configuration data values must be unique - so check the default values */ |
| 3560 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3561 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3562 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3563 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3564 | 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] | 3565 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3566 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3567 | if (dynamic) { |
| 3568 | free((char*)val); |
| 3569 | } |
| 3570 | return LY_EVALID; |
| 3571 | } |
| 3572 | } |
| 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | /* 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] | 3577 | |
| 3578 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3579 | if (llist->min) { |
| 3580 | llist->flags |= LYS_MAND_TRUE; |
| 3581 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3582 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3583 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3584 | done: |
| 3585 | return ret; |
| 3586 | } |
| 3587 | |
| 3588 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3589 | * @brief Compile information about list's uniques. |
| 3590 | * @param[in] ctx Compile context. |
| 3591 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3592 | * @param[in] uniques Sized array list of unique statements. |
| 3593 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3594 | * @return LY_ERR value. |
| 3595 | */ |
| 3596 | static LY_ERR |
| 3597 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3598 | { |
| 3599 | LY_ERR ret = LY_SUCCESS; |
| 3600 | struct lysc_node_leaf **key, ***unique; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3601 | struct lysc_node *parent; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3602 | const char *keystr, *delim; |
| 3603 | size_t len; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3604 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3605 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3606 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3607 | |
| 3608 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3609 | config = -1; |
| 3610 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3611 | keystr = uniques[v]; |
| 3612 | while (keystr) { |
| 3613 | delim = strpbrk(keystr, " \t\n"); |
| 3614 | if (delim) { |
| 3615 | len = delim - keystr; |
| 3616 | while (isspace(*delim)) { |
| 3617 | ++delim; |
| 3618 | } |
| 3619 | } else { |
| 3620 | len = strlen(keystr); |
| 3621 | } |
| 3622 | |
| 3623 | /* unique node must be present */ |
| 3624 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3625 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3626 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3627 | if (ret != LY_SUCCESS) { |
| 3628 | if (ret == LY_EDENIED) { |
| 3629 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3630 | "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] | 3631 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3632 | } |
| 3633 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3634 | } else if (flags) { |
| 3635 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3636 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 3637 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3638 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3639 | } |
| 3640 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3641 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3642 | /* all referenced leafs must be of the same config type */ |
| 3643 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3644 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3645 | "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3646 | return LY_EVALID; |
| 3647 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3648 | config = 1; |
| 3649 | } else { /* LYS_CONFIG_R */ |
| 3650 | config = 0; |
| 3651 | } |
| 3652 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 3653 | /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */ |
| 3654 | for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) { |
| 3655 | if (parent->nodetype == LYS_LIST) { |
| 3656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3657 | "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name); |
| 3658 | return LY_EVALID; |
| 3659 | } |
| 3660 | } |
| 3661 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3662 | /* check status */ |
| 3663 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3664 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3665 | |
| 3666 | /* mark leaf as unique */ |
| 3667 | (*key)->flags |= LYS_UNIQUE; |
| 3668 | |
| 3669 | /* next unique value in line */ |
| 3670 | keystr = delim; |
| 3671 | } |
| 3672 | /* next unique definition */ |
| 3673 | } |
| 3674 | |
| 3675 | return LY_SUCCESS; |
| 3676 | } |
| 3677 | |
| 3678 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3679 | * @brief Compile parsed list node information. |
| 3680 | * @param[in] ctx Compile context |
| 3681 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3682 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3683 | * is enriched with the list-specific information. |
| 3684 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3685 | */ |
| 3686 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3687 | 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] | 3688 | { |
| 3689 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3690 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3691 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3692 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3693 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3694 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3695 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3696 | LY_ERR ret = LY_SUCCESS; |
| 3697 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3698 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3699 | if (list->min) { |
| 3700 | list->flags |= LYS_MAND_TRUE; |
| 3701 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3702 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3703 | |
| 3704 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3705 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3706 | } |
| 3707 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 3708 | 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] | 3709 | if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 3710 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 3711 | ly_set_add(&ctx->xpath, list, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 3712 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3713 | |
| 3714 | /* keys */ |
| 3715 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3716 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3717 | return LY_EVALID; |
| 3718 | } |
| 3719 | |
| 3720 | /* find all the keys (must be direct children) */ |
| 3721 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3722 | if (!keystr) { |
| 3723 | /* keyless list */ |
| 3724 | list->flags |= LYS_KEYLESS; |
| 3725 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3726 | while (keystr) { |
| 3727 | delim = strpbrk(keystr, " \t\n"); |
| 3728 | if (delim) { |
| 3729 | len = delim - keystr; |
| 3730 | while (isspace(*delim)) { |
| 3731 | ++delim; |
| 3732 | } |
| 3733 | } else { |
| 3734 | len = strlen(keystr); |
| 3735 | } |
| 3736 | |
| 3737 | /* key node must be present */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3738 | 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] | 3739 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3740 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3741 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3742 | return LY_EVALID; |
| 3743 | } |
| 3744 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3745 | if (key->flags & LYS_KEY) { |
| 3746 | /* the node was already marked as a key */ |
| 3747 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3748 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3749 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3750 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3751 | |
| 3752 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3753 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3754 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3755 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3756 | return LY_EVALID; |
| 3757 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3758 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3759 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3760 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3761 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3762 | "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] | 3763 | return LY_EVALID; |
| 3764 | } |
| 3765 | } else { |
| 3766 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3767 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3768 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3769 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3770 | return LY_EVALID; |
| 3771 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3772 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3774 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3775 | return LY_EVALID; |
| 3776 | } |
| 3777 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3778 | |
| 3779 | /* check status */ |
| 3780 | 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] | 3781 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3782 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3783 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3784 | if (key->dflt) { |
| 3785 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3786 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3787 | free(key->dflt); |
| 3788 | key->dflt = NULL; |
| 3789 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3790 | } |
| 3791 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3792 | key->flags |= LYS_KEY; |
| 3793 | |
| 3794 | /* move it to the correct position */ |
| 3795 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3796 | /* fix links in closest previous siblings of the key */ |
| 3797 | if (key->next) { |
| 3798 | key->next->prev = key->prev; |
| 3799 | } else { |
| 3800 | /* last child */ |
| 3801 | list->child->prev = key->prev; |
| 3802 | } |
| 3803 | if (key->prev->next) { |
| 3804 | key->prev->next = key->next; |
| 3805 | } |
| 3806 | /* fix links in the key */ |
| 3807 | if (prev_key) { |
| 3808 | key->prev = (struct lysc_node*)prev_key; |
| 3809 | key->next = prev_key->next; |
| 3810 | } else { |
| 3811 | key->prev = list->child->prev; |
| 3812 | key->next = list->child; |
| 3813 | } |
| 3814 | /* fix links in closes future siblings of the key */ |
| 3815 | if (prev_key) { |
| 3816 | if (prev_key->next) { |
| 3817 | prev_key->next->prev = (struct lysc_node*)key; |
| 3818 | } else { |
| 3819 | list->child->prev = (struct lysc_node*)key; |
| 3820 | } |
| 3821 | prev_key->next = (struct lysc_node*)key; |
| 3822 | } else { |
| 3823 | list->child->prev = (struct lysc_node*)key; |
| 3824 | } |
| 3825 | /* fix links in parent */ |
| 3826 | if (!key->prev->next) { |
| 3827 | list->child = (struct lysc_node*)key; |
| 3828 | } |
| 3829 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3830 | |
| 3831 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3832 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3833 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3834 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | /* uniques */ |
| 3838 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3839 | 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] | 3840 | } |
| 3841 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3842 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 3843 | 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] | 3844 | |
| 3845 | done: |
| 3846 | return ret; |
| 3847 | } |
| 3848 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 3849 | /** |
| 3850 | * @brief Do some checks and set the default choice's case. |
| 3851 | * |
| 3852 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 3853 | * |
| 3854 | * @param[in] ctx Compile context. |
| 3855 | * @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, |
| 3856 | * not the reference to the imported module. |
| 3857 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 3858 | * @return LY_ERR value. |
| 3859 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3860 | static LY_ERR |
| 3861 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 3862 | { |
| 3863 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 3864 | const char *prefix = NULL, *name; |
| 3865 | size_t prefix_len = 0; |
| 3866 | |
| 3867 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3868 | name = strchr(dflt, ':'); |
| 3869 | if (name) { |
| 3870 | prefix = dflt; |
| 3871 | prefix_len = name - prefix; |
| 3872 | ++name; |
| 3873 | } else { |
| 3874 | name = dflt; |
| 3875 | } |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 3876 | if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3877 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 3878 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3879 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 3880 | prefix_len, prefix); |
| 3881 | return LY_EVALID; |
| 3882 | } |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3883 | 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] | 3884 | if (!ch->dflt) { |
| 3885 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3886 | "Default case \"%s\" not found.", dflt); |
| 3887 | return LY_EVALID; |
| 3888 | } |
| 3889 | /* no mandatory nodes directly under the default case */ |
| 3890 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3891 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 3892 | break; |
| 3893 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3894 | if (iter->flags & LYS_MAND_TRUE) { |
| 3895 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3896 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 3897 | return LY_EVALID; |
| 3898 | } |
| 3899 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3900 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3901 | return LY_SUCCESS; |
| 3902 | } |
| 3903 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3904 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3905 | 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] | 3906 | { |
| 3907 | struct lys_module *mod; |
| 3908 | const char *prefix = NULL, *name; |
| 3909 | size_t prefix_len = 0; |
| 3910 | struct lysc_node_case *cs; |
| 3911 | struct lysc_node *node; |
| 3912 | |
| 3913 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 3914 | name = strchr(dflt, ':'); |
| 3915 | if (name) { |
| 3916 | prefix = dflt; |
| 3917 | prefix_len = name - prefix; |
| 3918 | ++name; |
| 3919 | } else { |
| 3920 | name = dflt; |
| 3921 | } |
| 3922 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 3923 | if (prefix) { |
| 3924 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 3925 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3926 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 3927 | "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] | 3928 | return LY_EVALID; |
| 3929 | } |
| 3930 | } else { |
| 3931 | mod = ctx->mod; |
| 3932 | } |
| 3933 | /* get the default case */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 3934 | 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] | 3935 | if (!cs) { |
| 3936 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3937 | "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] | 3938 | return LY_EVALID; |
| 3939 | } |
| 3940 | |
| 3941 | /* check that there is no mandatory node */ |
| 3942 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 3943 | if (node->parent != (struct lysc_node*)cs) { |
| 3944 | break; |
| 3945 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3946 | if (node->flags & LYS_MAND_TRUE) { |
| 3947 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3948 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 3949 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3950 | return LY_EVALID; |
| 3951 | } |
| 3952 | } |
| 3953 | |
| 3954 | /* set the default case in choice */ |
| 3955 | ch->dflt = cs; |
| 3956 | cs->flags |= LYS_SET_DFLT; |
| 3957 | |
| 3958 | return LY_SUCCESS; |
| 3959 | } |
| 3960 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3961 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3962 | * @brief Compile parsed choice node information. |
| 3963 | * @param[in] ctx Compile context |
| 3964 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3965 | * @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] | 3966 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3967 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3968 | */ |
| 3969 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3970 | 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] | 3971 | { |
| 3972 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 3973 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 3974 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3975 | LY_ERR ret = LY_SUCCESS; |
| 3976 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3977 | LY_LIST_FOR(ch_p->child, child_p) { |
| 3978 | if (child_p->nodetype == LYS_CASE) { |
| 3979 | 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] | 3980 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3981 | } |
| 3982 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3983 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3984 | } |
| 3985 | } |
| 3986 | |
| 3987 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 3988 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3989 | 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] | 3990 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3991 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3992 | return ret; |
| 3993 | } |
| 3994 | |
| 3995 | /** |
| 3996 | * @brief Compile parsed anydata or anyxml node information. |
| 3997 | * @param[in] ctx Compile context |
| 3998 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3999 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4000 | * is enriched with the any-specific information. |
| 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_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] | 4005 | { |
| 4006 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4007 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4008 | unsigned int u; |
| 4009 | LY_ERR ret = LY_SUCCESS; |
| 4010 | |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4011 | 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] | 4012 | if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) { |
| 4013 | /* do not check "must" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 4014 | ly_set_add(&ctx->xpath, any, 0); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 4015 | } |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4016 | |
| 4017 | if (any->flags & LYS_CONFIG_W) { |
| 4018 | 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] | 4019 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4020 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4021 | done: |
| 4022 | return ret; |
| 4023 | } |
| 4024 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4025 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4026 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4027 | * |
| 4028 | * @param[in] ctx Compile context |
| 4029 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4030 | * the choice itself is expected instead of a specific case node. |
| 4031 | * @param[in] node Schema node to connect into the list. |
| 4032 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 4033 | * 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] | 4034 | */ |
| 4035 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4036 | 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] | 4037 | { |
| 4038 | struct lysc_node **children; |
| 4039 | |
| 4040 | if (node->nodetype == LYS_CASE) { |
| 4041 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4042 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4043 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4044 | } |
| 4045 | if (children) { |
| 4046 | if (!(*children)) { |
| 4047 | /* first child */ |
| 4048 | *children = node; |
| 4049 | } else if (*children != node) { |
| 4050 | /* by the condition in previous branch we cover the choice/case children |
| 4051 | * - the children list is shared by the choice and the the first case, in addition |
| 4052 | * the first child of each case must be referenced from the case node. So the node is |
| 4053 | * actually always already inserted in case it is the first children - so here such |
| 4054 | * a situation actually corresponds to the first branch */ |
| 4055 | /* insert at the end of the parent's children list */ |
| 4056 | (*children)->prev->next = node; |
| 4057 | node->prev = (*children)->prev; |
| 4058 | (*children)->prev = node; |
| 4059 | |
| 4060 | /* check the name uniqueness */ |
| 4061 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4062 | lysc_node_notifs(parent), node->name, node)) { |
| 4063 | return LY_EEXIST; |
| 4064 | } |
| 4065 | } |
| 4066 | } |
| 4067 | return LY_SUCCESS; |
| 4068 | } |
| 4069 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4070 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4071 | * @brief Prepare the case structure in choice node for the new data node. |
| 4072 | * |
| 4073 | * 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 |
| 4074 | * created in the choice when the first child was processed. |
| 4075 | * |
| 4076 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4077 | * @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, |
| 4078 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4079 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4080 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4081 | * @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, |
| 4082 | * 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] | 4083 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4084 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4085 | 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] | 4086 | { |
| 4087 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4088 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4089 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4090 | unsigned int u; |
| 4091 | LY_ERR ret; |
| 4092 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4093 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4094 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4095 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4096 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4097 | return NULL; \ |
| 4098 | } \ |
| 4099 | } |
| 4100 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4101 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4102 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4103 | |
| 4104 | /* we have to add an implicit case node into the parent choice */ |
| 4105 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4106 | DUP_STRING(ctx->ctx, child->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4107 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4108 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4109 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4110 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4111 | /* the case is already present since the child is not its first children */ |
| 4112 | return (struct lysc_node_case*)ch->cases->prev; |
| 4113 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4114 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4115 | |
| 4116 | /* explicit parent case is not present (this is its first child) */ |
| 4117 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4118 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4119 | cs->parent = (struct lysc_node*)ch; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4120 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4121 | cs->sp = node_p; |
| 4122 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4123 | /* 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] | 4124 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4125 | |
| 4126 | if (node_p->when) { |
| 4127 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4128 | 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] | 4129 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4130 | |
| 4131 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4132 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 4133 | ly_set_add(&ctx->xpath, cs, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4134 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4135 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4136 | 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] | 4137 | } else { |
| 4138 | LOGINT(ctx->ctx); |
| 4139 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4140 | } |
| 4141 | cs->module = ctx->mod; |
| 4142 | cs->prev = (struct lysc_node*)cs; |
| 4143 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4144 | 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] | 4145 | cs->child = child; |
| 4146 | |
| 4147 | return cs; |
| 4148 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4149 | if (cs) { |
| 4150 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4151 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4152 | return NULL; |
| 4153 | |
| 4154 | #undef UNIQUE_CHECK |
| 4155 | } |
| 4156 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4157 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4158 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4159 | * |
| 4160 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4161 | * @param[in] node Target node where the config is supposed to be changed. |
| 4162 | * @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] | 4163 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4164 | * 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] | 4165 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4166 | * @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] | 4167 | * @return LY_ERR value. |
| 4168 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4169 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4170 | 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] | 4171 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4172 | { |
| 4173 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4174 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4175 | |
| 4176 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4177 | /* nothing to do */ |
| 4178 | return LY_SUCCESS; |
| 4179 | } |
| 4180 | |
| 4181 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4182 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4183 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4184 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4185 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4186 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4187 | return LY_EVALID; |
| 4188 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4189 | node->flags |= LYS_SET_CONFIG; |
| 4190 | } else { |
| 4191 | if (node->flags & LYS_SET_CONFIG) { |
| 4192 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4193 | /* setting config flags, but have node with explicit config true */ |
| 4194 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4195 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4196 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4197 | return LY_EVALID; |
| 4198 | } |
| 4199 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4200 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4201 | return LY_SUCCESS; |
| 4202 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4203 | } |
| 4204 | node->flags &= ~LYS_CONFIG_MASK; |
| 4205 | node->flags |= config; |
| 4206 | |
| 4207 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4208 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4209 | 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] | 4210 | } |
| 4211 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4212 | return LY_SUCCESS; |
| 4213 | } |
| 4214 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4215 | /** |
| 4216 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4217 | * |
| 4218 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4219 | * the flag to such parents from a mandatory children. |
| 4220 | * |
| 4221 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4222 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4223 | * (mandatory children was removed). |
| 4224 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4225 | void |
| 4226 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4227 | { |
| 4228 | struct lysc_node *iter; |
| 4229 | |
| 4230 | if (add) { /* set flag */ |
| 4231 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4232 | parent = parent->parent) { |
| 4233 | parent->flags |= LYS_MAND_TRUE; |
| 4234 | } |
| 4235 | } else { /* unset flag */ |
| 4236 | 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] | 4237 | 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] | 4238 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4239 | /* there is another mandatory node */ |
| 4240 | return; |
| 4241 | } |
| 4242 | } |
| 4243 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4244 | parent->flags &= ~LYS_MAND_TRUE; |
| 4245 | } |
| 4246 | } |
| 4247 | } |
| 4248 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4249 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4250 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4251 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4252 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4253 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4254 | */ |
| 4255 | static void |
| 4256 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4257 | { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4258 | LY_ARRAY_SIZE_TYPE v; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4259 | size_t len; |
| 4260 | |
| 4261 | len = strlen(aug_p->nodeid); |
| 4262 | LY_ARRAY_FOR(result, v) { |
| 4263 | if (strlen(result[v]->nodeid) <= len) { |
| 4264 | continue; |
| 4265 | } |
| 4266 | if (v < LY_ARRAY_SIZE(result)) { |
| 4267 | /* move the rest of array */ |
| 4268 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4269 | break; |
| 4270 | } |
| 4271 | } |
| 4272 | result[v] = aug_p; |
| 4273 | LY_ARRAY_INCREMENT(result); |
| 4274 | } |
| 4275 | |
| 4276 | /** |
| 4277 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4278 | * |
| 4279 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4280 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4281 | * |
| 4282 | * @param[in] ctx Compile context. |
| 4283 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4284 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4285 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4286 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4287 | * @return LY_ERR value. |
| 4288 | */ |
| 4289 | LY_ERR |
| 4290 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4291 | { |
| 4292 | struct lysp_augment **result = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4293 | LY_ARRAY_SIZE_TYPE u, v, count = 0; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4294 | |
| 4295 | assert(augments); |
| 4296 | |
| 4297 | /* get count of the augments in module and all its submodules */ |
| 4298 | if (aug_p) { |
| 4299 | count += LY_ARRAY_SIZE(aug_p); |
| 4300 | } |
| 4301 | LY_ARRAY_FOR(inc_p, u) { |
| 4302 | if (inc_p[u].submodule->augments) { |
| 4303 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | if (!count) { |
| 4308 | *augments = NULL; |
| 4309 | return LY_SUCCESS; |
| 4310 | } |
| 4311 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4312 | |
| 4313 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4314 | * together, so there can be even /z/y betwwen them. */ |
| 4315 | LY_ARRAY_FOR(aug_p, u) { |
| 4316 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4317 | } |
| 4318 | LY_ARRAY_FOR(inc_p, u) { |
| 4319 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4320 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4321 | } |
| 4322 | } |
| 4323 | |
| 4324 | *augments = result; |
| 4325 | return LY_SUCCESS; |
| 4326 | } |
| 4327 | |
| 4328 | /** |
| 4329 | * @brief Compile the parsed augment connecting it into its target. |
| 4330 | * |
| 4331 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4332 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4333 | * are already implemented and compiled. |
| 4334 | * |
| 4335 | * @param[in] ctx Compile context. |
| 4336 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4337 | * @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 |
| 4338 | * children in case of the augmenting uses data. |
| 4339 | * @return LY_SUCCESS on success. |
| 4340 | * @return LY_EVALID on failure. |
| 4341 | */ |
| 4342 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4343 | 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] | 4344 | { |
| 4345 | LY_ERR ret = LY_SUCCESS; |
| 4346 | struct lysp_node *node_p, *case_node_p; |
| 4347 | struct lysc_node *target; /* target target of the augment */ |
| 4348 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4349 | struct lysc_when **when, *when_shared; |
| 4350 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4351 | uint16_t flags = 0; |
| 4352 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4353 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4354 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4355 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4356 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4357 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4358 | 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] | 4359 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4360 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4361 | if (ret != LY_SUCCESS) { |
| 4362 | if (ret == LY_EDENIED) { |
| 4363 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4364 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4365 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4366 | } |
| 4367 | return LY_EVALID; |
| 4368 | } |
| 4369 | |
| 4370 | /* check for mandatory nodes |
| 4371 | * - new cases augmenting some choice can have mandatory nodes |
| 4372 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4373 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4374 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4375 | allow_mandatory = 1; |
| 4376 | } |
| 4377 | |
| 4378 | when_shared = NULL; |
| 4379 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4380 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4381 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 4382 | && !((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] | 4383 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4384 | && !(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] | 4385 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4386 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4387 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4388 | return LY_EVALID; |
| 4389 | } |
| 4390 | |
| 4391 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4392 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4393 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4394 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4395 | } else { |
| 4396 | 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] | 4397 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4398 | } |
| 4399 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4400 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4401 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4402 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4403 | * 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] | 4404 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4405 | /* 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] | 4406 | 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] | 4407 | } else if (target->nodetype == LYS_CHOICE) { |
| 4408 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4409 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4410 | } else { |
| 4411 | /* the compiled node is the last child of the target */ |
Radek Krejci | 7c7783d | 2020-04-08 15:34:39 +0200 | [diff] [blame] | 4412 | node = (struct lysc_node*)lysc_node_children(target, flags); |
| 4413 | if (!node) { |
| 4414 | /* there is no data children (compiled nodes is e.g. notification or action or nothing) */ |
| 4415 | break; |
| 4416 | } |
| 4417 | node = node->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4418 | } |
| 4419 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4420 | 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] | 4421 | node->flags &= ~LYS_MAND_TRUE; |
| 4422 | lys_compile_mandatory_parents(target, 0); |
| 4423 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4424 | "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] | 4425 | return LY_EVALID; |
| 4426 | } |
| 4427 | |
| 4428 | /* pass augment's when to all the children */ |
| 4429 | if (aug_p->when) { |
| 4430 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4431 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4432 | 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] | 4433 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4434 | |
| 4435 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4436 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 4437 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4438 | } |
| 4439 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4440 | when_shared = *when; |
| 4441 | } else { |
| 4442 | ++when_shared->refcount; |
| 4443 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4444 | |
| 4445 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4446 | /* 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^] | 4447 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4448 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4449 | } |
| 4450 | } |
| 4451 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4452 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4453 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4454 | switch (target->nodetype) { |
| 4455 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4456 | 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] | 4457 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4458 | 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] | 4459 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4460 | break; |
| 4461 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4462 | 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] | 4463 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4464 | 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] | 4465 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4466 | break; |
| 4467 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4468 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4469 | if (aug_p->actions) { |
| 4470 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4471 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4472 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4473 | return LY_EVALID; |
| 4474 | } |
| 4475 | if (aug_p->notifs) { |
| 4476 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4477 | "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] | 4478 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4479 | return LY_EVALID; |
| 4480 | } |
| 4481 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4482 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4483 | lysc_update_path(ctx, NULL, NULL); |
| 4484 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4485 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4486 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4487 | return ret; |
| 4488 | } |
| 4489 | |
| 4490 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4491 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4492 | * |
| 4493 | * @param[in] ctx Compile context. |
| 4494 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4495 | * @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] | 4496 | * @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] | 4497 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4498 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4499 | * 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] | 4500 | * @return LY_ERR value. |
| 4501 | */ |
| 4502 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4503 | 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] | 4504 | { |
| 4505 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4506 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4507 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4508 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4509 | return LY_EVALID; |
| 4510 | } |
| 4511 | |
| 4512 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4513 | /* check if node has default value */ |
| 4514 | if (node->nodetype & LYS_LEAF) { |
| 4515 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4516 | if (refine_flag) { |
| 4517 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4518 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4519 | return LY_EVALID; |
| 4520 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4521 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4522 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4523 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4524 | |
| 4525 | /* update the list of incomplete default values if needed */ |
| 4526 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4527 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4528 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4529 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4530 | free(leaf->dflt); |
| 4531 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4532 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4533 | } |
| 4534 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4535 | if (refine_flag) { |
| 4536 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4537 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4538 | return LY_EVALID; |
| 4539 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4540 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4541 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4542 | 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] | 4543 | return LY_EVALID; |
| 4544 | } |
| 4545 | |
| 4546 | node->flags &= ~LYS_MAND_FALSE; |
| 4547 | node->flags |= LYS_MAND_TRUE; |
| 4548 | lys_compile_mandatory_parents(node->parent, 1); |
| 4549 | } else { |
| 4550 | /* make mandatory false */ |
| 4551 | node->flags &= ~LYS_MAND_TRUE; |
| 4552 | node->flags |= LYS_MAND_FALSE; |
| 4553 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4554 | 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] | 4555 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4556 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4557 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4558 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4559 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4560 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4561 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4562 | } |
| 4563 | } |
| 4564 | return LY_SUCCESS; |
| 4565 | } |
| 4566 | |
| 4567 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4568 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4569 | * If present, also apply uses's modificators. |
| 4570 | * |
| 4571 | * @param[in] ctx Compile context |
| 4572 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4573 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4574 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4575 | * the compile context. |
| 4576 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4577 | */ |
| 4578 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4579 | 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] | 4580 | { |
| 4581 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4582 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4583 | /* 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] | 4584 | struct lysc_node_container context_node_fake = |
| 4585 | {.nodetype = LYS_CONTAINER, |
| 4586 | .module = ctx->mod, |
| 4587 | .flags = parent ? parent->flags : 0, |
| 4588 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4589 | .prev = (struct lysc_node*)&context_node_fake, |
| 4590 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4591 | struct lysp_grp *grp = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4592 | LY_ARRAY_SIZE_TYPE u, v; |
| 4593 | uint32_t grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4594 | int found; |
| 4595 | const char *id, *name, *prefix; |
| 4596 | size_t prefix_len, name_len; |
| 4597 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4598 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4599 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4600 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4601 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4602 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4603 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4604 | struct lysp_augment **augments = NULL; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4605 | LY_ARRAY_SIZE_TYPE actions_index = 0, notifs_index = 0; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4606 | struct lysc_notif **notifs = NULL; |
| 4607 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4608 | |
| 4609 | /* search for the grouping definition */ |
| 4610 | found = 0; |
| 4611 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4612 | 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] | 4613 | if (prefix) { |
| 4614 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4615 | if (!mod) { |
| 4616 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4617 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4618 | return LY_EVALID; |
| 4619 | } |
| 4620 | } else { |
| 4621 | mod = ctx->mod_def; |
| 4622 | } |
| 4623 | if (mod == ctx->mod_def) { |
| 4624 | 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] | 4625 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4626 | LY_ARRAY_FOR(grp, u) { |
| 4627 | if (!strcmp(grp[u].name, name)) { |
| 4628 | grp = &grp[u]; |
| 4629 | found = 1; |
| 4630 | break; |
| 4631 | } |
| 4632 | } |
| 4633 | } |
| 4634 | } |
| 4635 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4636 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4637 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4638 | if (grp) { |
| 4639 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4640 | if (!strcmp(grp[u].name, name)) { |
| 4641 | grp = &grp[u]; |
| 4642 | found = 1; |
| 4643 | } |
| 4644 | } |
| 4645 | } |
| 4646 | if (!found && mod->parsed->includes) { |
| 4647 | /* ... and all the submodules */ |
| 4648 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4649 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4650 | if (grp) { |
| 4651 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4652 | if (!strcmp(grp[v].name, name)) { |
| 4653 | grp = &grp[v]; |
| 4654 | found = 1; |
| 4655 | } |
| 4656 | } |
| 4657 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4658 | } |
| 4659 | } |
| 4660 | } |
| 4661 | if (!found) { |
| 4662 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4663 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4664 | return LY_EVALID; |
| 4665 | } |
| 4666 | |
| 4667 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4668 | grp_stack_count = ctx->groupings.count; |
| 4669 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4670 | if (grp_stack_count == ctx->groupings.count) { |
| 4671 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4672 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4673 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4674 | return LY_EVALID; |
| 4675 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4676 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4677 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4678 | grp->flags |= LYS_USED_GRP; |
| 4679 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4680 | |
| 4681 | /* switch context's mod_def */ |
| 4682 | mod_old = ctx->mod_def; |
| 4683 | ctx->mod_def = mod; |
| 4684 | |
| 4685 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4686 | 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] | 4687 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4688 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4689 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4690 | /* 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] | 4691 | 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] | 4692 | |
| 4693 | /* some preparation for applying refines */ |
| 4694 | if (grp->data == node_p) { |
| 4695 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4696 | if (parent) { |
| 4697 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4698 | } else if (ctx->mod->compiled->data) { |
| 4699 | child = ctx->mod->compiled->data; |
| 4700 | } else { |
| 4701 | child = NULL; |
| 4702 | } |
| 4703 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4704 | } |
| 4705 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4706 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4707 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4708 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4709 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4710 | /* 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] | 4711 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4712 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4713 | if (!when_shared) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4714 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup); |
| 4715 | |
| 4716 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4717 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 4718 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 4719 | } |
| 4720 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4721 | when_shared = *when; |
| 4722 | } else { |
| 4723 | ++when_shared->refcount; |
| 4724 | (*when) = when_shared; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4725 | |
| 4726 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4727 | /* 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^] | 4728 | ly_set_add(&ctx->xpath, child, 0); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4729 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4730 | } |
| 4731 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4732 | } |
| 4733 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4734 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4735 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4736 | /* 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] | 4737 | 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] | 4738 | } |
| 4739 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4740 | /* compile actions */ |
| 4741 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4742 | if (actions) { |
| 4743 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4744 | 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] | 4745 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4746 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4747 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4748 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4749 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | /* compile notifications */ |
| 4754 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4755 | if (notifs) { |
| 4756 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4757 | 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] | 4758 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4759 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4760 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4761 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4762 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4763 | } |
| 4764 | } |
| 4765 | |
| 4766 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4767 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4768 | 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] | 4769 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4770 | 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] | 4771 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4772 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4773 | /* reload previous context's mod_def */ |
| 4774 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4775 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4776 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4777 | /* apply refine */ |
| 4778 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4779 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4780 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4781 | 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] | 4782 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4783 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4784 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4785 | |
| 4786 | /* default value */ |
| 4787 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4788 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4789 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 4790 | "Invalid refine of default - %s cannot hold %"LY_PRI_ARRAY_SIZE_TYPE" default values.", |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4791 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4792 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4793 | } |
| 4794 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4795 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4796 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4797 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4798 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4799 | } |
| 4800 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4801 | struct ly_err_item *err = NULL; |
| 4802 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4803 | if (leaf->dflt) { |
| 4804 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4805 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4806 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4807 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4808 | } else { |
| 4809 | /* prepare a new one */ |
| 4810 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4811 | leaf->dflt->realtype = leaf->type; |
| 4812 | } |
| 4813 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4814 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4815 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4816 | 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] | 4817 | 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] | 4818 | leaf->dflt->realtype->refcount++; |
| 4819 | if (err) { |
| 4820 | ly_err_print(err); |
| 4821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4822 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4823 | ly_err_free(err); |
| 4824 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4825 | if (rc == LY_EINCOMPLETE) { |
| 4826 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4827 | 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] | 4828 | |
| 4829 | /* but in general result is so far ok */ |
| 4830 | rc = LY_SUCCESS; |
| 4831 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4832 | LY_CHECK_GOTO(rc, cleanup); |
| 4833 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4834 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4835 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4836 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4837 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4838 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4839 | "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] | 4840 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4841 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4842 | |
| 4843 | /* remove previous set of default values */ |
| 4844 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4845 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4846 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 4847 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 4848 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4849 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4850 | LY_ARRAY_FREE(llist->dflts); |
| 4851 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4852 | LY_ARRAY_FREE(llist->dflts_mods); |
| 4853 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4854 | |
| 4855 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4856 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4857 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4858 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4859 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4860 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 4861 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4862 | LY_ARRAY_INCREMENT(llist->dflts); |
| 4863 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 4864 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4865 | 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] | 4866 | 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] | 4867 | 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] | 4868 | llist->dflts[u]->realtype->refcount++; |
| 4869 | if (err) { |
| 4870 | ly_err_print(err); |
| 4871 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4872 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 4873 | ly_err_free(err); |
| 4874 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4875 | if (rc == LY_EINCOMPLETE) { |
| 4876 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4877 | 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] | 4878 | |
| 4879 | /* but in general result is so far ok */ |
| 4880 | rc = LY_SUCCESS; |
| 4881 | } |
| 4882 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4883 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4884 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4885 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4886 | if (((struct lysc_node_choice*)node)->dflt) { |
| 4887 | /* unset LYS_SET_DFLT from the current default case */ |
| 4888 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 4889 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4890 | 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] | 4891 | } |
| 4892 | } |
| 4893 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4894 | /* description */ |
| 4895 | if (rfn->dsc) { |
| 4896 | FREE_STRING(ctx->ctx, node->dsc); |
| 4897 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 4898 | } |
| 4899 | |
| 4900 | /* reference */ |
| 4901 | if (rfn->ref) { |
| 4902 | FREE_STRING(ctx->ctx, node->ref); |
| 4903 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 4904 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4905 | |
| 4906 | /* config */ |
| 4907 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4908 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4909 | 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] | 4910 | } else { |
| 4911 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 4912 | flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4913 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | /* mandatory */ |
| 4917 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4918 | 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] | 4919 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4920 | |
| 4921 | /* presence */ |
| 4922 | if (rfn->presence) { |
| 4923 | if (node->nodetype != LYS_CONTAINER) { |
| 4924 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4925 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 4926 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4927 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 4928 | } |
| 4929 | node->flags |= LYS_PRESENCE; |
| 4930 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4931 | |
| 4932 | /* must */ |
| 4933 | if (rfn->musts) { |
| 4934 | switch (node->nodetype) { |
| 4935 | case LYS_LEAF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4936 | 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] | 4937 | break; |
| 4938 | case LYS_LEAFLIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4939 | 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] | 4940 | break; |
| 4941 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4942 | 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] | 4943 | break; |
| 4944 | case LYS_CONTAINER: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4945 | 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] | 4946 | break; |
| 4947 | case LYS_ANYXML: |
| 4948 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 4949 | 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] | 4950 | break; |
| 4951 | default: |
| 4952 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4953 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 4954 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4955 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4956 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 4957 | ly_set_add(&ctx->xpath, node, 0); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 4958 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4959 | |
| 4960 | /* min/max-elements */ |
| 4961 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 4962 | switch (node->nodetype) { |
| 4963 | case LYS_LEAFLIST: |
| 4964 | if (rfn->flags & LYS_SET_MAX) { |
| 4965 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4966 | } |
| 4967 | if (rfn->flags & LYS_SET_MIN) { |
| 4968 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4969 | if (rfn->min) { |
| 4970 | node->flags |= LYS_MAND_TRUE; |
| 4971 | lys_compile_mandatory_parents(node->parent, 1); |
| 4972 | } else { |
| 4973 | node->flags &= ~LYS_MAND_TRUE; |
| 4974 | lys_compile_mandatory_parents(node->parent, 0); |
| 4975 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4976 | } |
| 4977 | break; |
| 4978 | case LYS_LIST: |
| 4979 | if (rfn->flags & LYS_SET_MAX) { |
| 4980 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 4981 | } |
| 4982 | if (rfn->flags & LYS_SET_MIN) { |
| 4983 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4984 | if (rfn->min) { |
| 4985 | node->flags |= LYS_MAND_TRUE; |
| 4986 | lys_compile_mandatory_parents(node->parent, 1); |
| 4987 | } else { |
| 4988 | node->flags &= ~LYS_MAND_TRUE; |
| 4989 | lys_compile_mandatory_parents(node->parent, 0); |
| 4990 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4991 | } |
| 4992 | break; |
| 4993 | default: |
| 4994 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4995 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 4996 | (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] | 4997 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 4998 | } |
| 4999 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5000 | |
| 5001 | /* if-feature */ |
| 5002 | if (rfn->iffeatures) { |
| 5003 | /* 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] | 5004 | 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] | 5005 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5006 | |
| 5007 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5008 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5009 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5010 | /* 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] | 5011 | for (uint32_t i = 0; i < refined.count; ++i) { |
| 5012 | node = (struct lysc_node*)refined.objs[i]; |
| 5013 | rfn = &uses_p->refines[i]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5014 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5015 | |
| 5016 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5017 | if ((node->flags & LYS_MAND_TRUE) && |
| 5018 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5019 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5020 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5021 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5022 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5026 | if (node->nodetype == LYS_LIST) { |
| 5027 | min = ((struct lysc_node_list*)node)->min; |
| 5028 | max = ((struct lysc_node_list*)node)->max; |
| 5029 | } else { |
| 5030 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5031 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5032 | } |
| 5033 | if (min > max) { |
| 5034 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5035 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5036 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5037 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5038 | } |
| 5039 | } |
| 5040 | } |
| 5041 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5042 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5043 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5044 | |
| 5045 | cleanup: |
| 5046 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5047 | if (context_node_fake.child) { |
| 5048 | context_node_fake.child->prev = child; |
| 5049 | } |
| 5050 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5051 | child->parent = parent; |
| 5052 | } |
| 5053 | |
| 5054 | if (uses_p->augments || uses_p->refines) { |
| 5055 | /* 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] | 5056 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5057 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5058 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5059 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5060 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5061 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5062 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5063 | } |
| 5064 | } |
| 5065 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5066 | /* reload previous context's mod_def */ |
| 5067 | ctx->mod_def = mod_old; |
| 5068 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5069 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5070 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5071 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5072 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5073 | |
| 5074 | return ret; |
| 5075 | } |
| 5076 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5077 | static int |
| 5078 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5079 | { |
| 5080 | struct lysp_node *iter; |
| 5081 | int len = 0; |
| 5082 | |
| 5083 | *path = NULL; |
| 5084 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5085 | char *s = *path; |
| 5086 | char *id; |
| 5087 | |
| 5088 | switch (iter->nodetype) { |
| 5089 | case LYS_USES: |
| 5090 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5091 | break; |
| 5092 | case LYS_GROUPING: |
| 5093 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5094 | break; |
| 5095 | case LYS_AUGMENT: |
| 5096 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5097 | break; |
| 5098 | default: |
| 5099 | id = strdup(iter->name); |
| 5100 | break; |
| 5101 | } |
| 5102 | |
| 5103 | if (!iter->parent) { |
| 5104 | /* print prefix */ |
| 5105 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5106 | } else { |
| 5107 | /* prefix is the same as in parent */ |
| 5108 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5109 | } |
| 5110 | free(s); |
| 5111 | free(id); |
| 5112 | } |
| 5113 | |
| 5114 | if (len < 0) { |
| 5115 | free(*path); |
| 5116 | *path = NULL; |
| 5117 | } else if (len == 0) { |
| 5118 | *path = strdup("/"); |
| 5119 | len = 1; |
| 5120 | } |
| 5121 | return len; |
| 5122 | } |
| 5123 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5124 | /** |
| 5125 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5126 | * |
| 5127 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5128 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5129 | */ |
| 5130 | static LY_ERR |
| 5131 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5132 | { |
| 5133 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5134 | char *path; |
| 5135 | int len; |
| 5136 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5137 | struct lysp_node_uses fake_uses = { |
| 5138 | .parent = node_p, |
| 5139 | .nodetype = LYS_USES, |
| 5140 | .flags = 0, .next = NULL, |
| 5141 | .name = grp->name, |
| 5142 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5143 | .refines = NULL, .augments = NULL |
| 5144 | }; |
| 5145 | struct lysc_node_container fake_container = { |
| 5146 | .nodetype = LYS_CONTAINER, |
| 5147 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5148 | .module = ctx->mod, |
| 5149 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5150 | .prev = (struct lysc_node*)&fake_container, |
| 5151 | .name = "fake", |
| 5152 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5153 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5154 | }; |
| 5155 | |
| 5156 | if (grp->parent) { |
| 5157 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5158 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5159 | |
| 5160 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5161 | if (len < 0) { |
| 5162 | LOGMEM(ctx->ctx); |
| 5163 | return LY_EMEM; |
| 5164 | } |
| 5165 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5166 | ctx->path_len = (uint16_t)len; |
| 5167 | free(path); |
| 5168 | |
| 5169 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5170 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5171 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5172 | lysc_update_path(ctx, NULL, NULL); |
| 5173 | lysc_update_path(ctx, NULL, NULL); |
| 5174 | |
| 5175 | ctx->path_len = 1; |
| 5176 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5177 | |
| 5178 | /* cleanup */ |
| 5179 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5180 | |
| 5181 | return ret; |
| 5182 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5183 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5184 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5185 | * @brief Compile parsed schema node information. |
| 5186 | * @param[in] ctx Compile context |
| 5187 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5188 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5189 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5190 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5191 | * @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). |
| 5192 | * 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] | 5193 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5194 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5195 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5196 | 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] | 5197 | { |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5198 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5199 | struct lysc_node *node; |
| 5200 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5201 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5202 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5203 | 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] | 5204 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5205 | if (node_p->nodetype != LYS_USES) { |
| 5206 | lysc_update_path(ctx, parent, node_p->name); |
| 5207 | } else { |
| 5208 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5209 | lysc_update_path(ctx, NULL, node_p->name); |
| 5210 | } |
| 5211 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5212 | switch (node_p->nodetype) { |
| 5213 | case LYS_CONTAINER: |
| 5214 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5215 | node_compile_spec = lys_compile_node_container; |
| 5216 | break; |
| 5217 | case LYS_LEAF: |
| 5218 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5219 | node_compile_spec = lys_compile_node_leaf; |
| 5220 | break; |
| 5221 | case LYS_LIST: |
| 5222 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5223 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5224 | break; |
| 5225 | case LYS_LEAFLIST: |
| 5226 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5227 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5228 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5229 | case LYS_CHOICE: |
| 5230 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5231 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5232 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5233 | case LYS_ANYXML: |
| 5234 | case LYS_ANYDATA: |
| 5235 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5236 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5237 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5238 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5239 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5240 | lysc_update_path(ctx, NULL, NULL); |
| 5241 | lysc_update_path(ctx, NULL, NULL); |
| 5242 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5243 | default: |
| 5244 | LOGINT(ctx->ctx); |
| 5245 | return LY_EINT; |
| 5246 | } |
| 5247 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5248 | node->nodetype = node_p->nodetype; |
| 5249 | node->module = ctx->mod; |
| 5250 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5251 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5252 | |
| 5253 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5254 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5255 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5256 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5257 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5258 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5259 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5260 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5261 | node->flags |= LYS_CONFIG_R; |
| 5262 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5263 | /* config not explicitely set, inherit it from parent */ |
| 5264 | if (parent) { |
| 5265 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5266 | } else { |
| 5267 | /* default is config true */ |
| 5268 | node->flags |= LYS_CONFIG_W; |
| 5269 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5270 | } else { |
| 5271 | /* config set explicitely */ |
| 5272 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5273 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5274 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5275 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5276 | "Configuration node cannot be child of any state data node."); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5277 | ret = LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5278 | goto error; |
| 5279 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5280 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5281 | /* *list ordering */ |
| 5282 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5283 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5284 | 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] | 5285 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5286 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5287 | node->flags &= ~LYS_ORDBY_MASK; |
| 5288 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5289 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5290 | /* default ordering is system */ |
| 5291 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5292 | } |
| 5293 | } |
| 5294 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5295 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5296 | * 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] | 5297 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5298 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5299 | * 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] | 5300 | 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] | 5301 | } |
| 5302 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5303 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5304 | node->sp = node_p; |
| 5305 | } |
| 5306 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5307 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5308 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5309 | if (node_p->when) { |
| 5310 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5311 | 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] | 5312 | |
| 5313 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 5314 | /* do not check "when" semantics in a grouping */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 5315 | ly_set_add(&ctx->xpath, node, 0); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 5316 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5317 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5318 | 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] | 5319 | |
| 5320 | /* nodetype-specific part */ |
Radek Krejci | 1c54f46 | 2020-05-12 17:25:34 +0200 | [diff] [blame] | 5321 | LY_CHECK_GOTO(ret = node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5322 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5323 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5324 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5325 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5326 | if (node->flags & LYS_MAND_TRUE) { |
| 5327 | lys_compile_mandatory_parents(parent, 1); |
| 5328 | } |
| 5329 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5330 | lysc_update_path(ctx, NULL, NULL); |
| 5331 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5332 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5333 | if (parent) { |
| 5334 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5335 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5336 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5337 | } else { |
| 5338 | lysc_update_path(ctx, parent, node->name); |
| 5339 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5340 | 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] | 5341 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5342 | if (uses_status) { |
| 5343 | |
| 5344 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5345 | /* 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] | 5346 | * it directly gets the same status flags as the choice; |
| 5347 | * 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] | 5348 | 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] | 5349 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5350 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5351 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5352 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5353 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5354 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5355 | 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] | 5356 | |
| 5357 | if (parent->nodetype == LYS_CHOICE) { |
| 5358 | lysc_update_path(ctx, NULL, NULL); |
| 5359 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5360 | } else { |
| 5361 | /* top-level element */ |
| 5362 | if (!ctx->mod->compiled->data) { |
| 5363 | ctx->mod->compiled->data = node; |
| 5364 | } else { |
| 5365 | /* insert at the end of the module's top-level nodes list */ |
| 5366 | ctx->mod->compiled->data->prev->next = node; |
| 5367 | node->prev = ctx->mod->compiled->data->prev; |
| 5368 | ctx->mod->compiled->data->prev = node; |
| 5369 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5370 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5371 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5372 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5373 | return LY_EVALID; |
| 5374 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5375 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5376 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5377 | |
| 5378 | return LY_SUCCESS; |
| 5379 | |
| 5380 | error: |
| 5381 | lysc_node_free(ctx->ctx, node); |
| 5382 | return ret; |
| 5383 | } |
| 5384 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5385 | static void |
| 5386 | lysc_disconnect(struct lysc_node *node) |
| 5387 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5388 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5389 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5390 | int remove_cs = 0; |
| 5391 | |
| 5392 | parent = node->parent; |
| 5393 | |
| 5394 | /* parent's first child */ |
| 5395 | if (!parent) { |
| 5396 | return; |
| 5397 | } |
| 5398 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5399 | cs = (struct lysc_node_case*)node; |
| 5400 | } else if (parent->nodetype == LYS_CASE) { |
| 5401 | /* disconnecting some node in a case */ |
| 5402 | cs = (struct lysc_node_case*)parent; |
| 5403 | parent = cs->parent; |
| 5404 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5405 | if (child == node) { |
| 5406 | if (cs->child == child) { |
| 5407 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5408 | /* case with a single child -> remove also the case */ |
| 5409 | child->parent = NULL; |
| 5410 | remove_cs = 1; |
| 5411 | } else { |
| 5412 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5413 | } |
| 5414 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5415 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5416 | } |
| 5417 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5418 | if (!remove_cs) { |
| 5419 | cs = NULL; |
| 5420 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5421 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5422 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5423 | } |
| 5424 | |
| 5425 | if (cs) { |
| 5426 | if (remove_cs) { |
| 5427 | /* cs has only one child which is being also removed */ |
| 5428 | lysc_disconnect((struct lysc_node*)cs); |
| 5429 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5430 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5431 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5432 | /* default case removed */ |
| 5433 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5434 | } |
| 5435 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5436 | /* first case removed */ |
| 5437 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5438 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5439 | if (cs->child) { |
| 5440 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5441 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5442 | prev = cs->child->prev; |
| 5443 | } /* else all the children are under a single case */ |
| 5444 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5445 | if (child->parent != (struct lysc_node*)cs) { |
| 5446 | break; |
| 5447 | } |
| 5448 | lysc_node_free(node->module->ctx, child); |
| 5449 | } |
| 5450 | if (prev) { |
| 5451 | if (prev->next) { |
| 5452 | prev->next = child; |
| 5453 | } |
| 5454 | if (child) { |
| 5455 | child->prev = prev; |
| 5456 | } else { |
| 5457 | /* link from the first child under the cases */ |
| 5458 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5459 | } |
| 5460 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5461 | } |
| 5462 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5466 | if (node->prev->next) { |
| 5467 | node->prev->next = node->next; |
| 5468 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5469 | if (node->next) { |
| 5470 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5471 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5472 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5473 | if (child) { |
| 5474 | child->prev = node->prev; |
| 5475 | } |
| 5476 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5477 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5478 | } |
| 5479 | } |
| 5480 | |
| 5481 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5482 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5483 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5484 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5485 | struct ly_set devs_p = {0}; |
| 5486 | struct ly_set targets = {0}; |
| 5487 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5488 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5489 | struct lysc_action *rpcs; |
| 5490 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5491 | struct lysp_deviation *dev; |
| 5492 | struct lysp_deviate *d, **dp_new; |
| 5493 | struct lysp_deviate_add *d_add; |
| 5494 | struct lysp_deviate_del *d_del; |
| 5495 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5496 | LY_ARRAY_SIZE_TYPE u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5497 | struct lysc_deviation { |
| 5498 | const char *nodeid; |
| 5499 | struct lysc_node *target; /* target node of the deviation */ |
| 5500 | 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] | 5501 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5502 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5503 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5504 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5505 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5506 | const char *prefix, *name, *nodeid, *dflt; |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 5507 | struct lys_module *mod, **dev_mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5508 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5509 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5510 | |
| 5511 | /* get all deviations from the module and all its submodules ... */ |
| 5512 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5513 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5514 | } |
| 5515 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5516 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5517 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5518 | } |
| 5519 | } |
| 5520 | if (!devs_p.count) { |
| 5521 | /* nothing to do */ |
| 5522 | return LY_SUCCESS; |
| 5523 | } |
| 5524 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5525 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5526 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5527 | /* ... and group them by the target node */ |
| 5528 | devs = calloc(devs_p.count, sizeof *devs); |
| 5529 | for (u = 0; u < devs_p.count; ++u) { |
| 5530 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5531 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5532 | |
| 5533 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5534 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5535 | (const struct lysc_node**)&target, &flags), cleanup); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5536 | if (target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5537 | /* move the target pointer to input/output to make them different from the action and |
| 5538 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5539 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5540 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5541 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5542 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5543 | flags |= LYSC_OPT_INTERNAL; |
| 5544 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5545 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5546 | flags |= LYSC_OPT_INTERNAL; |
| 5547 | } |
| 5548 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5549 | /* insert into the set of targets with duplicity detection */ |
| 5550 | i = ly_set_add(&targets, target, 0); |
| 5551 | if (!devs[i]) { |
| 5552 | /* new record */ |
| 5553 | devs[i] = calloc(1, sizeof **devs); |
| 5554 | devs[i]->target = target; |
| 5555 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5556 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5557 | } |
| 5558 | /* add deviates into the deviation's list of deviates */ |
| 5559 | for (d = dev->deviates; d; d = d->next) { |
| 5560 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5561 | *dp_new = d; |
| 5562 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5563 | devs[i]->not_supported = 1; |
| 5564 | } |
| 5565 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5566 | |
| 5567 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | /* MACROS for deviates checking */ |
| 5571 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5572 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5573 | 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] | 5574 | goto cleanup; \ |
| 5575 | } |
| 5576 | |
| 5577 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5578 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5579 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%"LY_PRI_ARRAY_SIZE_TYPE") %s properties.", \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5580 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5581 | goto cleanup; \ |
| 5582 | } |
| 5583 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5584 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5585 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5586 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5587 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5588 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5589 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5590 | goto cleanup; \ |
| 5591 | } |
| 5592 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5593 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5594 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5595 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5596 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5597 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5598 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5599 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5600 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5601 | goto cleanup; \ |
| 5602 | } |
| 5603 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5604 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5605 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5606 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5607 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5608 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5609 | goto cleanup; \ |
| 5610 | } |
| 5611 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5612 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5613 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5614 | 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] | 5615 | goto cleanup; \ |
| 5616 | } |
| 5617 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5618 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5619 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5620 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5621 | "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] | 5622 | goto cleanup; \ |
| 5623 | } |
| 5624 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5625 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5626 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5627 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5628 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5629 | 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] | 5630 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5631 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5632 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5633 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5634 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5635 | goto cleanup; \ |
| 5636 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5637 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5638 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5639 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5640 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5641 | (LY_ARRAY_SIZE(((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] | 5642 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5643 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5644 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5645 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | /* apply deviations */ |
| 5649 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5650 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5651 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5652 | struct ly_err_item *err = NULL; |
| 5653 | |
| 5654 | dflt = NULL; |
| 5655 | changed_type = 0; |
| 5656 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5657 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5658 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5659 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5660 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5661 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5662 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5663 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5664 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5665 | } |
| 5666 | } |
| 5667 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5668 | /* not-supported */ |
| 5669 | if (devs[u]->not_supported) { |
| 5670 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 5671 | LOGWRN(ctx->ctx, "Useless multiple (%"LY_PRI_ARRAY_SIZE_TYPE") deviates on node \"%s\" since the node is not-supported.", |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5672 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5673 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5674 | |
| 5675 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5676 | if (devs[u]->target->parent) { \ |
| 5677 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5678 | } else { \ |
| 5679 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5680 | } \ |
| 5681 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5682 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5683 | } \ |
| 5684 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5685 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5686 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5687 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5688 | } |
| 5689 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5690 | if (devs[u]->target->nodetype & (LYS_RPC | LYS_ACTION)) { |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5691 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5692 | /* remove RPC's/action's input */ |
| 5693 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5694 | 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] | 5695 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5696 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5697 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5698 | /* remove RPC's/action's output */ |
| 5699 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5700 | 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] | 5701 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5702 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5703 | } else { |
| 5704 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5705 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5706 | } |
| 5707 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5708 | /* remove Notification */ |
| 5709 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5710 | } else { |
| 5711 | /* remove the target node */ |
| 5712 | lysc_disconnect(devs[u]->target); |
| 5713 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5714 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5715 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5716 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5717 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5718 | continue; |
| 5719 | } |
| 5720 | |
| 5721 | /* list of deviates (not-supported is not present in the list) */ |
| 5722 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5723 | d = devs[u]->deviates[v]; |
| 5724 | |
| 5725 | switch (d->mod) { |
| 5726 | case LYS_DEV_ADD: |
| 5727 | d_add = (struct lysp_deviate_add*)d; |
| 5728 | /* [units-stmt] */ |
| 5729 | if (d_add->units) { |
| 5730 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5731 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5732 | |
| 5733 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5734 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5735 | } |
| 5736 | |
| 5737 | /* *must-stmt */ |
| 5738 | if (d_add->musts) { |
| 5739 | switch (devs[u]->target->nodetype) { |
| 5740 | case LYS_CONTAINER: |
| 5741 | case LYS_LIST: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5742 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
| 5743 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5744 | break; |
| 5745 | case LYS_LEAF: |
| 5746 | case LYS_LEAFLIST: |
| 5747 | case LYS_ANYDATA: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5748 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
| 5749 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5750 | break; |
| 5751 | case LYS_NOTIF: |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5752 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
| 5753 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5754 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5755 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5756 | case LYS_ACTION: |
| 5757 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5758 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
| 5759 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5760 | break; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 5761 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
Radek Krejci | c71ac5b | 2019-09-10 15:34:22 +0200 | [diff] [blame] | 5762 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
| 5763 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5764 | break; |
| 5765 | } |
| 5766 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5767 | default: |
| 5768 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5769 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5770 | goto cleanup; |
| 5771 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 5772 | ly_set_add(&ctx->xpath, devs[u]->target, 0); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5776 | if (d_add->uniques) { |
| 5777 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5778 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5779 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5780 | |
| 5781 | /* *default-stmt */ |
| 5782 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5783 | switch (devs[u]->target->nodetype) { |
| 5784 | case LYS_LEAF: |
| 5785 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5786 | 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] | 5787 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5788 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5789 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5790 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5791 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5792 | } else { |
| 5793 | /* prepare new default value storage */ |
| 5794 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5795 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5796 | dflt = d_add->dflts[0]; |
| 5797 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5798 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5799 | /* mark the new default values as leaf's own */ |
| 5800 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5801 | break; |
| 5802 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5803 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5804 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5805 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5806 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5807 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5808 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5809 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5810 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5811 | LY_ARRAY_FREE(llist->dflts); |
| 5812 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5813 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5814 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5815 | } |
| 5816 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5817 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5818 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5819 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5820 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5821 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5822 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5823 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5824 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5825 | llist->dflts[x]->realtype = llist->type; |
| 5826 | 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] | 5827 | 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] | 5828 | (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] | 5829 | llist->dflts[x]->realtype->refcount++; |
| 5830 | if (err) { |
| 5831 | ly_err_print(err); |
| 5832 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5833 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5834 | d_add->dflts[x - y], err->msg); |
| 5835 | ly_err_free(err); |
| 5836 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5837 | if (rc == LY_EINCOMPLETE) { |
| 5838 | /* postpone default compilation when the tree is complete */ |
| 5839 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5840 | |
| 5841 | /* but in general result is so far ok */ |
| 5842 | rc = LY_SUCCESS; |
| 5843 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5844 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5845 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5846 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5847 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5848 | break; |
| 5849 | case LYS_CHOICE: |
| 5850 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 5851 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 5852 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 5853 | * 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] | 5854 | 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] | 5855 | goto cleanup; |
| 5856 | } |
| 5857 | break; |
| 5858 | default: |
| 5859 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5860 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5861 | goto cleanup; |
| 5862 | } |
| 5863 | } |
| 5864 | |
| 5865 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5866 | if (d_add->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5867 | 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] | 5868 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5869 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 5870 | goto cleanup; |
| 5871 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5872 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5873 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 5874 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5875 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5876 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 5877 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5878 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 5879 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5880 | goto cleanup; |
| 5881 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5882 | 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] | 5883 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5884 | |
| 5885 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5886 | if (d_add->flags & LYS_MAND_MASK) { |
| 5887 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 5888 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5889 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 5890 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 5891 | goto cleanup; |
| 5892 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5893 | 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] | 5894 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5895 | |
| 5896 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5897 | if (d_add->flags & LYS_SET_MIN) { |
| 5898 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5899 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 5900 | /* change value */ |
| 5901 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 5902 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5903 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 5904 | /* change value */ |
| 5905 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 5906 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5907 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5908 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 5909 | goto cleanup; |
| 5910 | } |
| 5911 | if (d_add->min) { |
| 5912 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 5913 | } |
| 5914 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5915 | |
| 5916 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5917 | if (d_add->flags & LYS_SET_MAX) { |
| 5918 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 5919 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 5920 | /* change value */ |
| 5921 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5922 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 5923 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 5924 | /* change value */ |
| 5925 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 5926 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5927 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5928 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 5929 | goto cleanup; |
| 5930 | } |
| 5931 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5932 | |
| 5933 | break; |
| 5934 | case LYS_DEV_DELETE: |
| 5935 | d_del = (struct lysp_deviate_del*)d; |
| 5936 | |
| 5937 | /* [units-stmt] */ |
| 5938 | if (d_del->units) { |
| 5939 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5940 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 5941 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 5942 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 5943 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 5944 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5945 | goto cleanup; |
| 5946 | } |
| 5947 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5948 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | /* *must-stmt */ |
| 5952 | if (d_del->musts) { |
| 5953 | switch (devs[u]->target->nodetype) { |
| 5954 | case LYS_CONTAINER: |
| 5955 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5956 | 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] | 5957 | break; |
| 5958 | case LYS_LEAF: |
| 5959 | case LYS_LEAFLIST: |
| 5960 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5961 | 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] | 5962 | break; |
| 5963 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5964 | 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] | 5965 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 5966 | case LYS_RPC: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5967 | case LYS_ACTION: |
| 5968 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5969 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5970 | break; |
| 5971 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5972 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 5973 | break; |
| 5974 | } |
| 5975 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5976 | default: |
| 5977 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5978 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5979 | goto cleanup; |
| 5980 | } |
| 5981 | } |
| 5982 | |
| 5983 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5984 | if (d_del->uniques) { |
| 5985 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 5986 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 5987 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 5988 | LY_ARRAY_FOR(list->uniques, z) { |
| 5989 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 5990 | nodeid = strpbrk(name, " \t\n"); |
| 5991 | if (nodeid) { |
Radek Krejci | 7f9b651 | 2019-09-18 13:11:09 +0200 | [diff] [blame] | 5992 | if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5993 | break; |
| 5994 | } |
| 5995 | while (isspace(*nodeid)) { |
| 5996 | ++nodeid; |
| 5997 | } |
| 5998 | } else { |
| 5999 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6000 | break; |
| 6001 | } |
| 6002 | } |
| 6003 | } |
| 6004 | if (!name) { |
| 6005 | /* complete match - remove the unique */ |
| 6006 | LY_ARRAY_DECREMENT(list->uniques); |
| 6007 | LY_ARRAY_FREE(list->uniques[z]); |
| 6008 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6009 | --z; |
| 6010 | break; |
| 6011 | } |
| 6012 | } |
| 6013 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6014 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6015 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6016 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6017 | goto cleanup; |
| 6018 | } |
| 6019 | } |
| 6020 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6021 | LY_ARRAY_FREE(list->uniques); |
| 6022 | list->uniques = NULL; |
| 6023 | } |
| 6024 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6025 | |
| 6026 | /* *default-stmt */ |
| 6027 | if (d_del->dflts) { |
| 6028 | switch (devs[u]->target->nodetype) { |
| 6029 | case LYS_LEAF: |
| 6030 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6031 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6032 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6033 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6034 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6035 | 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] | 6036 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6037 | if (i) { |
| 6038 | free((char*)dflt); |
| 6039 | } |
| 6040 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6041 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6042 | d_del->dflts[0], dflt); |
| 6043 | goto cleanup; |
| 6044 | } |
| 6045 | if (i) { |
| 6046 | free((char*)dflt); |
| 6047 | } |
| 6048 | dflt = NULL; |
| 6049 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6050 | /* update the list of incomplete default values if needed */ |
| 6051 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6052 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6053 | /* remove the default specification */ |
| 6054 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6055 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6056 | free(leaf->dflt); |
| 6057 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6058 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6059 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6060 | break; |
| 6061 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6062 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6063 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6064 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6065 | 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] | 6066 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6067 | if (i) { |
| 6068 | free((char*)dflt); |
| 6069 | } |
| 6070 | dflt = NULL; |
| 6071 | break; |
| 6072 | } |
| 6073 | if (i) { |
| 6074 | free((char*)dflt); |
| 6075 | } |
| 6076 | dflt = NULL; |
| 6077 | } |
| 6078 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6079 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6080 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6081 | goto cleanup; |
| 6082 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6083 | |
| 6084 | /* update the list of incomplete default values if needed */ |
| 6085 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6086 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6087 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6088 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6089 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6090 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6091 | free(llist->dflts[y]); |
| 6092 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts)); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6093 | memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods)); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6094 | } |
| 6095 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6096 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6097 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6098 | LY_ARRAY_FREE(llist->dflts); |
| 6099 | llist->dflts = NULL; |
| 6100 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6101 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6102 | break; |
| 6103 | case LYS_CHOICE: |
| 6104 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6105 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6106 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6107 | 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] | 6108 | if (prefix) { |
| 6109 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6110 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6111 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6112 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6113 | "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] | 6114 | goto cleanup; |
| 6115 | } |
| 6116 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6117 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6118 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6119 | "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] | 6120 | goto cleanup; |
| 6121 | } |
| 6122 | } |
| 6123 | /* else { |
| 6124 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6125 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6126 | * the module of the default case, just matching its name */ |
| 6127 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6129 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6130 | 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] | 6131 | goto cleanup; |
| 6132 | } |
| 6133 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6134 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6135 | break; |
| 6136 | default: |
| 6137 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6138 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6139 | goto cleanup; |
| 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | break; |
| 6144 | case LYS_DEV_REPLACE: |
| 6145 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6146 | |
| 6147 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6148 | if (d_rpl->type) { |
| 6149 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6150 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6151 | 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] | 6152 | |
| 6153 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6154 | /* the target has default from the previous type - remove it */ |
| 6155 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6156 | /* update the list of incomplete default values if needed */ |
| 6157 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6158 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6159 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6160 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6161 | free(leaf->dflt); |
| 6162 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6163 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6164 | } else { /* LYS_LEAFLIST */ |
| 6165 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6166 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6167 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6168 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6169 | free(llist->dflts[x]); |
| 6170 | } |
| 6171 | LY_ARRAY_FREE(llist->dflts); |
| 6172 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6173 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6174 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6175 | } |
| 6176 | } |
| 6177 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6178 | /* there is no default value, do not set changed_type after type compilation |
| 6179 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6180 | changed_type = -1; |
| 6181 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6182 | 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] | 6183 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6184 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6185 | |
| 6186 | /* [units-stmt] */ |
| 6187 | if (d_rpl->units) { |
| 6188 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6189 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6190 | units, "replacing", "units", d_rpl->units); |
| 6191 | |
| 6192 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6193 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6194 | } |
| 6195 | |
| 6196 | /* [default-stmt] */ |
| 6197 | if (d_rpl->dflt) { |
| 6198 | switch (devs[u]->target->nodetype) { |
| 6199 | case LYS_LEAF: |
| 6200 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6201 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6202 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6203 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6204 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6205 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6206 | dflt = d_rpl->dflt; |
| 6207 | /* 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] | 6208 | break; |
| 6209 | case LYS_CHOICE: |
| 6210 | 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] | 6211 | 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] | 6212 | goto cleanup; |
| 6213 | } |
| 6214 | break; |
| 6215 | default: |
| 6216 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6217 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6218 | goto cleanup; |
| 6219 | } |
| 6220 | } |
| 6221 | |
| 6222 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6223 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6224 | 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] | 6225 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6226 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6227 | goto cleanup; |
| 6228 | } |
| 6229 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6230 | 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] | 6231 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6232 | goto cleanup; |
| 6233 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6234 | 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] | 6235 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6236 | |
| 6237 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6238 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6239 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6240 | 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] | 6241 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6242 | goto cleanup; |
| 6243 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6244 | 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] | 6245 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6246 | |
| 6247 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6248 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6249 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6250 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6251 | /* change value */ |
| 6252 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6253 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6254 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6255 | /* change value */ |
| 6256 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6257 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6258 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6259 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6260 | goto cleanup; |
| 6261 | } |
| 6262 | if (d_rpl->min) { |
| 6263 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6264 | } |
| 6265 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6266 | |
| 6267 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6268 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6269 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6270 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6271 | /* change value */ |
| 6272 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6273 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6274 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6275 | /* change value */ |
| 6276 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6277 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6278 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6279 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6280 | goto cleanup; |
| 6281 | } |
| 6282 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6283 | |
| 6284 | break; |
| 6285 | default: |
| 6286 | LOGINT(ctx->ctx); |
| 6287 | goto cleanup; |
| 6288 | } |
| 6289 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6290 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6291 | /* final check when all deviations of a single target node are applied */ |
| 6292 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6293 | /* check min-max compatibility */ |
| 6294 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6295 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6296 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6297 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6298 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6299 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6300 | } else { |
| 6301 | min = max = 0; |
| 6302 | } |
| 6303 | if (min > max) { |
| 6304 | 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] | 6305 | "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] | 6306 | goto cleanup; |
| 6307 | } |
| 6308 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6309 | if (dflt) { |
| 6310 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6311 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6312 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6313 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6314 | 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] | 6315 | 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] | 6316 | leaf->dflt->realtype->refcount++; |
| 6317 | if (err) { |
| 6318 | ly_err_print(err); |
| 6319 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6320 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6321 | ly_err_free(err); |
| 6322 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6323 | if (rc == LY_EINCOMPLETE) { |
| 6324 | /* postpone default compilation when the tree is complete */ |
| 6325 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6326 | |
| 6327 | /* but in general result is so far ok */ |
| 6328 | rc = LY_SUCCESS; |
| 6329 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6330 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6331 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6332 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6333 | int dynamic; |
| 6334 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6335 | 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] | 6336 | |
| 6337 | /* update the list of incomplete default values if needed */ |
| 6338 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6339 | |
| 6340 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6341 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6342 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6343 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6344 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6345 | 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] | 6346 | 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] | 6347 | leaf->dflt->realtype->refcount++; |
| 6348 | if (err) { |
| 6349 | ly_err_print(err); |
| 6350 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6351 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6352 | ly_err_free(err); |
| 6353 | } |
| 6354 | if (dynamic) { |
| 6355 | free((void*)dflt); |
| 6356 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6357 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6358 | if (rc == LY_EINCOMPLETE) { |
| 6359 | /* postpone default compilation when the tree is complete */ |
| 6360 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6361 | |
| 6362 | /* but in general result is so far ok */ |
| 6363 | rc = LY_SUCCESS; |
| 6364 | } |
| 6365 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6366 | } else { /* LYS_LEAFLIST */ |
| 6367 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6368 | 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] | 6369 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6370 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6371 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6372 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6373 | 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] | 6374 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6375 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6376 | llist->dflts[x]->realtype->refcount++; |
| 6377 | if (err) { |
| 6378 | ly_err_print(err); |
| 6379 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6380 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6381 | dflt, err->msg); |
| 6382 | ly_err_free(err); |
| 6383 | } |
| 6384 | if (dynamic) { |
| 6385 | free((void*)dflt); |
| 6386 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6387 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6388 | if (rc == LY_EINCOMPLETE) { |
| 6389 | /* postpone default compilation when the tree is complete */ |
| 6390 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6391 | |
| 6392 | /* but in general result is so far ok */ |
| 6393 | rc = LY_SUCCESS; |
| 6394 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6395 | LY_CHECK_GOTO(rc, cleanup); |
| 6396 | } |
| 6397 | } |
| 6398 | } |
| 6399 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6400 | /* check mandatory - default compatibility */ |
| 6401 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6402 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6403 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6404 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6405 | "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] | 6406 | goto cleanup; |
| 6407 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6408 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6409 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6410 | 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] | 6411 | goto cleanup; |
| 6412 | } |
| 6413 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6414 | /* mandatory node under a default case */ |
| 6415 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6416 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6417 | 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] | 6418 | goto cleanup; |
| 6419 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6420 | |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 6421 | /* add this module into the target module deviated_by */ |
| 6422 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[u]->target->module->compiled->deviated_by, dev_mod, ret, cleanup); |
| 6423 | *dev_mod = mod_p->mod; |
| 6424 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6425 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6426 | } |
| 6427 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6428 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6429 | ret = LY_SUCCESS; |
| 6430 | |
| 6431 | cleanup: |
| 6432 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6433 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6434 | free(devs[u]); |
| 6435 | } |
| 6436 | free(devs); |
| 6437 | ly_set_erase(&targets, NULL); |
| 6438 | ly_set_erase(&devs_p, NULL); |
| 6439 | |
| 6440 | return ret; |
| 6441 | } |
| 6442 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6443 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6444 | * @brief Compile the given YANG submodule into the main module. |
| 6445 | * @param[in] ctx Compile context |
| 6446 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6447 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6448 | */ |
| 6449 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6450 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6451 | { |
| 6452 | unsigned int u; |
| 6453 | LY_ERR ret = LY_SUCCESS; |
| 6454 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6455 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6456 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6457 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6458 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6459 | if (!mainmod->mod->off_features) { |
| 6460 | /* features are compiled directly into the compiled module structure, |
| 6461 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6462 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6463 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6464 | LY_CHECK_GOTO(ret, error); |
| 6465 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6466 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6467 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6468 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6469 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6470 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6471 | /* data nodes */ |
| 6472 | LY_LIST_FOR(submod->data, node_p) { |
| 6473 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6474 | LY_CHECK_GOTO(ret, error); |
| 6475 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6476 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6477 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6478 | 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] | 6479 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6480 | error: |
| 6481 | return ret; |
| 6482 | } |
| 6483 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6484 | static void * |
| 6485 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6486 | { |
| 6487 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6488 | if (substmts[u].stmt == stmt) { |
| 6489 | return substmts[u].storage; |
| 6490 | } |
| 6491 | } |
| 6492 | return NULL; |
| 6493 | } |
| 6494 | |
| 6495 | LY_ERR |
| 6496 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6497 | { |
| 6498 | LY_ERR ret = LY_EVALID, r; |
| 6499 | unsigned int u; |
| 6500 | struct lysp_stmt *stmt; |
| 6501 | void *parsed = NULL, **compiled = NULL; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6502 | |
| 6503 | /* check for invalid substatements */ |
| 6504 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
Radek Krejci | f56e2a4 | 2019-09-09 14:15:25 +0200 | [diff] [blame] | 6505 | if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) { |
| 6506 | continue; |
| 6507 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6508 | for (u = 0; substmts[u].stmt; ++u) { |
| 6509 | if (substmts[u].stmt == stmt->kw) { |
| 6510 | break; |
| 6511 | } |
| 6512 | } |
| 6513 | if (!substmts[u].stmt) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6514 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.", |
| 6515 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6516 | goto cleanup; |
| 6517 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6518 | } |
| 6519 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6520 | /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */ |
| 6521 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6522 | /* keep order of the processing the same as the order in the defined substmts, |
| 6523 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6524 | for (u = 0; substmts[u].stmt; ++u) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6525 | int stmt_present = 0; |
| 6526 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6527 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6528 | if (substmts[u].stmt != stmt->kw) { |
| 6529 | continue; |
| 6530 | } |
| 6531 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6532 | stmt_present = 1; |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6533 | if (substmts[u].storage) { |
| 6534 | switch (stmt->kw) { |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6535 | case LY_STMT_STATUS: |
| 6536 | assert(substmts[u].cardinality < LY_STMT_CARD_SOME); |
| 6537 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup); |
| 6538 | break; |
| 6539 | case LY_STMT_UNITS: { |
| 6540 | const char **units; |
| 6541 | |
| 6542 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6543 | /* single item */ |
| 6544 | if (*((const char **)substmts[u].storage)) { |
| 6545 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6546 | goto cleanup; |
| 6547 | } |
| 6548 | units = (const char **)substmts[u].storage; |
| 6549 | } else { |
| 6550 | /* sized array */ |
| 6551 | const char ***units_array = (const char ***)substmts[u].storage; |
| 6552 | LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup); |
| 6553 | } |
| 6554 | *units = lydict_insert(ctx->ctx, stmt->arg, 0); |
| 6555 | break; |
| 6556 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6557 | case LY_STMT_TYPE: { |
| 6558 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6559 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6560 | |
| 6561 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6562 | /* single item */ |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6563 | if (*(struct lysc_type**)substmts[u].storage) { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6564 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6565 | goto cleanup; |
| 6566 | } |
| 6567 | compiled = substmts[u].storage; |
| 6568 | } else { |
| 6569 | /* sized array */ |
| 6570 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6571 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6572 | compiled = (void*)type; |
| 6573 | } |
| 6574 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6575 | 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] | 6576 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6577 | 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] | 6578 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6579 | lysp_type_free(ctx->ctx, parsed); |
| 6580 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6581 | break; |
| 6582 | } |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6583 | case LY_STMT_IF_FEATURE: { |
| 6584 | struct lysc_iffeature *iff = NULL; |
| 6585 | |
| 6586 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6587 | /* single item */ |
| 6588 | if (((struct lysc_iffeature*)substmts[u].storage)->features) { |
| 6589 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6590 | goto cleanup; |
| 6591 | } |
| 6592 | iff = (struct lysc_iffeature*)substmts[u].storage; |
| 6593 | } else { |
| 6594 | /* sized array */ |
| 6595 | struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage; |
| 6596 | LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup); |
| 6597 | } |
| 6598 | LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup); |
| 6599 | break; |
| 6600 | } |
| 6601 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc), |
| 6602 | * 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] | 6603 | default: |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6604 | 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.", |
| 6605 | stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6606 | goto cleanup; |
| 6607 | } |
| 6608 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6609 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6610 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 6611 | if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) { |
| 6612 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".", |
| 6613 | ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : ""); |
| 6614 | goto cleanup; |
| 6615 | } |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6616 | } |
| 6617 | |
| 6618 | ret = LY_SUCCESS; |
| 6619 | |
| 6620 | cleanup: |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6621 | return ret; |
| 6622 | } |
| 6623 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6624 | /** |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6625 | * @brief Check when for cyclic dependencies. |
| 6626 | * @param[in] set Set with all the referenced nodes. |
| 6627 | * @param[in] node Node whose "when" referenced nodes are in @p set. |
| 6628 | * @return LY_ERR value |
| 6629 | */ |
| 6630 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 6631 | 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] | 6632 | { |
| 6633 | struct lyxp_set tmp_set; |
| 6634 | struct lyxp_set_scnode *xp_scnode; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6635 | uint32_t i, j; |
| 6636 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6637 | int idx; |
| 6638 | struct lysc_when *when; |
| 6639 | LY_ERR ret = LY_SUCCESS; |
| 6640 | |
| 6641 | memset(&tmp_set, 0, sizeof tmp_set); |
| 6642 | |
| 6643 | /* prepare in_ctx of the set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6644 | for ( i = 0; i < set->used; ++i) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6645 | xp_scnode = &set->val.scnodes[i]; |
| 6646 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6647 | if (xp_scnode->in_ctx != -1) { |
| 6648 | /* check node when, skip the context node (it was just checked) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6649 | xp_scnode->in_ctx = 1; |
| 6650 | } |
| 6651 | } |
| 6652 | |
| 6653 | for (i = 0; i < set->used; ++i) { |
| 6654 | xp_scnode = &set->val.scnodes[i]; |
| 6655 | if (xp_scnode->in_ctx != 1) { |
| 6656 | /* already checked */ |
| 6657 | continue; |
| 6658 | } |
| 6659 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6660 | 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] | 6661 | || !xp_scnode->scnode->when) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6662 | /* no when to check */ |
| 6663 | xp_scnode->in_ctx = 0; |
| 6664 | continue; |
| 6665 | } |
| 6666 | |
| 6667 | node = xp_scnode->scnode; |
| 6668 | do { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6669 | LY_ARRAY_FOR(node->when, u) { |
| 6670 | when = node->when[u]; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 6671 | ret = lyxp_atomize(when->cond, LYD_SCHEMA, when->module, when->context, |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6672 | when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA); |
| 6673 | if (ret != LY_SUCCESS) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6674 | 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] | 6675 | goto cleanup; |
| 6676 | } |
| 6677 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6678 | for (j = 0; j < tmp_set.used; ++j) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6679 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6680 | if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6681 | /* try to find this node in our set */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6682 | 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] | 6683 | if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6684 | 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] | 6685 | ret = LY_EVALID; |
| 6686 | goto cleanup; |
| 6687 | } |
| 6688 | |
| 6689 | /* needs to be checked, if in both sets, will be ignored */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6690 | tmp_set.val.scnodes[j].in_ctx = 1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6691 | } else { |
| 6692 | /* no when, nothing to check */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6693 | tmp_set.val.scnodes[j].in_ctx = 0; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6694 | } |
| 6695 | } |
| 6696 | |
| 6697 | /* merge this set into the global when set */ |
| 6698 | lyxp_set_scnode_merge(set, &tmp_set); |
| 6699 | } |
| 6700 | |
| 6701 | /* check when of non-data parents as well */ |
| 6702 | node = node->parent; |
| 6703 | } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 6704 | |
Michal Vasko | 251f56e | 2019-11-14 16:06:47 +0100 | [diff] [blame] | 6705 | /* this node when was checked (xp_scnode could have been reallocd) */ |
| 6706 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6707 | } |
| 6708 | |
| 6709 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6710 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6711 | return ret; |
| 6712 | } |
| 6713 | |
| 6714 | /** |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6715 | * @brief Check when/must expressions of a node on a compiled schema tree. |
| 6716 | * @param[in] ctx Compile context. |
| 6717 | * @param[in] node Node to check. |
| 6718 | * @return LY_ERR value |
| 6719 | */ |
| 6720 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 6721 | 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] | 6722 | { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6723 | struct lyxp_set tmp_set; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6724 | uint32_t i; |
| 6725 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6726 | int opts, input_done = 0; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6727 | struct lysc_when **when = NULL; |
| 6728 | struct lysc_must *musts = NULL; |
| 6729 | LY_ERR ret = LY_SUCCESS; |
| 6730 | |
| 6731 | memset(&tmp_set, 0, sizeof tmp_set); |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6732 | opts = LYXP_SCNODE_SCHEMA; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6733 | |
| 6734 | switch (node->nodetype) { |
| 6735 | case LYS_CONTAINER: |
| 6736 | when = ((struct lysc_node_container *)node)->when; |
| 6737 | musts = ((struct lysc_node_container *)node)->musts; |
| 6738 | break; |
| 6739 | case LYS_CHOICE: |
| 6740 | when = ((struct lysc_node_choice *)node)->when; |
| 6741 | break; |
| 6742 | case LYS_LEAF: |
| 6743 | when = ((struct lysc_node_leaf *)node)->when; |
| 6744 | musts = ((struct lysc_node_leaf *)node)->musts; |
| 6745 | break; |
| 6746 | case LYS_LEAFLIST: |
| 6747 | when = ((struct lysc_node_leaflist *)node)->when; |
| 6748 | musts = ((struct lysc_node_leaflist *)node)->musts; |
| 6749 | break; |
| 6750 | case LYS_LIST: |
| 6751 | when = ((struct lysc_node_list *)node)->when; |
| 6752 | musts = ((struct lysc_node_list *)node)->musts; |
| 6753 | break; |
| 6754 | case LYS_ANYXML: |
| 6755 | case LYS_ANYDATA: |
| 6756 | when = ((struct lysc_node_anydata *)node)->when; |
| 6757 | musts = ((struct lysc_node_anydata *)node)->musts; |
| 6758 | break; |
| 6759 | case LYS_CASE: |
| 6760 | when = ((struct lysc_node_case *)node)->when; |
| 6761 | break; |
| 6762 | case LYS_NOTIF: |
| 6763 | musts = ((struct lysc_notif *)node)->musts; |
| 6764 | break; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6765 | case LYS_RPC: |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6766 | case LYS_ACTION: |
| 6767 | /* first process input musts */ |
| 6768 | musts = ((struct lysc_action *)node)->input.musts; |
| 6769 | break; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6770 | default: |
| 6771 | /* nothing to check */ |
| 6772 | break; |
| 6773 | } |
| 6774 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6775 | /* check "when" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6776 | LY_ARRAY_FOR(when, u) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 6777 | 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] | 6778 | 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] | 6779 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6780 | 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] | 6781 | goto cleanup; |
| 6782 | } |
| 6783 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6784 | ctx->path[0] = '\0'; |
| 6785 | 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] | 6786 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6787 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6788 | if ((tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[i].in_ctx != -1)) { |
| 6789 | struct lysc_node *schema = tmp_set.val.scnodes[i].scnode; |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6790 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6791 | /* 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] | 6792 | 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] | 6793 | schema->name); |
| 6794 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6795 | |
| 6796 | /* check dummy node accessing */ |
| 6797 | if (schema == node) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6798 | 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] | 6799 | ret = LY_EVALID; |
| 6800 | goto cleanup; |
| 6801 | } |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6802 | } |
| 6803 | } |
| 6804 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6805 | /* check cyclic dependencies */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 6806 | ret = lys_compile_unres_when_cyclic(&tmp_set, node); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6807 | LY_CHECK_GOTO(ret, cleanup); |
| 6808 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6809 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6810 | } |
| 6811 | |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6812 | check_musts: |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6813 | /* check "must" */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6814 | LY_ARRAY_FOR(musts, u) { |
| 6815 | 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] | 6816 | if (ret != LY_SUCCESS) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6817 | 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] | 6818 | goto cleanup; |
| 6819 | } |
| 6820 | |
Michal Vasko | dc052f3 | 2019-11-07 11:11:38 +0100 | [diff] [blame] | 6821 | ctx->path[0] = '\0'; |
| 6822 | 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] | 6823 | for (i = 0; i < tmp_set.used; ++i) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6824 | /* skip roots'n'stuff */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 6825 | if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6826 | /* 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] | 6827 | ret = lysc_check_status(ctx, node->flags, musts[u].module, node->name, tmp_set.val.scnodes[i].scnode->flags, |
| 6828 | 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] | 6829 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6830 | } |
| 6831 | } |
| 6832 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6833 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6834 | } |
| 6835 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 6836 | if ((node->nodetype & (LYS_RPC | LYS_ACTION)) && !input_done) { |
Michal Vasko | 5d8756a | 2019-11-07 15:21:00 +0100 | [diff] [blame] | 6837 | /* now check output musts */ |
| 6838 | input_done = 1; |
| 6839 | musts = ((struct lysc_action *)node)->output.musts; |
| 6840 | opts = LYXP_SCNODE_OUTPUT; |
| 6841 | goto check_musts; |
| 6842 | } |
| 6843 | |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6844 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6845 | lyxp_set_free_content(&tmp_set); |
Michal Vasko | 175012e | 2019-11-06 15:49:14 +0100 | [diff] [blame] | 6846 | return ret; |
| 6847 | } |
| 6848 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6849 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 6850 | lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref) |
| 6851 | { |
| 6852 | const struct lysc_node *target = NULL; |
| 6853 | struct ly_path *p; |
| 6854 | struct lysc_type *type; |
| 6855 | |
| 6856 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6857 | |
| 6858 | /* try to find the target */ |
| 6859 | LY_CHECK_RET(ly_path_compile(node->module, node, lref->path, LY_PATH_LREF_TRUE, lys_resolve_prefix, lref->path_context, |
| 6860 | LYD_SCHEMA, &p)); |
| 6861 | |
| 6862 | /* get the target node */ |
| 6863 | target = p[LY_ARRAY_SIZE(p) - 1].node; |
| 6864 | ly_path_free(node->module->ctx, p); |
| 6865 | |
| 6866 | if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 6867 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6868 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
| 6869 | lref->path->expr, lys_nodetype2str(target->nodetype)); |
| 6870 | return LY_EVALID; |
| 6871 | } |
| 6872 | |
| 6873 | /* check status */ |
| 6874 | ctx->path[0] = '\0'; |
| 6875 | lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 6876 | ctx->path_len = strlen(ctx->path); |
| 6877 | if (lysc_check_status(ctx, node->flags, node->module, node->name, target->flags, target->module, target->name)) { |
| 6878 | return LY_EVALID; |
| 6879 | } |
| 6880 | ctx->path_len = 1; |
| 6881 | ctx->path[1] = '\0'; |
| 6882 | |
| 6883 | /* check config */ |
| 6884 | if (lref->require_instance && (node->flags & LYS_CONFIG_W)) { |
| 6885 | if (target->flags & LYS_CONFIG_R) { |
| 6886 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed" |
| 6887 | " to represent configuration data (as the leafref does), but it does not.", lref->path->expr); |
| 6888 | return LY_EVALID; |
| 6889 | } |
| 6890 | } |
| 6891 | |
| 6892 | /* store the target's type and check for circular chain of leafrefs */ |
| 6893 | lref->realtype = ((struct lysc_node_leaf *)target)->type; |
| 6894 | for (type = lref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref *)type)->realtype) { |
| 6895 | if (type == (struct lysc_type *)lref) { |
| 6896 | /* circular chain detected */ |
| 6897 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6898 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", lref->path->expr); |
| 6899 | return LY_EVALID; |
| 6900 | } |
| 6901 | } |
| 6902 | |
| 6903 | /* check if leafref and its target are under common if-features */ |
| 6904 | if (lys_compile_leafref_features_validate(node, target)) { |
| 6905 | LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, |
| 6906 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of" |
| 6907 | " features applicable to the leafref itself.", lref->path->expr); |
| 6908 | return LY_EVALID; |
| 6909 | } |
| 6910 | |
| 6911 | return LY_SUCCESS; |
| 6912 | } |
| 6913 | |
| 6914 | static LY_ERR |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 6915 | lys_compile_ietf_netconf_wd_annotation(struct lysc_ctx *ctx, struct lys_module *mod) |
| 6916 | { |
| 6917 | struct lysc_ext_instance *ext; |
| 6918 | struct lysp_ext_instance *ext_p = NULL; |
| 6919 | struct lysp_stmt *stmt; |
| 6920 | const struct lys_module *ext_mod; |
| 6921 | LY_ERR ret = LY_SUCCESS; |
| 6922 | |
| 6923 | /* create the parsed extension instance manually */ |
| 6924 | ext_p = calloc(1, sizeof *ext_p); |
| 6925 | LY_CHECK_ERR_GOTO(!ext_p, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
| 6926 | ext_p->name = lydict_insert(ctx->ctx, "md:annotation", 0); |
| 6927 | ext_p->argument = lydict_insert(ctx->ctx, "default", 0); |
| 6928 | ext_p->insubstmt = LYEXT_SUBSTMT_SELF; |
| 6929 | ext_p->insubstmt_index = 0; |
| 6930 | |
| 6931 | stmt = calloc(1, sizeof *ext_p->child); |
| 6932 | stmt->stmt = lydict_insert(ctx->ctx, "type", 0); |
| 6933 | stmt->arg = lydict_insert(ctx->ctx, "boolean", 0); |
| 6934 | stmt->kw = LY_STMT_TYPE; |
| 6935 | ext_p->child = stmt; |
| 6936 | |
| 6937 | /* allocate new extension instance */ |
| 6938 | LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->exts, ext, ret, cleanup); |
| 6939 | |
| 6940 | /* manually get extension definition module */ |
| 6941 | ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata"); |
| 6942 | |
| 6943 | /* compile the extension instance */ |
| 6944 | LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup); |
| 6945 | |
| 6946 | cleanup: |
| 6947 | lysp_ext_instance_free(ctx->ctx, ext_p); |
| 6948 | free(ext_p); |
| 6949 | return ret; |
| 6950 | } |
| 6951 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 6952 | static LY_ERR |
| 6953 | lys_compile_unres(struct lysc_ctx *ctx) |
| 6954 | { |
| 6955 | struct lysc_node *node; |
| 6956 | struct lysc_type *type, *typeiter; |
| 6957 | struct lysc_type_leafref *lref; |
| 6958 | LY_ARRAY_SIZE_TYPE v; |
| 6959 | uint32_t i; |
| 6960 | |
| 6961 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6962 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6963 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
| 6964 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 6965 | node = ctx->leafrefs.objs[i]; |
| 6966 | assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST)); |
| 6967 | type = ((struct lysc_node_leaf *)node)->type; |
| 6968 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6969 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, (struct lysc_type_leafref *)type)); |
| 6970 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6971 | LY_ARRAY_FOR(((struct lysc_type_union *)type)->types, v) { |
| 6972 | if (((struct lysc_type_union *)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6973 | lref = (struct lysc_type_leafref *)((struct lysc_type_union *)type)->types[v]; |
| 6974 | LY_CHECK_RET(lys_compile_unres_leafref(ctx, node, lref)); |
| 6975 | } |
| 6976 | } |
| 6977 | } |
| 6978 | } |
| 6979 | for (i = 0; i < ctx->leafrefs.count; ++i) { |
| 6980 | /* store pointer to the real type */ |
| 6981 | type = ((struct lysc_node_leaf *)ctx->leafrefs.objs[i])->type; |
| 6982 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6983 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6984 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6985 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6986 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 6987 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6988 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6989 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6990 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6991 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6992 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6993 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6994 | } |
| 6995 | } |
| 6996 | } |
| 6997 | } |
| 6998 | |
| 6999 | /* check xpath */ |
| 7000 | for (i = 0; i < ctx->xpath.count; ++i) { |
| 7001 | LY_CHECK_RET(lys_compile_unres_xpath(ctx, ctx->xpath.objs[i])); |
| 7002 | } |
| 7003 | |
| 7004 | /* finish incomplete default values compilation */ |
| 7005 | for (i = 0; i < ctx->dflts.count; ++i) { |
| 7006 | struct ly_err_item *err = NULL; |
| 7007 | struct lysc_incomplete_dflt *r = ctx->dflts.objs[i]; |
| 7008 | LY_ERR ret; |
| 7009 | ret = r->dflt->realtype->plugin->store(ctx->ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original), |
| 7010 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 7011 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 7012 | if (err) { |
| 7013 | ly_err_print(err); |
| 7014 | ctx->path[0] = '\0'; |
| 7015 | lysc_path(r->context_node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 7016 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 7017 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 7018 | ly_err_free(err); |
| 7019 | } |
| 7020 | LY_CHECK_RET(ret); |
| 7021 | } |
| 7022 | |
| 7023 | return LY_SUCCESS; |
| 7024 | } |
| 7025 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7026 | LY_ERR |
| 7027 | lys_compile(struct lys_module *mod, int options) |
| 7028 | { |
| 7029 | struct lysc_ctx ctx = {0}; |
| 7030 | struct lysc_module *mod_c; |
| 7031 | struct lysp_module *sp; |
| 7032 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7033 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7034 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7035 | struct lys_module *m; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7036 | LY_ARRAY_SIZE_TYPE u, v; |
| 7037 | uint32_t i; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7038 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7039 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7040 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 7041 | |
| 7042 | if (!mod->implemented) { |
| 7043 | /* just imported modules are not compiled */ |
| 7044 | return LY_SUCCESS; |
| 7045 | } |
| 7046 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7047 | sp = mod->parsed; |
| 7048 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7049 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7050 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7051 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7052 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7053 | ctx.path_len = 1; |
| 7054 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7055 | |
| 7056 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 7057 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 7058 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7059 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7060 | 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] | 7061 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7062 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 7063 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7064 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7065 | |
| 7066 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7067 | if (mod->off_features) { |
| 7068 | /* there is already precompiled array of features */ |
| 7069 | mod_c->features = mod->off_features; |
| 7070 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7071 | } else { |
| 7072 | /* features are compiled directly into the compiled module structure, |
| 7073 | * 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] | 7074 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7075 | LY_CHECK_GOTO(ret, error); |
| 7076 | } |
| 7077 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 7078 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 7079 | * are present. */ |
| 7080 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7081 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7082 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7083 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7084 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7085 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7086 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7087 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7088 | 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] | 7089 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 7090 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7091 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7092 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7093 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 7094 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7095 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7096 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7097 | COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7098 | if (sp->identities) { |
| 7099 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 7100 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 7101 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7102 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7103 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7104 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7105 | 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] | 7106 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7107 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7108 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 7109 | 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] | 7110 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7111 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7112 | 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] | 7113 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7114 | LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7115 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 7116 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7117 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7118 | LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7119 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 7120 | /* extension instances TODO cover extension instances from submodules */ |
| 7121 | 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] | 7122 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 7123 | /* finish compilation for all unresolved items in the context */ |
| 7124 | LY_CHECK_GOTO(ret = lys_compile_unres(&ctx), error); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7125 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 7126 | /* validate non-instantiated groupings from the parsed schema, |
| 7127 | * without it we would accept even the schemas with invalid grouping specification */ |
| 7128 | ctx.options |= LYSC_OPT_GROUPING; |
| 7129 | LY_ARRAY_FOR(sp->groupings, u) { |
| 7130 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7131 | 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] | 7132 | } |
| 7133 | } |
| 7134 | LY_LIST_FOR(sp->data, node_p) { |
| 7135 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 7136 | LY_ARRAY_FOR(grps, u) { |
| 7137 | if (!(grps[u].flags & LYS_USED_GRP)) { |
Radek Krejci | d3acfce | 2019-09-09 14:48:50 +0200 | [diff] [blame] | 7138 | 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] | 7139 | } |
| 7140 | } |
| 7141 | } |
| 7142 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 7143 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 7144 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 7145 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 7146 | } |
| 7147 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 7148 | #if 0 |
| 7149 | /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang |
| 7150 | * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the |
| 7151 | * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have |
| 7152 | * the anotation definitions available in the internal schema structure. */ |
| 7153 | if (ly_strequal(mod->name, "ietf-netconf", 0)) { |
| 7154 | if (lyp_add_ietf_netconf_annotations(mod)) { |
| 7155 | lys_free(mod, NULL, 1, 1); |
| 7156 | return NULL; |
| 7157 | } |
| 7158 | } |
| 7159 | #endif |
| 7160 | |
| 7161 | /* add ietf-netconf-with-defaults "default" metadata to the compiled module */ |
| 7162 | if (!strcmp(mod->name, "ietf-netconf-with-defaults")) { |
| 7163 | LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error); |
| 7164 | } |
| 7165 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7166 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 7167 | ly_set_erase(&ctx.xpath, NULL); |
| 7168 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7169 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7170 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7171 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 7172 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7173 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7174 | lysp_module_free(mod->parsed); |
| 7175 | ((struct lys_module*)mod)->parsed = NULL; |
| 7176 | } |
| 7177 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 7178 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7179 | /* remove flag of the modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7180 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7181 | m = ctx.ctx->list.objs[i]; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7182 | if (m->implemented == 2) { |
| 7183 | m->implemented = 1; |
| 7184 | } |
| 7185 | } |
| 7186 | } |
| 7187 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7188 | ((struct lys_module*)mod)->compiled = mod_c; |
| 7189 | return LY_SUCCESS; |
| 7190 | |
| 7191 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7192 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 7193 | ly_set_erase(&ctx.dflts, free); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame^] | 7194 | ly_set_erase(&ctx.xpath, NULL); |
| 7195 | ly_set_erase(&ctx.leafrefs, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 7196 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 7197 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7198 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7199 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7200 | mod->compiled = NULL; |
| 7201 | |
| 7202 | /* revert compilation of modules implemented by dependency */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 7203 | for (i = 0; i < ctx.ctx->list.count; ++i) { |
| 7204 | m = ctx.ctx->list.objs[i]; |
Michal Vasko | 2947ce1 | 2019-12-16 10:37:19 +0100 | [diff] [blame] | 7205 | if ((m->implemented == 2) && m->compiled) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 7206 | /* revert features list to the precompiled state */ |
| 7207 | lys_feature_precompile_revert(&ctx, m); |
| 7208 | /* mark module as imported-only / not-implemented */ |
| 7209 | m->implemented = 0; |
| 7210 | /* free the compiled version of the module */ |
| 7211 | lysc_module_free(m->compiled, NULL); |
| 7212 | m->compiled = NULL; |
| 7213 | } |
| 7214 | } |
| 7215 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 7216 | return ret; |
| 7217 | } |