blob: 87eb6c8713bcf12eca0072e6258a77854d75964c [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
18#include "log.h"
19#include "schema_compile_node.h"
20#include "set.h"
21#include "tree_schema.h"
22
23/**
24 * @defgroup scflags Schema compile flags
25 *
26 * Flags are currently used only internally - the compilation process does not have a public interface and it is
27 * integrated in the schema parsers. The current options set does not make sense for public used, but it can be a way
28 * to modify behavior of the compilation process in future.
29 *
30 * @{
31 */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010032#define LYS_COMPILE_RPC_INPUT LYS_CONFIG_W /**< Internal option when compiling schema tree of RPC/action input */
33#define LYS_COMPILE_RPC_OUTPUT LYS_CONFIG_R /**< Internal option when compiling schema tree of RPC/action output */
34#define LYS_COMPILE_RPC_MASK LYS_CONFIG_MASK /**< mask for the internal RPC options */
35#define LYS_COMPILE_NOTIFICATION 0x04 /**< Internal option when compiling schema tree of Notification */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020036
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010037#define LYS_COMPILE_GROUPING 0x08 /**< Compiling (validation) of a non-instantiated grouping.
38 In this case not all the restrictions are checked since they can
39 be valid only in the real placement of the grouping.
40 TODO - what specifically is not done */
41#define LYS_COMPILE_DISABLED 0x10 /**< Compiling a disabled subtree (by its if-features). Meaning
42 it will be removed at the end of compilation and should not be
43 added to any unres sets. */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020044/** @} scflags */
45
46/**
47 * @brief internal context for compilation
48 */
49struct lysc_ctx {
50 struct ly_ctx *ctx;
51 struct lys_module *cur_mod; /**< module currently being compiled, used as the current module for unprefixed nodes */
52 struct lysp_module *pmod; /**< parsed module being processed, used for searching imports to resolve prefixed nodes */
53 struct ly_set groupings; /**< stack for groupings circular check */
54 struct ly_set xpath; /**< when/must to check */
55 struct ly_set leafrefs; /**< to validate leafref's targets */
56 struct ly_set dflts; /**< set of incomplete default values */
57 struct ly_set tpdf_chain;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010058 struct ly_set disabled; /**< set of compiled nodes whose if-feature(s) was not satisifed */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020059 struct ly_set augs; /**< set of compiled non-applied top-level augments */
60 struct ly_set devs; /**< set of compiled non-applied deviations */
61 struct ly_set uses_augs; /**< set of compiled non-applied uses augments */
62 struct ly_set uses_rfns; /**< set of compiled non-applied uses refines */
63 uint32_t path_len;
64 uint32_t options; /**< various @ref scflags. */
65#define LYSC_CTX_BUFSIZE 4078
66 char path[LYSC_CTX_BUFSIZE];
67};
68
69/**
70 * @brief Structure for remembering default values of leaves and leaf-lists. They are resolved at schema compilation
71 * end when the whole schema tree is available.
72 */
73struct lysc_unres_dflt {
74 union {
75 struct lysc_node_leaf *leaf;
76 struct lysc_node_leaflist *llist;
77 };
78 struct lysp_qname *dflt;
79 struct lysp_qname *dflts; /**< this is a sized array */
80};
81
82/**
83 * @brief Duplicate string into dictionary
84 * @param[in] CTX libyang context of the dictionary.
85 * @param[in] ORIG String to duplicate.
86 * @param[out] DUP Where to store the result.
87 */
88#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
89
90#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
91
92#define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \
93 if (ORIG_ARRAY) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +010094 LY_ARRAY_COUNT_TYPE __u; \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020095 LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \
Michal Vasko5347e3a2020-11-03 17:14:57 +010096 LY_ARRAY_FOR(ORIG_ARRAY, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020097 LY_ARRAY_INCREMENT(NEW_ARRAY); \
Michal Vasko5347e3a2020-11-03 17:14:57 +010098 LY_CHECK_RET(DUP_FUNC(CTX, &(NEW_ARRAY)[__u], &(ORIG_ARRAY)[__u])); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020099 } \
100 }
101
Michal Vasko5347e3a2020-11-03 17:14:57 +0100102#define COMPILE_OP_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, FUNC, USES_STATUS, RET, GOTO) \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200103 if (ARRAY_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100104 LY_ARRAY_COUNT_TYPE __u = (ARRAY_C) ? LY_ARRAY_COUNT(ARRAY_C) : 0; \
105 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, __u + LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
106 LY_ARRAY_FOR(ARRAY_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200107 LY_ARRAY_INCREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100108 RET = FUNC(CTX, &(ARRAY_P)[__u], PARENT, &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1], USES_STATUS); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200109 if (RET == LY_EDENIED) { \
110 LY_ARRAY_DECREMENT(ARRAY_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100111 RET = LY_SUCCESS; \
112 } else if (RET) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200113 goto GOTO; \
114 } \
115 } \
116 }
117
Michal Vasko5347e3a2020-11-03 17:14:57 +0100118#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, FUNC, 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], &(ARRAY_C)[LY_ARRAY_COUNT(ARRAY_C) - 1]); \
125 LY_CHECK_GOTO(RET, GOTO); \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200126 } \
127 }
128
129#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
130 if (EXTS_P) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100131 LY_ARRAY_COUNT_TYPE __u = (EXT_C) ? LY_ARRAY_COUNT(EXT_C) : 0; \
132 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, __u + LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
133 LY_ARRAY_FOR(EXTS_P, __u) { \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200134 LY_ARRAY_INCREMENT(EXT_C); \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100135 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 +0100136 if (RET == LY_ENOT) { \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100137 LY_ARRAY_DECREMENT(EXT_C); \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100138 RET = LY_SUCCESS; \
Michal Vasko5347e3a2020-11-03 17:14:57 +0100139 } else if (RET) { \
140 goto GOTO; \
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100141 } \
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200142 } \
143 }
144
145/**
146 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
147 *
148 * @param[in] ctx Compilation context.
149 * @param[in] ext_p Parsed extension instance.
150 * @param[in,out] ext Prepared compiled extension instance.
151 * @param[in] parent Extension instance parent.
152 * @param[in] parent_type Extension instance parent type.
153 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100154 * @return LY_SUCCESS on success.
155 * @return LY_ENOT if the extension is disabled and should be ignored.
156 * @return LY_ERR on error.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200157 */
158LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
159 LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
160
161/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200162 * @brief Compile information from the identity statement
163 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200164 * 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 +0200165 *
166 * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
167 * @param[in] ctx libyang context.
168 * @param[in] parsed_mod Module with the identities.
169 * @param[in] identities_p Array of the parsed identity definitions to precompile.
170 * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
171 * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
172 * to be processed.
173 * @return LY_ERR value.
174 */
175LY_ERR lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
176 struct lysp_ident *identities_p, struct lysc_ident **identities);
177
178/**
179 * @brief Find and process the referenced base identities from another identity or identityref
180 *
181 * For bases in identity set backlinks to them from the base identities. For identityref, store
182 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
183 * to distinguish these two use cases.
184 *
185 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
186 * @param[in] base_pmod Module where to resolve @p bases_p prefixes.
187 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
188 * @param[in] ident Referencing identity to work with, NULL for identityref.
189 * @param[in] bases Array of bases of identityref to fill in.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100190 * @param[in] enabled Whether the base is disabled, must be set if @p ident is set.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200191 * @return LY_ERR value.
192 */
193LY_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 +0100194 struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200195
196/**
197 * @brief Check statement's status for invalid combination.
198 *
199 * The modX parameters are used just to determine if both flags are in the same module,
200 * so any of the schema module structure can be used, but both modules must be provided
201 * in the same type.
202 *
203 * @param[in] ctx Compile context for logging.
204 * @param[in] flags1 Flags of the referencing node.
205 * @param[in] mod1 Module of the referencing node,
206 * @param[in] name1 Schema node name of the referencing node.
207 * @param[in] flags2 Flags of the referenced node.
208 * @param[in] mod2 Module of the referenced node,
209 * @param[in] name2 Schema node name of the referenced node.
210 * @return LY_ERR value
211 */
212LY_ERR lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2,
213 void *mod2, const char *name2);
214
215/**
Michal Vasko25d6ad02020-10-22 12:20:22 +0200216 * @brief Check parsed expression for any prefixes of unimplemented modules.
217 *
218 * @param[in] ctx libyang context.
219 * @param[in] expr Parsed expression.
220 * @param[in] format Prefix format.
221 * @param[in] prefix_data Format-specific data (see ::ly_resolve_prefix()).
222 * @param[in] implement Whether all the non-implemented modules should are implemented or the first
223 * non-implemented module, if any, returned in @p mod_p.
224 * @param[out] mod_p Module that is not implemented.
225 * @return LY_SUCCESS on success.
226 * @return LY_ERR on error.
227 */
228LY_ERR lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_PREFIX_FORMAT format,
229 void *prefix_data, ly_bool implement, const struct lys_module **mod_p);
230
231/**
Michal Vasko916aefb2020-11-02 15:43:16 +0100232 * @brief Recompile the whole context based on the current flags.
233 *
234 * @param[in] ctx Context to recompile.
235 * @param[in] skip Module to skip. If set, recompilation logs normally and stops on error.
236 * If not set, recompilation hides any errors and prints just generic messages even though it should always succeed.
237 * @return LY_SUCCESS on success.
238 * @return LY_ERR on error.
239 */
240LY_ERR lys_recompile(struct ly_ctx *ctx, const struct lys_module *skip);
241
242/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200243 * @brief Compile printable schema into a validated schema linking all the references.
244 *
245 * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed
246 * member is used as input and ::lys_module#compiled is used to hold the result of the compilation.
247 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
248 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
249 */
250LY_ERR lys_compile(struct lys_module *mod, uint32_t options);
251
252#endif /* LY_SCHEMA_COMPILE_H_ */