blob: 3522e9abb7a538a209be852b84641e5b12bd5d71 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 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#include "common.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
27#include "set.h"
28#include "plugins_types.h"
29#include "tree.h"
30#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010031#include "tree_schema_internal.h"
32#include "xpath.h"
33
34/**
35 * @brief Duplicate string into dictionary
36 * @param[in] CTX libyang context of the dictionary.
37 * @param[in] ORIG String to duplicate.
38 * @param[out] DUP Where to store the result.
39 */
40#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
41
Radek Krejciec4da802019-05-02 13:02:41 +020042#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010043 if (ARRAY_P) { \
44 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010045 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010046 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
47 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020048 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010049 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
50 } \
51 }
52
Radek Krejciec4da802019-05-02 13:02:41 +020053#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010054 if (ARRAY_P) { \
55 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
56 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
57 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
58 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020059 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010060 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
61 } \
62 }
63
Radek Krejciec4da802019-05-02 13:02:41 +020064#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010065 if (ARRAY_P) { \
66 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
67 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
68 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
69 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020070 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010071 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 } \
73 }
74
Radek Krejciec4da802019-05-02 13:02:41 +020075#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010076 if (MEMBER_P) { \
77 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
78 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020079 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010080 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
81 }
82
Radek Krejciec4da802019-05-02 13:02:41 +020083#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010084 if (MEMBER_P) { \
85 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
86 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
87 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020088 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +010089 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
90 }
91
Radek Krejcid05cbd92018-12-05 14:26:40 +010092#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
93 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010094 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
95 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010096 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
97 return LY_EVALID; \
98 } \
99 } \
100 }
101
Radek Krejci19a96102018-11-15 13:38:09 +0100102static struct lysc_ext_instance *
103lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
104{
Radek Krejcie7b95092019-05-15 11:03:07 +0200105 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100106 (void) ctx;
107 (void) orig;
108 return NULL;
109}
110
Radek Krejcib56c7502019-02-13 14:19:54 +0100111/**
Radek Krejci327de162019-06-14 12:52:07 +0200112 * @brief Update path in the compile context.
113 *
114 * @param[in] ctx Compile context with the path.
115 * @param[in] parent Parent of the current node to check difference of the node's module. The current module is taken from lysc_ctx::mod.
116 * @param[in] name Name of the node to update path with. If NULL, the last segment is removed. If the format is `{keyword}`, the following
117 * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used).
118 */
119static void
120lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
121{
122 int len;
123 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
124
125 if (!name) {
126 /* removing last path segment */
127 if (ctx->path[ctx->path_len - 1] == '}') {
128 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
129 if (ctx->path[ctx->path_len] == '=') {
130 ctx->path[ctx->path_len++] = '}';
131 } else {
132 /* not a top-level special tag, remove also preceiding '/' */
133 goto remove_nodelevel;
134 }
135 } else {
136remove_nodelevel:
137 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
138 if (ctx->path_len == 0) {
139 /* top-level (last segment) */
140 ++ctx->path_len;
141 }
142 }
143 /* set new terminating NULL-byte */
144 ctx->path[ctx->path_len] = '\0';
145 } else {
146 if (ctx->path_len > 1) {
147 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
148 /* extension of the special tag */
149 nextlevel = 2;
150 --ctx->path_len;
151 } else {
152 /* there is already some path, so add next level */
153 nextlevel = 1;
154 }
155 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
156
157 if (nextlevel != 2) {
158 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
159 /* module not changed, print the name unprefixed */
160 len = sprintf(&ctx->path[ctx->path_len], "%s%s", nextlevel ? "/" : "", name);
161 } else {
162 len = sprintf(&ctx->path[ctx->path_len], "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
163 }
164 } else {
165 len = sprintf(&ctx->path[ctx->path_len], "='%s'}", name);
166 }
167 ctx->path_len += len;
168 }
169}
170
171/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100172 * @brief Duplicate the compiled pattern structure.
173 *
174 * Instead of duplicating memory, the reference counter in the @p orig is increased.
175 *
176 * @param[in] orig The pattern structure to duplicate.
177 * @return The duplicated structure to use.
178 */
Radek Krejci19a96102018-11-15 13:38:09 +0100179static struct lysc_pattern*
180lysc_pattern_dup(struct lysc_pattern *orig)
181{
182 ++orig->refcount;
183 return orig;
184}
185
Radek Krejcib56c7502019-02-13 14:19:54 +0100186/**
187 * @brief Duplicate the array of compiled patterns.
188 *
189 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
190 *
191 * @param[in] ctx Libyang context for logging.
192 * @param[in] orig The patterns sized array to duplicate.
193 * @return New sized array as a copy of @p orig.
194 * @return NULL in case of memory allocation error.
195 */
Radek Krejci19a96102018-11-15 13:38:09 +0100196static struct lysc_pattern**
197lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
198{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100199 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100200 unsigned int u;
201
Radek Krejcib56c7502019-02-13 14:19:54 +0100202 assert(orig);
203
Radek Krejci19a96102018-11-15 13:38:09 +0100204 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
205 LY_ARRAY_FOR(orig, u) {
206 dup[u] = lysc_pattern_dup(orig[u]);
207 LY_ARRAY_INCREMENT(dup);
208 }
209 return dup;
210}
211
Radek Krejcib56c7502019-02-13 14:19:54 +0100212/**
213 * @brief Duplicate compiled range structure.
214 *
215 * @param[in] ctx Libyang context for logging.
216 * @param[in] orig The range structure to be duplicated.
217 * @return New compiled range structure as a copy of @p orig.
218 * @return NULL in case of memory allocation error.
219 */
Radek Krejci19a96102018-11-15 13:38:09 +0100220struct lysc_range*
221lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
222{
223 struct lysc_range *dup;
224 LY_ERR ret;
225
Radek Krejcib56c7502019-02-13 14:19:54 +0100226 assert(orig);
227
Radek Krejci19a96102018-11-15 13:38:09 +0100228 dup = calloc(1, sizeof *dup);
229 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
230 if (orig->parts) {
231 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
232 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
233 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
234 }
235 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
236 DUP_STRING(ctx, orig->emsg, dup->emsg);
237 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
238
239 return dup;
240cleanup:
241 free(dup);
242 (void) ret; /* set but not used due to the return type */
243 return NULL;
244}
245
Radek Krejcib56c7502019-02-13 14:19:54 +0100246/**
247 * @brief Stack for processing if-feature expressions.
248 */
Radek Krejci19a96102018-11-15 13:38:09 +0100249struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100250 int size; /**< number of items in the stack */
251 int index; /**< first empty item */
252 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100253};
254
Radek Krejcib56c7502019-02-13 14:19:54 +0100255/**
256 * @brief Add @ref ifftokens into the stack.
257 * @param[in] stack The if-feature stack to use.
258 * @param[in] value One of the @ref ifftokens to store in the stack.
259 * @return LY_EMEM in case of memory allocation error
260 * @return LY_ESUCCESS if the value successfully stored.
261 */
Radek Krejci19a96102018-11-15 13:38:09 +0100262static LY_ERR
263iff_stack_push(struct iff_stack *stack, uint8_t value)
264{
265 if (stack->index == stack->size) {
266 stack->size += 4;
267 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
268 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
269 }
270 stack->stack[stack->index++] = value;
271 return LY_SUCCESS;
272}
273
Radek Krejcib56c7502019-02-13 14:19:54 +0100274/**
275 * @brief Get (and remove) the last item form the stack.
276 * @param[in] stack The if-feature stack to use.
277 * @return The value from the top of the stack.
278 */
Radek Krejci19a96102018-11-15 13:38:09 +0100279static uint8_t
280iff_stack_pop(struct iff_stack *stack)
281{
Radek Krejcib56c7502019-02-13 14:19:54 +0100282 assert(stack && stack->index);
283
Radek Krejci19a96102018-11-15 13:38:09 +0100284 stack->index--;
285 return stack->stack[stack->index];
286}
287
Radek Krejcib56c7502019-02-13 14:19:54 +0100288/**
289 * @brief Clean up the stack.
290 * @param[in] stack The if-feature stack to use.
291 */
Radek Krejci19a96102018-11-15 13:38:09 +0100292static void
293iff_stack_clean(struct iff_stack *stack)
294{
295 stack->size = 0;
296 free(stack->stack);
297}
298
Radek Krejcib56c7502019-02-13 14:19:54 +0100299/**
300 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
301 * (libyang format of the if-feature expression).
302 * @param[in,out] list The 2bits array to modify.
303 * @param[in] op The operand (@ref ifftokens) to store.
304 * @param[in] pos Position (0-based) where to store the given @p op.
305 */
Radek Krejci19a96102018-11-15 13:38:09 +0100306static void
307iff_setop(uint8_t *list, uint8_t op, int pos)
308{
309 uint8_t *item;
310 uint8_t mask = 3;
311
312 assert(pos >= 0);
313 assert(op <= 3); /* max 2 bits */
314
315 item = &list[pos / 4];
316 mask = mask << 2 * (pos % 4);
317 *item = (*item) & ~mask;
318 *item = (*item) | (op << 2 * (pos % 4));
319}
320
Radek Krejcib56c7502019-02-13 14:19:54 +0100321#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
322#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100323
Radek Krejci0af46292019-01-11 16:02:31 +0100324/**
325 * @brief Find a feature of the given name and referenced in the given module.
326 *
327 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
328 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
329 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
330 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
331 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
332 * assigned till that time will be still valid.
333 *
334 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
335 * @param[in] name Name of the feature including possible prefix.
336 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
337 * @return Pointer to the feature structure if found, NULL otherwise.
338 */
Radek Krejci19a96102018-11-15 13:38:09 +0100339static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100340lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100341{
342 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100343 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100344
345 for (i = 0; i < len; ++i) {
346 if (name[i] == ':') {
347 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100348 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100349 LY_CHECK_RET(!mod, NULL);
350
351 name = &name[i + 1];
352 len = len - i - 1;
353 }
354 }
355
356 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100357 if (mod->implemented) {
358 /* module is implemented so there is already the compiled schema */
359 flist = mod->compiled->features;
360 } else {
361 flist = mod->off_features;
362 }
363 LY_ARRAY_FOR(flist, i) {
364 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100365 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
366 return f;
367 }
368 }
369
370 return NULL;
371}
372
373static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200374lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext)
Radek Krejci19a96102018-11-15 13:38:09 +0100375{
376 const char *name;
377 unsigned int u;
378 const struct lys_module *mod;
379 struct lysp_ext *edef = NULL;
380
381 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
382 ext->insubstmt = ext_p->insubstmt;
383 ext->insubstmt_index = ext_p->insubstmt_index;
384
385 /* get module where the extension definition should be placed */
386 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100387 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100388 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
389 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
390 LY_EVALID);
391 LY_CHECK_ERR_RET(!mod->parsed->extensions,
392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
393 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100394 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100395 LY_EVALID);
396 name = &ext_p->name[u + 1];
397 /* find the extension definition there */
398 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
399 if (!strcmp(name, mod->parsed->extensions[u].name)) {
400 edef = &mod->parsed->extensions[u];
401 break;
402 }
403 }
404 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
405 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
406 LY_EVALID);
Radek Krejcia1911222019-07-22 17:24:50 +0200407 /* TODO extension plugins */
Radek Krejci19a96102018-11-15 13:38:09 +0100408
409 return LY_SUCCESS;
410}
411
Radek Krejcib56c7502019-02-13 14:19:54 +0100412/**
413 * @brief Compile information from the if-feature statement
414 * @param[in] ctx Compile context.
415 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100416 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
417 * @return LY_ERR value.
418 */
Radek Krejci19a96102018-11-15 13:38:09 +0100419static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200420lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100421{
422 const char *c = *value;
423 int r, rc = EXIT_FAILURE;
424 int i, j, last_not, checkversion = 0;
425 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
426 uint8_t op;
427 struct iff_stack stack = {0, 0, NULL};
428 struct lysc_feature *f;
429
430 assert(c);
431
432 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
433 for (i = j = last_not = 0; c[i]; i++) {
434 if (c[i] == '(') {
435 j++;
436 checkversion = 1;
437 continue;
438 } else if (c[i] == ')') {
439 j--;
440 continue;
441 } else if (isspace(c[i])) {
442 checkversion = 1;
443 continue;
444 }
445
446 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200447 int sp;
448 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
449 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100450 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
451 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
452 return LY_EVALID;
453 } else if (!isspace(c[i + r])) {
454 /* feature name starting with the not/and/or */
455 last_not = 0;
456 f_size++;
457 } else if (c[i] == 'n') { /* not operation */
458 if (last_not) {
459 /* double not */
460 expr_size = expr_size - 2;
461 last_not = 0;
462 } else {
463 last_not = 1;
464 }
465 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200466 if (f_exp != f_size) {
467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
468 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
469 return LY_EVALID;
470 }
Radek Krejci19a96102018-11-15 13:38:09 +0100471 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200472
Radek Krejci19a96102018-11-15 13:38:09 +0100473 /* not a not operation */
474 last_not = 0;
475 }
476 i += r;
477 } else {
478 f_size++;
479 last_not = 0;
480 }
481 expr_size++;
482
483 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200484 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100485 i--;
486 break;
487 }
488 i++;
489 }
490 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200491 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100492 /* not matching count of ( and ) */
493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
494 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
495 return LY_EVALID;
496 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200497 if (f_exp != f_size) {
498 /* features do not match the needed arguments for the logical operations */
499 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
500 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
501 "the required number of operands for the operations.", *value);
502 return LY_EVALID;
503 }
Radek Krejci19a96102018-11-15 13:38:09 +0100504
505 if (checkversion || expr_size > 1) {
506 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100507 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
509 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
510 return LY_EVALID;
511 }
512 }
513
514 /* allocate the memory */
515 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
516 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
517 stack.stack = malloc(expr_size * sizeof *stack.stack);
518 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
519
520 stack.size = expr_size;
521 f_size--; expr_size--; /* used as indexes from now */
522
523 for (i--; i >= 0; i--) {
524 if (c[i] == ')') {
525 /* push it on stack */
526 iff_stack_push(&stack, LYS_IFF_RP);
527 continue;
528 } else if (c[i] == '(') {
529 /* pop from the stack into result all operators until ) */
530 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
531 iff_setop(iff->expr, op, expr_size--);
532 }
533 continue;
534 } else if (isspace(c[i])) {
535 continue;
536 }
537
538 /* end of operator or operand -> find beginning and get what is it */
539 j = i + 1;
540 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
541 i--;
542 }
543 i++; /* go back by one step */
544
545 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
546 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
547 /* double not */
548 iff_stack_pop(&stack);
549 } else {
550 /* not has the highest priority, so do not pop from the stack
551 * as in case of AND and OR */
552 iff_stack_push(&stack, LYS_IFF_NOT);
553 }
554 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
555 /* as for OR - pop from the stack all operators with the same or higher
556 * priority and store them to the result, then push the AND to the stack */
557 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
558 op = iff_stack_pop(&stack);
559 iff_setop(iff->expr, op, expr_size--);
560 }
561 iff_stack_push(&stack, LYS_IFF_AND);
562 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
563 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
564 op = iff_stack_pop(&stack);
565 iff_setop(iff->expr, op, expr_size--);
566 }
567 iff_stack_push(&stack, LYS_IFF_OR);
568 } else {
569 /* feature name, length is j - i */
570
571 /* add it to the expression */
572 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
573
574 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100575 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
576 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
577 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
578 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100579 iff->features[f_size] = f;
580 LY_ARRAY_INCREMENT(iff->features);
581 f_size--;
582 }
583 }
584 while (stack.index) {
585 op = iff_stack_pop(&stack);
586 iff_setop(iff->expr, op, expr_size--);
587 }
588
589 if (++expr_size || ++f_size) {
590 /* not all expected operators and operands found */
591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
592 "Invalid value \"%s\" of if-feature - processing error.", *value);
593 rc = LY_EINT;
594 } else {
595 rc = LY_SUCCESS;
596 }
597
598error:
599 /* cleanup */
600 iff_stack_clean(&stack);
601
602 return rc;
603}
604
Radek Krejcib56c7502019-02-13 14:19:54 +0100605/**
606 * @brief Compile information from the when statement
607 * @param[in] ctx Compile context.
608 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100609 * @param[out] when Pointer where to store pointer to the created compiled when structure.
610 * @return LY_ERR value.
611 */
Radek Krejci19a96102018-11-15 13:38:09 +0100612static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200613lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100614{
615 unsigned int u;
616 LY_ERR ret = LY_SUCCESS;
617
Radek Krejci00b874b2019-02-12 10:54:50 +0100618 *when = calloc(1, sizeof **when);
619 (*when)->refcount = 1;
620 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
621 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
622 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
623 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejciec4da802019-05-02 13:02:41 +0200624 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100625
626done:
627 return ret;
628}
629
Radek Krejcib56c7502019-02-13 14:19:54 +0100630/**
631 * @brief Compile information from the must statement
632 * @param[in] ctx Compile context.
633 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100634 * @param[in,out] must Prepared (empty) compiled must structure to fill.
635 * @return LY_ERR value.
636 */
Radek Krejci19a96102018-11-15 13:38:09 +0100637static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200638lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100639{
640 unsigned int u;
641 LY_ERR ret = LY_SUCCESS;
642
643 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
644 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
645
646 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
647 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100648 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
649 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejciec4da802019-05-02 13:02:41 +0200650 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100651
652done:
653 return ret;
654}
655
Radek Krejcib56c7502019-02-13 14:19:54 +0100656/**
657 * @brief Compile information from the import statement
658 * @param[in] ctx Compile context.
659 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100660 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
661 * @return LY_ERR value.
662 */
Radek Krejci19a96102018-11-15 13:38:09 +0100663static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200664lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100665{
666 unsigned int u;
667 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100668 LY_ERR ret = LY_SUCCESS;
669
670 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejciec4da802019-05-02 13:02:41 +0200671 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100672 imp->module = imp_p->module;
673
Radek Krejci7f2a5362018-11-28 13:05:37 +0100674 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
675 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100676 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100677 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100678 /* try to use filepath if present */
679 if (imp->module->filepath) {
680 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
681 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100682 if (mod != imp->module) {
683 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100684 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100685 mod = NULL;
686 }
687 }
688 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100689 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100690 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100691 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100692 return LY_ENOTFOUND;
693 }
694 }
Radek Krejci19a96102018-11-15 13:38:09 +0100695 }
696
697done:
698 return ret;
699}
700
Radek Krejcib56c7502019-02-13 14:19:54 +0100701/**
702 * @brief Compile information from the identity statement
703 *
704 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
705 *
706 * @param[in] ctx Compile context.
707 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100708 * @param[in] idents List of so far compiled identities to check the name uniqueness.
709 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
710 * @return LY_ERR value.
711 */
Radek Krejci19a96102018-11-15 13:38:09 +0100712static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200713lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100714{
715 unsigned int u;
716 LY_ERR ret = LY_SUCCESS;
717
Radek Krejci327de162019-06-14 12:52:07 +0200718 lysc_update_path(ctx, NULL, ident_p->name);
719
Radek Krejcid05cbd92018-12-05 14:26:40 +0100720 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100721 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200722 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
723 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200724 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200725 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100726 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejciec4da802019-05-02 13:02:41 +0200727 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100728 ident->flags = ident_p->flags;
729
Radek Krejci327de162019-06-14 12:52:07 +0200730 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100731done:
732 return ret;
733}
734
Radek Krejcib56c7502019-02-13 14:19:54 +0100735/**
736 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
737 *
738 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
739 *
740 * @param[in] ctx Compile context for logging.
741 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
742 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
743 * @return LY_SUCCESS if everything is ok.
744 * @return LY_EVALID if the identity is derived from itself.
745 */
Radek Krejci38222632019-02-12 16:55:05 +0100746static LY_ERR
747lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
748{
749 LY_ERR ret = LY_EVALID;
750 unsigned int u, v;
751 struct ly_set recursion = {0};
752 struct lysc_ident *drv;
753
754 if (!derived) {
755 return LY_SUCCESS;
756 }
757
758 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
759 if (ident == derived[u]) {
760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
761 "Identity \"%s\" is indirectly derived from itself.", ident->name);
762 goto cleanup;
763 }
764 ly_set_add(&recursion, derived[u], 0);
765 }
766
767 for (v = 0; v < recursion.count; ++v) {
768 drv = recursion.objs[v];
769 if (!drv->derived) {
770 continue;
771 }
772 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
773 if (ident == drv->derived[u]) {
774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
775 "Identity \"%s\" is indirectly derived from itself.", ident->name);
776 goto cleanup;
777 }
778 ly_set_add(&recursion, drv->derived[u], 0);
779 }
780 }
781 ret = LY_SUCCESS;
782
783cleanup:
784 ly_set_erase(&recursion, NULL);
785 return ret;
786}
787
Radek Krejcia3045382018-11-22 14:30:31 +0100788/**
789 * @brief Find and process the referenced base identities from another identity or identityref
790 *
Radek Krejciaca74032019-06-04 08:53:06 +0200791 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100792 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
793 * to distinguish these two use cases.
794 *
795 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
796 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
797 * @param[in] ident Referencing identity to work with.
798 * @param[in] bases Array of bases of identityref to fill in.
799 * @return LY_ERR value.
800 */
Radek Krejci19a96102018-11-15 13:38:09 +0100801static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100802lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +0100803{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100804 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100805 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100806 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100807 struct lysc_ident **idref;
808
809 assert(ident || bases);
810
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100811 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100812 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
813 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
814 return LY_EVALID;
815 }
816
817 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
818 s = strchr(bases_p[u], ':');
819 if (s) {
820 /* prefixed identity */
821 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100822 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100823 } else {
824 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100825 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100826 }
827 if (!mod) {
828 if (ident) {
829 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
830 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
831 } else {
832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
833 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
834 }
835 return LY_EVALID;
836 }
837 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100838 if (mod->compiled && mod->compiled->identities) {
839 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
840 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100841 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100842 if (ident == &mod->compiled->identities[v]) {
843 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
844 "Identity \"%s\" is derived from itself.", ident->name);
845 return LY_EVALID;
846 }
847 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100848 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100849 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100850 *idref = ident;
851 } else {
852 /* we have match! store the found identity */
853 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100854 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100855 }
856 break;
857 }
858 }
859 }
860 if (!idref || !(*idref)) {
861 if (ident) {
862 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
863 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
864 } else {
865 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
866 "Unable to find base (%s) of identityref.", bases_p[u]);
867 }
868 return LY_EVALID;
869 }
870 }
871 return LY_SUCCESS;
872}
873
Radek Krejcia3045382018-11-22 14:30:31 +0100874/**
875 * @brief For the given array of identities, set the backlinks from all their base identities.
876 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
877 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
878 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
879 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
880 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100881static LY_ERR
882lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
883{
884 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100885
886 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
887 if (!idents_p[i].bases) {
888 continue;
889 }
Radek Krejci327de162019-06-14 12:52:07 +0200890 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100891 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +0200892 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100893 }
894 return LY_SUCCESS;
895}
896
Radek Krejci0af46292019-01-11 16:02:31 +0100897LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +0200898lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +0100899{
900 unsigned int offset = 0, u;
901 struct lysc_ctx context = {0};
902
Radek Krejci327de162019-06-14 12:52:07 +0200903 assert(ctx_sc || ctx);
904
905 if (!ctx_sc) {
906 context.ctx = ctx;
907 context.mod = module;
908 context.path_len = 1;
909 context.path[0] = '/';
910 ctx_sc = &context;
911 }
Radek Krejci0af46292019-01-11 16:02:31 +0100912
913 if (!features_p) {
914 return LY_SUCCESS;
915 }
916 if (*features) {
917 offset = LY_ARRAY_SIZE(*features);
918 }
919
Radek Krejci327de162019-06-14 12:52:07 +0200920 lysc_update_path(ctx_sc, NULL, "{feature}");
921 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +0100922 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +0200923 lysc_update_path(ctx_sc, NULL, features_p[u].name);
924
Radek Krejci0af46292019-01-11 16:02:31 +0100925 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +0200926 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
927 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
928 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
929 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +0100930 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +0200931 (*features)[offset + u].module = ctx_sc->mod;
932
933 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +0100934 }
Radek Krejci327de162019-06-14 12:52:07 +0200935 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +0100936
937 return LY_SUCCESS;
938}
939
Radek Krejcia3045382018-11-22 14:30:31 +0100940/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100941 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100942 *
943 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
944 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100945 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100946 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
947 * being currently processed).
948 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +0100949 * @return LY_SUCCESS if everything is ok.
950 * @return LY_EVALID if the feature references indirectly itself.
951 */
952static LY_ERR
953lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
954{
955 LY_ERR ret = LY_EVALID;
956 unsigned int u, v;
957 struct ly_set recursion = {0};
958 struct lysc_feature *drv;
959
960 if (!depfeatures) {
961 return LY_SUCCESS;
962 }
963
964 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
965 if (feature == depfeatures[u]) {
966 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
967 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
968 goto cleanup;
969 }
970 ly_set_add(&recursion, depfeatures[u], 0);
971 }
972
973 for (v = 0; v < recursion.count; ++v) {
974 drv = recursion.objs[v];
975 if (!drv->depfeatures) {
976 continue;
977 }
978 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
979 if (feature == drv->depfeatures[u]) {
980 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
981 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
982 goto cleanup;
983 }
984 ly_set_add(&recursion, drv->depfeatures[u], 0);
985 }
986 }
987 ret = LY_SUCCESS;
988
989cleanup:
990 ly_set_erase(&recursion, NULL);
991 return ret;
992}
993
994/**
Radek Krejci0af46292019-01-11 16:02:31 +0100995 * @brief Create pre-compiled features array.
996 *
997 * See lys_feature_precompile() for more details.
998 *
Radek Krejcia3045382018-11-22 14:30:31 +0100999 * @param[in] ctx Compile context.
1000 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001001 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001002 * @return LY_ERR value.
1003 */
Radek Krejci19a96102018-11-15 13:38:09 +01001004static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001005lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001006{
Radek Krejci0af46292019-01-11 16:02:31 +01001007 unsigned int u, v, x;
1008 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001009 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001010
Radek Krejci327de162019-06-14 12:52:07 +02001011
Radek Krejci0af46292019-01-11 16:02:31 +01001012 /* find the preprecompiled feature */
1013 LY_ARRAY_FOR(features, x) {
1014 if (strcmp(features[x].name, feature_p->name)) {
1015 continue;
1016 }
1017 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001018 lysc_update_path(ctx, NULL, "{feature}");
1019 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001020
Radek Krejci0af46292019-01-11 16:02:31 +01001021 /* finish compilation started in lys_feature_precompile() */
Radek Krejciec4da802019-05-02 13:02:41 +02001022 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done);
1023 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001024 if (feature->iffeatures) {
1025 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1026 if (feature->iffeatures[u].features) {
1027 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001028 /* check for circular dependency - direct reference first,... */
1029 if (feature == feature->iffeatures[u].features[v]) {
1030 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1031 "Feature \"%s\" is referenced from itself.", feature->name);
1032 return LY_EVALID;
1033 }
1034 /* ... and indirect circular reference */
1035 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1036
Radek Krejci0af46292019-01-11 16:02:31 +01001037 /* add itself into the dependants list */
1038 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1039 *df = feature;
1040 }
Radek Krejci19a96102018-11-15 13:38:09 +01001041 }
Radek Krejci19a96102018-11-15 13:38:09 +01001042 }
1043 }
Radek Krejci327de162019-06-14 12:52:07 +02001044 lysc_update_path(ctx, NULL, NULL);
1045 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001046 done:
1047 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001048 }
Radek Krejci0af46292019-01-11 16:02:31 +01001049
1050 LOGINT(ctx->ctx);
1051 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001052}
1053
Radek Krejcib56c7502019-02-13 14:19:54 +01001054/**
1055 * @brief Revert compiled list of features back to the precompiled state.
1056 *
1057 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1058 * The features are supposed to be stored again as off_features in ::lys_module structure.
1059 *
1060 * @param[in] ctx Compilation context.
1061 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1062 * and supposed to hold the off_features list.
1063 */
Radek Krejci95710c92019-02-11 15:49:55 +01001064static void
1065lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1066{
1067 unsigned int u, v;
1068
1069 /* keep the off_features list until the complete lys_module is freed */
1070 mod->off_features = mod->compiled->features;
1071 mod->compiled->features = NULL;
1072
1073 /* in the off_features list, remove all the parts (from finished compiling process)
1074 * which may points into the data being freed here */
1075 LY_ARRAY_FOR(mod->off_features, u) {
1076 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1077 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1078 }
1079 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1080 mod->off_features[u].iffeatures = NULL;
1081
1082 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1083 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1084 }
1085 LY_ARRAY_FREE(mod->off_features[u].exts);
1086 mod->off_features[u].exts = NULL;
1087 }
1088}
1089
Radek Krejcia3045382018-11-22 14:30:31 +01001090/**
1091 * @brief Validate and normalize numeric value from a range definition.
1092 * @param[in] ctx Compile context.
1093 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1094 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1095 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1096 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1097 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1098 * @param[in] value String value of the range boundary.
1099 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
1100 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1101 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1102 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001103LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001104range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001105{
Radek Krejci6cba4292018-11-15 17:33:29 +01001106 size_t fraction = 0, size;
1107
Radek Krejci19a96102018-11-15 13:38:09 +01001108 *len = 0;
1109
1110 assert(value);
1111 /* parse value */
1112 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1113 return LY_EVALID;
1114 }
1115
1116 if ((value[*len] == '-') || (value[*len] == '+')) {
1117 ++(*len);
1118 }
1119
1120 while (isdigit(value[*len])) {
1121 ++(*len);
1122 }
1123
1124 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001125 if (basetype == LY_TYPE_DEC64) {
1126 goto decimal;
1127 } else {
1128 *valcopy = strndup(value, *len);
1129 return LY_SUCCESS;
1130 }
Radek Krejci19a96102018-11-15 13:38:09 +01001131 }
1132 fraction = *len;
1133
1134 ++(*len);
1135 while (isdigit(value[*len])) {
1136 ++(*len);
1137 }
1138
Radek Krejci6cba4292018-11-15 17:33:29 +01001139 if (basetype == LY_TYPE_DEC64) {
1140decimal:
1141 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001142 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001143 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1144 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1145 *len, value, frdigits);
1146 return LY_EINVAL;
1147 }
1148 if (fraction) {
1149 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1150 } else {
1151 size = (*len) + frdigits + 1;
1152 }
1153 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001154 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1155
Radek Krejci6cba4292018-11-15 17:33:29 +01001156 (*valcopy)[size - 1] = '\0';
1157 if (fraction) {
1158 memcpy(&(*valcopy)[0], &value[0], fraction);
1159 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1160 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1161 } else {
1162 memcpy(&(*valcopy)[0], &value[0], *len);
1163 memset(&(*valcopy)[*len], '0', frdigits);
1164 }
Radek Krejci19a96102018-11-15 13:38:09 +01001165 }
1166 return LY_SUCCESS;
1167}
1168
Radek Krejcia3045382018-11-22 14:30:31 +01001169/**
1170 * @brief Check that values in range are in ascendant order.
1171 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001172 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1173 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001174 * @param[in] value Current value to check.
1175 * @param[in] prev_value The last seen value.
1176 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1177 */
Radek Krejci19a96102018-11-15 13:38:09 +01001178static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001179range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001180{
1181 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001182 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001183 return LY_EEXIST;
1184 }
1185 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001186 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001187 return LY_EEXIST;
1188 }
1189 }
1190 return LY_SUCCESS;
1191}
1192
Radek Krejcia3045382018-11-22 14:30:31 +01001193/**
1194 * @brief Set min/max value of the range part.
1195 * @param[in] ctx Compile context.
1196 * @param[in] part Range part structure to fill.
1197 * @param[in] max Flag to distinguish if storing min or max value.
1198 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1199 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1200 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1201 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1202 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001203 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001204 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1205 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1206 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1207 * frdigits value), LY_EMEM.
1208 */
Radek Krejci19a96102018-11-15 13:38:09 +01001209static LY_ERR
1210range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001211 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001212{
1213 LY_ERR ret = LY_SUCCESS;
1214 char *valcopy = NULL;
1215 size_t len;
1216
1217 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001218 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001219 LY_CHECK_GOTO(ret, finalize);
1220 }
1221 if (!valcopy && base_range) {
1222 if (max) {
1223 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1224 } else {
1225 part->min_64 = base_range->parts[0].min_64;
1226 }
1227 if (!first) {
1228 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1229 }
1230 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001231 }
1232
1233 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001234 case LY_TYPE_INT8: /* range */
1235 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001236 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001237 } else if (max) {
1238 part->max_64 = INT64_C(127);
1239 } else {
1240 part->min_64 = INT64_C(-128);
1241 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001242 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001243 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001244 }
1245 break;
1246 case LY_TYPE_INT16: /* range */
1247 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001248 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001249 } else if (max) {
1250 part->max_64 = INT64_C(32767);
1251 } else {
1252 part->min_64 = INT64_C(-32768);
1253 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001254 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001255 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001256 }
1257 break;
1258 case LY_TYPE_INT32: /* range */
1259 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001260 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001261 } else if (max) {
1262 part->max_64 = INT64_C(2147483647);
1263 } else {
1264 part->min_64 = INT64_C(-2147483648);
1265 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001266 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001267 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001268 }
1269 break;
1270 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001271 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001272 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001273 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001274 max ? &part->max_64 : &part->min_64);
1275 } else if (max) {
1276 part->max_64 = INT64_C(9223372036854775807);
1277 } else {
1278 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1279 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001280 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001281 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001282 }
1283 break;
1284 case LY_TYPE_UINT8: /* range */
1285 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001286 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001287 } else if (max) {
1288 part->max_u64 = UINT64_C(255);
1289 } else {
1290 part->min_u64 = UINT64_C(0);
1291 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001292 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001293 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001294 }
1295 break;
1296 case LY_TYPE_UINT16: /* range */
1297 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001298 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001299 } else if (max) {
1300 part->max_u64 = UINT64_C(65535);
1301 } else {
1302 part->min_u64 = UINT64_C(0);
1303 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001304 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001305 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001306 }
1307 break;
1308 case LY_TYPE_UINT32: /* range */
1309 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001310 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001311 } else if (max) {
1312 part->max_u64 = UINT64_C(4294967295);
1313 } else {
1314 part->min_u64 = UINT64_C(0);
1315 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001316 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001317 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001318 }
1319 break;
1320 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001321 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001322 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001323 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001324 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001325 } else if (max) {
1326 part->max_u64 = UINT64_C(18446744073709551615);
1327 } else {
1328 part->min_u64 = UINT64_C(0);
1329 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001330 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001331 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001332 }
1333 break;
1334 default:
1335 LOGINT(ctx->ctx);
1336 ret = LY_EINT;
1337 }
1338
Radek Krejci5969f272018-11-23 10:03:58 +01001339finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001340 if (ret == LY_EDENIED) {
1341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1342 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1343 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1344 } else if (ret == LY_EVALID) {
1345 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1346 "Invalid %s restriction - invalid value \"%s\".",
1347 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1348 } else if (ret == LY_EEXIST) {
1349 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1350 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001351 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001352 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001353 } else if (!ret && value) {
1354 *value = *value + len;
1355 }
1356 free(valcopy);
1357 return ret;
1358}
1359
Radek Krejcia3045382018-11-22 14:30:31 +01001360/**
1361 * @brief Compile the parsed range restriction.
1362 * @param[in] ctx Compile context.
1363 * @param[in] range_p Parsed range structure to compile.
1364 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1365 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1366 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1367 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1368 * range restriction must be more restrictive than the base_range.
1369 * @param[in,out] range Pointer to the created current range structure.
1370 * @return LY_ERR value.
1371 */
Radek Krejci19a96102018-11-15 13:38:09 +01001372static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001373lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001374 struct lysc_range *base_range, struct lysc_range **range)
1375{
1376 LY_ERR ret = LY_EVALID;
1377 const char *expr;
1378 struct lysc_range_part *parts = NULL, *part;
1379 int range_expected = 0, uns;
1380 unsigned int parts_done = 0, u, v;
1381
1382 assert(range);
1383 assert(range_p);
1384
1385 expr = range_p->arg;
1386 while(1) {
1387 if (isspace(*expr)) {
1388 ++expr;
1389 } else if (*expr == '\0') {
1390 if (range_expected) {
1391 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1392 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1393 length_restr ? "length" : "range", range_p->arg);
1394 goto cleanup;
1395 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1397 "Invalid %s restriction - unexpected end of the expression (%s).",
1398 length_restr ? "length" : "range", range_p->arg);
1399 goto cleanup;
1400 }
1401 parts_done++;
1402 break;
1403 } else if (!strncmp(expr, "min", 3)) {
1404 if (parts) {
1405 /* min cannot be used elsewhere than in the first part */
1406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1407 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1408 expr - range_p->arg, range_p->arg);
1409 goto cleanup;
1410 }
1411 expr += 3;
1412
1413 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001414 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001415 part->max_64 = part->min_64;
1416 } else if (*expr == '|') {
1417 if (!parts || range_expected) {
1418 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1419 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1420 goto cleanup;
1421 }
1422 expr++;
1423 parts_done++;
1424 /* process next part of the expression */
1425 } else if (!strncmp(expr, "..", 2)) {
1426 expr += 2;
1427 while (isspace(*expr)) {
1428 expr++;
1429 }
1430 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1432 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1433 goto cleanup;
1434 }
1435 /* continue expecting the upper boundary */
1436 range_expected = 1;
1437 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1438 /* number */
1439 if (range_expected) {
1440 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001441 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001442 range_expected = 0;
1443 } else {
1444 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1445 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001446 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001447 part->max_64 = part->min_64;
1448 }
1449
1450 /* continue with possible another expression part */
1451 } else if (!strncmp(expr, "max", 3)) {
1452 expr += 3;
1453 while (isspace(*expr)) {
1454 expr++;
1455 }
1456 if (*expr != '\0') {
1457 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1458 length_restr ? "length" : "range", expr);
1459 goto cleanup;
1460 }
1461 if (range_expected) {
1462 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001463 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001464 range_expected = 0;
1465 } else {
1466 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1467 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001468 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001469 part->min_64 = part->max_64;
1470 }
1471 } else {
1472 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1473 length_restr ? "length" : "range", expr);
1474 goto cleanup;
1475 }
1476 }
1477
1478 /* check with the previous range/length restriction */
1479 if (base_range) {
1480 switch (basetype) {
1481 case LY_TYPE_BINARY:
1482 case LY_TYPE_UINT8:
1483 case LY_TYPE_UINT16:
1484 case LY_TYPE_UINT32:
1485 case LY_TYPE_UINT64:
1486 case LY_TYPE_STRING:
1487 uns = 1;
1488 break;
1489 case LY_TYPE_DEC64:
1490 case LY_TYPE_INT8:
1491 case LY_TYPE_INT16:
1492 case LY_TYPE_INT32:
1493 case LY_TYPE_INT64:
1494 uns = 0;
1495 break;
1496 default:
1497 LOGINT(ctx->ctx);
1498 ret = LY_EINT;
1499 goto cleanup;
1500 }
1501 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1502 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1503 goto baseerror;
1504 }
1505 /* current lower bound is not lower than the base */
1506 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1507 /* base has single value */
1508 if (base_range->parts[v].min_64 == parts[u].min_64) {
1509 /* both lower bounds are the same */
1510 if (parts[u].min_64 != parts[u].max_64) {
1511 /* current continues with a range */
1512 goto baseerror;
1513 } else {
1514 /* equal single values, move both forward */
1515 ++v;
1516 continue;
1517 }
1518 } else {
1519 /* base is single value lower than current range, so the
1520 * value from base range is removed in the current,
1521 * move only base and repeat checking */
1522 ++v;
1523 --u;
1524 continue;
1525 }
1526 } else {
1527 /* base is the range */
1528 if (parts[u].min_64 == parts[u].max_64) {
1529 /* current is a single value */
1530 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1531 /* current is behind the base range, so base range is omitted,
1532 * move the base and keep the current for further check */
1533 ++v;
1534 --u;
1535 } /* else it is within the base range, so move the current, but keep the base */
1536 continue;
1537 } else {
1538 /* both are ranges - check the higher bound, the lower was already checked */
1539 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1540 /* higher bound is higher than the current higher bound */
1541 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1542 /* but the current lower bound is also higher, so the base range is omitted,
1543 * continue with the same current, but move the base */
1544 --u;
1545 ++v;
1546 continue;
1547 }
1548 /* current range starts within the base range but end behind it */
1549 goto baseerror;
1550 } else {
1551 /* current range is smaller than the base,
1552 * move current, but stay with the base */
1553 continue;
1554 }
1555 }
1556 }
1557 }
1558 if (u != parts_done) {
1559baseerror:
1560 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1561 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1562 length_restr ? "length" : "range", range_p->arg);
1563 goto cleanup;
1564 }
1565 }
1566
1567 if (!(*range)) {
1568 *range = calloc(1, sizeof **range);
1569 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1570 }
1571
Radek Krejcic8b31002019-01-08 10:24:45 +01001572 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001573 if (range_p->eapptag) {
1574 lydict_remove(ctx->ctx, (*range)->eapptag);
1575 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1576 }
1577 if (range_p->emsg) {
1578 lydict_remove(ctx->ctx, (*range)->emsg);
1579 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1580 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001581 if (range_p->dsc) {
1582 lydict_remove(ctx->ctx, (*range)->dsc);
1583 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1584 }
1585 if (range_p->ref) {
1586 lydict_remove(ctx->ctx, (*range)->ref);
1587 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1588 }
Radek Krejci19a96102018-11-15 13:38:09 +01001589 /* extensions are taken only from the last range by the caller */
1590
1591 (*range)->parts = parts;
1592 parts = NULL;
1593 ret = LY_SUCCESS;
1594cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001595 LY_ARRAY_FREE(parts);
1596
1597 return ret;
1598}
1599
1600/**
1601 * @brief Checks pattern syntax.
1602 *
Radek Krejcia3045382018-11-22 14:30:31 +01001603 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001604 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001605 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Radek Krejcia3045382018-11-22 14:30:31 +01001606 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001607 */
1608static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001609lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001610{
Radek Krejci54579462019-04-30 12:47:06 +02001611 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001612 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001613 int err_code;
1614 const char *orig_ptr;
1615 PCRE2_SIZE err_offset;
1616 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001617#define URANGE_LEN 19
1618 char *ublock2urange[][2] = {
1619 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1620 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1621 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1622 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1623 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1624 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1625 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1626 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1627 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1628 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1629 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1630 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1631 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1632 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1633 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1634 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1635 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1636 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1637 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1638 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1639 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1640 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1641 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1642 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1643 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1644 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1645 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1646 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1647 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1648 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1649 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1650 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1651 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1652 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1653 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1654 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1655 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1656 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1657 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1658 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1659 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1660 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1661 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1662 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1663 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1664 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1665 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1666 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1667 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1668 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1669 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1670 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1671 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1672 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1673 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1674 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1675 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1676 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1677 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1678 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1679 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1680 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1681 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1682 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1683 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1684 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1685 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1686 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1687 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1688 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1689 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1690 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1691 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1692 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1693 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1694 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1695 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1696 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1697 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1698 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1699 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1700 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1701 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1702 {NULL, NULL}
1703 };
1704
1705 /* adjust the expression to a Perl equivalent
1706 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1707
1708 /* we need to replace all "$" with "\$", count them now */
1709 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1710
1711 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1712 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1713 perl_regex[0] = '\0';
1714
1715 ptr = perl_regex;
1716
Radek Krejci19a96102018-11-15 13:38:09 +01001717 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1718 if (orig_ptr[0] == '$') {
1719 ptr += sprintf(ptr, "\\$");
1720 } else if (orig_ptr[0] == '^') {
1721 ptr += sprintf(ptr, "\\^");
1722 } else {
1723 ptr[0] = orig_ptr[0];
1724 ++ptr;
1725 }
1726 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001727 ptr[0] = '\0';
1728 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001729
1730 /* substitute Unicode Character Blocks with exact Character Ranges */
1731 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1732 start = ptr - perl_regex;
1733
1734 ptr = strchr(ptr, '}');
1735 if (!ptr) {
1736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1737 pattern, perl_regex + start + 2, "unterminated character property");
1738 free(perl_regex);
1739 return LY_EVALID;
1740 }
1741 end = (ptr - perl_regex) + 1;
1742
1743 /* need more space */
1744 if (end - start < URANGE_LEN) {
1745 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1746 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1747 }
1748
1749 /* find our range */
1750 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1751 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1752 break;
1753 }
1754 }
1755 if (!ublock2urange[idx][0]) {
1756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1757 pattern, perl_regex + start + 5, "unknown block name");
1758 free(perl_regex);
1759 return LY_EVALID;
1760 }
1761
1762 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1763 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1764 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1765 ++count;
1766 }
1767 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1768 --count;
1769 }
1770 }
1771 if (count) {
1772 /* skip brackets */
1773 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1774 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1775 } else {
1776 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1777 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1778 }
1779 }
1780
1781 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001782 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1783 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001784 &err_code, &err_offset, NULL);
1785 if (!code_local) {
1786 PCRE2_UCHAR err_msg[256] = {0};
1787 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1789 free(perl_regex);
1790 return LY_EVALID;
1791 }
1792 free(perl_regex);
1793
Radek Krejci54579462019-04-30 12:47:06 +02001794 if (code) {
1795 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001796 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001797 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001798 }
1799
1800 return LY_SUCCESS;
1801
1802#undef URANGE_LEN
1803}
1804
Radek Krejcia3045382018-11-22 14:30:31 +01001805/**
1806 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1807 * @param[in] ctx Compile context.
1808 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001809 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1810 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1811 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1812 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1813 */
Radek Krejci19a96102018-11-15 13:38:09 +01001814static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001815lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001816 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1817{
1818 struct lysc_pattern **pattern;
1819 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001820 LY_ERR ret = LY_SUCCESS;
1821
1822 /* first, copy the patterns from the base type */
1823 if (base_patterns) {
1824 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1825 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1826 }
1827
1828 LY_ARRAY_FOR(patterns_p, u) {
1829 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1830 *pattern = calloc(1, sizeof **pattern);
1831 ++(*pattern)->refcount;
1832
Radek Krejci54579462019-04-30 12:47:06 +02001833 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001834 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001835
1836 if (patterns_p[u].arg[0] == 0x15) {
1837 (*pattern)->inverted = 1;
1838 }
Radek Krejci54579462019-04-30 12:47:06 +02001839 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001840 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1841 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001842 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1843 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02001844 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001845 }
1846done:
1847 return ret;
1848}
1849
Radek Krejcia3045382018-11-22 14:30:31 +01001850/**
1851 * @brief map of the possible restrictions combination for the specific built-in type.
1852 */
Radek Krejci19a96102018-11-15 13:38:09 +01001853static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1854 0 /* LY_TYPE_UNKNOWN */,
1855 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001856 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1857 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1858 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1859 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1860 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001861 LYS_SET_BIT /* LY_TYPE_BITS */,
1862 0 /* LY_TYPE_BOOL */,
1863 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1864 0 /* LY_TYPE_EMPTY */,
1865 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1866 LYS_SET_BASE /* LY_TYPE_IDENT */,
1867 LYS_SET_REQINST /* LY_TYPE_INST */,
1868 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001869 LYS_SET_TYPE /* LY_TYPE_UNION */,
1870 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001871 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001872 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001873 LYS_SET_RANGE /* LY_TYPE_INT64 */
1874};
1875
1876/**
1877 * @brief stringification of the YANG built-in data types
1878 */
1879const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1880 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1881 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001882};
1883
Radek Krejcia3045382018-11-22 14:30:31 +01001884/**
1885 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1886 * @param[in] ctx Compile context.
1887 * @param[in] enums_p Array of the parsed enum structures to compile.
1888 * @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.
Radek Krejcia3045382018-11-22 14:30:31 +01001889 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1890 * @param[out] enums Newly created array of the compiled enums information for the current type.
1891 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1892 */
Radek Krejci19a96102018-11-15 13:38:09 +01001893static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001894lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02001895 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01001896{
1897 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001898 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01001899 int32_t value = 0;
1900 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001901 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01001902
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001903 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1905 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1906 return LY_EVALID;
1907 }
1908
1909 LY_ARRAY_FOR(enums_p, u) {
1910 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1911 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001912 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1913 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02001914 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01001915 if (base_enums) {
1916 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1917 LY_ARRAY_FOR(base_enums, v) {
1918 if (!strcmp(e->name, base_enums[v].name)) {
1919 break;
1920 }
1921 }
1922 if (v == LY_ARRAY_SIZE(base_enums)) {
1923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1924 "Invalid %s - derived type adds new item \"%s\".",
1925 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1926 return LY_EVALID;
1927 }
1928 match = v;
1929 }
1930
1931 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02001932 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01001933 if (enums_p[u].flags & LYS_SET_VALUE) {
1934 e->value = (int32_t)enums_p[u].value;
1935 if (!u || e->value >= value) {
1936 value = e->value + 1;
1937 }
1938 /* check collision with other values */
1939 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1940 if (e->value == (*enums)[v].value) {
1941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1942 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1943 e->value, e->name, (*enums)[v].name);
1944 return LY_EVALID;
1945 }
1946 }
1947 } else if (base_enums) {
1948 /* inherit the assigned value */
1949 e->value = base_enums[match].value;
1950 if (!u || e->value >= value) {
1951 value = e->value + 1;
1952 }
1953 } else {
1954 /* assign value automatically */
1955 if (u && value == INT32_MIN) {
1956 /* counter overflow */
1957 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1958 "Invalid enumeration - it is not possible to auto-assign enum value for "
1959 "\"%s\" since the highest value is already 2147483647.", e->name);
1960 return LY_EVALID;
1961 }
1962 e->value = value++;
1963 }
1964 } else { /* LY_TYPE_BITS */
1965 if (enums_p[u].flags & LYS_SET_VALUE) {
1966 e->value = (int32_t)enums_p[u].value;
1967 if (!u || (uint32_t)e->value >= position) {
1968 position = (uint32_t)e->value + 1;
1969 }
1970 /* check collision with other values */
1971 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1972 if (e->value == (*enums)[v].value) {
1973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1974 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1975 (uint32_t)e->value, e->name, (*enums)[v].name);
1976 return LY_EVALID;
1977 }
1978 }
1979 } else if (base_enums) {
1980 /* inherit the assigned value */
1981 e->value = base_enums[match].value;
1982 if (!u || (uint32_t)e->value >= position) {
1983 position = (uint32_t)e->value + 1;
1984 }
1985 } else {
1986 /* assign value automatically */
1987 if (u && position == 0) {
1988 /* counter overflow */
1989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1990 "Invalid bits - it is not possible to auto-assign bit position for "
1991 "\"%s\" since the highest value is already 4294967295.", e->name);
1992 return LY_EVALID;
1993 }
1994 e->value = position++;
1995 }
1996 }
1997
1998 if (base_enums) {
1999 /* the assigned values must not change from the derived type */
2000 if (e->value != base_enums[match].value) {
2001 if (basetype == LY_TYPE_ENUM) {
2002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2003 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2004 e->name, base_enums[match].value, e->value);
2005 } else {
2006 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2007 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2008 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2009 }
2010 return LY_EVALID;
2011 }
2012 }
2013
Radek Krejciec4da802019-05-02 13:02:41 +02002014 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
2015 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002016
2017 if (basetype == LY_TYPE_BITS) {
2018 /* keep bits ordered by position */
2019 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2020 if (v != u) {
2021 memcpy(&storage, e, sizeof *e);
2022 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2023 memcpy(&(*enums)[v], &storage, sizeof storage);
2024 }
2025 }
2026 }
2027
2028done:
2029 return ret;
2030}
2031
Radek Krejcia3045382018-11-22 14:30:31 +01002032#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2033 for ((NODE) = (NODE)->parent; \
2034 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2035 (NODE) = (NODE)->parent); \
2036 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2037 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2038 TERM; \
2039 }
2040
2041/**
2042 * @brief Validate the predicate(s) from the leafref path.
2043 * @param[in] ctx Compile context
2044 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2045 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002046 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002047 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002048 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002049 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2050 */
2051static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002052lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002053 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002054{
2055 LY_ERR ret = LY_EVALID;
2056 const struct lys_module *mod;
2057 const struct lysc_node *src_node, *dst_node;
2058 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2059 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01002060 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01002061 const char *start, *end, *pke_end;
2062 struct ly_set keys = {0};
2063 int i;
2064
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002065 assert(path_context);
2066
Radek Krejcia3045382018-11-22 14:30:31 +01002067 while (**predicate == '[') {
2068 start = (*predicate)++;
2069
2070 while (isspace(**predicate)) {
2071 ++(*predicate);
2072 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002073 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002074 while (isspace(**predicate)) {
2075 ++(*predicate);
2076 }
2077 if (**predicate != '=') {
2078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002079 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2080 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002081 goto cleanup;
2082 }
2083 ++(*predicate);
2084 while (isspace(**predicate)) {
2085 ++(*predicate);
2086 }
2087
2088 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2090 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2091 goto cleanup;
2092 }
2093 --pke_end;
2094 while (isspace(*pke_end)) {
2095 --pke_end;
2096 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002097 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002098 /* localize path-key-expr */
2099 pke_start = path_key_expr = *predicate;
2100 /* move after the current predicate */
2101 *predicate = end + 1;
2102
2103 /* source (must be leaf or leaf-list) */
2104 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002105 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002106 if (!mod) {
2107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2108 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002109 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002110 goto cleanup;
2111 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002112 if (!mod->implemented) {
2113 /* make the module implemented */
2114 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2115 }
Radek Krejcia3045382018-11-22 14:30:31 +01002116 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002117 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002118 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002119 src_node = NULL;
2120 if (context_node->keys) {
2121 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2122 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2123 src_node = (const struct lysc_node*)context_node->keys[u];
2124 break;
2125 }
2126 }
2127 }
Radek Krejcia3045382018-11-22 14:30:31 +01002128 if (!src_node) {
2129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002130 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002131 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002132 goto cleanup;
2133 }
Radek Krejcia3045382018-11-22 14:30:31 +01002134
2135 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002136 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002137 i = ly_set_add(&keys, (void*)src_node, 0);
2138 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002139 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002140 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002141 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002142 *predicate - start, start, src_node->name);
2143 goto cleanup;
2144 }
2145
2146 /* destination */
2147 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002148 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002149
2150 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002151 if (strncmp(path_key_expr, "current", 7)) {
2152error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2154 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2155 *predicate - start, start);
2156 goto cleanup;
2157 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002158 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2159 if (*path_key_expr != '(') {
2160 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002161 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002162 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2163 if (*path_key_expr != ')') {
2164 goto error_current_function_invocation;
2165 }
2166 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002167
2168 if (*path_key_expr != '/') {
2169 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2170 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2171 *predicate - start, start);
2172 goto cleanup;
2173 }
2174 ++path_key_expr;
2175 while (isspace(*path_key_expr)) {
2176 ++path_key_expr;
2177 }
2178
2179 /* rel-path-keyexpr:
2180 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2181 while (!strncmp(path_key_expr, "..", 2)) {
2182 ++dest_parent_times;
2183 path_key_expr += 2;
2184 while (isspace(*path_key_expr)) {
2185 ++path_key_expr;
2186 }
2187 if (*path_key_expr != '/') {
2188 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2189 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2190 *predicate - start, start);
2191 goto cleanup;
2192 }
2193 ++path_key_expr;
2194 while (isspace(*path_key_expr)) {
2195 ++path_key_expr;
2196 }
2197
2198 /* path is supposed to be evaluated in data tree, so we have to skip
2199 * all schema nodes that cannot be instantiated in data tree */
2200 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2201 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2202 *predicate - start, start);
2203 }
2204 if (!dest_parent_times) {
2205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2206 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2207 *predicate - start, start);
2208 goto cleanup;
2209 }
2210 if (path_key_expr == pke_end) {
2211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2212 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2213 *predicate - start, start);
2214 goto cleanup;
2215 }
2216
2217 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002218 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002219 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002220 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002221 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2222 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002223 goto cleanup;
2224 }
2225
2226 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002227 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002228 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002229 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002230 }
2231 if (!mod) {
2232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002233 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002234 *predicate - start, start, dst_len, dst);
2235 goto cleanup;
2236 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002237 if (!mod->implemented) {
2238 /* make the module implemented */
2239 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2240 }
Radek Krejcia3045382018-11-22 14:30:31 +01002241
2242 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2243 if (!dst_node) {
2244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002245 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002246 *predicate - start, start, path_key_expr - pke_start, pke_start);
2247 goto cleanup;
2248 }
2249 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002250 if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcia3045382018-11-22 14:30:31 +01002251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002252 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002253 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2254 goto cleanup;
2255 }
2256 }
2257
2258 ret = LY_SUCCESS;
2259cleanup:
2260 ly_set_erase(&keys, NULL);
2261 return ret;
2262}
2263
2264/**
2265 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2266 *
2267 * path-arg = absolute-path / relative-path
2268 * absolute-path = 1*("/" (node-identifier *path-predicate))
2269 * relative-path = 1*(".." "/") descendant-path
2270 *
2271 * @param[in,out] path Path to parse.
2272 * @param[out] prefix Prefix of the token, NULL if there is not any.
2273 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2274 * @param[out] name Name of the token.
2275 * @param[out] nam_len Length of the name.
2276 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2277 * must not be changed between consecutive calls. -1 if the
2278 * path is absolute.
2279 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2280 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2281 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002282LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002283lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2284 int *parent_times, int *has_predicate)
2285{
2286 int par_times = 0;
2287
2288 assert(path && *path);
2289 assert(parent_times);
2290 assert(prefix);
2291 assert(prefix_len);
2292 assert(name);
2293 assert(name_len);
2294 assert(has_predicate);
2295
2296 *prefix = NULL;
2297 *prefix_len = 0;
2298 *name = NULL;
2299 *name_len = 0;
2300 *has_predicate = 0;
2301
2302 if (!*parent_times) {
2303 if (!strncmp(*path, "..", 2)) {
2304 *path += 2;
2305 ++par_times;
2306 while (!strncmp(*path, "/..", 3)) {
2307 *path += 3;
2308 ++par_times;
2309 }
2310 }
2311 if (par_times) {
2312 *parent_times = par_times;
2313 } else {
2314 *parent_times = -1;
2315 }
2316 }
2317
2318 if (**path != '/') {
2319 return LY_EINVAL;
2320 }
2321 /* skip '/' */
2322 ++(*path);
2323
2324 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002325 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002326
2327 if ((**path == '/' && (*path)[1]) || !**path) {
2328 /* path continues by another token or this is the last token */
2329 return LY_SUCCESS;
2330 } else if ((*path)[0] != '[') {
2331 /* unexpected character */
2332 return LY_EINVAL;
2333 } else {
2334 /* predicate starting with [ */
2335 *has_predicate = 1;
2336 return LY_SUCCESS;
2337 }
2338}
2339
2340/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002341 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2342 *
2343 * The set of features used for target must be a subset of features used for the leafref.
2344 * This is not a perfect, we should compare the truth tables but it could require too much resources
2345 * and RFC 7950 does not require it explicitely, so we simplify that.
2346 *
2347 * @param[in] refnode The leafref node.
2348 * @param[in] target Tha target node of the leafref.
2349 * @return LY_SUCCESS or LY_EVALID;
2350 */
2351static LY_ERR
2352lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2353{
2354 LY_ERR ret = LY_EVALID;
2355 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002356 unsigned int u, v, count;
2357 struct ly_set features = {0};
2358
2359 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002360 if (iter->iffeatures) {
2361 LY_ARRAY_FOR(iter->iffeatures, u) {
2362 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2363 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002364 }
2365 }
2366 }
2367 }
2368
2369 /* we should have, in features set, a superset of features applicable to the target node.
2370 * So when adding features applicable to the target into the features set, we should not be
2371 * able to actually add any new feature, otherwise it is not a subset of features applicable
2372 * to the leafref itself. */
2373 count = features.count;
2374 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002375 if (iter->iffeatures) {
2376 LY_ARRAY_FOR(iter->iffeatures, u) {
2377 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2378 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002379 /* new feature was added (or LY_EMEM) */
2380 goto cleanup;
2381 }
2382 }
2383 }
2384 }
2385 }
2386 ret = LY_SUCCESS;
2387
2388cleanup:
2389 ly_set_erase(&features, NULL);
2390 return ret;
2391}
2392
2393/**
Radek Krejcia3045382018-11-22 14:30:31 +01002394 * @brief Validate the leafref path.
2395 * @param[in] ctx Compile context
2396 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002397 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002398 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2399 */
2400static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002401lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002402{
2403 const struct lysc_node *node = NULL, *parent = NULL;
2404 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002405 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002406 const char *id, *prefix, *name;
2407 size_t prefix_len, name_len;
2408 int parent_times = 0, has_predicate;
2409 unsigned int iter, u;
2410 LY_ERR ret = LY_SUCCESS;
2411
2412 assert(ctx);
2413 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002414 assert(leafref);
2415
Radek Krejci1c0c3442019-07-23 16:08:47 +02002416 ctx->path[0] = '\0';
2417 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2418 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002419
Radek Krejcia3045382018-11-22 14:30:31 +01002420 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002421 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002422 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2423 if (!iter) { /* first iteration */
2424 /* precess ".." in relative paths */
2425 if (parent_times > 0) {
2426 /* move from the context node */
2427 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2428 /* path is supposed to be evaluated in data tree, so we have to skip
2429 * all schema nodes that cannot be instantiated in data tree */
2430 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002431 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002432 }
2433 }
2434 }
2435
2436 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002437 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002438 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002439 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002440 }
2441 if (!mod) {
2442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002443 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2444 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002445 return LY_EVALID;
2446 }
Radek Krejci3d929562019-02-21 11:25:55 +01002447 if (!mod->implemented) {
2448 /* make the module implemented */
2449 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2450 }
Radek Krejcia3045382018-11-22 14:30:31 +01002451
2452 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2453 if (!node) {
2454 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002455 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002456 return LY_EVALID;
2457 }
2458 parent = node;
2459
2460 if (has_predicate) {
2461 /* we have predicate, so the current result must be list */
2462 if (node->nodetype != LYS_LIST) {
2463 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2464 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002465 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002466 return LY_EVALID;
2467 }
2468
Radek Krejcibade82a2018-12-05 10:13:48 +01002469 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2470 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002471 }
2472
2473 ++iter;
2474 }
2475 if (ret) {
2476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002477 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002478 return LY_EVALID;
2479 }
2480
2481 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2483 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002484 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002485 return LY_EVALID;
2486 }
2487
2488 /* check status */
2489 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2490 return LY_EVALID;
2491 }
2492
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002493 /* check config */
2494 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2495 if (node->flags & LYS_CONFIG_R) {
2496 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2497 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2498 leafref->path);
2499 return LY_EVALID;
2500 }
2501 }
2502
Radek Krejci412ddfa2018-11-23 11:44:11 +01002503 /* store the target's type and check for circular chain of leafrefs */
2504 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2505 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2506 if (type == (struct lysc_type*)leafref) {
2507 /* circular chain detected */
2508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2509 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2510 return LY_EVALID;
2511 }
2512 }
2513
Radek Krejci58d171e2018-11-23 13:50:55 +01002514 /* check if leafref and its target are under common if-features */
2515 if (lys_compile_leafref_features_validate(startnode, node)) {
2516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2517 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2518 leafref->path);
2519 return LY_EVALID;
2520 }
2521
Radek Krejci327de162019-06-14 12:52:07 +02002522 ctx->path_len = 1;
2523 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002524 return LY_SUCCESS;
2525}
2526
Radek Krejcicdfecd92018-11-26 11:27:32 +01002527static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002528 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002529/**
2530 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2531 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002532 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2533 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2534 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2535 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002536 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002537 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002538 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002539 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2540 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2541 * @param[out] type Newly created type structure with the filled information about the type.
2542 * @return LY_ERR value.
2543 */
Radek Krejci19a96102018-11-15 13:38:09 +01002544static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002545lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002546 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002547 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002548{
2549 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002550 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002551 struct lysc_type_bin *bin;
2552 struct lysc_type_num *num;
2553 struct lysc_type_str *str;
2554 struct lysc_type_bits *bits;
2555 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002556 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002557 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002558 struct lysc_type_union *un, *un_aux;
2559 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002560
2561 switch (basetype) {
2562 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002563 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002564
2565 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002566 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002567 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2568 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002569 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002570 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002571 }
2572 }
2573
2574 if (tpdfname) {
2575 type_p->compiled = *type;
2576 *type = calloc(1, sizeof(struct lysc_type_bin));
2577 }
2578 break;
2579 case LY_TYPE_BITS:
2580 /* RFC 7950 9.7 - bits */
2581 bits = (struct lysc_type_bits*)(*type);
2582 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002583 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002584 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2585 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002586 }
2587
Radek Krejci555cb5b2018-11-16 14:54:33 +01002588 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002589 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002590 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002592 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002594 free(*type);
2595 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002596 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002597 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002598 }
2599
2600 if (tpdfname) {
2601 type_p->compiled = *type;
2602 *type = calloc(1, sizeof(struct lysc_type_bits));
2603 }
2604 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002605 case LY_TYPE_DEC64:
2606 dec = (struct lysc_type_dec*)(*type);
2607
2608 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002609 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002610 if (!type_p->fraction_digits) {
2611 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002613 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002615 free(*type);
2616 *type = NULL;
2617 }
2618 return LY_EVALID;
2619 }
2620 } else if (type_p->fraction_digits) {
2621 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002622 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2624 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002625 tpdfname);
2626 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002627 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2628 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002629 free(*type);
2630 *type = NULL;
2631 }
2632 return LY_EVALID;
2633 }
2634 dec->fraction_digits = type_p->fraction_digits;
2635
2636 /* RFC 7950 9.2.4 - range */
2637 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002638 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2639 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002640 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002641 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002642 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002643 }
2644
2645 if (tpdfname) {
2646 type_p->compiled = *type;
2647 *type = calloc(1, sizeof(struct lysc_type_dec));
2648 }
2649 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002650 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002651 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002652
2653 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002654 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002655 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2656 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002657 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002658 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002659 }
2660 } else if (base && ((struct lysc_type_str*)base)->length) {
2661 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2662 }
2663
2664 /* RFC 7950 9.4.5 - pattern */
2665 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002666 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002667 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002668 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2669 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2670 }
2671
2672 if (tpdfname) {
2673 type_p->compiled = *type;
2674 *type = calloc(1, sizeof(struct lysc_type_str));
2675 }
2676 break;
2677 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002678 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002679
2680 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002681 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002682 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002683 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002684 }
2685
Radek Krejci555cb5b2018-11-16 14:54:33 +01002686 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002687 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002688 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002690 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002692 free(*type);
2693 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002694 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002695 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002696 }
2697
2698 if (tpdfname) {
2699 type_p->compiled = *type;
2700 *type = calloc(1, sizeof(struct lysc_type_enum));
2701 }
2702 break;
2703 case LY_TYPE_INT8:
2704 case LY_TYPE_UINT8:
2705 case LY_TYPE_INT16:
2706 case LY_TYPE_UINT16:
2707 case LY_TYPE_INT32:
2708 case LY_TYPE_UINT32:
2709 case LY_TYPE_INT64:
2710 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002711 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002712
2713 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002714 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002715 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2716 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002717 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002718 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002719 }
2720 }
2721
2722 if (tpdfname) {
2723 type_p->compiled = *type;
2724 *type = calloc(1, sizeof(struct lysc_type_num));
2725 }
2726 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002727 case LY_TYPE_IDENT:
2728 idref = (struct lysc_type_identityref*)(*type);
2729
2730 /* RFC 7950 9.10.2 - base */
2731 if (type_p->bases) {
2732 if (base) {
2733 /* only the directly derived identityrefs can contain base specification */
2734 if (tpdfname) {
2735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002736 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002737 tpdfname);
2738 } else {
2739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002740 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002741 free(*type);
2742 *type = NULL;
2743 }
2744 return LY_EVALID;
2745 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002746 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002747 }
2748
2749 if (!base && !type_p->flags) {
2750 /* type derived from identityref built-in type must contain at least one base */
2751 if (tpdfname) {
2752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2753 } else {
2754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2755 free(*type);
2756 *type = NULL;
2757 }
2758 return LY_EVALID;
2759 }
2760
2761 if (tpdfname) {
2762 type_p->compiled = *type;
2763 *type = calloc(1, sizeof(struct lysc_type_identityref));
2764 }
2765 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002766 case LY_TYPE_LEAFREF:
2767 /* RFC 7950 9.9.3 - require-instance */
2768 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002769 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002770 if (tpdfname) {
2771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2772 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2773 } else {
2774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2775 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2776 free(*type);
2777 *type = NULL;
2778 }
2779 return LY_EVALID;
2780 }
Radek Krejcia3045382018-11-22 14:30:31 +01002781 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002782 } else if (base) {
2783 /* inherit */
2784 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002785 } else {
2786 /* default is true */
2787 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2788 }
2789 if (type_p->path) {
2790 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002791 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002792 } else if (base) {
2793 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002794 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002795 } else if (tpdfname) {
2796 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2797 return LY_EVALID;
2798 } else {
2799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2800 free(*type);
2801 *type = NULL;
2802 return LY_EVALID;
2803 }
2804 if (tpdfname) {
2805 type_p->compiled = *type;
2806 *type = calloc(1, sizeof(struct lysc_type_leafref));
2807 }
2808 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002809 case LY_TYPE_INST:
2810 /* RFC 7950 9.9.3 - require-instance */
2811 if (type_p->flags & LYS_SET_REQINST) {
2812 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2813 } else {
2814 /* default is true */
2815 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2816 }
2817
2818 if (tpdfname) {
2819 type_p->compiled = *type;
2820 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2821 }
2822 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002823 case LY_TYPE_UNION:
2824 un = (struct lysc_type_union*)(*type);
2825
2826 /* RFC 7950 7.4 - type */
2827 if (type_p->types) {
2828 if (base) {
2829 /* only the directly derived union can contain types specification */
2830 if (tpdfname) {
2831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2832 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2833 tpdfname);
2834 } else {
2835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2836 "Invalid type substatement for the type not directly derived from union built-in type.");
2837 free(*type);
2838 *type = NULL;
2839 }
2840 return LY_EVALID;
2841 }
2842 /* compile the type */
2843 additional = 0;
2844 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2845 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002846 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002847 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2848 /* add space for additional types from the union subtype */
2849 un_aux = (struct lysc_type_union *)un->types[u + additional];
2850 p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types)));
2851 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2852 un->types = (void*)((uint32_t*)(p) + 1);
2853
2854 /* copy subtypes of the subtype union */
2855 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2856 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2857 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2858 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2859 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2860 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2861 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2862 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2863 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2864 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2865 /* TODO extensions */
2866
2867 } else {
2868 un->types[u + additional] = un_aux->types[v];
2869 ++un_aux->types[v]->refcount;
2870 }
2871 ++additional;
2872 LY_ARRAY_INCREMENT(un->types);
2873 }
2874 /* compensate u increment in main loop */
2875 --additional;
2876
2877 /* free the replaced union subtype */
2878 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2879 } else {
2880 LY_ARRAY_INCREMENT(un->types);
2881 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002882 }
2883 }
2884
2885 if (!base && !type_p->flags) {
2886 /* type derived from union built-in type must contain at least one type */
2887 if (tpdfname) {
2888 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2889 } else {
2890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2891 free(*type);
2892 *type = NULL;
2893 }
2894 return LY_EVALID;
2895 }
2896
2897 if (tpdfname) {
2898 type_p->compiled = *type;
2899 *type = calloc(1, sizeof(struct lysc_type_union));
2900 }
2901 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002902 case LY_TYPE_BOOL:
2903 case LY_TYPE_EMPTY:
2904 case LY_TYPE_UNKNOWN: /* just to complete switch */
2905 break;
2906 }
2907 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2908done:
2909 return ret;
2910}
2911
Radek Krejcia3045382018-11-22 14:30:31 +01002912/**
2913 * @brief Compile information about the leaf/leaf-list's type.
2914 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002915 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2916 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2917 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2918 * @param[in] context_name Name of the context node or referencing typedef for logging.
2919 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002920 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002921 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002922 * @return LY_ERR value.
2923 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002924static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002925lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002926 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002927{
2928 LY_ERR ret = LY_SUCCESS;
2929 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002930 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002931 struct type_context {
2932 const struct lysp_tpdf *tpdf;
2933 struct lysp_node *node;
2934 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002935 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002936 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002937 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002938 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002939 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002940 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002941
2942 (*type) = NULL;
2943
2944 tctx = calloc(1, sizeof *tctx);
2945 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002946 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002947 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2948 ret == LY_SUCCESS;
2949 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2950 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2951 if (basetype) {
2952 break;
2953 }
2954
2955 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002956 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2957 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002958 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2959
Radek Krejcicdfecd92018-11-26 11:27:32 +01002960 if (units && !*units) {
2961 /* inherit units */
2962 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2963 }
Radek Krejci01342af2019-01-03 15:18:08 +01002964 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002965 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002966 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002967 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002968 }
Radek Krejci01342af2019-01-03 15:18:08 +01002969 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002970 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2971 break;
2972 }
2973
Radek Krejci19a96102018-11-15 13:38:09 +01002974 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002975 /* it is not necessary to continue, the rest of the chain was already compiled,
2976 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002977 basetype = tctx->tpdf->type.compiled->basetype;
2978 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002979 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002980 dummyloops = 1;
2981 goto preparenext;
2982 } else {
2983 tctx = NULL;
2984 break;
2985 }
Radek Krejci19a96102018-11-15 13:38:09 +01002986 }
2987
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002988 /* circular typedef reference detection */
2989 for (u = 0; u < tpdf_chain.count; u++) {
2990 /* local part */
2991 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2992 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2994 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2995 free(tctx);
2996 ret = LY_EVALID;
2997 goto cleanup;
2998 }
2999 }
3000 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3001 /* global part for unions corner case */
3002 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3003 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3004 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3005 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3006 free(tctx);
3007 ret = LY_EVALID;
3008 goto cleanup;
3009 }
3010 }
3011
Radek Krejci19a96102018-11-15 13:38:09 +01003012 /* store information for the following processing */
3013 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3014
Radek Krejcicdfecd92018-11-26 11:27:32 +01003015preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003016 /* prepare next loop */
3017 tctx_prev = tctx;
3018 tctx = calloc(1, sizeof *tctx);
3019 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3020 }
3021 free(tctx);
3022
3023 /* allocate type according to the basetype */
3024 switch (basetype) {
3025 case LY_TYPE_BINARY:
3026 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003027 break;
3028 case LY_TYPE_BITS:
3029 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003030 break;
3031 case LY_TYPE_BOOL:
3032 case LY_TYPE_EMPTY:
3033 *type = calloc(1, sizeof(struct lysc_type));
3034 break;
3035 case LY_TYPE_DEC64:
3036 *type = calloc(1, sizeof(struct lysc_type_dec));
3037 break;
3038 case LY_TYPE_ENUM:
3039 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003040 break;
3041 case LY_TYPE_IDENT:
3042 *type = calloc(1, sizeof(struct lysc_type_identityref));
3043 break;
3044 case LY_TYPE_INST:
3045 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3046 break;
3047 case LY_TYPE_LEAFREF:
3048 *type = calloc(1, sizeof(struct lysc_type_leafref));
3049 break;
3050 case LY_TYPE_STRING:
3051 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003052 break;
3053 case LY_TYPE_UNION:
3054 *type = calloc(1, sizeof(struct lysc_type_union));
3055 break;
3056 case LY_TYPE_INT8:
3057 case LY_TYPE_UINT8:
3058 case LY_TYPE_INT16:
3059 case LY_TYPE_UINT16:
3060 case LY_TYPE_INT32:
3061 case LY_TYPE_UINT32:
3062 case LY_TYPE_INT64:
3063 case LY_TYPE_UINT64:
3064 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003065 break;
3066 case LY_TYPE_UNKNOWN:
3067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3068 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3069 ret = LY_EVALID;
3070 goto cleanup;
3071 }
3072 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003073 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3075 ly_data_type2str[basetype]);
3076 free(*type);
3077 (*type) = NULL;
3078 ret = LY_EVALID;
3079 goto cleanup;
3080 }
3081
3082 /* get restrictions from the referred typedefs */
3083 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3084 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003085
3086 /* remember the typedef context for circular check */
3087 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3088
Radek Krejci43699232018-11-23 14:59:46 +01003089 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003090 base = tctx->tpdf->type.compiled;
3091 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003092 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003093 /* no change, just use the type information from the base */
3094 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3095 ++base->refcount;
3096 continue;
3097 }
3098
3099 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003100 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3102 tctx->tpdf->name, ly_data_type2str[basetype]);
3103 ret = LY_EVALID;
3104 goto cleanup;
3105 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3106 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3107 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3108 tctx->tpdf->name, tctx->tpdf->dflt);
3109 ret = LY_EVALID;
3110 goto cleanup;
3111 }
3112
Radek Krejci19a96102018-11-15 13:38:09 +01003113 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003114 /* TODO user type plugins */
3115 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003116 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003117 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003118 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003119 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003120 LY_CHECK_GOTO(ret, cleanup);
3121 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003122 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003123 /* remove the processed typedef contexts from the stack for circular check */
3124 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003125
Radek Krejcic5c27e52018-11-15 14:38:11 +01003126 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003127 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003128 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003129 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003130 /* TODO user type plugins */
3131 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003132 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003133 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003134 LY_CHECK_GOTO(ret, cleanup);
3135 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003136 /* no specific restriction in leaf's type definition, copy from the base */
3137 free(*type);
3138 (*type) = base;
3139 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003140 }
Radek Krejcia1911222019-07-22 17:24:50 +02003141 if (dflt && !(*type)->dflt) {
3142 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003143 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003144 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3145 (*type)->dflt->realtype = (*type);
3146 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3147 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003148 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003149 if (err) {
3150 ly_err_print(err);
3151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3152 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3153 ly_err_free(err);
3154 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003155 if (ret == LY_EINCOMPLETE) {
3156 /* postpone default compilation when the tree is complete */
3157 struct lysc_incomplete_dflt *r;
3158 r = malloc(sizeof *r);
3159 r->context_node = NULL;
3160 r->dflt = (*type)->dflt;
3161 r->dflt_mod = (*type)->dflt_mod;
3162 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
3163
3164 /* but in general result is so far ok */
3165 ret = LY_SUCCESS;
3166 }
Radek Krejcia1911222019-07-22 17:24:50 +02003167 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003168 }
Radek Krejci19a96102018-11-15 13:38:09 +01003169
Radek Krejciec4da802019-05-02 13:02:41 +02003170 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003171
3172cleanup:
3173 ly_set_erase(&tpdf_chain, free);
3174 return ret;
3175}
3176
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003177/**
3178 * @brief Compile status information of the given node.
3179 *
3180 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3181 * has the status correctly set during the compilation.
3182 *
3183 * @param[in] ctx Compile context
3184 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3185 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3186 * the compatibility with the parent's status value.
3187 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3188 * @return LY_ERR value.
3189 */
3190static LY_ERR
3191lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3192{
3193 /* status - it is not inherited by specification, but it does not make sense to have
3194 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3195 if (!((*node_flags) & LYS_STATUS_MASK)) {
3196 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3197 if ((parent_flags & 0x3) != 0x3) {
3198 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3199 * combination of bits (0x3) which marks the uses_status value */
3200 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3201 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3202 }
3203 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3204 } else {
3205 (*node_flags) |= LYS_STATUS_CURR;
3206 }
3207 } else if (parent_flags & LYS_STATUS_MASK) {
3208 /* check status compatibility with the parent */
3209 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3210 if ((*node_flags) & LYS_STATUS_CURR) {
3211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3212 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3213 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3214 } else { /* LYS_STATUS_DEPRC */
3215 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3216 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3217 }
3218 return LY_EVALID;
3219 }
3220 }
3221 return LY_SUCCESS;
3222}
3223
Radek Krejci8cce8532019-03-05 11:27:45 +01003224/**
3225 * @brief Check uniqness of the node/action/notification name.
3226 *
3227 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3228 * structures, but they share the namespace so we need to check their name collisions.
3229 *
3230 * @param[in] ctx Compile context.
3231 * @param[in] children List (linked list) of data nodes to go through.
3232 * @param[in] actions List (sized array) of actions or RPCs to go through.
3233 * @param[in] notifs List (sized array) of Notifications to go through.
3234 * @param[in] name Name of the item to find in the given lists.
3235 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3236 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3237 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3238 */
3239static LY_ERR
3240lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3241 const struct lysc_action *actions, const struct lysc_notif *notifs,
3242 const char *name, void *exclude)
3243{
3244 const struct lysc_node *iter;
3245 unsigned int u;
3246
3247 LY_LIST_FOR(children, iter) {
3248 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3249 goto error;
3250 }
3251 }
3252 LY_ARRAY_FOR(actions, u) {
3253 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3254 goto error;
3255 }
3256 }
3257 LY_ARRAY_FOR(notifs, u) {
3258 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3259 goto error;
3260 }
3261 }
3262 return LY_SUCCESS;
3263error:
3264 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3265 return LY_EEXIST;
3266}
3267
Radek Krejciec4da802019-05-02 13:02:41 +02003268static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01003269
Radek Krejcia3045382018-11-22 14:30:31 +01003270/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003271 * @brief Compile parsed RPC/action schema node information.
3272 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003273 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003274 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003275 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3276 * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags).
3277 * Zero means no uses, non-zero value with no status bit set mean the default status.
3278 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3279 */
3280static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003281lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003282 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3283{
3284 LY_ERR ret = LY_SUCCESS;
3285 struct lysp_node *child_p;
3286 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003287 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003288
Radek Krejci327de162019-06-14 12:52:07 +02003289 lysc_update_path(ctx, parent, action_p->name);
3290
Radek Krejci8cce8532019-03-05 11:27:45 +01003291 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3292 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3293 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3294 action_p->name, action)) {
3295 return LY_EVALID;
3296 }
3297
Radek Krejciec4da802019-05-02 13:02:41 +02003298 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003299 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003300 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003301 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003302 return LY_EVALID;
3303 }
3304
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003305 action->nodetype = LYS_ACTION;
3306 action->module = ctx->mod;
3307 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003308 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003309 action->sp = action_p;
3310 }
3311 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3312
3313 /* status - it is not inherited by specification, but it does not make sense to have
3314 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3315 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3316
3317 DUP_STRING(ctx->ctx, action_p->name, action->name);
3318 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3319 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003320 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3321 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003322
3323 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003324 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003325 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3326 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3327 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003328 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003329 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003330 }
Radek Krejci327de162019-06-14 12:52:07 +02003331 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003332 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003333
3334 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003335 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003336 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3337 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3338 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003339 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003340 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003341 }
Radek Krejci327de162019-06-14 12:52:07 +02003342 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003343
Radek Krejci327de162019-06-14 12:52:07 +02003344 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003345cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003346 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003347 return ret;
3348}
3349
3350/**
Radek Krejci43981a32019-04-12 09:44:11 +02003351 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003352 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003353 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003354 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3355 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3356 * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags).
Radek Krejcifc11bd72019-04-11 16:00:05 +02003357 * Zero means no uses, non-zero value with no status bit set mean the default status.
3358 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3359 */
3360static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003361lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003362 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3363{
3364 LY_ERR ret = LY_SUCCESS;
3365 struct lysp_node *child_p;
3366 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003367 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003368
Radek Krejci327de162019-06-14 12:52:07 +02003369 lysc_update_path(ctx, parent, notif_p->name);
3370
Radek Krejcifc11bd72019-04-11 16:00:05 +02003371 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3372 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3373 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3374 notif_p->name, notif)) {
3375 return LY_EVALID;
3376 }
3377
Radek Krejciec4da802019-05-02 13:02:41 +02003378 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3380 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003381 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003382 return LY_EVALID;
3383 }
3384
3385 notif->nodetype = LYS_NOTIF;
3386 notif->module = ctx->mod;
3387 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003388 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003389 notif->sp = notif_p;
3390 }
3391 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3392
3393 /* status - it is not inherited by specification, but it does not make sense to have
3394 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3395 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3396
3397 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3398 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3399 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003400 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3401 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3402 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003403
Radek Krejciec4da802019-05-02 13:02:41 +02003404 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003405 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003406 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003407 }
3408
Radek Krejci327de162019-06-14 12:52:07 +02003409 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003410cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003411 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003412 return ret;
3413}
3414
3415/**
Radek Krejcia3045382018-11-22 14:30:31 +01003416 * @brief Compile parsed container node information.
3417 * @param[in] ctx Compile context
3418 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003419 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3420 * is enriched with the container-specific information.
3421 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3422 */
Radek Krejci19a96102018-11-15 13:38:09 +01003423static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003424lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003425{
3426 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3427 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3428 struct lysp_node *child_p;
3429 unsigned int u;
3430 LY_ERR ret = LY_SUCCESS;
3431
Radek Krejcife909632019-02-12 15:34:42 +01003432 if (cont_p->presence) {
3433 cont->flags |= LYS_PRESENCE;
3434 }
3435
Radek Krejci19a96102018-11-15 13:38:09 +01003436 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003437 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003438 }
3439
Radek Krejciec4da802019-05-02 13:02:41 +02003440 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3441 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3442 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003443
3444done:
3445 return ret;
3446}
3447
Radek Krejci33f72892019-02-21 10:36:58 +01003448/*
3449 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3450 * @param[in] ctx Compile context.
3451 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3452 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003453 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3454 * @return LY_ERR value.
3455 */
3456static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003457lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003458{
Radek Krejcia1911222019-07-22 17:24:50 +02003459 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003460 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3461
Radek Krejciec4da802019-05-02 13:02:41 +02003462 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type,
Radek Krejci33f72892019-02-21 10:36:58 +01003463 leaf->units ? NULL : &leaf->units));
3464 if (leaf->nodetype == LYS_LEAFLIST) {
3465 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003466 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3467 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003468 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003469 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3470 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3471 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3472 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003473 LY_ARRAY_INCREMENT(llist->dflts);
3474 }
3475 } else {
3476 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003477 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003478 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3479 leaf->dflt->realtype = leaf->type->dflt->realtype;
3480 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3481 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003482 }
3483 }
3484 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3485 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3486 ly_set_add(&ctx->unres, leaf, 0);
3487 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3488 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3489 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3490 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3491 ly_set_add(&ctx->unres, leaf, 0);
3492 }
3493 }
3494 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003495 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3496 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3497 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3498 return LY_EVALID;
3499 }
3500 }
3501
Radek Krejci33f72892019-02-21 10:36:58 +01003502 return LY_SUCCESS;
3503}
3504
Radek Krejcia3045382018-11-22 14:30:31 +01003505/**
3506 * @brief Compile parsed leaf node information.
3507 * @param[in] ctx Compile context
3508 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003509 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3510 * is enriched with the leaf-specific information.
3511 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3512 */
Radek Krejci19a96102018-11-15 13:38:09 +01003513static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003514lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003515{
3516 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3517 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3518 unsigned int u;
3519 LY_ERR ret = LY_SUCCESS;
3520
Radek Krejciec4da802019-05-02 13:02:41 +02003521 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003522 if (leaf_p->units) {
3523 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3524 leaf->flags |= LYS_SET_UNITS;
3525 }
Radek Krejcia1911222019-07-22 17:24:50 +02003526
3527 /* the dflt member is just filled to avoid getting the default value from the type */
3528 leaf->dflt = (void*)leaf_p->dflt;
3529 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3530
Radek Krejciccd20f12019-02-15 14:12:27 +01003531 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003532 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003533 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003534 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3535 leaf->dflt->realtype = leaf->type;
3536 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3537 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003538 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003539 leaf->dflt->realtype->refcount++;
3540 if (err) {
3541 ly_err_print(err);
3542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3543 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3544 ly_err_free(err);
3545 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003546 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003547 /* postpone default compilation when the tree is complete */
3548 struct lysc_incomplete_dflt *r;
3549 r = malloc(sizeof *r);
3550 r->context_node = node;
3551 r->dflt = leaf->dflt;
3552 r->dflt_mod = leaf->dflt_mod;
3553 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
3554
3555 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003556 ret = LY_SUCCESS;
3557 }
Radek Krejcia1911222019-07-22 17:24:50 +02003558 LY_CHECK_GOTO(ret, done);
Radek Krejci76b3e962018-12-14 17:01:25 +01003559 leaf->flags |= LYS_SET_DFLT;
3560 }
Radek Krejci43699232018-11-23 14:59:46 +01003561
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003562
Radek Krejci19a96102018-11-15 13:38:09 +01003563done:
3564 return ret;
3565}
3566
Radek Krejcia3045382018-11-22 14:30:31 +01003567/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003568 * @brief Compile parsed leaf-list node information.
3569 * @param[in] ctx Compile context
3570 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003571 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3572 * is enriched with the leaf-list-specific information.
3573 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3574 */
3575static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003576lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003577{
3578 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3579 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003580 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003581 LY_ERR ret = LY_SUCCESS;
3582
Radek Krejciec4da802019-05-02 13:02:41 +02003583 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003584 if (llist_p->units) {
3585 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3586 llist->flags |= LYS_SET_UNITS;
3587 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003588
Radek Krejcia1911222019-07-22 17:24:50 +02003589 /* the dflts member is just filled to avoid getting the default value from the type */
3590 llist->dflts = (void*)llist_p->dflts;
3591 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003592 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003593 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003594 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003595 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3596 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003597 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003598 LY_ARRAY_INCREMENT(llist->dflts_mods);
3599 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003600 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003601 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3602 llist->dflts[u]->realtype = llist->type;
3603 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3604 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003605 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003606 llist->dflts[u]->realtype->refcount++;
3607 if (err) {
3608 ly_err_print(err);
3609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3610 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3611 ly_err_free(err);
3612 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003613 if (ret == LY_EINCOMPLETE) {
3614 /* postpone default compilation when the tree is complete */
3615 struct lysc_incomplete_dflt *r;
3616 r = malloc(sizeof *r);
3617 r->context_node = node;
3618 r->dflt = llist->dflts[u];
3619 r->dflt_mod = llist->dflts_mods[u];
3620 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
3621
3622 /* but in general result is so far ok */
3623 ret = LY_SUCCESS;
3624 }
Radek Krejcia1911222019-07-22 17:24:50 +02003625 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003626 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003627 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003628 }
Radek Krejcia1911222019-07-22 17:24:50 +02003629 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3630 /* configuration data values must be unique - so check the default values */
3631 LY_ARRAY_FOR(llist->dflts, u) {
3632 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3633 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3634 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003635 const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02003636 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3637 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3638 if (dynamic) {
3639 free((char*)val);
3640 }
3641 return LY_EVALID;
3642 }
3643 }
3644 }
3645 }
3646
3647 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003648
3649 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003650 if (llist->min) {
3651 llist->flags |= LYS_MAND_TRUE;
3652 }
Radek Krejcib7408632018-11-28 17:12:11 +01003653 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003654
Radek Krejci0e5d8382018-11-28 16:37:53 +01003655done:
3656 return ret;
3657}
3658
3659/**
Radek Krejci7af64242019-02-18 13:07:53 +01003660 * @brief Compile information about list's uniques.
3661 * @param[in] ctx Compile context.
3662 * @param[in] context_module Module where the prefixes are going to be resolved.
3663 * @param[in] uniques Sized array list of unique statements.
3664 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3665 * @return LY_ERR value.
3666 */
3667static LY_ERR
3668lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3669{
3670 LY_ERR ret = LY_SUCCESS;
3671 struct lysc_node_leaf **key, ***unique;
3672 const char *keystr, *delim;
3673 size_t len;
3674 unsigned int v;
3675 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003676 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003677
3678 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3679 config = -1;
3680 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3681 keystr = uniques[v];
3682 while (keystr) {
3683 delim = strpbrk(keystr, " \t\n");
3684 if (delim) {
3685 len = delim - keystr;
3686 while (isspace(*delim)) {
3687 ++delim;
3688 }
3689 } else {
3690 len = strlen(keystr);
3691 }
3692
3693 /* unique node must be present */
3694 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003695 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3696 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003697 if (ret != LY_SUCCESS) {
3698 if (ret == LY_EDENIED) {
3699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003700 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003701 len, keystr, lys_nodetype2str((*key)->nodetype));
3702 }
3703 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003704 } else if (flags) {
3705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3706 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3707 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3708 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003709 }
3710
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003711
Radek Krejci7af64242019-02-18 13:07:53 +01003712 /* all referenced leafs must be of the same config type */
3713 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3714 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3715 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3716 return LY_EVALID;
3717 } else if ((*key)->flags & LYS_CONFIG_W) {
3718 config = 1;
3719 } else { /* LYS_CONFIG_R */
3720 config = 0;
3721 }
3722
3723 /* check status */
3724 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3725 (*key)->flags, (*key)->module, (*key)->name));
3726
3727 /* mark leaf as unique */
3728 (*key)->flags |= LYS_UNIQUE;
3729
3730 /* next unique value in line */
3731 keystr = delim;
3732 }
3733 /* next unique definition */
3734 }
3735
3736 return LY_SUCCESS;
3737}
3738
3739/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003740 * @brief Compile parsed list node information.
3741 * @param[in] ctx Compile context
3742 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003743 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3744 * is enriched with the list-specific information.
3745 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3746 */
3747static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003748lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003749{
3750 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3751 struct lysc_node_list *list = (struct lysc_node_list*)node;
3752 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003753 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003754 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003755 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003756 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757 LY_ERR ret = LY_SUCCESS;
3758
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003759 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003760 if (list->min) {
3761 list->flags |= LYS_MAND_TRUE;
3762 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003763 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3764
3765 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003766 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003767 }
3768
Radek Krejciec4da802019-05-02 13:02:41 +02003769 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003770
3771 /* keys */
3772 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3774 return LY_EVALID;
3775 }
3776
3777 /* find all the keys (must be direct children) */
3778 keystr = list_p->key;
3779 while (keystr) {
3780 delim = strpbrk(keystr, " \t\n");
3781 if (delim) {
3782 len = delim - keystr;
3783 while (isspace(*delim)) {
3784 ++delim;
3785 }
3786 } else {
3787 len = strlen(keystr);
3788 }
3789
3790 /* key node must be present */
3791 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3792 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3793 if (!(*key)) {
3794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3795 "The list's key \"%.*s\" not found.", len, keystr);
3796 return LY_EVALID;
3797 }
3798 /* keys must be unique */
3799 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3800 if (*key == list->keys[u]) {
3801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3802 "Duplicated key identifier \"%.*s\".", len, keystr);
3803 return LY_EVALID;
3804 }
3805 }
Radek Krejci327de162019-06-14 12:52:07 +02003806 lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003807 /* key must have the same config flag as the list itself */
3808 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3810 return LY_EVALID;
3811 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003812 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003813 /* YANG 1.0 denies key to be of empty type */
3814 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003816 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003817 return LY_EVALID;
3818 }
3819 } else {
3820 /* when and if-feature are illegal on list keys */
3821 if ((*key)->when) {
3822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003823 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003824 return LY_EVALID;
3825 }
3826 if ((*key)->iffeatures) {
3827 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003828 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003829 return LY_EVALID;
3830 }
3831 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003832
3833 /* check status */
3834 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3835 (*key)->flags, (*key)->module, (*key)->name));
3836
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003837 /* ignore default values of the key */
3838 if ((*key)->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003839 (*key)->dflt->realtype->plugin->free(ctx->ctx, (*key)->dflt);
3840 lysc_type_free(ctx->ctx, (*key)->dflt->realtype);
3841 free((*key)->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003842 (*key)->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003843 (*key)->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003844 }
3845 /* mark leaf as key */
3846 (*key)->flags |= LYS_KEY;
3847
3848 /* next key value */
3849 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003850 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003851 }
3852
3853 /* uniques */
3854 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003855 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003856 }
3857
Radek Krejciec4da802019-05-02 13:02:41 +02003858 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3859 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003860
3861done:
3862 return ret;
3863}
3864
Radek Krejcib56c7502019-02-13 14:19:54 +01003865/**
3866 * @brief Do some checks and set the default choice's case.
3867 *
3868 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3869 *
3870 * @param[in] ctx Compile context.
3871 * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself,
3872 * not the reference to the imported module.
3873 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3874 * @return LY_ERR value.
3875 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003876static LY_ERR
3877lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3878{
3879 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3880 const char *prefix = NULL, *name;
3881 size_t prefix_len = 0;
3882
3883 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3884 name = strchr(dflt, ':');
3885 if (name) {
3886 prefix = dflt;
3887 prefix_len = name - prefix;
3888 ++name;
3889 } else {
3890 name = dflt;
3891 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003892 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003893 /* prefixed default case make sense only for the prefix of the schema itself */
3894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3895 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3896 prefix_len, prefix);
3897 return LY_EVALID;
3898 }
3899 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3900 if (!ch->dflt) {
3901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3902 "Default case \"%s\" not found.", dflt);
3903 return LY_EVALID;
3904 }
3905 /* no mandatory nodes directly under the default case */
3906 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003907 if (iter->parent != (struct lysc_node*)ch->dflt) {
3908 break;
3909 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003910 if (iter->flags & LYS_MAND_TRUE) {
3911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3912 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3913 return LY_EVALID;
3914 }
3915 }
Radek Krejci01342af2019-01-03 15:18:08 +01003916 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003917 return LY_SUCCESS;
3918}
3919
Radek Krejciccd20f12019-02-15 14:12:27 +01003920static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003921lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003922{
3923 struct lys_module *mod;
3924 const char *prefix = NULL, *name;
3925 size_t prefix_len = 0;
3926 struct lysc_node_case *cs;
3927 struct lysc_node *node;
3928
3929 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3930 name = strchr(dflt, ':');
3931 if (name) {
3932 prefix = dflt;
3933 prefix_len = name - prefix;
3934 ++name;
3935 } else {
3936 name = dflt;
3937 }
3938 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3939 if (prefix) {
3940 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003942 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3943 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003944 return LY_EVALID;
3945 }
3946 } else {
3947 mod = ctx->mod;
3948 }
3949 /* get the default case */
3950 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3951 if (!cs) {
3952 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003953 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003954 return LY_EVALID;
3955 }
3956
3957 /* check that there is no mandatory node */
3958 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003959 if (node->parent != (struct lysc_node*)cs) {
3960 break;
3961 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003962 if (node->flags & LYS_MAND_TRUE) {
3963 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003964 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3965 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003966 return LY_EVALID;
3967 }
3968 }
3969
3970 /* set the default case in choice */
3971 ch->dflt = cs;
3972 cs->flags |= LYS_SET_DFLT;
3973
3974 return LY_SUCCESS;
3975}
3976
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003977/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003978 * @brief Compile parsed choice node information.
3979 * @param[in] ctx Compile context
3980 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003981 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003982 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003983 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3984 */
3985static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003986lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003987{
3988 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3989 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3990 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003991 LY_ERR ret = LY_SUCCESS;
3992
Radek Krejci056d0a82018-12-06 16:57:25 +01003993 LY_LIST_FOR(ch_p->child, child_p) {
3994 if (child_p->nodetype == LYS_CASE) {
3995 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003996 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003997 }
3998 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003999 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004000 }
4001 }
4002
4003 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004004 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004005 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004006 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004007
Radek Krejci9800fb82018-12-13 14:26:23 +01004008 return ret;
4009}
4010
4011/**
4012 * @brief Compile parsed anydata or anyxml node information.
4013 * @param[in] ctx Compile context
4014 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004015 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4016 * is enriched with the any-specific information.
4017 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4018 */
4019static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004020lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004021{
4022 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4023 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4024 unsigned int u;
4025 LY_ERR ret = LY_SUCCESS;
4026
Radek Krejciec4da802019-05-02 13:02:41 +02004027 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004028
4029 if (any->flags & LYS_CONFIG_W) {
4030 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
4031 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
4032 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004033done:
4034 return ret;
4035}
4036
Radek Krejcib56c7502019-02-13 14:19:54 +01004037/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004038 * @brief Connect the node into the siblings list and check its name uniqueness.
4039 *
4040 * @param[in] ctx Compile context
4041 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4042 * the choice itself is expected instead of a specific case node.
4043 * @param[in] node Schema node to connect into the list.
4044 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4045 */
4046static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004047lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004048{
4049 struct lysc_node **children;
4050
4051 if (node->nodetype == LYS_CASE) {
4052 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4053 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004054 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004055 }
4056 if (children) {
4057 if (!(*children)) {
4058 /* first child */
4059 *children = node;
4060 } else if (*children != node) {
4061 /* by the condition in previous branch we cover the choice/case children
4062 * - the children list is shared by the choice and the the first case, in addition
4063 * the first child of each case must be referenced from the case node. So the node is
4064 * actually always already inserted in case it is the first children - so here such
4065 * a situation actually corresponds to the first branch */
4066 /* insert at the end of the parent's children list */
4067 (*children)->prev->next = node;
4068 node->prev = (*children)->prev;
4069 (*children)->prev = node;
4070
4071 /* check the name uniqueness */
4072 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4073 lysc_node_notifs(parent), node->name, node)) {
4074 return LY_EEXIST;
4075 }
4076 }
4077 }
4078 return LY_SUCCESS;
4079}
4080
Radek Krejci95710c92019-02-11 15:49:55 +01004081/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004082 * @brief Get the XPath context node for the given schema node.
4083 * @param[in] start The schema node where the XPath expression appears.
4084 * @return The context node to evaluate XPath expression in given schema node.
4085 * @return NULL in case the context node is the root node.
4086 */
4087static struct lysc_node *
4088lysc_xpath_context(struct lysc_node *start)
4089{
4090 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4091 start = start->parent);
4092 return start;
4093}
4094
4095/**
4096 * @brief Prepare the case structure in choice node for the new data node.
4097 *
4098 * It is able to handle implicit as well as explicit cases and the situation when the case has multiple data nodes and the case was already
4099 * created in the choice when the first child was processed.
4100 *
4101 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004102 * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case,
4103 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004104 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4105 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4106 * @return The case structure where the child node belongs to, NULL in case of error. Note that the child is not connected into the siblings list,
4107 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004108 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004109static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004110lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child)
Radek Krejci056d0a82018-12-06 16:57:25 +01004111{
4112 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004113 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004114 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004115 unsigned int u;
4116 LY_ERR ret;
4117
Radek Krejci95710c92019-02-11 15:49:55 +01004118#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004119 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004120 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004121 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4122 return NULL; \
4123 } \
4124 }
4125
Radek Krejci95710c92019-02-11 15:49:55 +01004126 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4127 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004128
4129 /* we have to add an implicit case node into the parent choice */
4130 cs = calloc(1, sizeof(struct lysc_node_case));
4131 DUP_STRING(ctx->ctx, child->name, cs->name);
4132 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004133 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004134 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4135 /* the case is already present since the child is not its first children */
4136 return (struct lysc_node_case*)ch->cases->prev;
4137 }
Radek Krejci95710c92019-02-11 15:49:55 +01004138 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004139
4140 /* explicit parent case is not present (this is its first child) */
4141 cs = calloc(1, sizeof(struct lysc_node_case));
4142 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4143 cs->flags = LYS_STATUS_MASK & node_p->flags;
4144 cs->sp = node_p;
4145
Radek Krejcib1b59152019-01-07 13:21:56 +01004146 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejcibae745f2019-04-09 16:28:00 +02004147 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004148
4149 if (node_p->when) {
4150 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004151 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004152 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004153 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004154 }
Radek Krejciec4da802019-05-02 13:02:41 +02004155 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004156 } else {
4157 LOGINT(ctx->ctx);
4158 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004159 }
4160 cs->module = ctx->mod;
4161 cs->prev = (struct lysc_node*)cs;
4162 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004163 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004164 cs->parent = (struct lysc_node*)ch;
4165 cs->child = child;
4166
4167 return cs;
4168error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004169 if (cs) {
4170 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4171 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004172 return NULL;
4173
4174#undef UNIQUE_CHECK
4175}
4176
Radek Krejcib56c7502019-02-13 14:19:54 +01004177/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004178 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004179 *
4180 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004181 * @param[in] node Target node where the config is supposed to be changed.
4182 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004183 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4184 * done only on the node for which the refine was created. The function applies also recursively to apply the config change
Radek Krejci93dcc392019-02-19 10:43:38 +01004185 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4186 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01004187 * @return LY_ERR value.
4188 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004189static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004190lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004191 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004192{
4193 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004194 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004195
4196 if (config == (node->flags & LYS_CONFIG_MASK)) {
4197 /* nothing to do */
4198 return LY_SUCCESS;
4199 }
4200
4201 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004202 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004203 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4204 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004205 "Invalid %s of config - configuration node cannot be child of any state data node.",
4206 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004207 return LY_EVALID;
4208 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004209 node->flags |= LYS_SET_CONFIG;
4210 } else {
4211 if (node->flags & LYS_SET_CONFIG) {
4212 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4213 /* setting config flags, but have node with explicit config true */
4214 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004215 "Invalid %s of config - configuration node cannot be child of any state data node.",
4216 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004217 return LY_EVALID;
4218 }
4219 /* do not change config on nodes where the config is explicitely set, this does not apply to
4220 * nodes, which are being changed explicitly (targets of refine or deviation) */
4221 return LY_SUCCESS;
4222 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004223 }
4224 node->flags &= ~LYS_CONFIG_MASK;
4225 node->flags |= config;
4226
4227 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004228 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004229 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004230 }
4231
Radek Krejci76b3e962018-12-14 17:01:25 +01004232 return LY_SUCCESS;
4233}
4234
Radek Krejcib56c7502019-02-13 14:19:54 +01004235/**
4236 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4237 *
4238 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4239 * the flag to such parents from a mandatory children.
4240 *
4241 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4242 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4243 * (mandatory children was removed).
4244 */
Radek Krejcife909632019-02-12 15:34:42 +01004245void
4246lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4247{
4248 struct lysc_node *iter;
4249
4250 if (add) { /* set flag */
4251 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4252 parent = parent->parent) {
4253 parent->flags |= LYS_MAND_TRUE;
4254 }
4255 } else { /* unset flag */
4256 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004257 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004258 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004259 /* there is another mandatory node */
4260 return;
4261 }
4262 }
4263 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4264 parent->flags &= ~LYS_MAND_TRUE;
4265 }
4266 }
4267}
4268
Radek Krejci056d0a82018-12-06 16:57:25 +01004269/**
Radek Krejci3641f562019-02-13 15:38:40 +01004270 * @brief Internal sorting process for the lys_compile_augment_sort().
4271 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4272 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4273 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4274 */
4275static void
4276lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4277{
4278 unsigned int v;
4279 size_t len;
4280
4281 len = strlen(aug_p->nodeid);
4282 LY_ARRAY_FOR(result, v) {
4283 if (strlen(result[v]->nodeid) <= len) {
4284 continue;
4285 }
4286 if (v < LY_ARRAY_SIZE(result)) {
4287 /* move the rest of array */
4288 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4289 break;
4290 }
4291 }
4292 result[v] = aug_p;
4293 LY_ARRAY_INCREMENT(result);
4294}
4295
4296/**
4297 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4298 *
4299 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4300 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4301 *
4302 * @param[in] ctx Compile context.
4303 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4304 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4305 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4306 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4307 * @return LY_ERR value.
4308 */
4309LY_ERR
4310lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4311{
4312 struct lysp_augment **result = NULL;
4313 unsigned int u, v;
4314 size_t count = 0;
4315
4316 assert(augments);
4317
4318 /* get count of the augments in module and all its submodules */
4319 if (aug_p) {
4320 count += LY_ARRAY_SIZE(aug_p);
4321 }
4322 LY_ARRAY_FOR(inc_p, u) {
4323 if (inc_p[u].submodule->augments) {
4324 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4325 }
4326 }
4327
4328 if (!count) {
4329 *augments = NULL;
4330 return LY_SUCCESS;
4331 }
4332 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4333
4334 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4335 * together, so there can be even /z/y betwwen them. */
4336 LY_ARRAY_FOR(aug_p, u) {
4337 lys_compile_augment_sort_(&aug_p[u], result);
4338 }
4339 LY_ARRAY_FOR(inc_p, u) {
4340 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4341 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4342 }
4343 }
4344
4345 *augments = result;
4346 return LY_SUCCESS;
4347}
4348
4349/**
4350 * @brief Compile the parsed augment connecting it into its target.
4351 *
4352 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4353 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4354 * are already implemented and compiled.
4355 *
4356 * @param[in] ctx Compile context.
4357 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004358 * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's
4359 * children in case of the augmenting uses data.
4360 * @return LY_SUCCESS on success.
4361 * @return LY_EVALID on failure.
4362 */
4363LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004364lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004365{
4366 LY_ERR ret = LY_SUCCESS;
4367 struct lysp_node *node_p, *case_node_p;
4368 struct lysc_node *target; /* target target of the augment */
4369 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004370 struct lysc_when **when, *when_shared;
4371 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004372 uint16_t flags = 0;
4373 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004374 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004375
Radek Krejci327de162019-06-14 12:52:07 +02004376 lysc_update_path(ctx, NULL, "{augment}");
4377 lysc_update_path(ctx, NULL, aug_p->nodeid);
4378
Radek Krejci7af64242019-02-18 13:07:53 +01004379 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004380 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004381 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004382 if (ret != LY_SUCCESS) {
4383 if (ret == LY_EDENIED) {
4384 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4385 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4386 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4387 }
4388 return LY_EVALID;
4389 }
4390
4391 /* check for mandatory nodes
4392 * - new cases augmenting some choice can have mandatory nodes
4393 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4394 */
Radek Krejci733988a2019-02-15 15:12:44 +01004395 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004396 allow_mandatory = 1;
4397 }
4398
4399 when_shared = NULL;
4400 LY_LIST_FOR(aug_p->child, node_p) {
4401 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4402 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4403 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004404 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4405 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004407 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4408 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004409 return LY_EVALID;
4410 }
4411
4412 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004413 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004414 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004415 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004416 } else {
4417 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004418 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004419 }
4420 }
Radek Krejciec4da802019-05-02 13:02:41 +02004421 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004422
Radek Krejcife13da42019-02-15 14:51:01 +01004423 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4424 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004425 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004426 /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004427 for (node = (struct lysc_node*)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next);
Radek Krejci3641f562019-02-13 15:38:40 +01004428 } else if (target->nodetype == LYS_CHOICE) {
4429 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4430 node = ((struct lysc_node_choice*)target)->cases->prev;
4431 } else {
4432 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004433 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004434 }
4435
Radek Krejci733988a2019-02-15 15:12:44 +01004436 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004437 node->flags &= ~LYS_MAND_TRUE;
4438 lys_compile_mandatory_parents(target, 0);
4439 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004440 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004441 return LY_EVALID;
4442 }
4443
4444 /* pass augment's when to all the children */
4445 if (aug_p->when) {
4446 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4447 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004448 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004449 LY_CHECK_GOTO(ret, error);
4450 (*when)->context = lysc_xpath_context(target);
4451 when_shared = *when;
4452 } else {
4453 ++when_shared->refcount;
4454 (*when) = when_shared;
4455 }
4456 }
4457 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004458
Radek Krejciec4da802019-05-02 13:02:41 +02004459 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004460 switch (target->nodetype) {
4461 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004462 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004463 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004464 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004465 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004466 break;
4467 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004468 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004469 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004470 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004471 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004472 break;
4473 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004474 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004475 if (aug_p->actions) {
4476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004477 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4478 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004479 return LY_EVALID;
4480 }
4481 if (aug_p->notifs) {
4482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004483 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4484 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004485 return LY_EVALID;
4486 }
4487 }
Radek Krejci3641f562019-02-13 15:38:40 +01004488
Radek Krejci327de162019-06-14 12:52:07 +02004489 lysc_update_path(ctx, NULL, NULL);
4490 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004491error:
Radek Krejciec4da802019-05-02 13:02:41 +02004492 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004493 return ret;
4494}
4495
4496/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004497 * @brief Apply refined or deviated mandatory flag to the target node.
4498 *
4499 * @param[in] ctx Compile context.
4500 * @param[in] node Target node where the mandatory property is supposed to be changed.
4501 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004502 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejci551b12c2019-02-19 16:11:21 +01004503 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4504 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4505 * the tests are skipped here, but they are supposed to be performed after all the deviations are applied.
Radek Krejcif1421c22019-02-19 13:05:20 +01004506 * @return LY_ERR value.
4507 */
4508static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004509lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag)
Radek Krejcif1421c22019-02-19 13:05:20 +01004510{
4511 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004513 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4514 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004515 return LY_EVALID;
4516 }
4517
4518 if (mandatory_flag & LYS_MAND_TRUE) {
4519 /* check if node has default value */
4520 if (node->nodetype & LYS_LEAF) {
4521 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004522 if (refine_flag) {
4523 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004524 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004525 return LY_EVALID;
4526 }
Radek Krejcia1911222019-07-22 17:24:50 +02004527 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004528 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004529 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4530 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4531 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4532 free(leaf->dflt);
4533 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004534 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004535 }
4536 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004537 if (refine_flag) {
4538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004539 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004540 return LY_EVALID;
4541 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004542 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004543 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004544 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid refine of mandatory under the default case.");
Radek Krejcif1421c22019-02-19 13:05:20 +01004545 return LY_EVALID;
4546 }
4547
4548 node->flags &= ~LYS_MAND_FALSE;
4549 node->flags |= LYS_MAND_TRUE;
4550 lys_compile_mandatory_parents(node->parent, 1);
4551 } else {
4552 /* make mandatory false */
4553 node->flags &= ~LYS_MAND_TRUE;
4554 node->flags |= LYS_MAND_FALSE;
4555 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004556 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt && ((struct lysc_node_leaf*)node)->type->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004557 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004558 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004559 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004560 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4561 leaf->dflt->realtype = leaf->type->dflt->realtype;
4562 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4563 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004564 }
4565 }
4566 return LY_SUCCESS;
4567}
4568
4569/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004570 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4571 * If present, also apply uses's modificators.
4572 *
4573 * @param[in] ctx Compile context
4574 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004575 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4576 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4577 * the compile context.
4578 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4579 */
4580static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004581lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004582{
4583 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004584 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004585 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004586 struct lysc_node_container context_node_fake =
4587 {.nodetype = LYS_CONTAINER,
4588 .module = ctx->mod,
4589 .flags = parent ? parent->flags : 0,
4590 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004591 .prev = (struct lysc_node*)&context_node_fake,
4592 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004593 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004594 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004595 int found;
4596 const char *id, *name, *prefix;
4597 size_t prefix_len, name_len;
4598 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004599 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004600 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004601 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004602 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004603 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004604 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004605 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004606 unsigned int actions_index, notifs_index;
4607 struct lysc_notif **notifs = NULL;
4608 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004609
4610 /* search for the grouping definition */
4611 found = 0;
4612 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004613 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004614 if (prefix) {
4615 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4616 if (!mod) {
4617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004618 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004619 return LY_EVALID;
4620 }
4621 } else {
4622 mod = ctx->mod_def;
4623 }
4624 if (mod == ctx->mod_def) {
4625 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004626 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004627 LY_ARRAY_FOR(grp, u) {
4628 if (!strcmp(grp[u].name, name)) {
4629 grp = &grp[u];
4630 found = 1;
4631 break;
4632 }
4633 }
4634 }
4635 }
4636 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004637 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004638 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004639 if (grp) {
4640 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4641 if (!strcmp(grp[u].name, name)) {
4642 grp = &grp[u];
4643 found = 1;
4644 }
4645 }
4646 }
4647 if (!found && mod->parsed->includes) {
4648 /* ... and all the submodules */
4649 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4650 grp = mod->parsed->includes[u].submodule->groupings;
4651 if (grp) {
4652 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4653 if (!strcmp(grp[v].name, name)) {
4654 grp = &grp[v];
4655 found = 1;
4656 }
4657 }
4658 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004659 }
4660 }
4661 }
4662 if (!found) {
4663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4664 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4665 return LY_EVALID;
4666 }
4667
4668 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4669 grp_stack_count = ctx->groupings.count;
4670 ly_set_add(&ctx->groupings, (void*)grp, 0);
4671 if (grp_stack_count == ctx->groupings.count) {
4672 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4674 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4675 return LY_EVALID;
4676 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004677 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4678 /* remember that the grouping is instantiated to avoid its standalone validation */
4679 grp->flags |= LYS_USED_GRP;
4680 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004681
4682 /* switch context's mod_def */
4683 mod_old = ctx->mod_def;
4684 ctx->mod_def = mod;
4685
4686 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004687 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup);
Radek Krejcie86bf772018-12-14 11:39:53 +01004688
Radek Krejcifc11bd72019-04-11 16:00:05 +02004689 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004690 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004691 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
Radek Krejciec4da802019-05-02 13:02:41 +02004692 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004693
4694 /* some preparation for applying refines */
4695 if (grp->data == node_p) {
4696 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004697 if (parent) {
4698 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4699 } else if (ctx->mod->compiled->data) {
4700 child = ctx->mod->compiled->data;
4701 } else {
4702 child = NULL;
4703 }
4704 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004705 }
4706 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004707 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004708 LY_LIST_FOR(context_node_fake.child, child) {
4709 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004710
Radek Krejcifc11bd72019-04-11 16:00:05 +02004711 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004712 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004713 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004714 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004715 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004716 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004717 when_shared = *when;
4718 } else {
4719 ++when_shared->refcount;
4720 (*when) = when_shared;
4721 }
4722 }
Radek Krejci01342af2019-01-03 15:18:08 +01004723 }
4724 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004725 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004726 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004727 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004728 context_node_fake.child->prev = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01004729 }
4730
Radek Krejcifc11bd72019-04-11 16:00:05 +02004731 /* compile actions */
4732 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4733 if (actions) {
4734 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004735 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004736 if (*actions && (uses_p->augments || uses_p->refines)) {
4737 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4738 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4739 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4740 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4741 }
4742 }
4743
4744 /* compile notifications */
4745 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4746 if (notifs) {
4747 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004748 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004749 if (*notifs && (uses_p->augments || uses_p->refines)) {
4750 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4751 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4752 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4753 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4754 }
4755 }
4756
4757
Radek Krejci3641f562019-02-13 15:38:40 +01004758 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004759 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004760 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004761 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004762 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004763
Radek Krejcif0089082019-01-07 16:42:01 +01004764 /* reload previous context's mod_def */
4765 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004766 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004767
Radek Krejci76b3e962018-12-14 17:01:25 +01004768 /* apply refine */
4769 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004770 lysc_update_path(ctx, NULL, rfn->nodeid);
4771
Radek Krejci7af64242019-02-18 13:07:53 +01004772 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004773 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004774 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004775 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004776
4777 /* default value */
4778 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004779 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004781 "Invalid refine of default - %s cannot hold %d default values.",
4782 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004783 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004784 }
4785 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004787 "Invalid refine of default - %s cannot hold default value(s).",
4788 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004789 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004790 }
4791 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004792 struct ly_err_item *err = NULL;
4793 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4794 if (leaf->dflt) {
4795 /* remove the previous default value */
4796 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4797 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4798 } else {
4799 /* prepare a new one */
4800 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4801 leaf->dflt->realtype = leaf->type;
4802 }
4803 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004804 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004805 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4806 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004807 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004808 leaf->dflt->realtype->refcount++;
4809 if (err) {
4810 ly_err_print(err);
4811 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4812 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4813 ly_err_free(err);
4814 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004815 if (rc == LY_EINCOMPLETE) {
4816 /* postpone default compilation when the tree is complete */
4817 struct lysc_incomplete_dflt *r;
4818 r = malloc(sizeof *r);
4819 r->context_node = node;
4820 r->dflt = leaf->dflt;
4821 r->dflt_mod = leaf->dflt_mod;
4822 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
4823
4824 /* but in general result is so far ok */
4825 rc = LY_SUCCESS;
4826 }
Radek Krejcia1911222019-07-22 17:24:50 +02004827 LY_CHECK_GOTO(rc, cleanup);
4828 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004829 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004830 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4831
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004832 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4834 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004835 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004836 }
Radek Krejcia1911222019-07-22 17:24:50 +02004837
4838 /* remove previous set of default values */
4839 LY_ARRAY_FOR(llist->dflts, u) {
4840 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4841 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4842 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004843 }
Radek Krejcia1911222019-07-22 17:24:50 +02004844 LY_ARRAY_FREE(llist->dflts);
4845 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004846 LY_ARRAY_FREE(llist->dflts_mods);
4847 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004848
4849 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004850 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004851 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004852 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004853 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004854 LY_ARRAY_INCREMENT(llist->dflts_mods);
4855 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004856 LY_ARRAY_INCREMENT(llist->dflts);
4857 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4858 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004859 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004860 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004861 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004862 llist->dflts[u]->realtype->refcount++;
4863 if (err) {
4864 ly_err_print(err);
4865 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4866 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4867 ly_err_free(err);
4868 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004869 if (rc == LY_EINCOMPLETE) {
4870 /* postpone default compilation when the tree is complete */
4871 struct lysc_incomplete_dflt *r;
4872 r = malloc(sizeof *r);
4873 r->context_node = node;
4874 r->dflt = llist->dflts[u];
4875 r->dflt_mod = llist->dflts_mods[u];
4876 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
4877
4878 /* but in general result is so far ok */
4879 rc = LY_SUCCESS;
4880 }
4881 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004882 }
Radek Krejcia1911222019-07-22 17:24:50 +02004883 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004884 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004885 if (((struct lysc_node_choice*)node)->dflt) {
4886 /* unset LYS_SET_DFLT from the current default case */
4887 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4888 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004889 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004890 }
4891 }
4892
Radek Krejci12fb9142019-01-08 09:45:30 +01004893 /* description */
4894 if (rfn->dsc) {
4895 FREE_STRING(ctx->ctx, node->dsc);
4896 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4897 }
4898
4899 /* reference */
4900 if (rfn->ref) {
4901 FREE_STRING(ctx->ctx, node->ref);
4902 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4903 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004904
4905 /* config */
4906 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004907 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004908 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004909 } else {
4910 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4911 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4912 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004913 }
4914
4915 /* mandatory */
4916 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004917 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004918 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004919
4920 /* presence */
4921 if (rfn->presence) {
4922 if (node->nodetype != LYS_CONTAINER) {
4923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004924 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4925 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004926 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004927 }
4928 node->flags |= LYS_PRESENCE;
4929 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004930
4931 /* must */
4932 if (rfn->musts) {
4933 switch (node->nodetype) {
4934 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004935 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004936 break;
4937 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004938 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004939 break;
4940 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004941 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004942 break;
4943 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004944 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004945 break;
4946 case LYS_ANYXML:
4947 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004948 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004949 break;
4950 default:
4951 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004952 "Invalid refine of must statement - %s cannot hold any must statement.",
4953 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004954 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004955 }
4956 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004957
4958 /* min/max-elements */
4959 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4960 switch (node->nodetype) {
4961 case LYS_LEAFLIST:
4962 if (rfn->flags & LYS_SET_MAX) {
4963 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4964 }
4965 if (rfn->flags & LYS_SET_MIN) {
4966 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004967 if (rfn->min) {
4968 node->flags |= LYS_MAND_TRUE;
4969 lys_compile_mandatory_parents(node->parent, 1);
4970 } else {
4971 node->flags &= ~LYS_MAND_TRUE;
4972 lys_compile_mandatory_parents(node->parent, 0);
4973 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004974 }
4975 break;
4976 case LYS_LIST:
4977 if (rfn->flags & LYS_SET_MAX) {
4978 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4979 }
4980 if (rfn->flags & LYS_SET_MIN) {
4981 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004982 if (rfn->min) {
4983 node->flags |= LYS_MAND_TRUE;
4984 lys_compile_mandatory_parents(node->parent, 1);
4985 } else {
4986 node->flags &= ~LYS_MAND_TRUE;
4987 lys_compile_mandatory_parents(node->parent, 0);
4988 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004989 }
4990 break;
4991 default:
4992 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004993 "Invalid refine of %s statement - %s cannot hold this statement.",
4994 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004995 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004996 }
4997 }
Radek Krejcif0089082019-01-07 16:42:01 +01004998
4999 /* if-feature */
5000 if (rfn->iffeatures) {
5001 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005002 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005003 }
Radek Krejci327de162019-06-14 12:52:07 +02005004
5005 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005006 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005007
Radek Krejcif2271f12019-01-07 16:42:23 +01005008 /* do some additional checks of the changed nodes when all the refines are applied */
5009 for (u = 0; u < refined.count; ++u) {
5010 node = (struct lysc_node*)refined.objs[u];
5011 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005012 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005013
5014 /* check possible conflict with default value (default added, mandatory left true) */
5015 if ((node->flags & LYS_MAND_TRUE) &&
5016 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5017 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005019 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005020 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005021 }
5022
5023 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5024 if (node->nodetype == LYS_LIST) {
5025 min = ((struct lysc_node_list*)node)->min;
5026 max = ((struct lysc_node_list*)node)->max;
5027 } else {
5028 min = ((struct lysc_node_leaflist*)node)->min;
5029 max = ((struct lysc_node_leaflist*)node)->max;
5030 }
5031 if (min > max) {
5032 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005033 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5034 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005035 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005036 }
5037 }
5038 }
5039
Radek Krejci327de162019-06-14 12:52:07 +02005040 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005041 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005042
5043cleanup:
5044 /* fix connection of the children nodes from fake context node back into the parent */
5045 if (context_node_fake.child) {
5046 context_node_fake.child->prev = child;
5047 }
5048 LY_LIST_FOR(context_node_fake.child, child) {
5049 child->parent = parent;
5050 }
5051
5052 if (uses_p->augments || uses_p->refines) {
5053 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005054 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005055 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5056 LY_ARRAY_FREE(context_node_fake.actions);
5057 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005058 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005059 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5060 LY_ARRAY_FREE(context_node_fake.notifs);
5061 }
5062 }
5063
Radek Krejcie86bf772018-12-14 11:39:53 +01005064 /* reload previous context's mod_def */
5065 ctx->mod_def = mod_old;
5066 /* remove the grouping from the stack for circular groupings dependency check */
5067 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5068 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005069 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005070 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005071
5072 return ret;
5073}
5074
Radek Krejci327de162019-06-14 12:52:07 +02005075static int
5076lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5077{
5078 struct lysp_node *iter;
5079 int len = 0;
5080
5081 *path = NULL;
5082 for (iter = node; iter && len >= 0; iter = iter->parent) {
5083 char *s = *path;
5084 char *id;
5085
5086 switch (iter->nodetype) {
5087 case LYS_USES:
5088 asprintf(&id, "{uses='%s'}", iter->name);
5089 break;
5090 case LYS_GROUPING:
5091 asprintf(&id, "{grouping='%s'}", iter->name);
5092 break;
5093 case LYS_AUGMENT:
5094 asprintf(&id, "{augment='%s'}", iter->name);
5095 break;
5096 default:
5097 id = strdup(iter->name);
5098 break;
5099 }
5100
5101 if (!iter->parent) {
5102 /* print prefix */
5103 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5104 } else {
5105 /* prefix is the same as in parent */
5106 len = asprintf(path, "/%s%s", id, s ? s : "");
5107 }
5108 free(s);
5109 free(id);
5110 }
5111
5112 if (len < 0) {
5113 free(*path);
5114 *path = NULL;
5115 } else if (len == 0) {
5116 *path = strdup("/");
5117 len = 1;
5118 }
5119 return len;
5120}
5121
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005122/**
5123 * @brief Validate groupings that were defined but not directly used in the schema itself.
5124 *
5125 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5126 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5127 */
5128static LY_ERR
5129lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5130{
5131 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005132 char *path;
5133 int len;
5134
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005135 struct lysp_node_uses fake_uses = {
5136 .parent = node_p,
5137 .nodetype = LYS_USES,
5138 .flags = 0, .next = NULL,
5139 .name = grp->name,
5140 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5141 .refines = NULL, .augments = NULL
5142 };
5143 struct lysc_node_container fake_container = {
5144 .nodetype = LYS_CONTAINER,
5145 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5146 .module = ctx->mod,
5147 .sp = NULL, .parent = NULL, .next = NULL,
5148 .prev = (struct lysc_node*)&fake_container,
5149 .name = "fake",
5150 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5151 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5152 };
5153
5154 if (grp->parent) {
5155 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5156 }
Radek Krejci327de162019-06-14 12:52:07 +02005157
5158 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5159 if (len < 0) {
5160 LOGMEM(ctx->ctx);
5161 return LY_EMEM;
5162 }
5163 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5164 ctx->path_len = (uint16_t)len;
5165 free(path);
5166
5167 lysc_update_path(ctx, NULL, "{grouping}");
5168 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005169 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005170 lysc_update_path(ctx, NULL, NULL);
5171 lysc_update_path(ctx, NULL, NULL);
5172
5173 ctx->path_len = 1;
5174 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005175
5176 /* cleanup */
5177 lysc_node_container_free(ctx->ctx, &fake_container);
5178
5179 return ret;
5180}
Radek Krejcife909632019-02-12 15:34:42 +01005181
Radek Krejcie86bf772018-12-14 11:39:53 +01005182/**
Radek Krejcia3045382018-11-22 14:30:31 +01005183 * @brief Compile parsed schema node information.
5184 * @param[in] ctx Compile context
5185 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005186 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5187 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5188 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005189 * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags).
5190 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005191 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5192 */
Radek Krejci19a96102018-11-15 13:38:09 +01005193static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005194lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status)
Radek Krejci19a96102018-11-15 13:38:09 +01005195{
5196 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005197 struct lysc_node *node;
5198 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005199 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005200 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005201 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005202
Radek Krejci327de162019-06-14 12:52:07 +02005203 if (node_p->nodetype != LYS_USES) {
5204 lysc_update_path(ctx, parent, node_p->name);
5205 } else {
5206 lysc_update_path(ctx, NULL, "{uses}");
5207 lysc_update_path(ctx, NULL, node_p->name);
5208 }
5209
Radek Krejci19a96102018-11-15 13:38:09 +01005210 switch (node_p->nodetype) {
5211 case LYS_CONTAINER:
5212 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5213 node_compile_spec = lys_compile_node_container;
5214 break;
5215 case LYS_LEAF:
5216 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5217 node_compile_spec = lys_compile_node_leaf;
5218 break;
5219 case LYS_LIST:
5220 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005221 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005222 break;
5223 case LYS_LEAFLIST:
5224 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005225 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005226 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005227 case LYS_CHOICE:
5228 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005229 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005230 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005231 case LYS_ANYXML:
5232 case LYS_ANYDATA:
5233 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005234 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005235 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005236 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005237 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5238 lysc_update_path(ctx, NULL, NULL);
5239 lysc_update_path(ctx, NULL, NULL);
5240 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005241 default:
5242 LOGINT(ctx->ctx);
5243 return LY_EINT;
5244 }
5245 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5246 node->nodetype = node_p->nodetype;
5247 node->module = ctx->mod;
5248 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005249 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005250
5251 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005252 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005253 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005254 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005255 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5256 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005257 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005258 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005259 node->flags |= LYS_CONFIG_R;
5260 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005261 /* config not explicitely set, inherit it from parent */
5262 if (parent) {
5263 node->flags |= parent->flags & LYS_CONFIG_MASK;
5264 } else {
5265 /* default is config true */
5266 node->flags |= LYS_CONFIG_W;
5267 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005268 } else {
5269 /* config set explicitely */
5270 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005271 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005272 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5274 "Configuration node cannot be child of any state data node.");
5275 goto error;
5276 }
Radek Krejci19a96102018-11-15 13:38:09 +01005277
Radek Krejcia6d57732018-11-29 13:40:37 +01005278 /* *list ordering */
5279 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5280 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005281 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005282 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5283 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005284 node->flags &= ~LYS_ORDBY_MASK;
5285 node->flags |= LYS_ORDBY_SYSTEM;
5286 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5287 /* default ordering is system */
5288 node->flags |= LYS_ORDBY_SYSTEM;
5289 }
5290 }
5291
Radek Krejci19a96102018-11-15 13:38:09 +01005292 /* status - it is not inherited by specification, but it does not make sense to have
5293 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005294 if (!parent || parent->nodetype != LYS_CHOICE) {
5295 /* in case of choice/case's children, postpone the check to the moment we know if
5296 * the parent is choice (parent here) or some case (so we have to get its flags to check) */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005297 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005298 }
5299
Radek Krejciec4da802019-05-02 13:02:41 +02005300 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005301 node->sp = node_p;
5302 }
5303 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005304 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5305 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005306 if (node_p->when) {
5307 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005308 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005309 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005310 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005311 }
Radek Krejciec4da802019-05-02 13:02:41 +02005312 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
5313 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005314
5315 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005316 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005317
Radek Krejcife909632019-02-12 15:34:42 +01005318 /* inherit LYS_MAND_TRUE in parent containers */
5319 if (node->flags & LYS_MAND_TRUE) {
5320 lys_compile_mandatory_parents(parent, 1);
5321 }
5322
Radek Krejci327de162019-06-14 12:52:07 +02005323 lysc_update_path(ctx, NULL, NULL);
5324
Radek Krejci19a96102018-11-15 13:38:09 +01005325 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005326 if (parent) {
5327 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005328 if (node_p->parent->nodetype == LYS_CASE) {
5329 lysc_update_path(ctx, parent, node_p->parent->name);
5330 } else {
5331 lysc_update_path(ctx, parent, node->name);
5332 }
Radek Krejciec4da802019-05-02 13:02:41 +02005333 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005334 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005335 if (uses_status) {
5336
5337 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005338 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005339 * it directly gets the same status flags as the choice;
5340 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005341 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005342 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005343 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005344 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005345 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005346 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005347 }
Radek Krejciec4da802019-05-02 13:02:41 +02005348 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci327de162019-06-14 12:52:07 +02005349
5350 if (parent->nodetype == LYS_CHOICE) {
5351 lysc_update_path(ctx, NULL, NULL);
5352 }
Radek Krejci19a96102018-11-15 13:38:09 +01005353 } else {
5354 /* top-level element */
5355 if (!ctx->mod->compiled->data) {
5356 ctx->mod->compiled->data = node;
5357 } else {
5358 /* insert at the end of the module's top-level nodes list */
5359 ctx->mod->compiled->data->prev->next = node;
5360 node->prev = ctx->mod->compiled->data->prev;
5361 ctx->mod->compiled->data->prev = node;
5362 }
Radek Krejci327de162019-06-14 12:52:07 +02005363 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005364 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5365 ctx->mod->compiled->notifs, node->name, node)) {
5366 return LY_EVALID;
5367 }
Radek Krejci19a96102018-11-15 13:38:09 +01005368 }
Radek Krejci327de162019-06-14 12:52:07 +02005369 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005370
5371 return LY_SUCCESS;
5372
5373error:
5374 lysc_node_free(ctx->ctx, node);
5375 return ret;
5376}
5377
Radek Krejciccd20f12019-02-15 14:12:27 +01005378static void
5379lysc_disconnect(struct lysc_node *node)
5380{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005381 struct lysc_node *parent, *child, *prev = NULL, *next;
5382 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005383 int remove_cs = 0;
5384
5385 parent = node->parent;
5386
5387 /* parent's first child */
5388 if (!parent) {
5389 return;
5390 }
5391 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005392 cs = (struct lysc_node_case*)node;
5393 } else if (parent->nodetype == LYS_CASE) {
5394 /* disconnecting some node in a case */
5395 cs = (struct lysc_node_case*)parent;
5396 parent = cs->parent;
5397 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5398 if (child == node) {
5399 if (cs->child == child) {
5400 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5401 /* case with a single child -> remove also the case */
5402 child->parent = NULL;
5403 remove_cs = 1;
5404 } else {
5405 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005406 }
5407 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005408 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005409 }
5410 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005411 if (!remove_cs) {
5412 cs = NULL;
5413 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005414 } else if (lysc_node_children(parent, node->flags) == node) {
5415 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005416 }
5417
5418 if (cs) {
5419 if (remove_cs) {
5420 /* cs has only one child which is being also removed */
5421 lysc_disconnect((struct lysc_node*)cs);
5422 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5423 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005424 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5425 /* default case removed */
5426 ((struct lysc_node_choice*)parent)->dflt = NULL;
5427 }
5428 if (((struct lysc_node_choice*)parent)->cases == cs) {
5429 /* first case removed */
5430 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5431 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005432 if (cs->child) {
5433 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5434 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5435 prev = cs->child->prev;
5436 } /* else all the children are under a single case */
5437 LY_LIST_FOR_SAFE(cs->child, next, child) {
5438 if (child->parent != (struct lysc_node*)cs) {
5439 break;
5440 }
5441 lysc_node_free(node->module->ctx, child);
5442 }
5443 if (prev) {
5444 if (prev->next) {
5445 prev->next = child;
5446 }
5447 if (child) {
5448 child->prev = prev;
5449 } else {
5450 /* link from the first child under the cases */
5451 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5452 }
5453 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005454 }
5455 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005456 }
5457
5458 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005459 if (node->prev->next) {
5460 node->prev->next = node->next;
5461 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005462 if (node->next) {
5463 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005464 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005465 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005466 if (child) {
5467 child->prev = node->prev;
5468 }
5469 } else if (((struct lysc_node_choice*)parent)->cases) {
5470 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005471 }
5472}
5473
5474LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005475lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005476{
Radek Krejcia1911222019-07-22 17:24:50 +02005477 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005478 struct ly_set devs_p = {0};
5479 struct ly_set targets = {0};
5480 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005481 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005482 struct lysc_action *rpcs;
5483 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005484 struct lysp_deviation *dev;
5485 struct lysp_deviate *d, **dp_new;
5486 struct lysp_deviate_add *d_add;
5487 struct lysp_deviate_del *d_del;
5488 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005489 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005490 struct lysc_deviation {
5491 const char *nodeid;
5492 struct lysc_node *target; /* target node of the deviation */
5493 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005494 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005495 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5496 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005497 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005498 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005499 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005500 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005501 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005502 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005503
5504 /* get all deviations from the module and all its submodules ... */
5505 LY_ARRAY_FOR(mod_p->deviations, u) {
5506 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5507 }
5508 LY_ARRAY_FOR(mod_p->includes, v) {
5509 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5510 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5511 }
5512 }
5513 if (!devs_p.count) {
5514 /* nothing to do */
5515 return LY_SUCCESS;
5516 }
5517
Radek Krejci327de162019-06-14 12:52:07 +02005518 lysc_update_path(ctx, NULL, "{deviation}");
5519
Radek Krejciccd20f12019-02-15 14:12:27 +01005520 /* ... and group them by the target node */
5521 devs = calloc(devs_p.count, sizeof *devs);
5522 for (u = 0; u < devs_p.count; ++u) {
5523 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005524 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005525
5526 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005527 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5528 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005529 if (target->nodetype == LYS_ACTION) {
5530 /* move the target pointer to input/output to make them different from the action and
5531 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5532 * back to the RPC/action node due to a better compatibility and decision code in this function.
5533 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5534 if (flags & LYSC_OPT_RPC_INPUT) {
5535 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5536 flags |= LYSC_OPT_INTERNAL;
5537 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5538 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5539 flags |= LYSC_OPT_INTERNAL;
5540 }
5541 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005542 /* insert into the set of targets with duplicity detection */
5543 i = ly_set_add(&targets, target, 0);
5544 if (!devs[i]) {
5545 /* new record */
5546 devs[i] = calloc(1, sizeof **devs);
5547 devs[i]->target = target;
5548 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005549 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005550 }
5551 /* add deviates into the deviation's list of deviates */
5552 for (d = dev->deviates; d; d = d->next) {
5553 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5554 *dp_new = d;
5555 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5556 devs[i]->not_supported = 1;
5557 }
5558 }
Radek Krejci327de162019-06-14 12:52:07 +02005559
5560 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005561 }
5562
5563 /* MACROS for deviates checking */
5564#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5565 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
Radek Krejciccd20f12019-02-15 14:12:27 +01005567 goto cleanup; \
5568 }
5569
5570#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5571 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005572 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5573 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005574 goto cleanup; \
5575 }
5576
Radek Krejcia1911222019-07-22 17:24:50 +02005577
Radek Krejciccd20f12019-02-15 14:12:27 +01005578#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005579 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5580 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5581 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5582 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5583 goto cleanup; \
5584 }
5585
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005586#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005587 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005588 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005589 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5590 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005592 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5593 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005594 goto cleanup; \
5595 }
5596
Radek Krejci551b12c2019-02-19 16:11:21 +01005597#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5598 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005600 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5601 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005602 goto cleanup; \
5603 }
5604
Radek Krejciccd20f12019-02-15 14:12:27 +01005605#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5606 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005607 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005608 goto cleanup; \
5609 }
5610
Radek Krejci551b12c2019-02-19 16:11:21 +01005611#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5612 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5613 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005614 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005615 goto cleanup; \
5616 }
5617
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005618#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5619 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5620 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5621 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5622 if (!strcmp(((TYPE)devs[u]->target)->ARRAY_TRG[y]VALMEMBER_CMP, d_del->ARRAY_DEV[x]VALMEMBER)) { break; } \
Radek Krejciccd20f12019-02-15 14:12:27 +01005623 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005624 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005626 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5627 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005628 goto cleanup; \
5629 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005630 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5631 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5632 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5633 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5634 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005635 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005636 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5637 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5638 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005639 }
5640
5641 /* apply deviations */
5642 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005643 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5644 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5645 struct ly_err_item *err = NULL;
5646
5647 dflt = NULL;
5648 changed_type = 0;
5649
Radek Krejci327de162019-06-14 12:52:07 +02005650 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5651
Radek Krejcif538ce52019-03-05 10:46:14 +01005652 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5653 /* fix the target pointer in case of RPC's/action's input/output */
5654 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5655 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5656 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5657 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5658 }
5659 }
5660
Radek Krejciccd20f12019-02-15 14:12:27 +01005661 /* not-supported */
5662 if (devs[u]->not_supported) {
5663 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5664 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5665 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5666 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005667
5668#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5669 if (devs[u]->target->parent) { \
5670 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5671 } else { \
5672 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5673 } \
5674 LY_ARRAY_FOR(ARRAY, x) { \
5675 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5676 } \
5677 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5678 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5679 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5680 LY_ARRAY_DECREMENT(ARRAY); \
5681 }
5682
Radek Krejcif538ce52019-03-05 10:46:14 +01005683 if (devs[u]->target->nodetype == LYS_ACTION) {
5684 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5685 /* remove RPC's/action's input */
5686 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5687 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005688 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5689 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005690 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5691 /* remove RPC's/action's output */
5692 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5693 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005694 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5695 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005696 } else {
5697 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005698 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005699 }
5700 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005701 /* remove Notification */
5702 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005703 } else {
5704 /* remove the target node */
5705 lysc_disconnect(devs[u]->target);
5706 lysc_node_free(ctx->ctx, devs[u]->target);
5707 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005708
5709 continue;
5710 }
5711
5712 /* list of deviates (not-supported is not present in the list) */
5713 LY_ARRAY_FOR(devs[u]->deviates, v) {
5714 d = devs[u]->deviates[v];
5715
5716 switch (d->mod) {
5717 case LYS_DEV_ADD:
5718 d_add = (struct lysp_deviate_add*)d;
5719 /* [units-stmt] */
5720 if (d_add->units) {
5721 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5722 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5723
5724 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5725 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5726 }
5727
5728 /* *must-stmt */
5729 if (d_add->musts) {
5730 switch (devs[u]->target->nodetype) {
5731 case LYS_CONTAINER:
5732 case LYS_LIST:
5733 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005734 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005735 break;
5736 case LYS_LEAF:
5737 case LYS_LEAFLIST:
5738 case LYS_ANYDATA:
5739 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005740 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005741 break;
5742 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005743 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005744 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005745 break;
5746 case LYS_ACTION:
5747 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5748 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005749 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005750 break;
5751 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5752 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005753 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005754 break;
5755 }
5756 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005757 default:
5758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005759 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005760 goto cleanup;
5761 }
5762 }
5763
5764 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005765 if (d_add->uniques) {
5766 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5767 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5768 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005769
5770 /* *default-stmt */
5771 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005772 switch (devs[u]->target->nodetype) {
5773 case LYS_LEAF:
5774 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005775 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod);
Radek Krejcia1911222019-07-22 17:24:50 +02005776 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005777 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005778 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5779 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5780 } else {
5781 /* prepare new default value storage */
5782 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005783 }
Radek Krejcia1911222019-07-22 17:24:50 +02005784 dflt = d_add->dflts[0];
5785 /* parsing is done at the end after possible replace of the leaf's type */
5786
Radek Krejci551b12c2019-02-19 16:11:21 +01005787 /* mark the new default values as leaf's own */
5788 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005789 break;
5790 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005791 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005792 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005793 LY_ARRAY_FOR(llist->dflts, x) {
5794 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5795 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5796 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005797 }
Radek Krejcia1911222019-07-22 17:24:50 +02005798 LY_ARRAY_FREE(llist->dflts);
5799 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005800 LY_ARRAY_FREE(llist->dflts_mods);
5801 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005802 }
5803 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005804 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005805 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5806 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005807 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005808 LY_ARRAY_INCREMENT(llist->dflts_mods);
5809 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005810 LY_ARRAY_INCREMENT(llist->dflts);
5811 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5812 llist->dflts[x]->realtype = llist->type;
5813 rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]),
Radek Krejci1c0c3442019-07-23 16:08:47 +02005814 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
5815 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005816 llist->dflts[x]->realtype->refcount++;
5817 if (err) {
5818 ly_err_print(err);
5819 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5820 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5821 d_add->dflts[x - y], err->msg);
5822 ly_err_free(err);
5823 }
5824 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005825 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005826 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005827 devs[u]->target->flags |= LYS_SET_DFLT;
5828 break;
5829 case LYS_CHOICE:
5830 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5831 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5832 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5833 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005834 if (lys_compile_deviation_set_choice_dflt(ctx, d_add->dflts[0], (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005835 goto cleanup;
5836 }
5837 break;
5838 default:
5839 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005840 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005841 goto cleanup;
5842 }
5843 }
5844
5845 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005846 if (d_add->flags & LYS_CONFIG_MASK) {
5847 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005848 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005849 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5850 goto cleanup;
5851 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005852 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005853 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5854 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005855 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005856 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5857 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005858 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5859 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005860 goto cleanup;
5861 }
Radek Krejci327de162019-06-14 12:52:07 +02005862 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005863 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005864
5865 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005866 if (d_add->flags & LYS_MAND_MASK) {
5867 if (devs[u]->target->flags & LYS_MAND_MASK) {
5868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005869 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5870 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005871 goto cleanup;
5872 }
Radek Krejci327de162019-06-14 12:52:07 +02005873 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005874 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005875
5876 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005877 if (d_add->flags & LYS_SET_MIN) {
5878 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5879 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5880 /* change value */
5881 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5882 } else if (devs[u]->target->nodetype == LYS_LIST) {
5883 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5884 /* change value */
5885 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5886 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005887 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005888 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5889 goto cleanup;
5890 }
5891 if (d_add->min) {
5892 devs[u]->target->flags |= LYS_MAND_TRUE;
5893 }
5894 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005895
5896 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005897 if (d_add->flags & LYS_SET_MAX) {
5898 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5899 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5900 /* change value */
5901 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5902 } else if (devs[u]->target->nodetype == LYS_LIST) {
5903 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5904 /* change value */
5905 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5906 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005908 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5909 goto cleanup;
5910 }
5911 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005912
5913 break;
5914 case LYS_DEV_DELETE:
5915 d_del = (struct lysp_deviate_del*)d;
5916
5917 /* [units-stmt] */
5918 if (d_del->units) {
5919 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005920 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5921 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5923 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5924 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5925 goto cleanup;
5926 }
5927 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5928 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005929 }
5930
5931 /* *must-stmt */
5932 if (d_del->musts) {
5933 switch (devs[u]->target->nodetype) {
5934 case LYS_CONTAINER:
5935 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005936 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005937 break;
5938 case LYS_LEAF:
5939 case LYS_LEAFLIST:
5940 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005941 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005942 break;
5943 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005944 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005945 break;
5946 case LYS_ACTION:
5947 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5948 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5949 break;
5950 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5951 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5952 break;
5953 }
5954 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005955 default:
5956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005957 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005958 goto cleanup;
5959 }
5960 }
5961
5962 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005963 if (d_del->uniques) {
5964 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5965 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5966 LY_ARRAY_FOR(d_del->uniques, x) {
5967 LY_ARRAY_FOR(list->uniques, z) {
5968 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5969 nodeid = strpbrk(name, " \t\n");
5970 if (nodeid) {
5971 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5972 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5973 break;
5974 }
5975 while (isspace(*nodeid)) {
5976 ++nodeid;
5977 }
5978 } else {
5979 if (strcmp(name, list->uniques[z][y]->name)) {
5980 break;
5981 }
5982 }
5983 }
5984 if (!name) {
5985 /* complete match - remove the unique */
5986 LY_ARRAY_DECREMENT(list->uniques);
5987 LY_ARRAY_FREE(list->uniques[z]);
5988 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5989 --z;
5990 break;
5991 }
5992 }
5993 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5994 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005995 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5996 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01005997 goto cleanup;
5998 }
5999 }
6000 if (!LY_ARRAY_SIZE(list->uniques)) {
6001 LY_ARRAY_FREE(list->uniques);
6002 list->uniques = NULL;
6003 }
6004 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006005
6006 /* *default-stmt */
6007 if (d_del->dflts) {
6008 switch (devs[u]->target->nodetype) {
6009 case LYS_LEAF:
6010 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6011 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6012 dflt, "deleting", "default", d_del->dflts[0]);
6013
Radek Krejcia1911222019-07-22 17:24:50 +02006014 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006015 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006016 if (strcmp(dflt, d_del->dflts[0])) {
6017 if (i) {
6018 free((char*)dflt);
6019 }
6020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6021 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6022 d_del->dflts[0], dflt);
6023 goto cleanup;
6024 }
6025 if (i) {
6026 free((char*)dflt);
6027 }
6028 dflt = NULL;
6029
6030 /* remove the default specification */
6031 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6032 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6033 free(leaf->dflt);
6034 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006035 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006036 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006037 break;
6038 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006039 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6040 LY_ARRAY_FOR(d_del->dflts, x) {
6041 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006042 dflt = llist->type->plugin->print(llist->dflts[y], LYD_XML, lys_get_prefix, llist->dflts_mods[y], &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006043 if (!strcmp(dflt, d_del->dflts[x])) {
6044 if (i) {
6045 free((char*)dflt);
6046 }
6047 dflt = NULL;
6048 break;
6049 }
6050 if (i) {
6051 free((char*)dflt);
6052 }
6053 dflt = NULL;
6054 }
6055 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6057 "which does not match any of the target's property values.", d_del->dflts[x]);
6058 goto cleanup;
6059 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006060 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006061 LY_ARRAY_DECREMENT(llist->dflts);
6062 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6063 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6064 free(llist->dflts[y]);
6065 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006066 memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods));
Radek Krejcia1911222019-07-22 17:24:50 +02006067 }
6068 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006069 LY_ARRAY_FREE(llist->dflts_mods);
6070 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006071 LY_ARRAY_FREE(llist->dflts);
6072 llist->dflts = NULL;
6073 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006074 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006075 break;
6076 case LYS_CHOICE:
6077 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6078 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6079 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006080 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006081 if (prefix) {
6082 /* use module prefixes from the deviation module to match the module of the default case */
6083 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6084 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006085 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6086 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006087 goto cleanup;
6088 }
6089 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6090 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006091 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6092 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006093 goto cleanup;
6094 }
6095 }
6096 /* else {
6097 * strictly, the default prefix would point to the deviation module, but the value should actually
6098 * match the default string in the original module (usually unprefixed), so in this case we do not check
6099 * the module of the default case, just matching its name */
6100 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006102 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6103 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006104 goto cleanup;
6105 }
6106 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6107 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6108 break;
6109 default:
6110 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006111 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006112 goto cleanup;
6113 }
6114 }
6115
6116 break;
6117 case LYS_DEV_REPLACE:
6118 d_rpl = (struct lysp_deviate_rpl*)d;
6119
6120 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006121 if (d_rpl->type) {
6122 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6123 /* type is mandatory, so checking for its presence is not necessary */
6124 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006125
6126 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6127 /* the target has default from the previous type - remove it */
6128 if (devs[u]->target->nodetype == LYS_LEAF) {
6129 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6130 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6131 free(leaf->dflt);
6132 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006133 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006134 } else { /* LYS_LEAFLIST */
6135 LY_ARRAY_FOR(llist->dflts, x) {
6136 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6137 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6138 free(llist->dflts[x]);
6139 }
6140 LY_ARRAY_FREE(llist->dflts);
6141 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006142 LY_ARRAY_FREE(llist->dflts_mods);
6143 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006144 }
6145 }
6146 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006147 /* there is no default value, do not set changed_type after type compilation
6148 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006149 changed_type = -1;
6150 }
Radek Krejciec4da802019-05-02 13:02:41 +02006151 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006152 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006153 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006154
6155 /* [units-stmt] */
6156 if (d_rpl->units) {
6157 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6158 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6159 units, "replacing", "units", d_rpl->units);
6160
6161 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6162 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6163 }
6164
6165 /* [default-stmt] */
6166 if (d_rpl->dflt) {
6167 switch (devs[u]->target->nodetype) {
6168 case LYS_LEAF:
6169 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6170 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006171 /* first, remove the default value taken from the type */
6172 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6173 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6174 dflt = d_rpl->dflt;
6175 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006176 break;
6177 case LYS_CHOICE:
6178 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006179 if (lys_compile_deviation_set_choice_dflt(ctx, d_rpl->dflt, (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006180 goto cleanup;
6181 }
6182 break;
6183 default:
6184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006185 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006186 goto cleanup;
6187 }
6188 }
6189
6190 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006191 if (d_rpl->flags & LYS_CONFIG_MASK) {
6192 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006194 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6195 goto cleanup;
6196 }
6197 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006198 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006199 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6200 goto cleanup;
6201 }
Radek Krejci327de162019-06-14 12:52:07 +02006202 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006203 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006204
6205 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006206 if (d_rpl->flags & LYS_MAND_MASK) {
6207 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006208 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006209 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6210 goto cleanup;
6211 }
Radek Krejci327de162019-06-14 12:52:07 +02006212 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006213 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006214
6215 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006216 if (d_rpl->flags & LYS_SET_MIN) {
6217 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6218 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6219 /* change value */
6220 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6221 } else if (devs[u]->target->nodetype == LYS_LIST) {
6222 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6223 /* change value */
6224 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6225 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006227 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6228 goto cleanup;
6229 }
6230 if (d_rpl->min) {
6231 devs[u]->target->flags |= LYS_MAND_TRUE;
6232 }
6233 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006234
6235 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006236 if (d_rpl->flags & LYS_SET_MAX) {
6237 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6238 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6239 /* change value */
6240 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6241 } else if (devs[u]->target->nodetype == LYS_LIST) {
6242 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6243 /* change value */
6244 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6245 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006246 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006247 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6248 goto cleanup;
6249 }
6250 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006251
6252 break;
6253 default:
6254 LOGINT(ctx->ctx);
6255 goto cleanup;
6256 }
6257 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006258
Radek Krejci33f72892019-02-21 10:36:58 +01006259 /* final check when all deviations of a single target node are applied */
6260
Radek Krejci551b12c2019-02-19 16:11:21 +01006261 /* check min-max compatibility */
6262 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6263 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6264 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6265 } else if (devs[u]->target->nodetype == LYS_LIST) {
6266 min = ((struct lysc_node_list*)devs[u]->target)->min;
6267 max = ((struct lysc_node_list*)devs[u]->target)->max;
6268 } else {
6269 min = max = 0;
6270 }
6271 if (min > max) {
6272 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
Radek Krejci327de162019-06-14 12:52:07 +02006273 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006274 goto cleanup;
6275 }
6276
Radek Krejcia1911222019-07-22 17:24:50 +02006277 if (dflt) {
6278 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006279 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006280 leaf->dflt->realtype = leaf->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02006281 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
6282 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006283 leaf->dflt->realtype->refcount++;
6284 if (err) {
6285 ly_err_print(err);
6286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6287 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6288 ly_err_free(err);
6289 }
6290 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006291 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006292 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6293 int dynamic;
6294 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006295 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02006296 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6297 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6298 leaf->dflt->realtype = leaf->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02006299 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
6300 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006301 leaf->dflt->realtype->refcount++;
6302 if (err) {
6303 ly_err_print(err);
6304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6305 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6306 ly_err_free(err);
6307 }
6308 if (dynamic) {
6309 free((void*)dflt);
6310 }
6311 LY_CHECK_GOTO(rc, cleanup);
6312 dflt = NULL;
6313 } else { /* LYS_LEAFLIST */
6314 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006315 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LYD_XML, lys_get_prefix, llist->dflts_mods[x], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02006316 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6317 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6318 llist->dflts[x]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02006319 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
6320 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6321 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006322 llist->dflts[x]->realtype->refcount++;
6323 if (err) {
6324 ly_err_print(err);
6325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6326 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6327 dflt, err->msg);
6328 ly_err_free(err);
6329 }
6330 if (dynamic) {
6331 free((void*)dflt);
6332 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006333 dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006334 LY_CHECK_GOTO(rc, cleanup);
6335 }
6336 }
6337 }
6338
Radek Krejci551b12c2019-02-19 16:11:21 +01006339 /* check mandatory - default compatibility */
6340 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6341 && (devs[u]->target->flags & LYS_SET_DFLT)
6342 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006344 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006345 goto cleanup;
6346 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6347 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6348 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006349 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
Radek Krejci551b12c2019-02-19 16:11:21 +01006350 goto cleanup;
6351 }
6352 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6353 /* mandatory node under a default case */
6354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006355 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6356 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006357 goto cleanup;
6358 }
Radek Krejci33f72892019-02-21 10:36:58 +01006359
Radek Krejci327de162019-06-14 12:52:07 +02006360 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006361 }
6362
Radek Krejci327de162019-06-14 12:52:07 +02006363 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006364 ret = LY_SUCCESS;
6365
6366cleanup:
6367 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6368 LY_ARRAY_FREE(devs[u]->deviates);
6369 free(devs[u]);
6370 }
6371 free(devs);
6372 ly_set_erase(&targets, NULL);
6373 ly_set_erase(&devs_p, NULL);
6374
6375 return ret;
6376}
6377
Radek Krejcib56c7502019-02-13 14:19:54 +01006378/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006379 * @brief Compile the given YANG submodule into the main module.
6380 * @param[in] ctx Compile context
6381 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006382 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6383 */
6384LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006385lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006386{
6387 unsigned int u;
6388 LY_ERR ret = LY_SUCCESS;
6389 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006390 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006391 struct lysc_module *mainmod = ctx->mod->compiled;
6392
Radek Krejci0af46292019-01-11 16:02:31 +01006393 if (!mainmod->mod->off_features) {
6394 /* features are compiled directly into the compiled module structure,
6395 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6396 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci327de162019-06-14 12:52:07 +02006397 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01006398 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
6399 LY_CHECK_GOTO(ret, error);
6400 }
6401
Radek Krejci327de162019-06-14 12:52:07 +02006402 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006403 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006404 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006405
Radek Krejci8cce8532019-03-05 11:27:45 +01006406 /* TODO data nodes */
6407
Radek Krejciec4da802019-05-02 13:02:41 +02006408 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6409 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006410
Radek Krejcid05cbd92018-12-05 14:26:40 +01006411error:
6412 return ret;
6413}
6414
Radek Krejci19a96102018-11-15 13:38:09 +01006415LY_ERR
6416lys_compile(struct lys_module *mod, int options)
6417{
6418 struct lysc_ctx ctx = {0};
6419 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006420 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006421 struct lysp_module *sp;
6422 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006423 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006424 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006425 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006426 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006427 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006428
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006429 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006430
6431 if (!mod->implemented) {
6432 /* just imported modules are not compiled */
6433 return LY_SUCCESS;
6434 }
6435
Radek Krejci19a96102018-11-15 13:38:09 +01006436 sp = mod->parsed;
6437
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006438 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006439 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006440 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006441 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006442 ctx.path_len = 1;
6443 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006444
6445 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006446 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6447 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006448
Radek Krejciec4da802019-05-02 13:02:41 +02006449 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006450 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006451 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006452 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6453 }
Radek Krejci0af46292019-01-11 16:02:31 +01006454 if (mod->off_features) {
6455 /* there is already precompiled array of features */
6456 mod_c->features = mod->off_features;
6457 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006458 } else {
6459 /* features are compiled directly into the compiled module structure,
6460 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci327de162019-06-14 12:52:07 +02006461 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006462 LY_CHECK_GOTO(ret, error);
6463 }
6464 /* finish feature compilation, not only for the main module, but also for the submodules.
6465 * Due to possible forward references, it must be done when all the features (including submodules)
6466 * are present. */
6467 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006468 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006469 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6470 }
Radek Krejci327de162019-06-14 12:52:07 +02006471 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006472 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006473 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006474 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006475 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006476 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6477 }
Radek Krejci327de162019-06-14 12:52:07 +02006478 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006479 }
Radek Krejci327de162019-06-14 12:52:07 +02006480 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006481
Radek Krejci327de162019-06-14 12:52:07 +02006482 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006483 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006484 if (sp->identities) {
6485 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6486 }
Radek Krejci327de162019-06-14 12:52:07 +02006487 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006488
Radek Krejci95710c92019-02-11 15:49:55 +01006489 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006490 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006491 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006492 LY_CHECK_GOTO(ret, error);
6493 }
Radek Krejci95710c92019-02-11 15:49:55 +01006494
Radek Krejciec4da802019-05-02 13:02:41 +02006495 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6496 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006497
Radek Krejci95710c92019-02-11 15:49:55 +01006498 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006499 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006500 LY_CHECK_GOTO(ret, error);
6501 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006502 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006503 LY_CHECK_GOTO(ret, error);
6504 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006505
Radek Krejci1c0c3442019-07-23 16:08:47 +02006506 /* finish incomplete default values compilation */
6507 for (u = 0; u < ctx.dflts.count; ++u) {
6508 struct ly_err_item *err = NULL;
6509 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6510 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6511 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6512 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6513 if (err) {
6514 ly_err_print(err);
6515 ctx.path[0] = '\0';
6516 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
6517 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
6518 "Invalid default - value does not fit the type (%s).", err->msg);
6519 ly_err_free(err);
6520 }
6521 LY_CHECK_GOTO(ret, error);
6522 }
6523
Radek Krejciccd20f12019-02-15 14:12:27 +01006524 /* deviations */
Radek Krejciec4da802019-05-02 13:02:41 +02006525 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006526 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006527
Radek Krejciec4da802019-05-02 13:02:41 +02006528 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006529
Radek Krejcia3045382018-11-22 14:30:31 +01006530 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006531 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6532 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6533 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
Radek Krejcia3045382018-11-22 14:30:31 +01006534 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006535 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006536 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6537 if (type->basetype == LY_TYPE_LEAFREF) {
6538 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006539 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type);
Radek Krejcia3045382018-11-22 14:30:31 +01006540 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006541 } else if (type->basetype == LY_TYPE_UNION) {
6542 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6543 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6544 /* validate the path */
6545 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6546 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6547 LY_CHECK_GOTO(ret, error);
6548 }
6549 }
Radek Krejcia3045382018-11-22 14:30:31 +01006550 }
6551 }
6552 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006553 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006554 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006555 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6556 if (type->basetype == LY_TYPE_LEAFREF) {
6557 /* store pointer to the real type */
6558 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6559 typeiter->basetype == LY_TYPE_LEAFREF;
6560 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6561 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006562 } else if (type->basetype == LY_TYPE_UNION) {
6563 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6564 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6565 /* store pointer to the real type */
6566 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6567 typeiter->basetype == LY_TYPE_LEAFREF;
6568 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6569 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6570 }
6571 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006572 }
6573 }
6574 }
Radek Krejciec4da802019-05-02 13:02:41 +02006575
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006576 /* validate non-instantiated groupings from the parsed schema,
6577 * without it we would accept even the schemas with invalid grouping specification */
6578 ctx.options |= LYSC_OPT_GROUPING;
6579 LY_ARRAY_FOR(sp->groupings, u) {
6580 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6581 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6582 }
6583 }
6584 LY_LIST_FOR(sp->data, node_p) {
6585 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6586 LY_ARRAY_FOR(grps, u) {
6587 if (!(grps[u].flags & LYS_USED_GRP)) {
6588 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6589 }
6590 }
6591 }
6592
Radek Krejci1c0c3442019-07-23 16:08:47 +02006593 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006594 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006595 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006596 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006597 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006598
Radek Krejciec4da802019-05-02 13:02:41 +02006599 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006600 lysp_module_free(mod->parsed);
6601 ((struct lys_module*)mod)->parsed = NULL;
6602 }
6603
Radek Krejciec4da802019-05-02 13:02:41 +02006604 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006605 /* remove flag of the modules implemented by dependency */
6606 for (u = 0; u < ctx.ctx->list.count; ++u) {
6607 m = ctx.ctx->list.objs[u];
6608 if (m->implemented == 2) {
6609 m->implemented = 1;
6610 }
6611 }
6612 }
6613
Radek Krejci19a96102018-11-15 13:38:09 +01006614 ((struct lys_module*)mod)->compiled = mod_c;
6615 return LY_SUCCESS;
6616
6617error:
Radek Krejci95710c92019-02-11 15:49:55 +01006618 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02006619 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006620 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006621 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006622 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006623 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006624 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006625 mod->compiled = NULL;
6626
6627 /* revert compilation of modules implemented by dependency */
6628 for (u = 0; u < ctx.ctx->list.count; ++u) {
6629 m = ctx.ctx->list.objs[u];
6630 if (m->implemented == 2) {
6631 /* revert features list to the precompiled state */
6632 lys_feature_precompile_revert(&ctx, m);
6633 /* mark module as imported-only / not-implemented */
6634 m->implemented = 0;
6635 /* free the compiled version of the module */
6636 lysc_module_free(m->compiled, NULL);
6637 m->compiled = NULL;
6638 }
6639 }
6640
Radek Krejci19a96102018-11-15 13:38:09 +01006641 return ret;
6642}