blob: 63665fbfaca1104fe43fe1f4e98a8d074530ab45 [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"
26#include "tree_data.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/**
32 * @defgroup scflags Schema compile flags
33 *
34 * Flags are currently used only internally - the compilation process does not have a public interface and it is
35 * integrated in the schema parsers. The current options set does not make sense for public used, but it can be a way
36 * to modify behavior of the compilation process in future.
37 *
38 * @{
39 */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010040#define LYS_COMPILE_RPC_INPUT LYS_CONFIG_W /**< Internal option when compiling schema tree of RPC/action input */
41#define LYS_COMPILE_RPC_OUTPUT LYS_CONFIG_R /**< Internal option when compiling schema tree of RPC/action output */
42#define LYS_COMPILE_RPC_MASK LYS_CONFIG_MASK /**< mask for the internal RPC options */
43#define LYS_COMPILE_NOTIFICATION 0x04 /**< Internal option when compiling schema tree of Notification */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020044
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010045#define LYS_COMPILE_GROUPING 0x08 /**< Compiling (validation) of a non-instantiated grouping.
46 In this case not all the restrictions are checked since they can
47 be valid only in the real placement of the grouping.
48 TODO - what specifically is not done */
49#define LYS_COMPILE_DISABLED 0x10 /**< Compiling a disabled subtree (by its if-features). Meaning
50 it will be removed at the end of compilation and should not be
51 added to any unres sets. */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020052/** @} scflags */
53
54/**
55 * @brief internal context for compilation
56 */
57struct lysc_ctx {
58 struct ly_ctx *ctx;
59 struct lys_module *cur_mod; /**< module currently being compiled, used as the current module for unprefixed nodes */
60 struct lysp_module *pmod; /**< parsed module being processed, used for searching imports to resolve prefixed nodes */
61 struct ly_set groupings; /**< stack for groupings circular check */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020062 struct ly_set tpdf_chain;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010063 struct ly_set disabled; /**< set of compiled nodes whose if-feature(s) was not satisifed */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020064 struct ly_set augs; /**< set of compiled non-applied top-level augments */
65 struct ly_set devs; /**< set of compiled non-applied deviations */
66 struct ly_set uses_augs; /**< set of compiled non-applied uses augments */
67 struct ly_set uses_rfns; /**< set of compiled non-applied uses refines */
Michal Vasko405cc9e2020-12-01 12:01:27 +010068 struct lys_glob_unres *unres; /**< global unres sets */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020069 uint32_t path_len;
70 uint32_t options; /**< various @ref scflags. */
71#define LYSC_CTX_BUFSIZE 4078
72 char path[LYSC_CTX_BUFSIZE];
73};
74
75/**
Michal Vasko405cc9e2020-12-01 12:01:27 +010076 * @brief Structure for unresolved items that may depend on any implemented module data so their resolution
77 * can only be performed after all module basic compilation is done.
78 */
79struct lys_glob_unres {
80 struct ly_set implementing; /**< set of YANG schemas being atomically implemented (compiled); the first added
81 module is always the explcitly implemented module, the other ones are dependencies */
82 struct ly_set creating; /**< set of YANG schemas being atomically created (parsed); it is a subset of implemented
83 and all these modules are freed if any error occurs */
84 struct ly_set xpath; /**< when/must to check */
85 struct ly_set leafrefs; /**< to validate leafref's targets */
86 struct ly_set dflts; /**< set of incomplete default values */
87 ly_bool recompile; /**< flag whether all the modules need to be recompiled (because of new deviations) */
88};
89
90/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020091 * @brief Structure for remembering default values of leaves and leaf-lists. They are resolved at schema compilation
92 * end when the whole schema tree is available.
93 */
94struct lysc_unres_dflt {
95 union {
96 struct lysc_node_leaf *leaf;
97 struct lysc_node_leaflist *llist;
98 };
99 struct lysp_qname *dflt;
100 struct lysp_qname *dflts; /**< this is a sized array */
101};
102
103/**
104 * @brief Duplicate string into dictionary
105 * @param[in] CTX libyang context of the dictionary.
106 * @param[in] ORIG String to duplicate.
107 * @param[out] DUP Where to store the result.
108 */
109#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
110
111#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
112
113#define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \
114 if (ORIG_ARRAY) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100115 LY_ARRAY_COUNT_TYPE __u; \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200116 LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100117 LY_ARRAY_FOR(ORIG_ARRAY, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200118 LY_ARRAY_INCREMENT(NEW_ARRAY); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100119 LY_CHECK_RET(DUP_FUNC(CTX, &(NEW_ARRAY)[__u], &(ORIG_ARRAY)[__u])); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200120 } \
121 }
122
Michal Vasko5347e3a2020-11-03 17:14:57 +0100123#define COMPILE_OP_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, FUNC, USES_STATUS, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100125 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
126 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
127 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200128 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100129 RET = FUNC(CTX, &(ARRAY_P)[__u], PARENT, &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1], USES_STATUS); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200130 if (RET == LY_EDENIED) { \
131 LY_ARRAY_DECREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100132 RET = LY_SUCCESS; \
133 } else if (RET) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200134 goto GOTO; \
135 } \
136 } \
137 }
138
Michal Vasko5347e3a2020-11-03 17:14:57 +0100139#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, FUNC, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200140 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100141 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
142 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
143 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200144 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100145 RET = FUNC(CTX, &(ARRAY_P)[__u], &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1]); \
146 LY_CHECK_GOTO(RET, GOTO); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200147 } \
148 }
149
150#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
151 if (EXTS_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100152 LY_ARRAY_COUNT_TYPE __u = (EXT_C) ? LY_ARRAY_COUNT(EXT_C) : 0; \
153 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, __u + LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
154 LY_ARRAY_FOR(EXTS_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200155 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100156 RET = lys_compile_ext(CTX, &(EXTS_P)[__u], &(EXT_C)[LY_ARRAY_COUNT(EXT_C) - 1], PARENT, PARENT_TYPE, NULL); \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100157 if (RET == LY_ENOT) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100158 LY_ARRAY_DECREMENT(EXT_C); \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100159 RET = LY_SUCCESS; \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100160 } else if (RET) { \
161 goto GOTO; \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100162 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200163 } \
164 }
165
166/**
167 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
168 *
169 * @param[in] ctx Compilation context.
170 * @param[in] ext_p Parsed extension instance.
171 * @param[in,out] ext Prepared compiled extension instance.
172 * @param[in] parent Extension instance parent.
173 * @param[in] parent_type Extension instance parent type.
174 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100175 * @return LY_SUCCESS on success.
176 * @return LY_ENOT if the extension is disabled and should be ignored.
177 * @return LY_ERR on error.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200178 */
179LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
180 LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
181
182/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200183 * @brief Compile information from the identity statement
184 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200185 * 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 +0200186 *
187 * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
188 * @param[in] ctx libyang context.
189 * @param[in] parsed_mod Module with the identities.
190 * @param[in] identities_p Array of the parsed identity definitions to precompile.
191 * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
192 * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
193 * to be processed.
194 * @return LY_ERR value.
195 */
196LY_ERR lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
197 struct lysp_ident *identities_p, struct lysc_ident **identities);
198
199/**
200 * @brief Find and process the referenced base identities from another identity or identityref
201 *
202 * For bases in identity set backlinks to them from the base identities. For identityref, store
203 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
204 * to distinguish these two use cases.
205 *
206 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
207 * @param[in] base_pmod Module where to resolve @p bases_p prefixes.
208 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
209 * @param[in] ident Referencing identity to work with, NULL for identityref.
210 * @param[in] bases Array of bases of identityref to fill in.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100211 * @param[in] enabled Whether the base is disabled, must be set if @p ident is set.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200212 * @return LY_ERR value.
213 */
214LY_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 +0100215 struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200216
217/**
218 * @brief Check statement's status for invalid combination.
219 *
220 * The modX parameters are used just to determine if both flags are in the same module,
221 * so any of the schema module structure can be used, but both modules must be provided
222 * in the same type.
223 *
224 * @param[in] ctx Compile context for logging.
225 * @param[in] flags1 Flags of the referencing node.
226 * @param[in] mod1 Module of the referencing node,
227 * @param[in] name1 Schema node name of the referencing node.
228 * @param[in] flags2 Flags of the referenced node.
229 * @param[in] mod2 Module of the referenced node,
230 * @param[in] name2 Schema node name of the referenced node.
231 * @return LY_ERR value
232 */
233LY_ERR lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2,
234 void *mod2, const char *name2);
235
236/**
Michal Vasko25d6ad02020-10-22 12:20:22 +0200237 * @brief Check parsed expression for any prefixes of unimplemented modules.
238 *
239 * @param[in] ctx libyang context.
240 * @param[in] expr Parsed expression.
241 * @param[in] format Prefix format.
242 * @param[in] prefix_data Format-specific data (see ::ly_resolve_prefix()).
243 * @param[in] implement Whether all the non-implemented modules should are implemented or the first
244 * non-implemented module, if any, returned in @p mod_p.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100245 * @param[in,out] unres Global unres structure of newly implemented modules.
Michal Vasko25d6ad02020-10-22 12:20:22 +0200246 * @param[out] mod_p Module that is not implemented.
247 * @return LY_SUCCESS on success.
248 * @return LY_ERR on error.
249 */
250LY_ERR lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_PREFIX_FORMAT format,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100251 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p);
252
253/**
254 * @brief Finish compilation of all the global unres sets.
255 *
256 * @param[in] ctx libyang context.
257 * @param[in] unres Global unres structure with the sets to resolve.
258 * @return LY_ERR value.
259 */
260LY_ERR lys_compile_unres_glob(struct ly_ctx *ctx, struct lys_glob_unres *unres);
261
262/**
263 * @brief Revert a failed compilation (free new modules, unimplement newly implemented modules).
264 *
265 * @param[in] ctx libyang context.
266 * @param[in] unres Global unres set with newly implemented modules.
267 */
268void lys_compile_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres);
269
270/**
271 * @brief Erase all the global unres sets.
272 *
273 * @param[in] ctx libyang context.
274 * @param[in] unres Global unres structure with the sets.
275 */
276void lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres);
Michal Vasko25d6ad02020-10-22 12:20:22 +0200277
278/**
Michal Vasko916aefb2020-11-02 15:43:16 +0100279 * @brief Recompile the whole context based on the current flags.
280 *
281 * @param[in] ctx Context to recompile.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100282 * @param[in] log Whether to log all the errors.
Michal Vasko916aefb2020-11-02 15:43:16 +0100283 * @return LY_SUCCESS on success.
284 * @return LY_ERR on error.
285 */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100286LY_ERR lys_recompile(struct ly_ctx *ctx, ly_bool log);
Michal Vasko916aefb2020-11-02 15:43:16 +0100287
288/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200289 * @brief Compile printable schema into a validated schema linking all the references.
290 *
291 * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed
292 * member is used as input and ::lys_module#compiled is used to hold the result of the compilation.
293 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Michal Vasko405cc9e2020-12-01 12:01:27 +0100294 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200295 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
296 */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100297LY_ERR lys_compile(struct lys_module *mod, uint32_t options, struct lys_glob_unres *unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200298
299#endif /* LY_SCHEMA_COMPILE_H_ */