Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file schema_compile.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 19a0902 | 2021-06-15 11:54:08 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 5 | * @brief Header for schema compilation. |
| 6 | * |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #ifndef LY_SCHEMA_COMPILE_H_ |
| 17 | #define LY_SCHEMA_COMPILE_H_ |
| 18 | |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
| 21 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 22 | #include "log.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 23 | #include "plugins_exts.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 24 | #include "set.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 25 | #include "tree.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 26 | #include "tree_schema.h" |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 27 | #include "tree_schema_free.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 28 | |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 29 | struct lyxp_expr; |
| 30 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 31 | /** |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 32 | * @brief YANG schema compilation context. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 33 | */ |
| 34 | struct lysc_ctx { |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 35 | struct ly_ctx *ctx; /**< libyang context */ |
Michal Vasko | b8df576 | 2021-01-12 15:15:53 +0100 | [diff] [blame] | 36 | struct lys_module *cur_mod; /**< module currently being compiled, |
| 37 | - identifier/path - used as the current module for unprefixed nodes |
| 38 | - augment - module where the augment is defined |
| 39 | - deviation - module where the deviation is defined |
| 40 | - uses - module where the uses is defined */ |
| 41 | struct lysp_module *pmod; /**< parsed module being processed, |
| 42 | - identifier/path - used for searching imports to resolve prefixed nodes |
| 43 | - augment - module where the augment is defined |
| 44 | - deviation - module where the deviation is defined |
| 45 | - uses - module where the grouping is defined */ |
Radek Krejci | 6b88a46 | 2021-02-17 12:39:34 +0100 | [diff] [blame] | 46 | struct lysc_ext_instance *ext; /**< extension instance being processed and serving as a source for its substatements |
| 47 | instead of the module itself */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 48 | struct ly_set groupings; /**< stack for groupings circular check */ |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 49 | struct ly_set tpdf_chain; /**< stack for typedefs circular check */ |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 50 | struct ly_set augs; /**< set of compiled non-applied top-level augments (stored ::lysc_augment *) */ |
| 51 | struct ly_set devs; /**< set of compiled non-applied deviations (stored ::lysc_deviation *) */ |
| 52 | struct ly_set uses_augs; /**< set of compiled non-applied uses augments (stored ::lysc_augment *) */ |
| 53 | struct ly_set uses_rfns; /**< set of compiled non-applied uses refines (stored ::lysc_refine *) */ |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 54 | struct lys_depset_unres *unres; /**< dependency set unres sets */ |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 55 | uint32_t path_len; /**< number of path bytes used */ |
Michal Vasko | 7c56592 | 2021-06-10 14:58:27 +0200 | [diff] [blame] | 56 | uint32_t compile_opts; /**< various @ref scflags. */ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 57 | struct lysf_ctx free_ctx; /**< freeing context for errors/recompilation */ |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 58 | |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 59 | #define LYSC_CTX_BUFSIZE 4078 |
Radek Krejci | 5f9a367 | 2021-03-05 21:35:22 +0100 | [diff] [blame] | 60 | char path[LYSC_CTX_BUFSIZE];/**< Path identifying the schema node currently being processed */ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /** |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 64 | * @brief Initalize local compilation context using libyang context. |
| 65 | * |
| 66 | * @param[out] CCTX Compile context. |
| 67 | * @param[in] CTX libyang context. |
| 68 | */ |
| 69 | #define LYSC_CTX_INIT_CTX(CCTX, CTX) \ |
| 70 | memset(&(CCTX), 0, sizeof (CCTX)); \ |
| 71 | (CCTX).ctx = (CTX); \ |
| 72 | (CCTX).path_len = 1; \ |
| 73 | (CCTX).path[0] = '/'; \ |
| 74 | (CCTX).free_ctx.ctx = (CTX) |
| 75 | |
| 76 | /** |
| 77 | * @brief Initalize local compilation context using a parsed module. |
| 78 | * |
| 79 | * @param[out] CCTX Compile context. |
| 80 | * @param[in] PMOD Parsed module. |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 81 | * @param[in] EXT Ancestor extension instance. |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 82 | */ |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 83 | #define LYSC_CTX_INIT_PMOD(CCTX, PMOD, EXT) \ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 84 | memset(&(CCTX), 0, sizeof (CCTX)); \ |
| 85 | (CCTX).ctx = (PMOD)->mod->ctx; \ |
| 86 | (CCTX).cur_mod = (PMOD)->mod; \ |
| 87 | (CCTX).pmod = (PMOD); \ |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 88 | (CCTX).ext = (EXT); \ |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 89 | (CCTX).path_len = 1; \ |
| 90 | (CCTX).path[0] = '/'; \ |
| 91 | (CCTX).free_ctx.ctx = (PMOD)->mod->ctx |
| 92 | |
| 93 | /** |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 94 | * @brief Structure for unresolved items that may depend on any implemented module data in the dependency set |
| 95 | * so their resolution can only be performed after the whole dep set compilation is done. |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 96 | */ |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 97 | struct lys_depset_unres { |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 98 | struct ly_set whens; /**< nodes with when to check */ |
| 99 | struct ly_set musts; /**< set of musts to check */ |
aPiecek | c6526b4 | 2021-07-12 15:21:39 +0200 | [diff] [blame] | 100 | struct ly_set leafrefs; /**< to validate target of leafrefs */ |
| 101 | struct ly_set dflts; /**< set of incomplete default values */ |
Michal Vasko | f4fa90d | 2021-11-11 15:05:19 +0100 | [diff] [blame] | 102 | struct ly_set disabled; /**< set of compiled nodes whose if-feature(s) was not satisfied |
| 103 | (stored ::lysc_node *) */ |
aPiecek | c6526b4 | 2021-07-12 15:21:39 +0200 | [diff] [blame] | 104 | struct ly_set disabled_leafrefs; /**< subset of the lys_depset_unres.disabled to validate target of disabled leafrefs */ |
Michal Vasko | f4fa90d | 2021-11-11 15:05:19 +0100 | [diff] [blame] | 105 | struct ly_set disabled_bitenums; /**< set of enumation/bits leaves/leaf-lists with bits/enums to disable |
| 106 | (stored ::lysc_node_leaf *) */ |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /** |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 110 | * @brief Unres structure global for compilation. |
| 111 | */ |
| 112 | struct lys_glob_unres { |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 113 | struct ly_set dep_sets; /**< set of dependency sets of modules, see ::lys_compile_depset_all() */ |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 114 | struct ly_set implementing; /**< set of YANG schemas being atomically implemented (compiled); the first added |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 115 | module is always the explicitly implemented module, the other ones are dependencies */ |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 116 | struct ly_set creating; /**< set of YANG schemas being atomically created (parsed); it is a subset of implemented |
| 117 | and all these modules are freed if any error occurs */ |
| 118 | struct lys_depset_unres ds_unres; /**< unres specific for the current dependency set */ |
| 119 | }; |
| 120 | |
| 121 | /** |
Michal Vasko | 4ed3bd5 | 2022-12-02 10:28:04 +0100 | [diff] [blame^] | 122 | * @brief Structure for storing schema node with a when expression. |
| 123 | */ |
| 124 | struct lysc_unres_when { |
| 125 | struct lysc_node *node; /**< node with the when expression */ |
| 126 | struct lysc_when *when; /**< one when expression of the node */ |
| 127 | }; |
| 128 | |
| 129 | /** |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 130 | * @brief Structure for storing schema nodes with must expressions and local module for each of them. |
| 131 | */ |
| 132 | struct lysc_unres_must { |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 133 | struct lysc_node *node; /**< node with the must expression(s) */ |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 134 | const struct lysp_module **local_mods; /**< sized array of local modules for must(s) */ |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 135 | struct lysc_ext_instance *ext; /**< ancestor extension instance of the must(s) */ |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * @brief Structure for storing leafref node and its local module. |
| 140 | */ |
| 141 | struct lysc_unres_leafref { |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 142 | struct lysc_node *node; /**< leaf/leaf-list node with leafref type */ |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 143 | const struct lysp_module *local_mod; /**< local module of the leafref type */ |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 144 | struct lysc_ext_instance *ext; /**< ancestor extension instance of the leafref */ |
Michal Vasko | c130e16 | 2021-10-19 11:30:00 +0200 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 148 | * @brief Structure for remembering default values of leaves and leaf-lists. They are resolved at schema compilation |
| 149 | * end when the whole schema tree is available. |
| 150 | */ |
| 151 | struct lysc_unres_dflt { |
| 152 | union { |
| 153 | struct lysc_node_leaf *leaf; |
| 154 | struct lysc_node_leaflist *llist; |
| 155 | }; |
| 156 | struct lysp_qname *dflt; |
| 157 | struct lysp_qname *dflts; /**< this is a sized array */ |
| 158 | }; |
| 159 | |
| 160 | /** |
| 161 | * @brief Duplicate string into dictionary |
| 162 | * @param[in] CTX libyang context of the dictionary. |
| 163 | * @param[in] ORIG String to duplicate. |
| 164 | * @param[out] DUP Where to store the result. |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 165 | * @param[out] RET Where to store the return code. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 166 | */ |
Michal Vasko | 899c7ce | 2022-02-18 09:18:37 +0100 | [diff] [blame] | 167 | #define DUP_STRING(CTX, ORIG, DUP, RET) RET = lydict_insert(CTX, ORIG, 0, &(DUP)) |
| 168 | #define DUP_STRING_RET(CTX, ORIG, DUP) LY_CHECK_RET(lydict_insert(CTX, ORIG, 0, &(DUP))) |
| 169 | #define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &(DUP)), GOTO) |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 170 | |
| 171 | #define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \ |
| 172 | if (ORIG_ARRAY) { \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 173 | LY_ARRAY_COUNT_TYPE __u; \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 174 | LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 175 | LY_ARRAY_FOR(ORIG_ARRAY, __u) { \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 176 | LY_ARRAY_INCREMENT(NEW_ARRAY); \ |
Michal Vasko | c621d86 | 2022-11-08 14:23:45 +0100 | [diff] [blame] | 177 | LY_CHECK_RET(DUP_FUNC(CTX, &(ORIG_ARRAY)[__u], &(NEW_ARRAY)[__u])); \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 178 | } \ |
| 179 | } |
| 180 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 181 | #define DUP_ARRAY2(CTX, PMOD, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \ |
| 182 | if (ORIG_ARRAY) { \ |
| 183 | LY_ARRAY_COUNT_TYPE __u; \ |
| 184 | LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \ |
| 185 | LY_ARRAY_FOR(ORIG_ARRAY, __u) { \ |
| 186 | LY_ARRAY_INCREMENT(NEW_ARRAY); \ |
Michal Vasko | c621d86 | 2022-11-08 14:23:45 +0100 | [diff] [blame] | 187 | LY_CHECK_RET(DUP_FUNC(CTX, PMOD, &(ORIG_ARRAY)[__u], &(NEW_ARRAY)[__u])); \ |
| 188 | } \ |
| 189 | } |
| 190 | |
| 191 | #define DUP_EXTS(CTX, PMOD, PARENT, PARENT_STMT, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \ |
| 192 | if (ORIG_ARRAY) { \ |
Michal Vasko | 086a38b | 2022-11-10 10:47:10 +0100 | [diff] [blame] | 193 | LY_ARRAY_COUNT_TYPE __u, __new_start; \ |
| 194 | __new_start = LY_ARRAY_COUNT(NEW_ARRAY); \ |
Michal Vasko | c621d86 | 2022-11-08 14:23:45 +0100 | [diff] [blame] | 195 | LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \ |
| 196 | LY_ARRAY_FOR(ORIG_ARRAY, __u) { \ |
| 197 | LY_ARRAY_INCREMENT(NEW_ARRAY); \ |
Michal Vasko | 086a38b | 2022-11-10 10:47:10 +0100 | [diff] [blame] | 198 | LY_CHECK_RET(DUP_FUNC(CTX, PMOD, PARENT, PARENT_STMT, &(ORIG_ARRAY)[__u], &(NEW_ARRAY)[__new_start + __u])); \ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 199 | } \ |
| 200 | } |
| 201 | |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 202 | #define COMPILE_OP_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, FUNC, USES_STATUS, RET, GOTO) \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 203 | if (ARRAY_P) { \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 204 | LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \ |
| 205 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 206 | LY_ARRAY_FOR(ARRAY_P, __u) { \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 207 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 208 | RET = FUNC(CTX, &(ARRAY_P)[__u], PARENT, &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1], USES_STATUS); \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 209 | if (RET == LY_EDENIED) { \ |
| 210 | LY_ARRAY_DECREMENT(ARRAY_C); \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 211 | RET = LY_SUCCESS; \ |
| 212 | } else if (RET) { \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 213 | goto GOTO; \ |
| 214 | } \ |
| 215 | } \ |
| 216 | } |
| 217 | |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 218 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, FUNC, RET, GOTO) \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 219 | if (ARRAY_P) { \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 220 | LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \ |
| 221 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \ |
| 222 | LY_ARRAY_FOR(ARRAY_P, __u) { \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 223 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 224 | RET = FUNC(CTX, &(ARRAY_P)[__u], &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1]); \ |
| 225 | LY_CHECK_GOTO(RET, GOTO); \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 226 | } \ |
| 227 | } |
| 228 | |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 229 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, RET, GOTO) \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 230 | if (EXTS_P) { \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 231 | LY_ARRAY_COUNT_TYPE __u = (EXT_C) ? LY_ARRAY_COUNT(EXT_C) : 0; \ |
| 232 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, __u + LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \ |
| 233 | LY_ARRAY_FOR(EXTS_P, __u) { \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 234 | LY_ARRAY_INCREMENT(EXT_C); \ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 235 | RET = lys_compile_ext(CTX, &(EXTS_P)[__u], &(EXT_C)[LY_ARRAY_COUNT(EXT_C) - 1], PARENT); \ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 236 | if (RET == LY_ENOT) { \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 237 | LY_ARRAY_DECREMENT(EXT_C); \ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 238 | RET = LY_SUCCESS; \ |
Michal Vasko | 5347e3a | 2020-11-03 17:14:57 +0100 | [diff] [blame] | 239 | } else if (RET) { \ |
| 240 | goto GOTO; \ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 241 | } \ |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 242 | } \ |
| 243 | } |
| 244 | |
| 245 | /** |
Michal Vasko | a0ba01e | 2022-10-19 13:26:57 +0200 | [diff] [blame] | 246 | * @brief Update path in the compile context, which is used for logging where the compilation failed. |
| 247 | * |
| 248 | * @param[in] ctx Compile context with the path. |
| 249 | * @param[in] parent_module Module of the current node's parent to check difference with the currently processed module |
| 250 | * (taken from @p ctx). |
| 251 | * @param[in] name Name of the node to update path with. If NULL, the last segment is removed. If the format is |
| 252 | * `{keyword}`, the following call updates the segment to the form `{keyword='name'}` (to remove this compound segment, |
| 253 | * 2 calls with NULL @p name must be used). |
| 254 | */ |
| 255 | void lysc_update_path(struct lysc_ctx *ctx, const struct lys_module *parent_module, const char *name); |
| 256 | |
| 257 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 258 | * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance. |
| 259 | * |
| 260 | * @param[in] ctx Compilation context. |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 261 | * @param[in] extp Parsed extension instance. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 262 | * @param[in,out] ext Prepared compiled extension instance. |
| 263 | * @param[in] parent Extension instance parent. |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 264 | * @return LY_SUCCESS on success. |
| 265 | * @return LY_ENOT if the extension is disabled and should be ignored. |
| 266 | * @return LY_ERR on error. |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 267 | */ |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 268 | LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *extp, struct lysc_ext_instance *ext, void *parent); |
| 269 | |
| 270 | /** |
| 271 | * @brief Compile information from the identity statement |
| 272 | * |
| 273 | * The backlinks to the identities derived from this one are supposed to be filled later via ::lys_compile_identity_bases(). |
| 274 | * |
| 275 | * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod. |
| 276 | * @param[in] ctx libyang context. |
| 277 | * @param[in] parsed_mod Module with the identities. |
| 278 | * @param[in] identities_p Array of the parsed identity definitions to precompile. |
| 279 | * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are |
| 280 | * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going |
| 281 | * to be processed. |
| 282 | * @return LY_ERR value. |
| 283 | */ |
| 284 | LY_ERR lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod, |
| 285 | const struct lysp_ident *identities_p, struct lysc_ident **identities); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 286 | |
| 287 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 288 | * @brief Find and process the referenced base identities from another identity or identityref |
| 289 | * |
| 290 | * For bases in identity set backlinks to them from the base identities. For identityref, store |
| 291 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 292 | * to distinguish these two use cases. |
| 293 | * |
| 294 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 295 | * @param[in] base_pmod Module where to resolve @p bases_p prefixes. |
| 296 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 297 | * @param[in] ident Referencing identity to work with, NULL for identityref. |
| 298 | * @param[in] bases Array of bases of identityref to fill in. |
| 299 | * @return LY_ERR value. |
| 300 | */ |
| 301 | LY_ERR lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p, |
aPiecek | f4a0a19 | 2021-08-03 15:14:17 +0200 | [diff] [blame] | 302 | struct lysc_ident *ident, struct lysc_ident ***bases); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 303 | |
| 304 | /** |
aPiecek | 6b3d542 | 2021-07-30 15:55:43 +0200 | [diff] [blame] | 305 | * @brief Perform a complet compilation of identites in a module and all its submodules. |
| 306 | * |
| 307 | * @param[in] mod Module to process. |
| 308 | * @return LY_ERR value. |
| 309 | */ |
| 310 | LY_ERR lys_compile_identities(struct lys_module *mod); |
| 311 | |
| 312 | /** |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 313 | * @brief Compile schema into a validated schema linking all the references. Must have been implemented before. |
| 314 | * |
| 315 | * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed |
| 316 | * member is used as input and ::lys_module#compiled is used to hold the result of the compilation. |
| 317 | * @param[in,out] unres Dep set unres structure to add to. |
| 318 | * @return LY_SUCCESS on success. |
| 319 | * @return LY_ERR on error. |
| 320 | */ |
| 321 | LY_ERR lys_compile(struct lys_module *mod, struct lys_depset_unres *unres); |
| 322 | |
| 323 | /** |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 324 | * @brief Check statement's status for invalid combination. |
| 325 | * |
| 326 | * The modX parameters are used just to determine if both flags are in the same module, |
| 327 | * so any of the schema module structure can be used, but both modules must be provided |
| 328 | * in the same type. |
| 329 | * |
| 330 | * @param[in] ctx Compile context for logging. |
| 331 | * @param[in] flags1 Flags of the referencing node. |
| 332 | * @param[in] mod1 Module of the referencing node, |
| 333 | * @param[in] name1 Schema node name of the referencing node. |
| 334 | * @param[in] flags2 Flags of the referenced node. |
| 335 | * @param[in] mod2 Module of the referenced node, |
| 336 | * @param[in] name2 Schema node name of the referenced node. |
| 337 | * @return LY_ERR value |
| 338 | */ |
| 339 | LY_ERR lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2, |
| 340 | void *mod2, const char *name2); |
| 341 | |
| 342 | /** |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 343 | * @brief Check parsed expression for any prefixes of unimplemented modules. |
| 344 | * |
| 345 | * @param[in] ctx libyang context. |
| 346 | * @param[in] expr Parsed expression. |
| 347 | * @param[in] format Prefix format. |
| 348 | * @param[in] prefix_data Format-specific data (see ::ly_resolve_prefix()). |
| 349 | * @param[in] implement Whether all the non-implemented modules should are implemented or the first |
| 350 | * non-implemented module, if any, returned in @p mod_p. |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 351 | * @param[in,out] unres Global unres structure of newly implemented modules. |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 352 | * @param[out] mod_p Module that is not implemented. |
| 353 | * @return LY_SUCCESS on success. |
Michal Vasko | 40c158c | 2021-04-28 17:01:03 +0200 | [diff] [blame] | 354 | * @return LY_ERECOMPILE if @p implement is set. |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 355 | * @return LY_ERR on error. |
| 356 | */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 357 | LY_ERR lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_VALUE_FORMAT format, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 358 | void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p); |
| 359 | |
| 360 | /** |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 361 | * @brief Compile all flagged modules in a dependency set, recursively if recompilation is needed. |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 362 | * |
Michal Vasko | 50bc09a | 2021-06-17 17:31:56 +0200 | [diff] [blame] | 363 | * Steps taken when adding a new module (::ly_ctx_load_module(), ::lys_parse()): |
| 364 | * |
| 365 | * 1) parse module and add it into context with all imports and includes also parsed and in context |
Michal Vasko | 6598824 | 2021-07-15 09:19:16 +0200 | [diff] [blame] | 366 | * (::lys_parse_load(), ::lys_parse_in(), lys_parse_localfile() - static) |
Michal Vasko | 50bc09a | 2021-06-17 17:31:56 +0200 | [diff] [blame] | 367 | * 2) implement it (perform one-time compilation tasks - compile identities and add reference to augment/deviation |
| 368 | * target modules, implement those as well, ::_lys_set_implemented()) |
| 369 | * 3) create dep set of the module (::lys_unres_dep_sets_create()) |
| 370 | * 4) (re)compile all the modules in the dep set and collect unres (::lys_compile_dep_set_r()) |
Michal Vasko | 6598824 | 2021-07-15 09:19:16 +0200 | [diff] [blame] | 371 | * 5) resolve unres (lys_compile_unres_depset() - static), new modules may be implemented like in 2) and if |
| 372 | * require recompilation, free all compiled modules and do 4) |
Michal Vasko | 50bc09a | 2021-06-17 17:31:56 +0200 | [diff] [blame] | 373 | * 6) all modules that needed to be (re)compiled are now, with all their dependencies |
| 374 | * |
| 375 | * What can cause new modules to be implemented when resolving unres in 5): |
| 376 | * - leafref |
| 377 | * - when, must |
Michal Vasko | 41369bd | 2021-06-23 12:03:23 +0200 | [diff] [blame] | 378 | * - identityref, instance-identifier default value |
Michal Vasko | 50bc09a | 2021-06-17 17:31:56 +0200 | [diff] [blame] | 379 | * - new implemented module augments, deviations |
| 380 | * |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 381 | * @param[in] ctx libyang context. |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 382 | * @param[in,out] unres Global unres to use. |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 383 | * @return LY_ERR value. |
| 384 | */ |
Michal Vasko | 709f9a5 | 2021-07-21 10:51:59 +0200 | [diff] [blame] | 385 | LY_ERR lys_compile_depset_all(struct ly_ctx *ctx, struct lys_glob_unres *unres); |
Michal Vasko | 916aefb | 2020-11-02 15:43:16 +0100 | [diff] [blame] | 386 | |
| 387 | /** |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 388 | * @brief Implement a single module. Does not actually compile, only marks to_compile! |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 389 | * |
| 390 | * @param[in] mod Module to implement. |
| 391 | * @param[in] features Features to set, see ::lys_set_features(). |
Michal Vasko | a9f807e | 2021-06-15 12:07:16 +0200 | [diff] [blame] | 392 | * @param[in,out] unres Global unres to use. |
| 393 | * @return LY_ERR value. |
Michal Vasko | 6533388 | 2021-06-10 14:12:16 +0200 | [diff] [blame] | 394 | */ |
| 395 | LY_ERR lys_implement(struct lys_module *mod, const char **features, struct lys_glob_unres *unres); |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 396 | |
| 397 | #endif /* LY_SCHEMA_COMPILE_H_ */ |