blob: 9c2e615cfed3f76d4598c4000d49aee9a87c7eaa [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko19a09022021-06-15 11:54:08 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02005 * @brief Header for schema compilation.
6 *
Michal Vasko19a09022021-06-15 11:54:08 +02007 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02008 *
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 Krejci47fab892020-11-05 17:02:41 +010019#include <stddef.h>
20#include <stdint.h>
21
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020022#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020023#include "set.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "tree.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020025#include "tree_schema.h"
26
Radek Krejci47fab892020-11-05 17:02:41 +010027struct lyxp_expr;
28
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020029/**
Radek Krejci5f9a3672021-03-05 21:35:22 +010030 * @brief YANG schema compilation context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020031 */
32struct lysc_ctx {
Radek Krejci5f9a3672021-03-05 21:35:22 +010033 struct ly_ctx *ctx; /**< libyang context */
Michal Vaskob8df5762021-01-12 15:15:53 +010034 struct lys_module *cur_mod; /**< module currently being compiled,
35 - identifier/path - used as the current module for unprefixed nodes
36 - augment - module where the augment is defined
37 - deviation - module where the deviation is defined
38 - uses - module where the uses is defined */
39 struct lysp_module *pmod; /**< parsed module being processed,
40 - identifier/path - used for searching imports to resolve prefixed nodes
41 - augment - module where the augment is defined
42 - deviation - module where the deviation is defined
43 - uses - module where the grouping is defined */
Radek Krejci6b88a462021-02-17 12:39:34 +010044 struct lysc_ext_instance *ext; /**< extension instance being processed and serving as a source for its substatements
45 instead of the module itself */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020046 struct ly_set groupings; /**< stack for groupings circular check */
Radek Krejci5f9a3672021-03-05 21:35:22 +010047 struct ly_set tpdf_chain; /**< stack for typedefs circular check */
Radek Krejci5f9a3672021-03-05 21:35:22 +010048 struct ly_set augs; /**< set of compiled non-applied top-level augments (stored ::lysc_augment *) */
49 struct ly_set devs; /**< set of compiled non-applied deviations (stored ::lysc_deviation *) */
50 struct ly_set uses_augs; /**< set of compiled non-applied uses augments (stored ::lysc_augment *) */
51 struct ly_set uses_rfns; /**< set of compiled non-applied uses refines (stored ::lysc_refine *) */
Michal Vaskof4258e12021-06-15 12:11:42 +020052 struct lys_depset_unres *unres; /**< dependency set unres sets */
Radek Krejci5f9a3672021-03-05 21:35:22 +010053 uint32_t path_len; /**< number of path bytes used */
Michal Vasko7c565922021-06-10 14:58:27 +020054 uint32_t compile_opts; /**< various @ref scflags. */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020055#define LYSC_CTX_BUFSIZE 4078
Radek Krejci5f9a3672021-03-05 21:35:22 +010056 char path[LYSC_CTX_BUFSIZE];/**< Path identifying the schema node currently being processed */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020057};
58
59/**
Michal Vaskof4258e12021-06-15 12:11:42 +020060 * @brief Structure for unresolved items that may depend on any implemented module data in the dependency set
61 * so their resolution can only be performed after the whole dep set compilation is done.
Michal Vasko405cc9e2020-12-01 12:01:27 +010062 */
Michal Vaskof4258e12021-06-15 12:11:42 +020063struct lys_depset_unres {
aPiecekc6526b42021-07-12 15:21:39 +020064 struct ly_set xpath; /**< when/must to check */
65 struct ly_set leafrefs; /**< to validate target of leafrefs */
66 struct ly_set dflts; /**< set of incomplete default values */
67 struct ly_set disabled; /**< set of compiled nodes whose if-feature(s) was not satisfied (stored ::lysc_node *) */
68 struct ly_set disabled_leafrefs; /**< subset of the lys_depset_unres.disabled to validate target of disabled leafrefs */
Michal Vasko405cc9e2020-12-01 12:01:27 +010069};
70
71/**
Michal Vaskof4258e12021-06-15 12:11:42 +020072 * @brief Unres structure global for compilation.
73 */
74struct lys_glob_unres {
Michal Vasko709f9a52021-07-21 10:51:59 +020075 struct ly_set dep_sets; /**< set of dependency sets of modules, see ::lys_compile_depset_all() */
Michal Vaskof4258e12021-06-15 12:11:42 +020076 struct ly_set implementing; /**< set of YANG schemas being atomically implemented (compiled); the first added
Radek Krejci84d7fd72021-07-14 18:32:21 +020077 module is always the explicitly implemented module, the other ones are dependencies */
Michal Vaskof4258e12021-06-15 12:11:42 +020078 struct ly_set creating; /**< set of YANG schemas being atomically created (parsed); it is a subset of implemented
79 and all these modules are freed if any error occurs */
80 struct lys_depset_unres ds_unres; /**< unres specific for the current dependency set */
81};
82
83/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020084 * @brief Structure for remembering default values of leaves and leaf-lists. They are resolved at schema compilation
85 * end when the whole schema tree is available.
86 */
87struct lysc_unres_dflt {
88 union {
89 struct lysc_node_leaf *leaf;
90 struct lysc_node_leaflist *llist;
91 };
92 struct lysp_qname *dflt;
93 struct lysp_qname *dflts; /**< this is a sized array */
94};
95
96/**
97 * @brief Duplicate string into dictionary
98 * @param[in] CTX libyang context of the dictionary.
99 * @param[in] ORIG String to duplicate.
100 * @param[out] DUP Where to store the result.
Radek Krejci84d7fd72021-07-14 18:32:21 +0200101 * @param[out] RET Where to store the return code.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200102 */
103#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
Radek Krejci771928a2021-01-19 13:42:36 +0100104#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 +0200105#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
106
107#define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \
108 if (ORIG_ARRAY) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100109 LY_ARRAY_COUNT_TYPE __u; \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200110 LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100111 LY_ARRAY_FOR(ORIG_ARRAY, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200112 LY_ARRAY_INCREMENT(NEW_ARRAY); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100113 LY_CHECK_RET(DUP_FUNC(CTX, &(NEW_ARRAY)[__u], &(ORIG_ARRAY)[__u])); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200114 } \
115 }
116
Michal Vasko5347e3a2020-11-03 17:14:57 +0100117#define COMPILE_OP_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, FUNC, USES_STATUS, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200118 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100119 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
120 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
121 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200122 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100123 RET = FUNC(CTX, &(ARRAY_P)[__u], PARENT, &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1], USES_STATUS); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 if (RET == LY_EDENIED) { \
125 LY_ARRAY_DECREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100126 RET = LY_SUCCESS; \
127 } else if (RET) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200128 goto GOTO; \
129 } \
130 } \
131 }
132
Michal Vasko5347e3a2020-11-03 17:14:57 +0100133#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, FUNC, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200134 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100135 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
136 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
137 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200138 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100139 RET = FUNC(CTX, &(ARRAY_P)[__u], &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1]); \
140 LY_CHECK_GOTO(RET, GOTO); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200141 } \
142 }
143
Radek Krejciab430862021-03-02 20:13:40 +0100144#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200145 if (EXTS_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100146 LY_ARRAY_COUNT_TYPE __u = (EXT_C) ? LY_ARRAY_COUNT(EXT_C) : 0; \
147 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, __u + LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
148 LY_ARRAY_FOR(EXTS_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200149 LY_ARRAY_INCREMENT(EXT_C); \
Radek Krejciab430862021-03-02 20:13:40 +0100150 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 +0100151 if (RET == LY_ENOT) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100152 LY_ARRAY_DECREMENT(EXT_C); \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100153 RET = LY_SUCCESS; \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100154 } else if (RET) { \
155 goto GOTO; \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100156 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200157 } \
158 }
159
160/**
161 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
162 *
163 * @param[in] ctx Compilation context.
164 * @param[in] ext_p Parsed extension instance.
165 * @param[in,out] ext Prepared compiled extension instance.
166 * @param[in] parent Extension instance parent.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200167 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100168 * @return LY_SUCCESS on success.
169 * @return LY_ENOT if the extension is disabled and should be ignored.
170 * @return LY_ERR on error.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200171 */
172LY_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 +0100173 const struct lys_module *ext_mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200174
175/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200176 * @brief Find and process the referenced base identities from another identity or identityref
177 *
178 * For bases in identity set backlinks to them from the base identities. For identityref, store
179 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
180 * to distinguish these two use cases.
181 *
182 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
183 * @param[in] base_pmod Module where to resolve @p bases_p prefixes.
184 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
185 * @param[in] ident Referencing identity to work with, NULL for identityref.
186 * @param[in] bases Array of bases of identityref to fill in.
187 * @return LY_ERR value.
188 */
189LY_ERR lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
aPiecekf4a0a192021-08-03 15:14:17 +0200190 struct lysc_ident *ident, struct lysc_ident ***bases);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200191
192/**
aPiecek6b3d5422021-07-30 15:55:43 +0200193 * @brief Perform a complet compilation of identites in a module and all its submodules.
194 *
195 * @param[in] mod Module to process.
196 * @return LY_ERR value.
197 */
198LY_ERR lys_compile_identities(struct lys_module *mod);
199
200/**
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200201 * @brief Compile schema into a validated schema linking all the references. Must have been implemented before.
202 *
203 * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed
204 * member is used as input and ::lys_module#compiled is used to hold the result of the compilation.
205 * @param[in,out] unres Dep set unres structure to add to.
206 * @return LY_SUCCESS on success.
207 * @return LY_ERR on error.
208 */
209LY_ERR lys_compile(struct lys_module *mod, struct lys_depset_unres *unres);
210
211/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200212 * @brief Check statement's status for invalid combination.
213 *
214 * The modX parameters are used just to determine if both flags are in the same module,
215 * so any of the schema module structure can be used, but both modules must be provided
216 * in the same type.
217 *
218 * @param[in] ctx Compile context for logging.
219 * @param[in] flags1 Flags of the referencing node.
220 * @param[in] mod1 Module of the referencing node,
221 * @param[in] name1 Schema node name of the referencing node.
222 * @param[in] flags2 Flags of the referenced node.
223 * @param[in] mod2 Module of the referenced node,
224 * @param[in] name2 Schema node name of the referenced node.
225 * @return LY_ERR value
226 */
227LY_ERR lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2,
228 void *mod2, const char *name2);
229
230/**
Michal Vasko25d6ad02020-10-22 12:20:22 +0200231 * @brief Check parsed expression for any prefixes of unimplemented modules.
232 *
233 * @param[in] ctx libyang context.
234 * @param[in] expr Parsed expression.
235 * @param[in] format Prefix format.
236 * @param[in] prefix_data Format-specific data (see ::ly_resolve_prefix()).
237 * @param[in] implement Whether all the non-implemented modules should are implemented or the first
238 * non-implemented module, if any, returned in @p mod_p.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100239 * @param[in,out] unres Global unres structure of newly implemented modules.
Michal Vasko25d6ad02020-10-22 12:20:22 +0200240 * @param[out] mod_p Module that is not implemented.
241 * @return LY_SUCCESS on success.
Michal Vasko40c158c2021-04-28 17:01:03 +0200242 * @return LY_ERECOMPILE if @p implement is set.
Michal Vasko25d6ad02020-10-22 12:20:22 +0200243 * @return LY_ERR on error.
244 */
Radek Krejci8df109d2021-04-23 12:19:08 +0200245LY_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 +0100246 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p);
247
248/**
Michal Vaskof4258e12021-06-15 12:11:42 +0200249 * @brief Compile all flagged modules in a dependency set, recursively if recompilation is needed.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100250 *
Michal Vasko50bc09a2021-06-17 17:31:56 +0200251 * Steps taken when adding a new module (::ly_ctx_load_module(), ::lys_parse()):
252 *
253 * 1) parse module and add it into context with all imports and includes also parsed and in context
Michal Vasko65988242021-07-15 09:19:16 +0200254 * (::lys_parse_load(), ::lys_parse_in(), lys_parse_localfile() - static)
Michal Vasko50bc09a2021-06-17 17:31:56 +0200255 * 2) implement it (perform one-time compilation tasks - compile identities and add reference to augment/deviation
256 * target modules, implement those as well, ::_lys_set_implemented())
257 * 3) create dep set of the module (::lys_unres_dep_sets_create())
258 * 4) (re)compile all the modules in the dep set and collect unres (::lys_compile_dep_set_r())
Michal Vasko65988242021-07-15 09:19:16 +0200259 * 5) resolve unres (lys_compile_unres_depset() - static), new modules may be implemented like in 2) and if
260 * require recompilation, free all compiled modules and do 4)
Michal Vasko50bc09a2021-06-17 17:31:56 +0200261 * 6) all modules that needed to be (re)compiled are now, with all their dependencies
262 *
263 * What can cause new modules to be implemented when resolving unres in 5):
264 * - leafref
265 * - when, must
Michal Vasko41369bd2021-06-23 12:03:23 +0200266 * - identityref, instance-identifier default value
Michal Vasko50bc09a2021-06-17 17:31:56 +0200267 * - new implemented module augments, deviations
268 *
Michal Vasko405cc9e2020-12-01 12:01:27 +0100269 * @param[in] ctx libyang context.
Michal Vaskof4258e12021-06-15 12:11:42 +0200270 * @param[in,out] unres Global unres to use.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100271 * @return LY_ERR value.
272 */
Michal Vasko709f9a52021-07-21 10:51:59 +0200273LY_ERR lys_compile_depset_all(struct ly_ctx *ctx, struct lys_glob_unres *unres);
Michal Vasko916aefb2020-11-02 15:43:16 +0100274
275/**
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200276 * @brief Implement a single module. Does not actually compile, only marks to_compile!
Michal Vasko65333882021-06-10 14:12:16 +0200277 *
278 * @param[in] mod Module to implement.
279 * @param[in] features Features to set, see ::lys_set_features().
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200280 * @param[in,out] unres Global unres to use.
281 * @return LY_ERR value.
Michal Vasko65333882021-06-10 14:12:16 +0200282 */
283LY_ERR lys_implement(struct lys_module *mod, const char **features, struct lys_glob_unres *unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200284
285#endif /* LY_SCHEMA_COMPILE_H_ */