blob: d186fb088d2abc1efba27aca5e632466158c7619 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Header for schema compilation.
5 *
6 * Copyright (c) 2015 - 2020 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
15#ifndef LY_SCHEMA_COMPILE_H_
16#define LY_SCHEMA_COMPILE_H_
17
Radek Krejci47fab892020-11-05 17:02:41 +010018#include <stddef.h>
19#include <stdint.h>
20
21#include "common.h"
22#include "dict.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020023#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020024#include "set.h"
Radek Krejci47fab892020-11-05 17:02:41 +010025#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010026#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020027#include "tree_schema.h"
28
Radek Krejci47fab892020-11-05 17:02:41 +010029struct lyxp_expr;
30
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020031/**
Radek Krejci5f9a3672021-03-05 21:35:22 +010032 * @brief YANG schema compilation context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020033 */
34struct lysc_ctx {
Radek Krejci5f9a3672021-03-05 21:35:22 +010035 struct ly_ctx *ctx; /**< libyang context */
Michal Vaskob8df5762021-01-12 15:15:53 +010036 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 Krejci6b88a462021-02-17 12:39:34 +010046 struct lysc_ext_instance *ext; /**< extension instance being processed and serving as a source for its substatements
47 instead of the module itself */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020048 struct ly_set groupings; /**< stack for groupings circular check */
Radek Krejci5f9a3672021-03-05 21:35:22 +010049 struct ly_set tpdf_chain; /**< stack for typedefs circular check */
Radek Krejci5f9a3672021-03-05 21:35:22 +010050 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 Vasko405cc9e2020-12-01 12:01:27 +010054 struct lys_glob_unres *unres; /**< global unres sets */
Radek Krejci5f9a3672021-03-05 21:35:22 +010055 uint32_t path_len; /**< number of path bytes used */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020056 uint32_t options; /**< various @ref scflags. */
57#define LYSC_CTX_BUFSIZE 4078
Radek Krejci5f9a3672021-03-05 21:35:22 +010058 char path[LYSC_CTX_BUFSIZE];/**< Path identifying the schema node currently being processed */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020059};
60
61/**
Michal Vasko405cc9e2020-12-01 12:01:27 +010062 * @brief Structure for unresolved items that may depend on any implemented module data so their resolution
63 * can only be performed after all module basic compilation is done.
64 */
65struct lys_glob_unres {
66 struct ly_set implementing; /**< set of YANG schemas being atomically implemented (compiled); the first added
67 module is always the explcitly implemented module, the other ones are dependencies */
68 struct ly_set creating; /**< set of YANG schemas being atomically created (parsed); it is a subset of implemented
69 and all these modules are freed if any error occurs */
70 struct ly_set xpath; /**< when/must to check */
71 struct ly_set leafrefs; /**< to validate leafref's targets */
72 struct ly_set dflts; /**< set of incomplete default values */
Michal Vasko30ab8e72021-04-19 12:47:52 +020073 struct ly_set disabled; /**< set of compiled nodes whose if-feature(s) was not satisfied (stored ::lysc_node *) */
Michal Vasko1ccbf542021-04-19 11:35:00 +020074 ly_bool full_compilation; /**< flag marking that all the currently implemented modules were compiled in this
75 compilation (meaning that all their disabled nodes are still present) */
Michal Vasko405cc9e2020-12-01 12:01:27 +010076};
77
78/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020079 * @brief Structure for remembering default values of leaves and leaf-lists. They are resolved at schema compilation
80 * end when the whole schema tree is available.
81 */
82struct lysc_unres_dflt {
83 union {
84 struct lysc_node_leaf *leaf;
85 struct lysc_node_leaflist *llist;
86 };
87 struct lysp_qname *dflt;
88 struct lysp_qname *dflts; /**< this is a sized array */
89};
90
91/**
92 * @brief Duplicate string into dictionary
93 * @param[in] CTX libyang context of the dictionary.
94 * @param[in] ORIG String to duplicate.
95 * @param[out] DUP Where to store the result.
96 */
97#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
Radek Krejci771928a2021-01-19 13:42:36 +010098#define DUP_STRING_RET(CTX, ORIG, DUP) if (ORIG) {LY_ERR __ret = lydict_insert(CTX, ORIG, 0, &DUP); LY_CHECK_RET(__ret);}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020099#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
100
101#define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \
102 if (ORIG_ARRAY) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100103 LY_ARRAY_COUNT_TYPE __u; \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200104 LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100105 LY_ARRAY_FOR(ORIG_ARRAY, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200106 LY_ARRAY_INCREMENT(NEW_ARRAY); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100107 LY_CHECK_RET(DUP_FUNC(CTX, &(NEW_ARRAY)[__u], &(ORIG_ARRAY)[__u])); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200108 } \
109 }
110
Michal Vasko5347e3a2020-11-03 17:14:57 +0100111#define COMPILE_OP_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, FUNC, USES_STATUS, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200112 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100113 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
114 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
115 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200116 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100117 RET = FUNC(CTX, &(ARRAY_P)[__u], PARENT, &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1], USES_STATUS); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200118 if (RET == LY_EDENIED) { \
119 LY_ARRAY_DECREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100120 RET = LY_SUCCESS; \
121 } else if (RET) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200122 goto GOTO; \
123 } \
124 } \
125 }
126
Michal Vasko5347e3a2020-11-03 17:14:57 +0100127#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, FUNC, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200128 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100129 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
130 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
131 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200132 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100133 RET = FUNC(CTX, &(ARRAY_P)[__u], &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1]); \
134 LY_CHECK_GOTO(RET, GOTO); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200135 } \
136 }
137
Radek Krejciab430862021-03-02 20:13:40 +0100138#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200139 if (EXTS_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100140 LY_ARRAY_COUNT_TYPE __u = (EXT_C) ? LY_ARRAY_COUNT(EXT_C) : 0; \
141 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, __u + LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
142 LY_ARRAY_FOR(EXTS_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200143 LY_ARRAY_INCREMENT(EXT_C); \
Radek Krejciab430862021-03-02 20:13:40 +0100144 RET = lys_compile_ext(CTX, &(EXTS_P)[__u], &(EXT_C)[LY_ARRAY_COUNT(EXT_C) - 1], PARENT, NULL); \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100145 if (RET == LY_ENOT) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100146 LY_ARRAY_DECREMENT(EXT_C); \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100147 RET = LY_SUCCESS; \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100148 } else if (RET) { \
149 goto GOTO; \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100150 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200151 } \
152 }
153
154/**
155 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
156 *
157 * @param[in] ctx Compilation context.
158 * @param[in] ext_p Parsed extension instance.
159 * @param[in,out] ext Prepared compiled extension instance.
160 * @param[in] parent Extension instance parent.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200161 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100162 * @return LY_SUCCESS on success.
163 * @return LY_ENOT if the extension is disabled and should be ignored.
164 * @return LY_ERR on error.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200165 */
166LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
Radek Krejciab430862021-03-02 20:13:40 +0100167 const struct lys_module *ext_mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200168
169/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200170 * @brief Compile information from the identity statement
171 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200172 * The backlinks to the identities derived from this one are supposed to be filled later via ::lys_compile_identity_bases().
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200173 *
174 * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
175 * @param[in] ctx libyang context.
176 * @param[in] parsed_mod Module with the identities.
177 * @param[in] identities_p Array of the parsed identity definitions to precompile.
178 * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
179 * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
180 * to be processed.
181 * @return LY_ERR value.
182 */
183LY_ERR lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
184 struct lysp_ident *identities_p, struct lysc_ident **identities);
185
186/**
187 * @brief Find and process the referenced base identities from another identity or identityref
188 *
189 * For bases in identity set backlinks to them from the base identities. For identityref, store
190 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
191 * to distinguish these two use cases.
192 *
193 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
194 * @param[in] base_pmod Module where to resolve @p bases_p prefixes.
195 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
196 * @param[in] ident Referencing identity to work with, NULL for identityref.
197 * @param[in] bases Array of bases of identityref to fill in.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100198 * @param[in] enabled Whether the base is disabled, must be set if @p ident is set.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200199 * @return LY_ERR value.
200 */
201LY_ERR lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100202 struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200203
204/**
205 * @brief Check statement's status for invalid combination.
206 *
207 * The modX parameters are used just to determine if both flags are in the same module,
208 * so any of the schema module structure can be used, but both modules must be provided
209 * in the same type.
210 *
211 * @param[in] ctx Compile context for logging.
212 * @param[in] flags1 Flags of the referencing node.
213 * @param[in] mod1 Module of the referencing node,
214 * @param[in] name1 Schema node name of the referencing node.
215 * @param[in] flags2 Flags of the referenced node.
216 * @param[in] mod2 Module of the referenced node,
217 * @param[in] name2 Schema node name of the referenced node.
218 * @return LY_ERR value
219 */
220LY_ERR lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2,
221 void *mod2, const char *name2);
222
223/**
Michal Vasko25d6ad02020-10-22 12:20:22 +0200224 * @brief Check parsed expression for any prefixes of unimplemented modules.
225 *
226 * @param[in] ctx libyang context.
227 * @param[in] expr Parsed expression.
228 * @param[in] format Prefix format.
229 * @param[in] prefix_data Format-specific data (see ::ly_resolve_prefix()).
230 * @param[in] implement Whether all the non-implemented modules should are implemented or the first
231 * non-implemented module, if any, returned in @p mod_p.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100232 * @param[in,out] unres Global unres structure of newly implemented modules.
Michal Vasko25d6ad02020-10-22 12:20:22 +0200233 * @param[out] mod_p Module that is not implemented.
234 * @return LY_SUCCESS on success.
Michal Vasko40c158c2021-04-28 17:01:03 +0200235 * @return LY_ERECOMPILE if @p implement is set.
Michal Vasko25d6ad02020-10-22 12:20:22 +0200236 * @return LY_ERR on error.
237 */
Radek Krejci8df109d2021-04-23 12:19:08 +0200238LY_ERR lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_VALUE_FORMAT format,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100239 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p);
240
241/**
242 * @brief Finish compilation of all the global unres sets.
Michal Vasko1ccbf542021-04-19 11:35:00 +0200243 * Will always finish the compilation (never return @p unres with `recompile` set).
Michal Vasko405cc9e2020-12-01 12:01:27 +0100244 *
245 * @param[in] ctx libyang context.
246 * @param[in] unres Global unres structure with the sets to resolve.
247 * @return LY_ERR value.
248 */
249LY_ERR lys_compile_unres_glob(struct ly_ctx *ctx, struct lys_glob_unres *unres);
250
251/**
252 * @brief Revert a failed compilation (free new modules, unimplement newly implemented modules).
253 *
254 * @param[in] ctx libyang context.
255 * @param[in] unres Global unres set with newly implemented modules.
256 */
257void lys_compile_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres);
258
259/**
Michal Vaskodf6319c2021-06-10 14:41:05 +0200260 * @brief Erase the global unres.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100261 *
262 * @param[in] ctx libyang context.
263 * @param[in] unres Global unres structure with the sets.
Michal Vaskodf6319c2021-06-10 14:41:05 +0200264 * @param[in] recompiled Whether to keep the set of new parsed and implemented modules.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100265 */
Michal Vaskodf6319c2021-06-10 14:41:05 +0200266void lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres, ly_bool recompiled);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200267
268/**
Michal Vasko916aefb2020-11-02 15:43:16 +0100269 * @brief Recompile the whole context based on the current flags.
270 *
271 * @param[in] ctx Context to recompile.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100272 * @param[in] log Whether to log all the errors.
Michal Vasko916aefb2020-11-02 15:43:16 +0100273 * @return LY_SUCCESS on success.
274 * @return LY_ERR on error.
275 */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100276LY_ERR lys_recompile(struct ly_ctx *ctx, ly_bool log);
Michal Vasko916aefb2020-11-02 15:43:16 +0100277
278/**
Michal Vasko65333882021-06-10 14:12:16 +0200279 * @brief Compile schema into a validated schema linking all the references.
280 *
281 * Implemented flag of @p mod must be set meaning this function should be called only if the module
282 * is being recompiled, otherwise call ::lys_implement().
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200283 *
284 * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed
285 * member is used as input and ::lys_module#compiled is used to hold the result of the compilation.
286 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Michal Vasko65333882021-06-10 14:12:16 +0200287 * @param[in,out] unres Global unres structure to add to.
288 * @return LY_SUCCESS on success.
289 * @return LY_ERR on error.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200290 */
Michal Vasko65333882021-06-10 14:12:16 +0200291LY_ERR lys_compile(struct lys_module *mod, uint32_t options, struct lys_glob_unres *unres);
292
293/**
294 * @brief Implement a single module, can be called recursively.
295 *
296 * @param[in] mod Module to implement.
297 * @param[in] features Features to set, see ::lys_set_features().
298 * @param[in,out] unres Global unres to add to.
299 * @return LY_SUCCESS on success.
300 * @return LY_ERECOMPILE on required recompilation, @p mod implemented flag is kept and
301 * is left in @p unres implementing set so that it is known that the next compilation is recompilation.
302 * @return LY_ERR on error.
303 */
304LY_ERR lys_implement(struct lys_module *mod, const char **features, struct lys_glob_unres *unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200305
306#endif /* LY_SCHEMA_COMPILE_H_ */