blob: e463de6d7fb5b0f451f51fde98d810739a05d843 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02005 * @brief Header for schema compilation of common nodes.
6 *
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02007 * Copyright (c) 2015 - 2022 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_NODE_H_
17#define LY_SCHEMA_COMPILE_NODE_H_
18
19#include <stddef.h>
Radek Krejci47fab892020-11-05 17:02:41 +010020#include <stdint.h>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020021
22#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010023#include "tree.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020024#include "tree_schema.h"
25
Radek Krejci47fab892020-11-05 17:02:41 +010026struct ly_ctx;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020027struct ly_set;
28struct lysc_ctx;
29
30/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020031 * @brief Compile information from the when statement by either standard compilation or by reusing
32 * another compiled when structure.
33 *
34 * @param[in] ctx Compile context.
35 * @param[in] when_p Parsed when structure.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020036 * @param[in] inherited_flags Inherited flags from a schema-only statement.
37 * @param[in] parent Parent node, if any.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038 * @param[in] ctx_node Context node for the when statement.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020039 * @param[in] node Compiled node to which to add the compiled when.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040 * @param[in,out] when_c Optional, pointer to the previously compiled @p when_p to be reused. Set to NULL
41 * for the first call.
42 * @return LY_ERR value.
43 */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +020044LY_ERR lys_compile_when(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags,
45 const struct lysc_node *parent, const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020046
47/**
Michal Vaskoedb0fa52022-10-04 10:36:00 +020048 * @brief Compile information from the must statement
49 *
50 * @param[in] ctx Compile context.
51 * @param[in] must_p The parsed must statement structure.
52 * @param[in,out] must Prepared (empty) compiled must structure to fill.
53 * @return LY_ERR value.
54 */
Michal Vasko193dacd2022-10-13 08:43:05 +020055LY_ERR lys_compile_must(struct lysc_ctx *ctx, const struct lysp_restr *must_p, struct lysc_must *must);
56
57/**
58 * @brief Compile the parsed range restriction.
59 *
60 * @param[in] ctx Compile context.
61 * @param[in] range_p Parsed range structure to compile.
62 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
63 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
64 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
65 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
66 * range restriction must be more restrictive than the base_range.
67 * @param[in,out] range Pointer to the created current range structure.
68 * @return LY_ERR value.
69 */
70LY_ERR lys_compile_type_range(struct lysc_ctx *ctx, const struct lysp_restr *range_p, LY_DATA_TYPE basetype,
71 ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range);
Michal Vaskoedb0fa52022-10-04 10:36:00 +020072
73/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020074 * @brief Checks pattern syntax.
75 *
76 * @param[in] ctx Context.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020077 * @param[in] pattern Pattern to check.
Radek Krejci84d7fd72021-07-14 18:32:21 +020078 * @param[in,out] code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020079 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
80 */
Radek Krejci2efc45b2020-12-22 16:25:44 +010081LY_ERR lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020082
83/**
Michal Vasko51de7b72022-04-29 09:50:22 +020084 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
Michal Vaskoedb0fa52022-10-04 10:36:00 +020085 *
Michal Vasko51de7b72022-04-29 09:50:22 +020086 * @param[in] ctx Compile context.
87 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
88 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
89 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
90 * @param[out] patterns Pointer to the storage for the patterns of the current type.
91 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
92 */
Michal Vasko193dacd2022-10-13 08:43:05 +020093LY_ERR lys_compile_type_patterns(struct lysc_ctx *ctx, const struct lysp_restr *patterns_p,
Michal Vasko51de7b72022-04-29 09:50:22 +020094 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns);
95
96/**
Michal Vasko193dacd2022-10-13 08:43:05 +020097 * @brief Compile parsed type's enum structures (for enumeration and bits types).
98 *
99 * @param[in] ctx Compile context.
100 * @param[in] enums_p Array of the parsed enum structures to compile.
101 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
102 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
103 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
104 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
105 */
106LY_ERR lys_compile_type_enums(struct lysc_ctx *ctx, const struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
107 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums);
108
109/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200110 * @brief Compile information about the leaf/leaf-list's type.
111 *
112 * @param[in] ctx Compile context.
113 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
114 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200115 * @param[in] context_name Name of the context node or referencing typedef for logging.
116 * @param[in] type_p Parsed type to compile.
117 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
118 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
119 * @param[out] dflt Default value for the type.
120 * @return LY_ERR value.
121 */
122LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
Michal Vasko193dacd2022-10-13 08:43:05 +0200123 const char *context_name, const struct lysp_type *type_p, struct lysc_type **type, const char **units,
Michal Vaskoa99b3572021-02-01 11:54:58 +0100124 struct lysp_qname **dflt);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200125
126/**
Michal Vasko193dacd2022-10-13 08:43:05 +0200127 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
128 * keep specific order of augments targetting the same node.
129 *
130 * @param[in] ctx Compile context
131 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
132 * the choice itself is expected instead of a specific case node.
133 * @param[in] node Schema node to connect into the list.
134 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
135 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
136 */
137LY_ERR lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node);
138
139/**
140 * @brief Compile parsed action's input/output node information.
141 *
142 * @param[in] ctx Compile context
143 * @param[in] pnode Parsed inout node.
144 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
145 * is enriched with the inout-specific information.
146 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
147 */
148LY_ERR lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node);
149
150/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200151 * @brief Compile choice children.
152 *
153 * @param[in] ctx Compile context
154 * @param[in] child_p Parsed choice children nodes.
155 * @param[in] node Compiled choice node to compile and add children to.
Radek Krejci84d7fd72021-07-14 18:32:21 +0200156 * @param[in,out] child_set Optional set to add all the compiled nodes into (can be more in case of uses).
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200157 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
158 */
159LY_ERR lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
160 struct ly_set *child_set);
161
162/**
163 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
164 *
165 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
166 * the flag to such parents from a mandatory children.
167 *
168 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
169 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
170 * (mandatory children was removed).
171 */
172void lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add);
173
174/**
175 * @brief Validate grouping that was defined but not used in the schema itself.
176 *
177 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
178 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
179 *
180 * @param[in] ctx Compile context.
181 * @param[in] pnode Parsed parent node of the grouping, NULL for top-level.
182 * @param[in] grp Parsed grouping node to check.
183 * @return LY_ERR value.
184 */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100185LY_ERR lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200186
187/**
188 * @brief Compile parsed schema node information.
189 *
190 * @param[in] ctx Compile context
191 * @param[in] pnode Parsed schema node.
192 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
193 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
194 * the compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200195 * @param[in] inherited_flags Inherited flags from a schema-only statement.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200196 * @param[in,out] child_set Optional set to add all the compiled nodes into (can be more in case of uses).
197 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
198 */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200199LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t inherited_flags,
200 struct ly_set *child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200201
202#endif /* LY_SCHEMA_COMPILE_NODE_H_ */