blob: a2331162a92c8351685860aaf31c082ba4cc8158 [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 */
32#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 0x08 /**< Internal option when compiling schema tree of Notification */
36
37#define LYS_COMPILE_GROUPING 0x10 /** Compiling (validation) of a non-instantiated grouping.
38 In this case not all the restrictions are checked since they can be valid only
39 in the real placement of the grouping. TODO - what specifically is not done */
40/** @} scflags */
41
42/**
43 * @brief internal context for compilation
44 */
45struct lysc_ctx {
46 struct ly_ctx *ctx;
47 struct lys_module *cur_mod; /**< module currently being compiled, used as the current module for unprefixed nodes */
48 struct lysp_module *pmod; /**< parsed module being processed, used for searching imports to resolve prefixed nodes */
49 struct ly_set groupings; /**< stack for groupings circular check */
50 struct ly_set xpath; /**< when/must to check */
51 struct ly_set leafrefs; /**< to validate leafref's targets */
52 struct ly_set dflts; /**< set of incomplete default values */
53 struct ly_set tpdf_chain;
54 struct ly_set augs; /**< set of compiled non-applied top-level augments */
55 struct ly_set devs; /**< set of compiled non-applied deviations */
56 struct ly_set uses_augs; /**< set of compiled non-applied uses augments */
57 struct ly_set uses_rfns; /**< set of compiled non-applied uses refines */
58 uint32_t path_len;
59 uint32_t options; /**< various @ref scflags. */
60#define LYSC_CTX_BUFSIZE 4078
61 char path[LYSC_CTX_BUFSIZE];
62};
63
64/**
65 * @brief Structure for remembering default values of leaves and leaf-lists. They are resolved at schema compilation
66 * end when the whole schema tree is available.
67 */
68struct lysc_unres_dflt {
69 union {
70 struct lysc_node_leaf *leaf;
71 struct lysc_node_leaflist *llist;
72 };
73 struct lysp_qname *dflt;
74 struct lysp_qname *dflts; /**< this is a sized array */
75};
76
77/**
78 * @brief Duplicate string into dictionary
79 * @param[in] CTX libyang context of the dictionary.
80 * @param[in] ORIG String to duplicate.
81 * @param[out] DUP Where to store the result.
82 */
83#define DUP_STRING(CTX, ORIG, DUP, RET) if (ORIG) {RET = lydict_insert(CTX, ORIG, 0, &DUP);}
84
85#define DUP_STRING_GOTO(CTX, ORIG, DUP, RET, GOTO) if (ORIG) {LY_CHECK_GOTO(RET = lydict_insert(CTX, ORIG, 0, &DUP), GOTO);}
86
87#define DUP_ARRAY(CTX, ORIG_ARRAY, NEW_ARRAY, DUP_FUNC) \
88 if (ORIG_ARRAY) { \
89 LY_ARRAY_COUNT_TYPE u; \
90 LY_ARRAY_CREATE_RET(CTX, NEW_ARRAY, LY_ARRAY_COUNT(ORIG_ARRAY), LY_EMEM); \
91 LY_ARRAY_FOR(ORIG_ARRAY, u) { \
92 LY_ARRAY_INCREMENT(NEW_ARRAY); \
93 LY_CHECK_RET(DUP_FUNC(CTX, &(NEW_ARRAY)[u], &(ORIG_ARRAY)[u])); \
94 } \
95 }
96
97#define COMPILE_OP_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
98 if (ARRAY_P) { \
99 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
100 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
101 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
102 LY_ARRAY_INCREMENT(ARRAY_C); \
103 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
104 if (RET == LY_EDENIED) { \
105 LY_ARRAY_DECREMENT(ARRAY_C); \
106 } else if (RET != LY_SUCCESS) { \
107 goto GOTO; \
108 } \
109 } \
110 }
111
112#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
113 if (ARRAY_P) { \
114 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_COUNT(ARRAY_P), RET, GOTO); \
115 LY_ARRAY_COUNT_TYPE __array_offset = LY_ARRAY_COUNT(ARRAY_C); \
116 for (ITER = 0; ITER < LY_ARRAY_COUNT(ARRAY_P); ++ITER) { \
117 LY_ARRAY_INCREMENT(ARRAY_C); \
118 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
119 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
120 } \
121 }
122
123#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
124 if (EXTS_P) { \
125 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_COUNT(EXTS_P), RET, GOTO); \
126 for (LY_ARRAY_COUNT_TYPE __exts_iter = 0, __array_offset = LY_ARRAY_COUNT(EXT_C); __exts_iter < LY_ARRAY_COUNT(EXTS_P); ++__exts_iter) { \
127 LY_ARRAY_INCREMENT(EXT_C); \
128 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE, NULL); \
129 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
130 } \
131 }
132
133/**
134 * @brief Fill in the prepared compiled extension instance structure according to the parsed extension instance.
135 *
136 * @param[in] ctx Compilation context.
137 * @param[in] ext_p Parsed extension instance.
138 * @param[in,out] ext Prepared compiled extension instance.
139 * @param[in] parent Extension instance parent.
140 * @param[in] parent_type Extension instance parent type.
141 * @param[in] ext_mod Optional module with the extension instance extension definition, set only for internal annotations.
142 * @return LY_ERR value.
143 */
144LY_ERR lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent,
145 LYEXT_PARENT parent_type, const struct lys_module *ext_mod);
146
147/**
148 * @brief Compile information from the if-feature statement
149 * @param[in] ctx Compile context.
150 * @param[in] qname The if-feature argument to process. It is pointer-to-qname just to unify the compile functions.
151 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
152 * @return LY_ERR value.
153 */
154LY_ERR lys_compile_iffeature(struct lysc_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff);
155
156/**
157 * @brief Compile information from the identity statement
158 *
159 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
160 *
161 * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
162 * @param[in] ctx libyang context.
163 * @param[in] parsed_mod Module with the identities.
164 * @param[in] identities_p Array of the parsed identity definitions to precompile.
165 * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
166 * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
167 * to be processed.
168 * @return LY_ERR value.
169 */
170LY_ERR lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
171 struct lysp_ident *identities_p, struct lysc_ident **identities);
172
173/**
174 * @brief Find and process the referenced base identities from another identity or identityref
175 *
176 * For bases in identity set backlinks to them from the base identities. For identityref, store
177 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
178 * to distinguish these two use cases.
179 *
180 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
181 * @param[in] base_pmod Module where to resolve @p bases_p prefixes.
182 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
183 * @param[in] ident Referencing identity to work with, NULL for identityref.
184 * @param[in] bases Array of bases of identityref to fill in.
185 * @return LY_ERR value.
186 */
187LY_ERR lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
188 struct lysc_ident *ident, struct lysc_ident ***bases);
189
190/**
191 * @brief Create pre-compiled features array.
192 *
193 * Features are compiled in two steps to allow forward references between them via their if-feature statements.
194 * In case of not implemented schemas, the precompiled list of features is stored in lys_module structure and
195 * the compilation is not finished (if-feature and extensions are missing) and all the features are permanently
196 * disabled without a chance to change it. The list is used as target for any if-feature statement in any
197 * implemented module to get valid data to evaluate its result. The compilation is finished via
198 * lys_feature_precompile_finish() in implemented modules. In case a not implemented module becomes implemented,
199 * the precompiled list is reused to finish the compilation to preserve pointers already used in various compiled
200 * if-feature structures.
201 *
202 * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
203 * @param[in] ctx libyang context.
204 * @param[in] parsed_mod Module with the features.
205 * @param[in] features_p Array of the parsed features definitions to precompile.
206 * @param[in,out] features Pointer to the storage of the (pre)compiled features array where the new features are
207 * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed features are going
208 * to be processed.
209 * @return LY_ERR value.
210 */
211LY_ERR lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
212 struct lysp_feature *features_p, struct lysc_feature **features);
213
214/**
215 * @brief Revert compiled list of features back to the precompiled state.
216 *
217 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
218 *
219 * @param[in] ctx Compilation context.
220 * @param[in] mod The module structure with the features to decompile.
221 */
222void lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod);
223
224/**
225 * @brief Check statement's status for invalid combination.
226 *
227 * The modX parameters are used just to determine if both flags are in the same module,
228 * so any of the schema module structure can be used, but both modules must be provided
229 * in the same type.
230 *
231 * @param[in] ctx Compile context for logging.
232 * @param[in] flags1 Flags of the referencing node.
233 * @param[in] mod1 Module of the referencing node,
234 * @param[in] name1 Schema node name of the referencing node.
235 * @param[in] flags2 Flags of the referenced node.
236 * @param[in] mod2 Module of the referenced node,
237 * @param[in] name2 Schema node name of the referenced node.
238 * @return LY_ERR value
239 */
240LY_ERR lysc_check_status(struct lysc_ctx *ctx, uint16_t flags1, void *mod1, const char *name1, uint16_t flags2,
241 void *mod2, const char *name2);
242
243/**
244 * @brief Compile printable schema into a validated schema linking all the references.
245 *
246 * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed
247 * member is used as input and ::lys_module#compiled is used to hold the result of the compilation.
248 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
249 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
250 */
251LY_ERR lys_compile(struct lys_module *mod, uint32_t options);
252
253#endif /* LY_SCHEMA_COMPILE_H_ */