blob: 1fd23387357131edd4d97e90a3f4f5ba71e63521 [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
22#include "common.h"
23#include "dict.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020024#include "log.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020025#include "set.h"
Radek Krejci47fab892020-11-05 17:02:41 +010026#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010027#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020028#include "tree_schema.h"
29
Radek Krejci47fab892020-11-05 17:02:41 +010030struct lyxp_expr;
31
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020032/**
Radek Krejci5f9a3672021-03-05 21:35:22 +010033 * @brief YANG schema compilation context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034 */
35struct lysc_ctx {
Radek Krejci5f9a3672021-03-05 21:35:22 +010036 struct ly_ctx *ctx; /**< libyang context */
Michal Vaskob8df5762021-01-12 15:15:53 +010037 struct lys_module *cur_mod; /**< module currently being compiled,
38 - identifier/path - used as the current module for unprefixed nodes
39 - augment - module where the augment is defined
40 - deviation - module where the deviation is defined
41 - uses - module where the uses is defined */
42 struct lysp_module *pmod; /**< parsed module being processed,
43 - identifier/path - used for searching imports to resolve prefixed nodes
44 - augment - module where the augment is defined
45 - deviation - module where the deviation is defined
46 - uses - module where the grouping is defined */
Radek Krejci6b88a462021-02-17 12:39:34 +010047 struct lysc_ext_instance *ext; /**< extension instance being processed and serving as a source for its substatements
48 instead of the module itself */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020049 struct ly_set groupings; /**< stack for groupings circular check */
Radek Krejci5f9a3672021-03-05 21:35:22 +010050 struct ly_set tpdf_chain; /**< stack for typedefs circular check */
Radek Krejci5f9a3672021-03-05 21:35:22 +010051 struct ly_set augs; /**< set of compiled non-applied top-level augments (stored ::lysc_augment *) */
52 struct ly_set devs; /**< set of compiled non-applied deviations (stored ::lysc_deviation *) */
53 struct ly_set uses_augs; /**< set of compiled non-applied uses augments (stored ::lysc_augment *) */
54 struct ly_set uses_rfns; /**< set of compiled non-applied uses refines (stored ::lysc_refine *) */
Michal Vaskof4258e12021-06-15 12:11:42 +020055 struct lys_depset_unres *unres; /**< dependency set unres sets */
Radek Krejci5f9a3672021-03-05 21:35:22 +010056 uint32_t path_len; /**< number of path bytes used */
Michal Vasko7c565922021-06-10 14:58:27 +020057 uint32_t compile_opts; /**< various @ref scflags. */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020058#define LYSC_CTX_BUFSIZE 4078
Radek Krejci5f9a3672021-03-05 21:35:22 +010059 char path[LYSC_CTX_BUFSIZE];/**< Path identifying the schema node currently being processed */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020060};
61
62/**
Michal Vaskof4258e12021-06-15 12:11:42 +020063 * @brief Structure for unresolved items that may depend on any implemented module data in the dependency set
64 * so their resolution can only be performed after the whole dep set compilation is done.
Michal Vasko405cc9e2020-12-01 12:01:27 +010065 */
Michal Vaskof4258e12021-06-15 12:11:42 +020066struct lys_depset_unres {
Michal Vasko405cc9e2020-12-01 12:01:27 +010067 struct ly_set xpath; /**< when/must to check */
68 struct ly_set leafrefs; /**< to validate leafref's targets */
69 struct ly_set dflts; /**< set of incomplete default values */
Michal Vasko30ab8e72021-04-19 12:47:52 +020070 struct ly_set disabled; /**< set of compiled nodes whose if-feature(s) was not satisfied (stored ::lysc_node *) */
Michal Vasko405cc9e2020-12-01 12:01:27 +010071};
72
73/**
Michal Vaskof4258e12021-06-15 12:11:42 +020074 * @brief Unres structure global for compilation.
75 */
76struct lys_glob_unres {
77 struct ly_set dep_sets; /**< set of dependency sets of modules, see ::ly_ctx_compile_deps_mod_r() */
78 struct ly_set implementing; /**< set of YANG schemas being atomically implemented (compiled); the first added
79 module is always the explcitly implemented module, the other ones are dependencies */
80 struct ly_set creating; /**< set of YANG schemas being atomically created (parsed); it is a subset of implemented
81 and all these modules are freed if any error occurs */
82 struct lys_depset_unres ds_unres; /**< unres specific for the current dependency set */
83};
84
85/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020086 * @brief Structure for remembering default values of leaves and leaf-lists. They are resolved at schema compilation
87 * end when the whole schema tree is available.
88 */
89struct lysc_unres_dflt {
90 union {
91 struct lysc_node_leaf *leaf;
92 struct lysc_node_leaflist *llist;
93 };
94 struct lysp_qname *dflt;
95 struct lysp_qname *dflts; /**< this is a sized array */
96};
97
98/**
99 * @brief Duplicate string into dictionary
100 * @param[in] CTX libyang context of the dictionary.
101 * @param[in] ORIG String to duplicate.
102 * @param[out] DUP Where to store the result.
103 */
104#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
Radek Krejci771928a2021-01-19 13:42:36 +0100105#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 +0200106#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
107
108#define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \
109 if (ORIG_ARRAY) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100110 LY_ARRAY_COUNT_TYPE __u; \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200111 LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100112 LY_ARRAY_FOR(ORIG_ARRAY, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200113 LY_ARRAY_INCREMENT(NEW_ARRAY); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100114 LY_CHECK_RET(DUP_FUNC(CTX, &(NEW_ARRAY)[__u], &(ORIG_ARRAY)[__u])); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200115 } \
116 }
117
Michal Vasko5347e3a2020-11-03 17:14:57 +0100118#define COMPILE_OP_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, FUNC, USES_STATUS, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200119 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100120 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
121 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
122 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200123 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100124 RET = FUNC(CTX, &(ARRAY_P)[__u], PARENT, &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1], USES_STATUS); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200125 if (RET == LY_EDENIED) { \
126 LY_ARRAY_DECREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100127 RET = LY_SUCCESS; \
128 } else if (RET) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200129 goto GOTO; \
130 } \
131 } \
132 }
133
Michal Vasko5347e3a2020-11-03 17:14:57 +0100134#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, FUNC, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200135 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100136 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
137 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
138 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200139 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100140 RET = FUNC(CTX, &(ARRAY_P)[__u], &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1]); \
141 LY_CHECK_GOTO(RET, GOTO); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200142 } \
143 }
144
Radek Krejciab430862021-03-02 20:13:40 +0100145#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200146 if (EXTS_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100147 LY_ARRAY_COUNT_TYPE __u = (EXT_C) ? LY_ARRAY_COUNT(EXT_C) : 0; \
148 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, __u + LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
149 LY_ARRAY_FOR(EXTS_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200150 LY_ARRAY_INCREMENT(EXT_C); \
Radek Krejciab430862021-03-02 20:13:40 +0100151 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 +0100152 if (RET == LY_ENOT) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100153 LY_ARRAY_DECREMENT(EXT_C); \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100154 RET = LY_SUCCESS; \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100155 } else if (RET) { \
156 goto GOTO; \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100157 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200158 } \
159 }
160
161/**
162 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
163 *
164 * @param[in] ctx Compilation context.
165 * @param[in] ext_p Parsed extension instance.
166 * @param[in,out] ext Prepared compiled extension instance.
167 * @param[in] parent Extension instance parent.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200168 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100169 * @return LY_SUCCESS on success.
170 * @return LY_ENOT if the extension is disabled and should be ignored.
171 * @return LY_ERR on error.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200172 */
173LY_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 +0100174 const struct lys_module *ext_mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200175
176/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200177 * @brief Compile information from the identity statement
178 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200179 * 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 +0200180 *
181 * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
182 * @param[in] ctx libyang context.
183 * @param[in] parsed_mod Module with the identities.
184 * @param[in] identities_p Array of the parsed identity definitions to precompile.
185 * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
186 * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
187 * to be processed.
188 * @return LY_ERR value.
189 */
190LY_ERR lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
191 struct lysp_ident *identities_p, struct lysc_ident **identities);
192
193/**
194 * @brief Find and process the referenced base identities from another identity or identityref
195 *
196 * For bases in identity set backlinks to them from the base identities. For identityref, store
197 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
198 * to distinguish these two use cases.
199 *
200 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
201 * @param[in] base_pmod Module where to resolve @p bases_p prefixes.
202 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
203 * @param[in] ident Referencing identity to work with, NULL for identityref.
204 * @param[in] bases Array of bases of identityref to fill in.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100205 * @param[in] enabled Whether the base is disabled, must be set if @p ident is set.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200206 * @return LY_ERR value.
207 */
208LY_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 +0100209 struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200210
211/**
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200212 * @brief Compile schema into a validated schema linking all the references. Must have been implemented before.
213 *
214 * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed
215 * member is used as input and ::lys_module#compiled is used to hold the result of the compilation.
216 * @param[in,out] unres Dep set unres structure to add to.
217 * @return LY_SUCCESS on success.
218 * @return LY_ERR on error.
219 */
220LY_ERR lys_compile(struct lys_module *mod, struct lys_depset_unres *unres);
221
222/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200223 * @brief Check statement's status for invalid combination.
224 *
225 * The modX parameters are used just to determine if both flags are in the same module,
226 * so any of the schema module structure can be used, but both modules must be provided
227 * in the same type.
228 *
229 * @param[in] ctx Compile context for logging.
230 * @param[in] flags1 Flags of the referencing node.
231 * @param[in] mod1 Module of the referencing node,
232 * @param[in] name1 Schema node name of the referencing node.
233 * @param[in] flags2 Flags of the referenced node.
234 * @param[in] mod2 Module of the referenced node,
235 * @param[in] name2 Schema node name of the referenced node.
236 * @return LY_ERR value
237 */
238LY_ERR lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2,
239 void *mod2, const char *name2);
240
241/**
Michal Vasko25d6ad02020-10-22 12:20:22 +0200242 * @brief Check parsed expression for any prefixes of unimplemented modules.
243 *
244 * @param[in] ctx libyang context.
245 * @param[in] expr Parsed expression.
246 * @param[in] format Prefix format.
247 * @param[in] prefix_data Format-specific data (see ::ly_resolve_prefix()).
248 * @param[in] implement Whether all the non-implemented modules should are implemented or the first
249 * non-implemented module, if any, returned in @p mod_p.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100250 * @param[in,out] unres Global unres structure of newly implemented modules.
Michal Vasko25d6ad02020-10-22 12:20:22 +0200251 * @param[out] mod_p Module that is not implemented.
252 * @return LY_SUCCESS on success.
Michal Vasko40c158c2021-04-28 17:01:03 +0200253 * @return LY_ERECOMPILE if @p implement is set.
Michal Vasko25d6ad02020-10-22 12:20:22 +0200254 * @return LY_ERR on error.
255 */
Radek Krejci8df109d2021-04-23 12:19:08 +0200256LY_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 +0100257 void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p);
258
259/**
Michal Vaskof4258e12021-06-15 12:11:42 +0200260 * @brief Compile all flagged modules in a dependency set, recursively if recompilation is needed.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100261 *
262 * @param[in] ctx libyang context.
Michal Vaskof4258e12021-06-15 12:11:42 +0200263 * @param[in] dep_set Dependency set to compile.
264 * @param[in,out] unres Global unres to use.
Michal Vasko405cc9e2020-12-01 12:01:27 +0100265 * @return LY_ERR value.
266 */
Michal Vaskof4258e12021-06-15 12:11:42 +0200267LY_ERR lys_compile_dep_set_r(struct ly_ctx *ctx, struct ly_set *dep_set, struct lys_glob_unres *unres);
Michal Vasko916aefb2020-11-02 15:43:16 +0100268
269/**
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200270 * @brief Implement a single module. Does not actually compile, only marks to_compile!
Michal Vasko65333882021-06-10 14:12:16 +0200271 *
272 * @param[in] mod Module to implement.
273 * @param[in] features Features to set, see ::lys_set_features().
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200274 * @param[in,out] unres Global unres to use.
275 * @return LY_ERR value.
Michal Vasko65333882021-06-10 14:12:16 +0200276 */
277LY_ERR lys_implement(struct lys_module *mod, const char **features, struct lys_glob_unres *unres);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200278
279#endif /* LY_SCHEMA_COMPILE_H_ */