blob: 38b367b88f94615e463603fa798cef7f75f21bb6 [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;
1898 unsigned int u, v, match;
1899 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;
Radek Krejci327de162019-06-14 12:52:07 +02002411 char *logpath;
Radek Krejcia3045382018-11-22 14:30:31 +01002412
2413 assert(ctx);
2414 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002415 assert(leafref);
2416
Radek Krejci327de162019-06-14 12:52:07 +02002417 logpath = lysc_path(startnode, LY_PATH_LOG);
2418 strncpy(ctx->path, logpath, LYSC_CTX_BUFSIZE - 1);
2419 ctx->path_len = strlen(logpath);
2420 free(logpath);
2421
Radek Krejcia3045382018-11-22 14:30:31 +01002422 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002423 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002424 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2425 if (!iter) { /* first iteration */
2426 /* precess ".." in relative paths */
2427 if (parent_times > 0) {
2428 /* move from the context node */
2429 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2430 /* path is supposed to be evaluated in data tree, so we have to skip
2431 * all schema nodes that cannot be instantiated in data tree */
2432 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002433 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002434 }
2435 }
2436 }
2437
2438 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002439 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002440 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002441 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002442 }
2443 if (!mod) {
2444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002445 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2446 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002447 return LY_EVALID;
2448 }
Radek Krejci3d929562019-02-21 11:25:55 +01002449 if (!mod->implemented) {
2450 /* make the module implemented */
2451 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2452 }
Radek Krejcia3045382018-11-22 14:30:31 +01002453
2454 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2455 if (!node) {
2456 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002457 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002458 return LY_EVALID;
2459 }
2460 parent = node;
2461
2462 if (has_predicate) {
2463 /* we have predicate, so the current result must be list */
2464 if (node->nodetype != LYS_LIST) {
2465 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2466 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002467 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002468 return LY_EVALID;
2469 }
2470
Radek Krejcibade82a2018-12-05 10:13:48 +01002471 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2472 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002473 }
2474
2475 ++iter;
2476 }
2477 if (ret) {
2478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002479 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002480 return LY_EVALID;
2481 }
2482
2483 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2485 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002486 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002487 return LY_EVALID;
2488 }
2489
2490 /* check status */
2491 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2492 return LY_EVALID;
2493 }
2494
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002495 /* check config */
2496 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2497 if (node->flags & LYS_CONFIG_R) {
2498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2499 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2500 leafref->path);
2501 return LY_EVALID;
2502 }
2503 }
2504
Radek Krejci412ddfa2018-11-23 11:44:11 +01002505 /* store the target's type and check for circular chain of leafrefs */
2506 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2507 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2508 if (type == (struct lysc_type*)leafref) {
2509 /* circular chain detected */
2510 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2511 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2512 return LY_EVALID;
2513 }
2514 }
2515
Radek Krejci58d171e2018-11-23 13:50:55 +01002516 /* check if leafref and its target are under common if-features */
2517 if (lys_compile_leafref_features_validate(startnode, node)) {
2518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2519 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2520 leafref->path);
2521 return LY_EVALID;
2522 }
2523
Radek Krejci327de162019-06-14 12:52:07 +02002524 ctx->path_len = 1;
2525 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002526 return LY_SUCCESS;
2527}
2528
Radek Krejcicdfecd92018-11-26 11:27:32 +01002529static 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 +02002530 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002531/**
2532 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2533 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002534 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2535 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2536 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2537 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002538 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002539 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002540 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002541 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2542 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2543 * @param[out] type Newly created type structure with the filled information about the type.
2544 * @return LY_ERR value.
2545 */
Radek Krejci19a96102018-11-15 13:38:09 +01002546static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002547lys_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 +02002548 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002549 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002550{
2551 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002552 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002553 struct lysc_type_bin *bin;
2554 struct lysc_type_num *num;
2555 struct lysc_type_str *str;
2556 struct lysc_type_bits *bits;
2557 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002558 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002559 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002560 struct lysc_type_union *un, *un_aux;
2561 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002562
2563 switch (basetype) {
2564 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002566
2567 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002568 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002569 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2570 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002571 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002572 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002573 }
2574 }
2575
2576 if (tpdfname) {
2577 type_p->compiled = *type;
2578 *type = calloc(1, sizeof(struct lysc_type_bin));
2579 }
2580 break;
2581 case LY_TYPE_BITS:
2582 /* RFC 7950 9.7 - bits */
2583 bits = (struct lysc_type_bits*)(*type);
2584 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002585 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002586 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2587 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002588 }
2589
Radek Krejci555cb5b2018-11-16 14:54:33 +01002590 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002592 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002594 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002596 free(*type);
2597 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002598 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002599 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002600 }
2601
2602 if (tpdfname) {
2603 type_p->compiled = *type;
2604 *type = calloc(1, sizeof(struct lysc_type_bits));
2605 }
2606 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002607 case LY_TYPE_DEC64:
2608 dec = (struct lysc_type_dec*)(*type);
2609
2610 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002611 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002612 if (!type_p->fraction_digits) {
2613 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002615 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002617 free(*type);
2618 *type = NULL;
2619 }
2620 return LY_EVALID;
2621 }
2622 } else if (type_p->fraction_digits) {
2623 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002624 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2626 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002627 tpdfname);
2628 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2630 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002631 free(*type);
2632 *type = NULL;
2633 }
2634 return LY_EVALID;
2635 }
2636 dec->fraction_digits = type_p->fraction_digits;
2637
2638 /* RFC 7950 9.2.4 - range */
2639 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002640 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2641 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002642 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002643 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002644 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002645 }
2646
2647 if (tpdfname) {
2648 type_p->compiled = *type;
2649 *type = calloc(1, sizeof(struct lysc_type_dec));
2650 }
2651 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002652 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002653 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002654
2655 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002656 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002657 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2658 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002659 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002660 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002661 }
2662 } else if (base && ((struct lysc_type_str*)base)->length) {
2663 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2664 }
2665
2666 /* RFC 7950 9.4.5 - pattern */
2667 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002668 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002669 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002670 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2671 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2672 }
2673
2674 if (tpdfname) {
2675 type_p->compiled = *type;
2676 *type = calloc(1, sizeof(struct lysc_type_str));
2677 }
2678 break;
2679 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002680 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002681
2682 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002683 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002684 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002685 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002686 }
2687
Radek Krejci555cb5b2018-11-16 14:54:33 +01002688 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002689 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002690 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002692 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002694 free(*type);
2695 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002696 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002697 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002698 }
2699
2700 if (tpdfname) {
2701 type_p->compiled = *type;
2702 *type = calloc(1, sizeof(struct lysc_type_enum));
2703 }
2704 break;
2705 case LY_TYPE_INT8:
2706 case LY_TYPE_UINT8:
2707 case LY_TYPE_INT16:
2708 case LY_TYPE_UINT16:
2709 case LY_TYPE_INT32:
2710 case LY_TYPE_UINT32:
2711 case LY_TYPE_INT64:
2712 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002713 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002714
2715 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002716 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002717 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2718 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002719 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002720 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002721 }
2722 }
2723
2724 if (tpdfname) {
2725 type_p->compiled = *type;
2726 *type = calloc(1, sizeof(struct lysc_type_num));
2727 }
2728 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002729 case LY_TYPE_IDENT:
2730 idref = (struct lysc_type_identityref*)(*type);
2731
2732 /* RFC 7950 9.10.2 - base */
2733 if (type_p->bases) {
2734 if (base) {
2735 /* only the directly derived identityrefs can contain base specification */
2736 if (tpdfname) {
2737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002738 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002739 tpdfname);
2740 } else {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002742 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002743 free(*type);
2744 *type = NULL;
2745 }
2746 return LY_EVALID;
2747 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002748 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002749 }
2750
2751 if (!base && !type_p->flags) {
2752 /* type derived from identityref built-in type must contain at least one base */
2753 if (tpdfname) {
2754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2755 } else {
2756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2757 free(*type);
2758 *type = NULL;
2759 }
2760 return LY_EVALID;
2761 }
2762
2763 if (tpdfname) {
2764 type_p->compiled = *type;
2765 *type = calloc(1, sizeof(struct lysc_type_identityref));
2766 }
2767 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002768 case LY_TYPE_LEAFREF:
2769 /* RFC 7950 9.9.3 - require-instance */
2770 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002771 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002772 if (tpdfname) {
2773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2774 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2775 } else {
2776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2777 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2778 free(*type);
2779 *type = NULL;
2780 }
2781 return LY_EVALID;
2782 }
Radek Krejcia3045382018-11-22 14:30:31 +01002783 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002784 } else if (base) {
2785 /* inherit */
2786 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002787 } else {
2788 /* default is true */
2789 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2790 }
2791 if (type_p->path) {
2792 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002793 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002794 } else if (base) {
2795 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002796 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002797 } else if (tpdfname) {
2798 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2799 return LY_EVALID;
2800 } else {
2801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2802 free(*type);
2803 *type = NULL;
2804 return LY_EVALID;
2805 }
2806 if (tpdfname) {
2807 type_p->compiled = *type;
2808 *type = calloc(1, sizeof(struct lysc_type_leafref));
2809 }
2810 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002811 case LY_TYPE_INST:
2812 /* RFC 7950 9.9.3 - require-instance */
2813 if (type_p->flags & LYS_SET_REQINST) {
2814 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2815 } else {
2816 /* default is true */
2817 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2818 }
2819
2820 if (tpdfname) {
2821 type_p->compiled = *type;
2822 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2823 }
2824 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002825 case LY_TYPE_UNION:
2826 un = (struct lysc_type_union*)(*type);
2827
2828 /* RFC 7950 7.4 - type */
2829 if (type_p->types) {
2830 if (base) {
2831 /* only the directly derived union can contain types specification */
2832 if (tpdfname) {
2833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2834 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2835 tpdfname);
2836 } else {
2837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2838 "Invalid type substatement for the type not directly derived from union built-in type.");
2839 free(*type);
2840 *type = NULL;
2841 }
2842 return LY_EVALID;
2843 }
2844 /* compile the type */
2845 additional = 0;
2846 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2847 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002848 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 +01002849 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2850 /* add space for additional types from the union subtype */
2851 un_aux = (struct lysc_type_union *)un->types[u + additional];
2852 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)));
2853 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2854 un->types = (void*)((uint32_t*)(p) + 1);
2855
2856 /* copy subtypes of the subtype union */
2857 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2858 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2859 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2860 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2861 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2862 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2863 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2864 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2865 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2866 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2867 /* TODO extensions */
2868
2869 } else {
2870 un->types[u + additional] = un_aux->types[v];
2871 ++un_aux->types[v]->refcount;
2872 }
2873 ++additional;
2874 LY_ARRAY_INCREMENT(un->types);
2875 }
2876 /* compensate u increment in main loop */
2877 --additional;
2878
2879 /* free the replaced union subtype */
2880 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2881 } else {
2882 LY_ARRAY_INCREMENT(un->types);
2883 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002884 }
2885 }
2886
2887 if (!base && !type_p->flags) {
2888 /* type derived from union built-in type must contain at least one type */
2889 if (tpdfname) {
2890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2891 } else {
2892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2893 free(*type);
2894 *type = NULL;
2895 }
2896 return LY_EVALID;
2897 }
2898
2899 if (tpdfname) {
2900 type_p->compiled = *type;
2901 *type = calloc(1, sizeof(struct lysc_type_union));
2902 }
2903 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002904 case LY_TYPE_BOOL:
2905 case LY_TYPE_EMPTY:
2906 case LY_TYPE_UNKNOWN: /* just to complete switch */
2907 break;
2908 }
2909 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2910done:
2911 return ret;
2912}
2913
Radek Krejcia3045382018-11-22 14:30:31 +01002914/**
2915 * @brief Compile information about the leaf/leaf-list's type.
2916 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002917 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2918 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2919 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2920 * @param[in] context_name Name of the context node or referencing typedef for logging.
2921 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002922 * @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 +01002923 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002924 * @return LY_ERR value.
2925 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002926static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002927lys_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 +02002928 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002929{
2930 LY_ERR ret = LY_SUCCESS;
2931 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002932 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002933 struct type_context {
2934 const struct lysp_tpdf *tpdf;
2935 struct lysp_node *node;
2936 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002937 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002938 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002939 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002940 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002941 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002942 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002943
2944 (*type) = NULL;
2945
2946 tctx = calloc(1, sizeof *tctx);
2947 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002948 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002949 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2950 ret == LY_SUCCESS;
2951 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2952 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2953 if (basetype) {
2954 break;
2955 }
2956
2957 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002958 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2959 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002960 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2961
Radek Krejcicdfecd92018-11-26 11:27:32 +01002962 if (units && !*units) {
2963 /* inherit units */
2964 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2965 }
Radek Krejci01342af2019-01-03 15:18:08 +01002966 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002967 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002968 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02002969 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002970 }
Radek Krejci01342af2019-01-03 15:18:08 +01002971 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002972 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2973 break;
2974 }
2975
Radek Krejci19a96102018-11-15 13:38:09 +01002976 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002977 /* it is not necessary to continue, the rest of the chain was already compiled,
2978 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002979 basetype = tctx->tpdf->type.compiled->basetype;
2980 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002981 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002982 dummyloops = 1;
2983 goto preparenext;
2984 } else {
2985 tctx = NULL;
2986 break;
2987 }
Radek Krejci19a96102018-11-15 13:38:09 +01002988 }
2989
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002990 /* circular typedef reference detection */
2991 for (u = 0; u < tpdf_chain.count; u++) {
2992 /* local part */
2993 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2994 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2996 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2997 free(tctx);
2998 ret = LY_EVALID;
2999 goto cleanup;
3000 }
3001 }
3002 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3003 /* global part for unions corner case */
3004 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3005 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3006 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3007 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3008 free(tctx);
3009 ret = LY_EVALID;
3010 goto cleanup;
3011 }
3012 }
3013
Radek Krejci19a96102018-11-15 13:38:09 +01003014 /* store information for the following processing */
3015 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3016
Radek Krejcicdfecd92018-11-26 11:27:32 +01003017preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003018 /* prepare next loop */
3019 tctx_prev = tctx;
3020 tctx = calloc(1, sizeof *tctx);
3021 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3022 }
3023 free(tctx);
3024
3025 /* allocate type according to the basetype */
3026 switch (basetype) {
3027 case LY_TYPE_BINARY:
3028 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003029 break;
3030 case LY_TYPE_BITS:
3031 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003032 break;
3033 case LY_TYPE_BOOL:
3034 case LY_TYPE_EMPTY:
3035 *type = calloc(1, sizeof(struct lysc_type));
3036 break;
3037 case LY_TYPE_DEC64:
3038 *type = calloc(1, sizeof(struct lysc_type_dec));
3039 break;
3040 case LY_TYPE_ENUM:
3041 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003042 break;
3043 case LY_TYPE_IDENT:
3044 *type = calloc(1, sizeof(struct lysc_type_identityref));
3045 break;
3046 case LY_TYPE_INST:
3047 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3048 break;
3049 case LY_TYPE_LEAFREF:
3050 *type = calloc(1, sizeof(struct lysc_type_leafref));
3051 break;
3052 case LY_TYPE_STRING:
3053 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003054 break;
3055 case LY_TYPE_UNION:
3056 *type = calloc(1, sizeof(struct lysc_type_union));
3057 break;
3058 case LY_TYPE_INT8:
3059 case LY_TYPE_UINT8:
3060 case LY_TYPE_INT16:
3061 case LY_TYPE_UINT16:
3062 case LY_TYPE_INT32:
3063 case LY_TYPE_UINT32:
3064 case LY_TYPE_INT64:
3065 case LY_TYPE_UINT64:
3066 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003067 break;
3068 case LY_TYPE_UNKNOWN:
3069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3070 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3071 ret = LY_EVALID;
3072 goto cleanup;
3073 }
3074 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003075 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3077 ly_data_type2str[basetype]);
3078 free(*type);
3079 (*type) = NULL;
3080 ret = LY_EVALID;
3081 goto cleanup;
3082 }
3083
3084 /* get restrictions from the referred typedefs */
3085 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3086 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003087
3088 /* remember the typedef context for circular check */
3089 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3090
Radek Krejci43699232018-11-23 14:59:46 +01003091 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003092 base = tctx->tpdf->type.compiled;
3093 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003094 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003095 /* no change, just use the type information from the base */
3096 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3097 ++base->refcount;
3098 continue;
3099 }
3100
3101 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003102 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3104 tctx->tpdf->name, ly_data_type2str[basetype]);
3105 ret = LY_EVALID;
3106 goto cleanup;
3107 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3108 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3109 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3110 tctx->tpdf->name, tctx->tpdf->dflt);
3111 ret = LY_EVALID;
3112 goto cleanup;
3113 }
3114
Radek Krejci19a96102018-11-15 13:38:09 +01003115 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003116 /* TODO user type plugins */
3117 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003118 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003119 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 +01003120 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003121 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003122 LY_CHECK_GOTO(ret, cleanup);
3123 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003124 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003125 /* remove the processed typedef contexts from the stack for circular check */
3126 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003127
Radek Krejcic5c27e52018-11-15 14:38:11 +01003128 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003129 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003130 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003131 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003132 /* TODO user type plugins */
3133 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003134 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003135 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 +01003136 LY_CHECK_GOTO(ret, cleanup);
3137 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003138 /* no specific restriction in leaf's type definition, copy from the base */
3139 free(*type);
3140 (*type) = base;
3141 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003142 }
Radek Krejcia1911222019-07-22 17:24:50 +02003143 if (dflt && !(*type)->dflt) {
3144 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003145 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003146 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3147 (*type)->dflt->realtype = (*type);
3148 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3149 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003150 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003151 if (err) {
3152 ly_err_print(err);
3153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3154 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3155 ly_err_free(err);
3156 }
3157 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003158 }
Radek Krejci19a96102018-11-15 13:38:09 +01003159
Radek Krejciec4da802019-05-02 13:02:41 +02003160 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003161
3162cleanup:
3163 ly_set_erase(&tpdf_chain, free);
3164 return ret;
3165}
3166
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003167/**
3168 * @brief Compile status information of the given node.
3169 *
3170 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3171 * has the status correctly set during the compilation.
3172 *
3173 * @param[in] ctx Compile context
3174 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3175 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3176 * the compatibility with the parent's status value.
3177 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3178 * @return LY_ERR value.
3179 */
3180static LY_ERR
3181lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3182{
3183 /* status - it is not inherited by specification, but it does not make sense to have
3184 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3185 if (!((*node_flags) & LYS_STATUS_MASK)) {
3186 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3187 if ((parent_flags & 0x3) != 0x3) {
3188 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3189 * combination of bits (0x3) which marks the uses_status value */
3190 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3191 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3192 }
3193 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3194 } else {
3195 (*node_flags) |= LYS_STATUS_CURR;
3196 }
3197 } else if (parent_flags & LYS_STATUS_MASK) {
3198 /* check status compatibility with the parent */
3199 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3200 if ((*node_flags) & LYS_STATUS_CURR) {
3201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3202 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3203 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3204 } else { /* LYS_STATUS_DEPRC */
3205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3206 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3207 }
3208 return LY_EVALID;
3209 }
3210 }
3211 return LY_SUCCESS;
3212}
3213
Radek Krejci8cce8532019-03-05 11:27:45 +01003214/**
3215 * @brief Check uniqness of the node/action/notification name.
3216 *
3217 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3218 * structures, but they share the namespace so we need to check their name collisions.
3219 *
3220 * @param[in] ctx Compile context.
3221 * @param[in] children List (linked list) of data nodes to go through.
3222 * @param[in] actions List (sized array) of actions or RPCs to go through.
3223 * @param[in] notifs List (sized array) of Notifications to go through.
3224 * @param[in] name Name of the item to find in the given lists.
3225 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3226 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3227 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3228 */
3229static LY_ERR
3230lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3231 const struct lysc_action *actions, const struct lysc_notif *notifs,
3232 const char *name, void *exclude)
3233{
3234 const struct lysc_node *iter;
3235 unsigned int u;
3236
3237 LY_LIST_FOR(children, iter) {
3238 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3239 goto error;
3240 }
3241 }
3242 LY_ARRAY_FOR(actions, u) {
3243 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3244 goto error;
3245 }
3246 }
3247 LY_ARRAY_FOR(notifs, u) {
3248 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3249 goto error;
3250 }
3251 }
3252 return LY_SUCCESS;
3253error:
3254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3255 return LY_EEXIST;
3256}
3257
Radek Krejciec4da802019-05-02 13:02:41 +02003258static 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 +01003259
Radek Krejcia3045382018-11-22 14:30:31 +01003260/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003261 * @brief Compile parsed RPC/action schema node information.
3262 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003263 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003264 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003265 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3266 * @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).
3267 * Zero means no uses, non-zero value with no status bit set mean the default status.
3268 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3269 */
3270static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003271lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003272 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3273{
3274 LY_ERR ret = LY_SUCCESS;
3275 struct lysp_node *child_p;
3276 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003277 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003278
Radek Krejci327de162019-06-14 12:52:07 +02003279 lysc_update_path(ctx, parent, action_p->name);
3280
Radek Krejci8cce8532019-03-05 11:27:45 +01003281 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3282 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3283 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3284 action_p->name, action)) {
3285 return LY_EVALID;
3286 }
3287
Radek Krejciec4da802019-05-02 13:02:41 +02003288 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003290 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003291 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003292 return LY_EVALID;
3293 }
3294
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003295 action->nodetype = LYS_ACTION;
3296 action->module = ctx->mod;
3297 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003298 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003299 action->sp = action_p;
3300 }
3301 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3302
3303 /* status - it is not inherited by specification, but it does not make sense to have
3304 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3305 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3306
3307 DUP_STRING(ctx->ctx, action_p->name, action->name);
3308 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3309 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003310 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3311 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003312
3313 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003314 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003315 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3316 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3317 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003318 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003319 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003320 }
Radek Krejci327de162019-06-14 12:52:07 +02003321 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003322 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003323
3324 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003325 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003326 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3327 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3328 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003329 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003330 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003331 }
Radek Krejci327de162019-06-14 12:52:07 +02003332 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003333
Radek Krejci327de162019-06-14 12:52:07 +02003334 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003335cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003336 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003337 return ret;
3338}
3339
3340/**
Radek Krejci43981a32019-04-12 09:44:11 +02003341 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003342 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003343 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003344 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3345 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3346 * @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 +02003347 * Zero means no uses, non-zero value with no status bit set mean the default status.
3348 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3349 */
3350static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003351lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003352 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3353{
3354 LY_ERR ret = LY_SUCCESS;
3355 struct lysp_node *child_p;
3356 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003357 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003358
Radek Krejci327de162019-06-14 12:52:07 +02003359 lysc_update_path(ctx, parent, notif_p->name);
3360
Radek Krejcifc11bd72019-04-11 16:00:05 +02003361 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3362 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3363 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3364 notif_p->name, notif)) {
3365 return LY_EVALID;
3366 }
3367
Radek Krejciec4da802019-05-02 13:02:41 +02003368 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003369 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3370 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003371 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003372 return LY_EVALID;
3373 }
3374
3375 notif->nodetype = LYS_NOTIF;
3376 notif->module = ctx->mod;
3377 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003378 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003379 notif->sp = notif_p;
3380 }
3381 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3382
3383 /* status - it is not inherited by specification, but it does not make sense to have
3384 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3385 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3386
3387 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3388 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3389 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003390 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3391 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3392 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003393
Radek Krejciec4da802019-05-02 13:02:41 +02003394 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003395 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003396 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003397 }
3398
Radek Krejci327de162019-06-14 12:52:07 +02003399 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003400cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003401 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003402 return ret;
3403}
3404
3405/**
Radek Krejcia3045382018-11-22 14:30:31 +01003406 * @brief Compile parsed container node information.
3407 * @param[in] ctx Compile context
3408 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003409 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3410 * is enriched with the container-specific information.
3411 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3412 */
Radek Krejci19a96102018-11-15 13:38:09 +01003413static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003414lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003415{
3416 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3417 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3418 struct lysp_node *child_p;
3419 unsigned int u;
3420 LY_ERR ret = LY_SUCCESS;
3421
Radek Krejcife909632019-02-12 15:34:42 +01003422 if (cont_p->presence) {
3423 cont->flags |= LYS_PRESENCE;
3424 }
3425
Radek Krejci19a96102018-11-15 13:38:09 +01003426 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003427 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003428 }
3429
Radek Krejciec4da802019-05-02 13:02:41 +02003430 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3431 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3432 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003433
3434done:
3435 return ret;
3436}
3437
Radek Krejci33f72892019-02-21 10:36:58 +01003438/*
3439 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3440 * @param[in] ctx Compile context.
3441 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3442 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003443 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3444 * @return LY_ERR value.
3445 */
3446static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003447lys_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 +01003448{
Radek Krejcia1911222019-07-22 17:24:50 +02003449 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003450 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3451
Radek Krejciec4da802019-05-02 13:02:41 +02003452 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 +01003453 leaf->units ? NULL : &leaf->units));
3454 if (leaf->nodetype == LYS_LEAFLIST) {
3455 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003456 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3457 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003458 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003459 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3460 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3461 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3462 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003463 LY_ARRAY_INCREMENT(llist->dflts);
3464 }
3465 } else {
3466 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003467 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003468 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3469 leaf->dflt->realtype = leaf->type->dflt->realtype;
3470 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3471 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003472 }
3473 }
3474 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3475 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3476 ly_set_add(&ctx->unres, leaf, 0);
3477 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3478 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3479 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3480 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3481 ly_set_add(&ctx->unres, leaf, 0);
3482 }
3483 }
3484 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003485 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3487 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3488 return LY_EVALID;
3489 }
3490 }
3491
Radek Krejci33f72892019-02-21 10:36:58 +01003492 return LY_SUCCESS;
3493}
3494
Radek Krejcia3045382018-11-22 14:30:31 +01003495/**
3496 * @brief Compile parsed leaf node information.
3497 * @param[in] ctx Compile context
3498 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003499 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3500 * is enriched with the leaf-specific information.
3501 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3502 */
Radek Krejci19a96102018-11-15 13:38:09 +01003503static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003504lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003505{
3506 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3507 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3508 unsigned int u;
3509 LY_ERR ret = LY_SUCCESS;
3510
Radek Krejciec4da802019-05-02 13:02:41 +02003511 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003512 if (leaf_p->units) {
3513 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3514 leaf->flags |= LYS_SET_UNITS;
3515 }
Radek Krejcia1911222019-07-22 17:24:50 +02003516
3517 /* the dflt member is just filled to avoid getting the default value from the type */
3518 leaf->dflt = (void*)leaf_p->dflt;
3519 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3520
Radek Krejciccd20f12019-02-15 14:12:27 +01003521 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003522 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003523 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003524 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3525 leaf->dflt->realtype = leaf->type;
3526 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3527 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003528 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, NULL, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003529 leaf->dflt->realtype->refcount++;
3530 if (err) {
3531 ly_err_print(err);
3532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3533 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3534 ly_err_free(err);
3535 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003536 if (ret == LY_EINCOMPLETE) {
3537 /* expected for some types with incomplete data */
3538 ret = LY_SUCCESS;
3539 }
Radek Krejcia1911222019-07-22 17:24:50 +02003540 LY_CHECK_GOTO(ret, done);
Radek Krejci76b3e962018-12-14 17:01:25 +01003541 leaf->flags |= LYS_SET_DFLT;
3542 }
Radek Krejci43699232018-11-23 14:59:46 +01003543
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003544
Radek Krejci19a96102018-11-15 13:38:09 +01003545done:
3546 return ret;
3547}
3548
Radek Krejcia3045382018-11-22 14:30:31 +01003549/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003550 * @brief Compile parsed leaf-list node information.
3551 * @param[in] ctx Compile context
3552 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003553 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3554 * is enriched with the leaf-list-specific information.
3555 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3556 */
3557static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003558lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003559{
3560 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3561 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003562 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003563 LY_ERR ret = LY_SUCCESS;
3564
Radek Krejciec4da802019-05-02 13:02:41 +02003565 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003566 if (llist_p->units) {
3567 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3568 llist->flags |= LYS_SET_UNITS;
3569 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003570
Radek Krejcia1911222019-07-22 17:24:50 +02003571 /* the dflts member is just filled to avoid getting the default value from the type */
3572 llist->dflts = (void*)llist_p->dflts;
3573 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003574 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003575 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003576 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003577 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3578 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003579 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003580 LY_ARRAY_INCREMENT(llist->dflts_mods);
3581 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003582 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003583 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3584 llist->dflts[u]->realtype = llist->type;
3585 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3586 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003587 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, NULL, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003588 llist->dflts[u]->realtype->refcount++;
3589 if (err) {
3590 ly_err_print(err);
3591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3592 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3593 ly_err_free(err);
3594 }
3595 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003596 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003597 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003598 }
Radek Krejcia1911222019-07-22 17:24:50 +02003599 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3600 /* configuration data values must be unique - so check the default values */
3601 LY_ARRAY_FOR(llist->dflts, u) {
3602 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3603 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3604 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003605 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 +02003606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3607 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3608 if (dynamic) {
3609 free((char*)val);
3610 }
3611 return LY_EVALID;
3612 }
3613 }
3614 }
3615 }
3616
3617 /* 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 +01003618
3619 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003620 if (llist->min) {
3621 llist->flags |= LYS_MAND_TRUE;
3622 }
Radek Krejcib7408632018-11-28 17:12:11 +01003623 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003624
Radek Krejci0e5d8382018-11-28 16:37:53 +01003625done:
3626 return ret;
3627}
3628
3629/**
Radek Krejci7af64242019-02-18 13:07:53 +01003630 * @brief Compile information about list's uniques.
3631 * @param[in] ctx Compile context.
3632 * @param[in] context_module Module where the prefixes are going to be resolved.
3633 * @param[in] uniques Sized array list of unique statements.
3634 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3635 * @return LY_ERR value.
3636 */
3637static LY_ERR
3638lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3639{
3640 LY_ERR ret = LY_SUCCESS;
3641 struct lysc_node_leaf **key, ***unique;
3642 const char *keystr, *delim;
3643 size_t len;
3644 unsigned int v;
3645 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003646 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003647
3648 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3649 config = -1;
3650 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3651 keystr = uniques[v];
3652 while (keystr) {
3653 delim = strpbrk(keystr, " \t\n");
3654 if (delim) {
3655 len = delim - keystr;
3656 while (isspace(*delim)) {
3657 ++delim;
3658 }
3659 } else {
3660 len = strlen(keystr);
3661 }
3662
3663 /* unique node must be present */
3664 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003665 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3666 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003667 if (ret != LY_SUCCESS) {
3668 if (ret == LY_EDENIED) {
3669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003670 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003671 len, keystr, lys_nodetype2str((*key)->nodetype));
3672 }
3673 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003674 } else if (flags) {
3675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3676 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3677 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3678 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003679 }
3680
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003681
Radek Krejci7af64242019-02-18 13:07:53 +01003682 /* all referenced leafs must be of the same config type */
3683 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3685 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3686 return LY_EVALID;
3687 } else if ((*key)->flags & LYS_CONFIG_W) {
3688 config = 1;
3689 } else { /* LYS_CONFIG_R */
3690 config = 0;
3691 }
3692
3693 /* check status */
3694 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3695 (*key)->flags, (*key)->module, (*key)->name));
3696
3697 /* mark leaf as unique */
3698 (*key)->flags |= LYS_UNIQUE;
3699
3700 /* next unique value in line */
3701 keystr = delim;
3702 }
3703 /* next unique definition */
3704 }
3705
3706 return LY_SUCCESS;
3707}
3708
3709/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003710 * @brief Compile parsed list node information.
3711 * @param[in] ctx Compile context
3712 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003713 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3714 * is enriched with the list-specific information.
3715 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3716 */
3717static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003718lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003719{
3720 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3721 struct lysc_node_list *list = (struct lysc_node_list*)node;
3722 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003723 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003724 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003725 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003726 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 LY_ERR ret = LY_SUCCESS;
3728
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003729 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003730 if (list->min) {
3731 list->flags |= LYS_MAND_TRUE;
3732 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003733 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3734
3735 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003736 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003737 }
3738
Radek Krejciec4da802019-05-02 13:02:41 +02003739 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003740
3741 /* keys */
3742 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3744 return LY_EVALID;
3745 }
3746
3747 /* find all the keys (must be direct children) */
3748 keystr = list_p->key;
3749 while (keystr) {
3750 delim = strpbrk(keystr, " \t\n");
3751 if (delim) {
3752 len = delim - keystr;
3753 while (isspace(*delim)) {
3754 ++delim;
3755 }
3756 } else {
3757 len = strlen(keystr);
3758 }
3759
3760 /* key node must be present */
3761 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3762 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3763 if (!(*key)) {
3764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3765 "The list's key \"%.*s\" not found.", len, keystr);
3766 return LY_EVALID;
3767 }
3768 /* keys must be unique */
3769 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3770 if (*key == list->keys[u]) {
3771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3772 "Duplicated key identifier \"%.*s\".", len, keystr);
3773 return LY_EVALID;
3774 }
3775 }
Radek Krejci327de162019-06-14 12:52:07 +02003776 lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003777 /* key must have the same config flag as the list itself */
3778 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3780 return LY_EVALID;
3781 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003782 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003783 /* YANG 1.0 denies key to be of empty type */
3784 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003786 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003787 return LY_EVALID;
3788 }
3789 } else {
3790 /* when and if-feature are illegal on list keys */
3791 if ((*key)->when) {
3792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003793 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003794 return LY_EVALID;
3795 }
3796 if ((*key)->iffeatures) {
3797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003798 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003799 return LY_EVALID;
3800 }
3801 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003802
3803 /* check status */
3804 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3805 (*key)->flags, (*key)->module, (*key)->name));
3806
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003807 /* ignore default values of the key */
3808 if ((*key)->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003809 (*key)->dflt->realtype->plugin->free(ctx->ctx, (*key)->dflt);
3810 lysc_type_free(ctx->ctx, (*key)->dflt->realtype);
3811 free((*key)->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003812 (*key)->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003813 (*key)->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003814 }
3815 /* mark leaf as key */
3816 (*key)->flags |= LYS_KEY;
3817
3818 /* next key value */
3819 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003820 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003821 }
3822
3823 /* uniques */
3824 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003825 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003826 }
3827
Radek Krejciec4da802019-05-02 13:02:41 +02003828 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3829 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003830
3831done:
3832 return ret;
3833}
3834
Radek Krejcib56c7502019-02-13 14:19:54 +01003835/**
3836 * @brief Do some checks and set the default choice's case.
3837 *
3838 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3839 *
3840 * @param[in] ctx Compile context.
3841 * @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,
3842 * not the reference to the imported module.
3843 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3844 * @return LY_ERR value.
3845 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003846static LY_ERR
3847lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3848{
3849 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3850 const char *prefix = NULL, *name;
3851 size_t prefix_len = 0;
3852
3853 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3854 name = strchr(dflt, ':');
3855 if (name) {
3856 prefix = dflt;
3857 prefix_len = name - prefix;
3858 ++name;
3859 } else {
3860 name = dflt;
3861 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003862 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003863 /* prefixed default case make sense only for the prefix of the schema itself */
3864 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3865 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3866 prefix_len, prefix);
3867 return LY_EVALID;
3868 }
3869 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3870 if (!ch->dflt) {
3871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3872 "Default case \"%s\" not found.", dflt);
3873 return LY_EVALID;
3874 }
3875 /* no mandatory nodes directly under the default case */
3876 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003877 if (iter->parent != (struct lysc_node*)ch->dflt) {
3878 break;
3879 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003880 if (iter->flags & LYS_MAND_TRUE) {
3881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3882 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3883 return LY_EVALID;
3884 }
3885 }
Radek Krejci01342af2019-01-03 15:18:08 +01003886 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003887 return LY_SUCCESS;
3888}
3889
Radek Krejciccd20f12019-02-15 14:12:27 +01003890static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003891lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003892{
3893 struct lys_module *mod;
3894 const char *prefix = NULL, *name;
3895 size_t prefix_len = 0;
3896 struct lysc_node_case *cs;
3897 struct lysc_node *node;
3898
3899 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3900 name = strchr(dflt, ':');
3901 if (name) {
3902 prefix = dflt;
3903 prefix_len = name - prefix;
3904 ++name;
3905 } else {
3906 name = dflt;
3907 }
3908 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3909 if (prefix) {
3910 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003912 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3913 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003914 return LY_EVALID;
3915 }
3916 } else {
3917 mod = ctx->mod;
3918 }
3919 /* get the default case */
3920 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3921 if (!cs) {
3922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003923 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003924 return LY_EVALID;
3925 }
3926
3927 /* check that there is no mandatory node */
3928 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003929 if (node->parent != (struct lysc_node*)cs) {
3930 break;
3931 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003932 if (node->flags & LYS_MAND_TRUE) {
3933 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003934 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3935 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003936 return LY_EVALID;
3937 }
3938 }
3939
3940 /* set the default case in choice */
3941 ch->dflt = cs;
3942 cs->flags |= LYS_SET_DFLT;
3943
3944 return LY_SUCCESS;
3945}
3946
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003947/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003948 * @brief Compile parsed choice node information.
3949 * @param[in] ctx Compile context
3950 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003951 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003952 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003953 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3954 */
3955static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003956lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003957{
3958 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3959 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3960 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003961 LY_ERR ret = LY_SUCCESS;
3962
Radek Krejci056d0a82018-12-06 16:57:25 +01003963 LY_LIST_FOR(ch_p->child, child_p) {
3964 if (child_p->nodetype == LYS_CASE) {
3965 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003966 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003967 }
3968 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003969 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003970 }
3971 }
3972
3973 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003974 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003975 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003976 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003977
Radek Krejci9800fb82018-12-13 14:26:23 +01003978 return ret;
3979}
3980
3981/**
3982 * @brief Compile parsed anydata or anyxml node information.
3983 * @param[in] ctx Compile context
3984 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01003985 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3986 * is enriched with the any-specific information.
3987 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3988 */
3989static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003990lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01003991{
3992 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3993 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3994 unsigned int u;
3995 LY_ERR ret = LY_SUCCESS;
3996
Radek Krejciec4da802019-05-02 13:02:41 +02003997 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01003998
3999 if (any->flags & LYS_CONFIG_W) {
4000 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
4001 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
4002 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004003done:
4004 return ret;
4005}
4006
Radek Krejcib56c7502019-02-13 14:19:54 +01004007/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004008 * @brief Connect the node into the siblings list and check its name uniqueness.
4009 *
4010 * @param[in] ctx Compile context
4011 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4012 * the choice itself is expected instead of a specific case node.
4013 * @param[in] node Schema node to connect into the list.
4014 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4015 */
4016static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004017lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004018{
4019 struct lysc_node **children;
4020
4021 if (node->nodetype == LYS_CASE) {
4022 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4023 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004024 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004025 }
4026 if (children) {
4027 if (!(*children)) {
4028 /* first child */
4029 *children = node;
4030 } else if (*children != node) {
4031 /* by the condition in previous branch we cover the choice/case children
4032 * - the children list is shared by the choice and the the first case, in addition
4033 * the first child of each case must be referenced from the case node. So the node is
4034 * actually always already inserted in case it is the first children - so here such
4035 * a situation actually corresponds to the first branch */
4036 /* insert at the end of the parent's children list */
4037 (*children)->prev->next = node;
4038 node->prev = (*children)->prev;
4039 (*children)->prev = node;
4040
4041 /* check the name uniqueness */
4042 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4043 lysc_node_notifs(parent), node->name, node)) {
4044 return LY_EEXIST;
4045 }
4046 }
4047 }
4048 return LY_SUCCESS;
4049}
4050
Radek Krejci95710c92019-02-11 15:49:55 +01004051/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004052 * @brief Get the XPath context node for the given schema node.
4053 * @param[in] start The schema node where the XPath expression appears.
4054 * @return The context node to evaluate XPath expression in given schema node.
4055 * @return NULL in case the context node is the root node.
4056 */
4057static struct lysc_node *
4058lysc_xpath_context(struct lysc_node *start)
4059{
4060 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4061 start = start->parent);
4062 return start;
4063}
4064
4065/**
4066 * @brief Prepare the case structure in choice node for the new data node.
4067 *
4068 * 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
4069 * created in the choice when the first child was processed.
4070 *
4071 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004072 * @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,
4073 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004074 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4075 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4076 * @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,
4077 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004078 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004079static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004080lys_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 +01004081{
4082 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004083 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004084 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004085 unsigned int u;
4086 LY_ERR ret;
4087
Radek Krejci95710c92019-02-11 15:49:55 +01004088#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004089 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004090 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004091 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4092 return NULL; \
4093 } \
4094 }
4095
Radek Krejci95710c92019-02-11 15:49:55 +01004096 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4097 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004098
4099 /* we have to add an implicit case node into the parent choice */
4100 cs = calloc(1, sizeof(struct lysc_node_case));
4101 DUP_STRING(ctx->ctx, child->name, cs->name);
4102 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004103 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004104 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4105 /* the case is already present since the child is not its first children */
4106 return (struct lysc_node_case*)ch->cases->prev;
4107 }
Radek Krejci95710c92019-02-11 15:49:55 +01004108 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004109
4110 /* explicit parent case is not present (this is its first child) */
4111 cs = calloc(1, sizeof(struct lysc_node_case));
4112 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4113 cs->flags = LYS_STATUS_MASK & node_p->flags;
4114 cs->sp = node_p;
4115
Radek Krejcib1b59152019-01-07 13:21:56 +01004116 /* 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 +02004117 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004118
4119 if (node_p->when) {
4120 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004121 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004122 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004123 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004124 }
Radek Krejciec4da802019-05-02 13:02:41 +02004125 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004126 } else {
4127 LOGINT(ctx->ctx);
4128 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004129 }
4130 cs->module = ctx->mod;
4131 cs->prev = (struct lysc_node*)cs;
4132 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004133 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004134 cs->parent = (struct lysc_node*)ch;
4135 cs->child = child;
4136
4137 return cs;
4138error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004139 if (cs) {
4140 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4141 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004142 return NULL;
4143
4144#undef UNIQUE_CHECK
4145}
4146
Radek Krejcib56c7502019-02-13 14:19:54 +01004147/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004148 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004149 *
4150 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004151 * @param[in] node Target node where the config is supposed to be changed.
4152 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004153 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4154 * 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 +01004155 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4156 * @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 +01004157 * @return LY_ERR value.
4158 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004159static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004160lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004161 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004162{
4163 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004164 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004165
4166 if (config == (node->flags & LYS_CONFIG_MASK)) {
4167 /* nothing to do */
4168 return LY_SUCCESS;
4169 }
4170
4171 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004172 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004173 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004175 "Invalid %s of config - configuration node cannot be child of any state data node.",
4176 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004177 return LY_EVALID;
4178 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004179 node->flags |= LYS_SET_CONFIG;
4180 } else {
4181 if (node->flags & LYS_SET_CONFIG) {
4182 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4183 /* setting config flags, but have node with explicit config true */
4184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004185 "Invalid %s of config - configuration node cannot be child of any state data node.",
4186 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004187 return LY_EVALID;
4188 }
4189 /* do not change config on nodes where the config is explicitely set, this does not apply to
4190 * nodes, which are being changed explicitly (targets of refine or deviation) */
4191 return LY_SUCCESS;
4192 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004193 }
4194 node->flags &= ~LYS_CONFIG_MASK;
4195 node->flags |= config;
4196
4197 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004198 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004199 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004200 }
4201
Radek Krejci76b3e962018-12-14 17:01:25 +01004202 return LY_SUCCESS;
4203}
4204
Radek Krejcib56c7502019-02-13 14:19:54 +01004205/**
4206 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4207 *
4208 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4209 * the flag to such parents from a mandatory children.
4210 *
4211 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4212 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4213 * (mandatory children was removed).
4214 */
Radek Krejcife909632019-02-12 15:34:42 +01004215void
4216lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4217{
4218 struct lysc_node *iter;
4219
4220 if (add) { /* set flag */
4221 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4222 parent = parent->parent) {
4223 parent->flags |= LYS_MAND_TRUE;
4224 }
4225 } else { /* unset flag */
4226 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004227 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004228 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004229 /* there is another mandatory node */
4230 return;
4231 }
4232 }
4233 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4234 parent->flags &= ~LYS_MAND_TRUE;
4235 }
4236 }
4237}
4238
Radek Krejci056d0a82018-12-06 16:57:25 +01004239/**
Radek Krejci3641f562019-02-13 15:38:40 +01004240 * @brief Internal sorting process for the lys_compile_augment_sort().
4241 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4242 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4243 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4244 */
4245static void
4246lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4247{
4248 unsigned int v;
4249 size_t len;
4250
4251 len = strlen(aug_p->nodeid);
4252 LY_ARRAY_FOR(result, v) {
4253 if (strlen(result[v]->nodeid) <= len) {
4254 continue;
4255 }
4256 if (v < LY_ARRAY_SIZE(result)) {
4257 /* move the rest of array */
4258 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4259 break;
4260 }
4261 }
4262 result[v] = aug_p;
4263 LY_ARRAY_INCREMENT(result);
4264}
4265
4266/**
4267 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4268 *
4269 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4270 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4271 *
4272 * @param[in] ctx Compile context.
4273 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4274 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4275 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4276 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4277 * @return LY_ERR value.
4278 */
4279LY_ERR
4280lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4281{
4282 struct lysp_augment **result = NULL;
4283 unsigned int u, v;
4284 size_t count = 0;
4285
4286 assert(augments);
4287
4288 /* get count of the augments in module and all its submodules */
4289 if (aug_p) {
4290 count += LY_ARRAY_SIZE(aug_p);
4291 }
4292 LY_ARRAY_FOR(inc_p, u) {
4293 if (inc_p[u].submodule->augments) {
4294 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4295 }
4296 }
4297
4298 if (!count) {
4299 *augments = NULL;
4300 return LY_SUCCESS;
4301 }
4302 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4303
4304 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4305 * together, so there can be even /z/y betwwen them. */
4306 LY_ARRAY_FOR(aug_p, u) {
4307 lys_compile_augment_sort_(&aug_p[u], result);
4308 }
4309 LY_ARRAY_FOR(inc_p, u) {
4310 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4311 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4312 }
4313 }
4314
4315 *augments = result;
4316 return LY_SUCCESS;
4317}
4318
4319/**
4320 * @brief Compile the parsed augment connecting it into its target.
4321 *
4322 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4323 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4324 * are already implemented and compiled.
4325 *
4326 * @param[in] ctx Compile context.
4327 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004328 * @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
4329 * children in case of the augmenting uses data.
4330 * @return LY_SUCCESS on success.
4331 * @return LY_EVALID on failure.
4332 */
4333LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004334lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004335{
4336 LY_ERR ret = LY_SUCCESS;
4337 struct lysp_node *node_p, *case_node_p;
4338 struct lysc_node *target; /* target target of the augment */
4339 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004340 struct lysc_when **when, *when_shared;
4341 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004342 uint16_t flags = 0;
4343 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004344 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004345
Radek Krejci327de162019-06-14 12:52:07 +02004346 lysc_update_path(ctx, NULL, "{augment}");
4347 lysc_update_path(ctx, NULL, aug_p->nodeid);
4348
Radek Krejci7af64242019-02-18 13:07:53 +01004349 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004350 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004351 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004352 if (ret != LY_SUCCESS) {
4353 if (ret == LY_EDENIED) {
4354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4355 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4356 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4357 }
4358 return LY_EVALID;
4359 }
4360
4361 /* check for mandatory nodes
4362 * - new cases augmenting some choice can have mandatory nodes
4363 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4364 */
Radek Krejci733988a2019-02-15 15:12:44 +01004365 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004366 allow_mandatory = 1;
4367 }
4368
4369 when_shared = NULL;
4370 LY_LIST_FOR(aug_p->child, node_p) {
4371 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4372 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4373 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004374 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4375 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004376 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004377 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4378 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004379 return LY_EVALID;
4380 }
4381
4382 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004383 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004384 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004385 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004386 } else {
4387 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004388 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004389 }
4390 }
Radek Krejciec4da802019-05-02 13:02:41 +02004391 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004392
Radek Krejcife13da42019-02-15 14:51:01 +01004393 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4394 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004395 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004396 /* 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 +01004397 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 +01004398 } else if (target->nodetype == LYS_CHOICE) {
4399 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4400 node = ((struct lysc_node_choice*)target)->cases->prev;
4401 } else {
4402 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004403 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004404 }
4405
Radek Krejci733988a2019-02-15 15:12:44 +01004406 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004407 node->flags &= ~LYS_MAND_TRUE;
4408 lys_compile_mandatory_parents(target, 0);
4409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004410 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004411 return LY_EVALID;
4412 }
4413
4414 /* pass augment's when to all the children */
4415 if (aug_p->when) {
4416 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4417 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004418 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004419 LY_CHECK_GOTO(ret, error);
4420 (*when)->context = lysc_xpath_context(target);
4421 when_shared = *when;
4422 } else {
4423 ++when_shared->refcount;
4424 (*when) = when_shared;
4425 }
4426 }
4427 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004428
Radek Krejciec4da802019-05-02 13:02:41 +02004429 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004430 switch (target->nodetype) {
4431 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004432 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004433 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004434 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004435 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004436 break;
4437 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004438 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004439 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004440 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004441 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004442 break;
4443 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004444 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004445 if (aug_p->actions) {
4446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004447 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4448 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004449 return LY_EVALID;
4450 }
4451 if (aug_p->notifs) {
4452 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004453 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4454 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004455 return LY_EVALID;
4456 }
4457 }
Radek Krejci3641f562019-02-13 15:38:40 +01004458
Radek Krejci327de162019-06-14 12:52:07 +02004459 lysc_update_path(ctx, NULL, NULL);
4460 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004461error:
Radek Krejciec4da802019-05-02 13:02:41 +02004462 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004463 return ret;
4464}
4465
4466/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004467 * @brief Apply refined or deviated mandatory flag to the target node.
4468 *
4469 * @param[in] ctx Compile context.
4470 * @param[in] node Target node where the mandatory property is supposed to be changed.
4471 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004472 * @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 +01004473 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4474 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4475 * 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 +01004476 * @return LY_ERR value.
4477 */
4478static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004479lys_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 +01004480{
4481 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004483 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4484 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004485 return LY_EVALID;
4486 }
4487
4488 if (mandatory_flag & LYS_MAND_TRUE) {
4489 /* check if node has default value */
4490 if (node->nodetype & LYS_LEAF) {
4491 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004492 if (refine_flag) {
4493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004494 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004495 return LY_EVALID;
4496 }
Radek Krejcia1911222019-07-22 17:24:50 +02004497 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004498 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004499 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4500 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4501 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4502 free(leaf->dflt);
4503 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004504 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004505 }
4506 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004507 if (refine_flag) {
4508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004509 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004510 return LY_EVALID;
4511 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004512 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004513 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004514 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 +01004515 return LY_EVALID;
4516 }
4517
4518 node->flags &= ~LYS_MAND_FALSE;
4519 node->flags |= LYS_MAND_TRUE;
4520 lys_compile_mandatory_parents(node->parent, 1);
4521 } else {
4522 /* make mandatory false */
4523 node->flags &= ~LYS_MAND_TRUE;
4524 node->flags |= LYS_MAND_FALSE;
4525 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004526 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 +01004527 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004528 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004529 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004530 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4531 leaf->dflt->realtype = leaf->type->dflt->realtype;
4532 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4533 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004534 }
4535 }
4536 return LY_SUCCESS;
4537}
4538
4539/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004540 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4541 * If present, also apply uses's modificators.
4542 *
4543 * @param[in] ctx Compile context
4544 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004545 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4546 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4547 * the compile context.
4548 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4549 */
4550static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004551lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004552{
4553 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004554 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004555 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004556 struct lysc_node_container context_node_fake =
4557 {.nodetype = LYS_CONTAINER,
4558 .module = ctx->mod,
4559 .flags = parent ? parent->flags : 0,
4560 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004561 .prev = (struct lysc_node*)&context_node_fake,
4562 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004563 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004564 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004565 int found;
4566 const char *id, *name, *prefix;
4567 size_t prefix_len, name_len;
4568 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004569 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004570 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004571 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004572 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004573 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004574 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004575 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004576 unsigned int actions_index, notifs_index;
4577 struct lysc_notif **notifs = NULL;
4578 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004579
4580 /* search for the grouping definition */
4581 found = 0;
4582 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004583 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004584 if (prefix) {
4585 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4586 if (!mod) {
4587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004588 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004589 return LY_EVALID;
4590 }
4591 } else {
4592 mod = ctx->mod_def;
4593 }
4594 if (mod == ctx->mod_def) {
4595 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004596 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004597 LY_ARRAY_FOR(grp, u) {
4598 if (!strcmp(grp[u].name, name)) {
4599 grp = &grp[u];
4600 found = 1;
4601 break;
4602 }
4603 }
4604 }
4605 }
4606 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004607 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004608 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004609 if (grp) {
4610 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4611 if (!strcmp(grp[u].name, name)) {
4612 grp = &grp[u];
4613 found = 1;
4614 }
4615 }
4616 }
4617 if (!found && mod->parsed->includes) {
4618 /* ... and all the submodules */
4619 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4620 grp = mod->parsed->includes[u].submodule->groupings;
4621 if (grp) {
4622 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4623 if (!strcmp(grp[v].name, name)) {
4624 grp = &grp[v];
4625 found = 1;
4626 }
4627 }
4628 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004629 }
4630 }
4631 }
4632 if (!found) {
4633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4634 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4635 return LY_EVALID;
4636 }
4637
4638 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4639 grp_stack_count = ctx->groupings.count;
4640 ly_set_add(&ctx->groupings, (void*)grp, 0);
4641 if (grp_stack_count == ctx->groupings.count) {
4642 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4644 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4645 return LY_EVALID;
4646 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004647 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4648 /* remember that the grouping is instantiated to avoid its standalone validation */
4649 grp->flags |= LYS_USED_GRP;
4650 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004651
4652 /* switch context's mod_def */
4653 mod_old = ctx->mod_def;
4654 ctx->mod_def = mod;
4655
4656 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004657 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 +01004658
Radek Krejcifc11bd72019-04-11 16:00:05 +02004659 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004660 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004661 /* 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 +02004662 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 +01004663
4664 /* some preparation for applying refines */
4665 if (grp->data == node_p) {
4666 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004667 if (parent) {
4668 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4669 } else if (ctx->mod->compiled->data) {
4670 child = ctx->mod->compiled->data;
4671 } else {
4672 child = NULL;
4673 }
4674 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004675 }
4676 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004677 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004678 LY_LIST_FOR(context_node_fake.child, child) {
4679 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004680
Radek Krejcifc11bd72019-04-11 16:00:05 +02004681 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004682 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004683 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004684 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004685 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004686 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004687 when_shared = *when;
4688 } else {
4689 ++when_shared->refcount;
4690 (*when) = when_shared;
4691 }
4692 }
Radek Krejci01342af2019-01-03 15:18:08 +01004693 }
4694 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004695 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004696 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004697 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004698 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 +01004699 }
4700
Radek Krejcifc11bd72019-04-11 16:00:05 +02004701 /* compile actions */
4702 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4703 if (actions) {
4704 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004705 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004706 if (*actions && (uses_p->augments || uses_p->refines)) {
4707 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4708 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4709 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4710 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4711 }
4712 }
4713
4714 /* compile notifications */
4715 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4716 if (notifs) {
4717 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004718 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004719 if (*notifs && (uses_p->augments || uses_p->refines)) {
4720 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4721 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4722 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4723 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4724 }
4725 }
4726
4727
Radek Krejci3641f562019-02-13 15:38:40 +01004728 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004729 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004730 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004731 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004732 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004733
Radek Krejcif0089082019-01-07 16:42:01 +01004734 /* reload previous context's mod_def */
4735 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004736 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004737
Radek Krejci76b3e962018-12-14 17:01:25 +01004738 /* apply refine */
4739 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004740 lysc_update_path(ctx, NULL, rfn->nodeid);
4741
Radek Krejci7af64242019-02-18 13:07:53 +01004742 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 +01004743 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004744 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004745 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004746
4747 /* default value */
4748 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004749 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004751 "Invalid refine of default - %s cannot hold %d default values.",
4752 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004753 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004754 }
4755 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004757 "Invalid refine of default - %s cannot hold default value(s).",
4758 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004759 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004760 }
4761 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004762 struct ly_err_item *err = NULL;
4763 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4764 if (leaf->dflt) {
4765 /* remove the previous default value */
4766 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4767 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4768 } else {
4769 /* prepare a new one */
4770 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4771 leaf->dflt->realtype = leaf->type;
4772 }
4773 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004774 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004775 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4776 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004777 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, NULL, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004778 leaf->dflt->realtype->refcount++;
4779 if (err) {
4780 ly_err_print(err);
4781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4782 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4783 ly_err_free(err);
4784 }
4785 LY_CHECK_GOTO(rc, cleanup);
4786 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004787 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004788 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4789
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004790 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4792 "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 +02004793 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004794 }
Radek Krejcia1911222019-07-22 17:24:50 +02004795
4796 /* remove previous set of default values */
4797 LY_ARRAY_FOR(llist->dflts, u) {
4798 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4799 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4800 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004801 }
Radek Krejcia1911222019-07-22 17:24:50 +02004802 LY_ARRAY_FREE(llist->dflts);
4803 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004804 LY_ARRAY_FREE(llist->dflts_mods);
4805 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004806
4807 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004808 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004809 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004810 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004811 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004812 LY_ARRAY_INCREMENT(llist->dflts_mods);
4813 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004814 LY_ARRAY_INCREMENT(llist->dflts);
4815 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4816 llist->dflts[u]->realtype = llist->type;
4817 ret = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
4818 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004819 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, NULL, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004820 llist->dflts[u]->realtype->refcount++;
4821 if (err) {
4822 ly_err_print(err);
4823 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4824 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4825 ly_err_free(err);
4826 }
4827 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004828 }
Radek Krejcia1911222019-07-22 17:24:50 +02004829 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004830 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004831 if (((struct lysc_node_choice*)node)->dflt) {
4832 /* unset LYS_SET_DFLT from the current default case */
4833 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4834 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004835 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 +01004836 }
4837 }
4838
Radek Krejci12fb9142019-01-08 09:45:30 +01004839 /* description */
4840 if (rfn->dsc) {
4841 FREE_STRING(ctx->ctx, node->dsc);
4842 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4843 }
4844
4845 /* reference */
4846 if (rfn->ref) {
4847 FREE_STRING(ctx->ctx, node->ref);
4848 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4849 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004850
4851 /* config */
4852 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004853 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004854 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004855 } else {
4856 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4857 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4858 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004859 }
4860
4861 /* mandatory */
4862 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004863 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004864 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004865
4866 /* presence */
4867 if (rfn->presence) {
4868 if (node->nodetype != LYS_CONTAINER) {
4869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004870 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4871 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004872 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004873 }
4874 node->flags |= LYS_PRESENCE;
4875 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004876
4877 /* must */
4878 if (rfn->musts) {
4879 switch (node->nodetype) {
4880 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004881 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 +01004882 break;
4883 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004884 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 +01004885 break;
4886 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004887 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 +01004888 break;
4889 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004890 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 +01004891 break;
4892 case LYS_ANYXML:
4893 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004894 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 +01004895 break;
4896 default:
4897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004898 "Invalid refine of must statement - %s cannot hold any must statement.",
4899 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004900 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004901 }
4902 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004903
4904 /* min/max-elements */
4905 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4906 switch (node->nodetype) {
4907 case LYS_LEAFLIST:
4908 if (rfn->flags & LYS_SET_MAX) {
4909 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4910 }
4911 if (rfn->flags & LYS_SET_MIN) {
4912 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004913 if (rfn->min) {
4914 node->flags |= LYS_MAND_TRUE;
4915 lys_compile_mandatory_parents(node->parent, 1);
4916 } else {
4917 node->flags &= ~LYS_MAND_TRUE;
4918 lys_compile_mandatory_parents(node->parent, 0);
4919 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004920 }
4921 break;
4922 case LYS_LIST:
4923 if (rfn->flags & LYS_SET_MAX) {
4924 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4925 }
4926 if (rfn->flags & LYS_SET_MIN) {
4927 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004928 if (rfn->min) {
4929 node->flags |= LYS_MAND_TRUE;
4930 lys_compile_mandatory_parents(node->parent, 1);
4931 } else {
4932 node->flags &= ~LYS_MAND_TRUE;
4933 lys_compile_mandatory_parents(node->parent, 0);
4934 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004935 }
4936 break;
4937 default:
4938 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004939 "Invalid refine of %s statement - %s cannot hold this statement.",
4940 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004941 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004942 }
4943 }
Radek Krejcif0089082019-01-07 16:42:01 +01004944
4945 /* if-feature */
4946 if (rfn->iffeatures) {
4947 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004948 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004949 }
Radek Krejci327de162019-06-14 12:52:07 +02004950
4951 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004952 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004953
Radek Krejcif2271f12019-01-07 16:42:23 +01004954 /* do some additional checks of the changed nodes when all the refines are applied */
4955 for (u = 0; u < refined.count; ++u) {
4956 node = (struct lysc_node*)refined.objs[u];
4957 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02004958 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004959
4960 /* check possible conflict with default value (default added, mandatory left true) */
4961 if ((node->flags & LYS_MAND_TRUE) &&
4962 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4963 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4964 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004965 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004966 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004967 }
4968
4969 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4970 if (node->nodetype == LYS_LIST) {
4971 min = ((struct lysc_node_list*)node)->min;
4972 max = ((struct lysc_node_list*)node)->max;
4973 } else {
4974 min = ((struct lysc_node_leaflist*)node)->min;
4975 max = ((struct lysc_node_leaflist*)node)->max;
4976 }
4977 if (min > max) {
4978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004979 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
4980 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004981 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004982 }
4983 }
4984 }
4985
Radek Krejci327de162019-06-14 12:52:07 +02004986 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004987 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004988
4989cleanup:
4990 /* fix connection of the children nodes from fake context node back into the parent */
4991 if (context_node_fake.child) {
4992 context_node_fake.child->prev = child;
4993 }
4994 LY_LIST_FOR(context_node_fake.child, child) {
4995 child->parent = parent;
4996 }
4997
4998 if (uses_p->augments || uses_p->refines) {
4999 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005000 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005001 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5002 LY_ARRAY_FREE(context_node_fake.actions);
5003 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005004 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005005 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5006 LY_ARRAY_FREE(context_node_fake.notifs);
5007 }
5008 }
5009
Radek Krejcie86bf772018-12-14 11:39:53 +01005010 /* reload previous context's mod_def */
5011 ctx->mod_def = mod_old;
5012 /* remove the grouping from the stack for circular groupings dependency check */
5013 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5014 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005015 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005016 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005017
5018 return ret;
5019}
5020
Radek Krejci327de162019-06-14 12:52:07 +02005021static int
5022lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5023{
5024 struct lysp_node *iter;
5025 int len = 0;
5026
5027 *path = NULL;
5028 for (iter = node; iter && len >= 0; iter = iter->parent) {
5029 char *s = *path;
5030 char *id;
5031
5032 switch (iter->nodetype) {
5033 case LYS_USES:
5034 asprintf(&id, "{uses='%s'}", iter->name);
5035 break;
5036 case LYS_GROUPING:
5037 asprintf(&id, "{grouping='%s'}", iter->name);
5038 break;
5039 case LYS_AUGMENT:
5040 asprintf(&id, "{augment='%s'}", iter->name);
5041 break;
5042 default:
5043 id = strdup(iter->name);
5044 break;
5045 }
5046
5047 if (!iter->parent) {
5048 /* print prefix */
5049 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5050 } else {
5051 /* prefix is the same as in parent */
5052 len = asprintf(path, "/%s%s", id, s ? s : "");
5053 }
5054 free(s);
5055 free(id);
5056 }
5057
5058 if (len < 0) {
5059 free(*path);
5060 *path = NULL;
5061 } else if (len == 0) {
5062 *path = strdup("/");
5063 len = 1;
5064 }
5065 return len;
5066}
5067
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005068/**
5069 * @brief Validate groupings that were defined but not directly used in the schema itself.
5070 *
5071 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5072 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5073 */
5074static LY_ERR
5075lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5076{
5077 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005078 char *path;
5079 int len;
5080
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005081 struct lysp_node_uses fake_uses = {
5082 .parent = node_p,
5083 .nodetype = LYS_USES,
5084 .flags = 0, .next = NULL,
5085 .name = grp->name,
5086 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5087 .refines = NULL, .augments = NULL
5088 };
5089 struct lysc_node_container fake_container = {
5090 .nodetype = LYS_CONTAINER,
5091 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5092 .module = ctx->mod,
5093 .sp = NULL, .parent = NULL, .next = NULL,
5094 .prev = (struct lysc_node*)&fake_container,
5095 .name = "fake",
5096 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5097 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5098 };
5099
5100 if (grp->parent) {
5101 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5102 }
Radek Krejci327de162019-06-14 12:52:07 +02005103
5104 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5105 if (len < 0) {
5106 LOGMEM(ctx->ctx);
5107 return LY_EMEM;
5108 }
5109 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5110 ctx->path_len = (uint16_t)len;
5111 free(path);
5112
5113 lysc_update_path(ctx, NULL, "{grouping}");
5114 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005115 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005116 lysc_update_path(ctx, NULL, NULL);
5117 lysc_update_path(ctx, NULL, NULL);
5118
5119 ctx->path_len = 1;
5120 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005121
5122 /* cleanup */
5123 lysc_node_container_free(ctx->ctx, &fake_container);
5124
5125 return ret;
5126}
Radek Krejcife909632019-02-12 15:34:42 +01005127
Radek Krejcie86bf772018-12-14 11:39:53 +01005128/**
Radek Krejcia3045382018-11-22 14:30:31 +01005129 * @brief Compile parsed schema node information.
5130 * @param[in] ctx Compile context
5131 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005132 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5133 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5134 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005135 * @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).
5136 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005137 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5138 */
Radek Krejci19a96102018-11-15 13:38:09 +01005139static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005140lys_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 +01005141{
5142 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005143 struct lysc_node *node;
5144 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005145 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005146 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005147 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005148
Radek Krejci327de162019-06-14 12:52:07 +02005149 if (node_p->nodetype != LYS_USES) {
5150 lysc_update_path(ctx, parent, node_p->name);
5151 } else {
5152 lysc_update_path(ctx, NULL, "{uses}");
5153 lysc_update_path(ctx, NULL, node_p->name);
5154 }
5155
Radek Krejci19a96102018-11-15 13:38:09 +01005156 switch (node_p->nodetype) {
5157 case LYS_CONTAINER:
5158 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5159 node_compile_spec = lys_compile_node_container;
5160 break;
5161 case LYS_LEAF:
5162 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5163 node_compile_spec = lys_compile_node_leaf;
5164 break;
5165 case LYS_LIST:
5166 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005167 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005168 break;
5169 case LYS_LEAFLIST:
5170 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005171 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005172 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005173 case LYS_CHOICE:
5174 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005175 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005176 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005177 case LYS_ANYXML:
5178 case LYS_ANYDATA:
5179 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005180 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005181 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005182 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005183 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5184 lysc_update_path(ctx, NULL, NULL);
5185 lysc_update_path(ctx, NULL, NULL);
5186 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005187 default:
5188 LOGINT(ctx->ctx);
5189 return LY_EINT;
5190 }
5191 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5192 node->nodetype = node_p->nodetype;
5193 node->module = ctx->mod;
5194 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005195 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005196
5197 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005198 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005199 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005200 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005201 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5202 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005203 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005204 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005205 node->flags |= LYS_CONFIG_R;
5206 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005207 /* config not explicitely set, inherit it from parent */
5208 if (parent) {
5209 node->flags |= parent->flags & LYS_CONFIG_MASK;
5210 } else {
5211 /* default is config true */
5212 node->flags |= LYS_CONFIG_W;
5213 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005214 } else {
5215 /* config set explicitely */
5216 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005217 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005218 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5219 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5220 "Configuration node cannot be child of any state data node.");
5221 goto error;
5222 }
Radek Krejci19a96102018-11-15 13:38:09 +01005223
Radek Krejcia6d57732018-11-29 13:40:37 +01005224 /* *list ordering */
5225 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5226 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005227 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005228 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5229 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005230 node->flags &= ~LYS_ORDBY_MASK;
5231 node->flags |= LYS_ORDBY_SYSTEM;
5232 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5233 /* default ordering is system */
5234 node->flags |= LYS_ORDBY_SYSTEM;
5235 }
5236 }
5237
Radek Krejci19a96102018-11-15 13:38:09 +01005238 /* status - it is not inherited by specification, but it does not make sense to have
5239 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005240 if (!parent || parent->nodetype != LYS_CHOICE) {
5241 /* in case of choice/case's children, postpone the check to the moment we know if
5242 * 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 +01005243 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 +01005244 }
5245
Radek Krejciec4da802019-05-02 13:02:41 +02005246 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005247 node->sp = node_p;
5248 }
5249 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005250 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5251 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005252 if (node_p->when) {
5253 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005254 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005255 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005256 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005257 }
Radek Krejciec4da802019-05-02 13:02:41 +02005258 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
5259 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005260
5261 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005262 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005263
Radek Krejcife909632019-02-12 15:34:42 +01005264 /* inherit LYS_MAND_TRUE in parent containers */
5265 if (node->flags & LYS_MAND_TRUE) {
5266 lys_compile_mandatory_parents(parent, 1);
5267 }
5268
Radek Krejci327de162019-06-14 12:52:07 +02005269 lysc_update_path(ctx, NULL, NULL);
5270
Radek Krejci19a96102018-11-15 13:38:09 +01005271 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005272 if (parent) {
5273 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005274 if (node_p->parent->nodetype == LYS_CASE) {
5275 lysc_update_path(ctx, parent, node_p->parent->name);
5276 } else {
5277 lysc_update_path(ctx, parent, node->name);
5278 }
Radek Krejciec4da802019-05-02 13:02:41 +02005279 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005280 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005281 if (uses_status) {
5282
5283 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005284 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005285 * it directly gets the same status flags as the choice;
5286 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005287 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005288 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005289 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005290 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005291 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005292 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005293 }
Radek Krejciec4da802019-05-02 13:02:41 +02005294 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 +02005295
5296 if (parent->nodetype == LYS_CHOICE) {
5297 lysc_update_path(ctx, NULL, NULL);
5298 }
Radek Krejci19a96102018-11-15 13:38:09 +01005299 } else {
5300 /* top-level element */
5301 if (!ctx->mod->compiled->data) {
5302 ctx->mod->compiled->data = node;
5303 } else {
5304 /* insert at the end of the module's top-level nodes list */
5305 ctx->mod->compiled->data->prev->next = node;
5306 node->prev = ctx->mod->compiled->data->prev;
5307 ctx->mod->compiled->data->prev = node;
5308 }
Radek Krejci327de162019-06-14 12:52:07 +02005309 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005310 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5311 ctx->mod->compiled->notifs, node->name, node)) {
5312 return LY_EVALID;
5313 }
Radek Krejci19a96102018-11-15 13:38:09 +01005314 }
Radek Krejci327de162019-06-14 12:52:07 +02005315 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005316
5317 return LY_SUCCESS;
5318
5319error:
5320 lysc_node_free(ctx->ctx, node);
5321 return ret;
5322}
5323
Radek Krejciccd20f12019-02-15 14:12:27 +01005324static void
5325lysc_disconnect(struct lysc_node *node)
5326{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005327 struct lysc_node *parent, *child, *prev = NULL, *next;
5328 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005329 int remove_cs = 0;
5330
5331 parent = node->parent;
5332
5333 /* parent's first child */
5334 if (!parent) {
5335 return;
5336 }
5337 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005338 cs = (struct lysc_node_case*)node;
5339 } else if (parent->nodetype == LYS_CASE) {
5340 /* disconnecting some node in a case */
5341 cs = (struct lysc_node_case*)parent;
5342 parent = cs->parent;
5343 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5344 if (child == node) {
5345 if (cs->child == child) {
5346 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5347 /* case with a single child -> remove also the case */
5348 child->parent = NULL;
5349 remove_cs = 1;
5350 } else {
5351 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005352 }
5353 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005354 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005355 }
5356 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005357 if (!remove_cs) {
5358 cs = NULL;
5359 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005360 } else if (lysc_node_children(parent, node->flags) == node) {
5361 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005362 }
5363
5364 if (cs) {
5365 if (remove_cs) {
5366 /* cs has only one child which is being also removed */
5367 lysc_disconnect((struct lysc_node*)cs);
5368 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5369 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005370 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5371 /* default case removed */
5372 ((struct lysc_node_choice*)parent)->dflt = NULL;
5373 }
5374 if (((struct lysc_node_choice*)parent)->cases == cs) {
5375 /* first case removed */
5376 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5377 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005378 if (cs->child) {
5379 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5380 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5381 prev = cs->child->prev;
5382 } /* else all the children are under a single case */
5383 LY_LIST_FOR_SAFE(cs->child, next, child) {
5384 if (child->parent != (struct lysc_node*)cs) {
5385 break;
5386 }
5387 lysc_node_free(node->module->ctx, child);
5388 }
5389 if (prev) {
5390 if (prev->next) {
5391 prev->next = child;
5392 }
5393 if (child) {
5394 child->prev = prev;
5395 } else {
5396 /* link from the first child under the cases */
5397 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5398 }
5399 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005400 }
5401 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005402 }
5403
5404 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005405 if (node->prev->next) {
5406 node->prev->next = node->next;
5407 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005408 if (node->next) {
5409 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005410 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005411 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005412 if (child) {
5413 child->prev = node->prev;
5414 }
5415 } else if (((struct lysc_node_choice*)parent)->cases) {
5416 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005417 }
5418}
5419
5420LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005421lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005422{
Radek Krejcia1911222019-07-22 17:24:50 +02005423 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005424 struct ly_set devs_p = {0};
5425 struct ly_set targets = {0};
5426 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005427 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005428 struct lysc_action *rpcs;
5429 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005430 struct lysp_deviation *dev;
5431 struct lysp_deviate *d, **dp_new;
5432 struct lysp_deviate_add *d_add;
5433 struct lysp_deviate_del *d_del;
5434 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005435 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005436 struct lysc_deviation {
5437 const char *nodeid;
5438 struct lysc_node *target; /* target node of the deviation */
5439 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005440 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005441 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5442 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005443 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005444 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005445 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005446 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005447 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005448 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005449
5450 /* get all deviations from the module and all its submodules ... */
5451 LY_ARRAY_FOR(mod_p->deviations, u) {
5452 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5453 }
5454 LY_ARRAY_FOR(mod_p->includes, v) {
5455 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5456 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5457 }
5458 }
5459 if (!devs_p.count) {
5460 /* nothing to do */
5461 return LY_SUCCESS;
5462 }
5463
Radek Krejci327de162019-06-14 12:52:07 +02005464 lysc_update_path(ctx, NULL, "{deviation}");
5465
Radek Krejciccd20f12019-02-15 14:12:27 +01005466 /* ... and group them by the target node */
5467 devs = calloc(devs_p.count, sizeof *devs);
5468 for (u = 0; u < devs_p.count; ++u) {
5469 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005470 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005471
5472 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005473 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5474 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005475 if (target->nodetype == LYS_ACTION) {
5476 /* move the target pointer to input/output to make them different from the action and
5477 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5478 * back to the RPC/action node due to a better compatibility and decision code in this function.
5479 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5480 if (flags & LYSC_OPT_RPC_INPUT) {
5481 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5482 flags |= LYSC_OPT_INTERNAL;
5483 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5484 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5485 flags |= LYSC_OPT_INTERNAL;
5486 }
5487 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005488 /* insert into the set of targets with duplicity detection */
5489 i = ly_set_add(&targets, target, 0);
5490 if (!devs[i]) {
5491 /* new record */
5492 devs[i] = calloc(1, sizeof **devs);
5493 devs[i]->target = target;
5494 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005495 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005496 }
5497 /* add deviates into the deviation's list of deviates */
5498 for (d = dev->deviates; d; d = d->next) {
5499 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5500 *dp_new = d;
5501 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5502 devs[i]->not_supported = 1;
5503 }
5504 }
Radek Krejci327de162019-06-14 12:52:07 +02005505
5506 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005507 }
5508
5509 /* MACROS for deviates checking */
5510#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5511 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005512 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 +01005513 goto cleanup; \
5514 }
5515
5516#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5517 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5519 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005520 goto cleanup; \
5521 }
5522
Radek Krejcia1911222019-07-22 17:24:50 +02005523
Radek Krejciccd20f12019-02-15 14:12:27 +01005524#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005525 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5527 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5528 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5529 goto cleanup; \
5530 }
5531
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005532#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005533 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005534 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005535 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5536 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005538 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5539 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005540 goto cleanup; \
5541 }
5542
Radek Krejci551b12c2019-02-19 16:11:21 +01005543#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5544 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5545 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005546 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5547 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005548 goto cleanup; \
5549 }
5550
Radek Krejciccd20f12019-02-15 14:12:27 +01005551#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5552 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005554 goto cleanup; \
5555 }
5556
Radek Krejci551b12c2019-02-19 16:11:21 +01005557#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5558 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005560 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005561 goto cleanup; \
5562 }
5563
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005564#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5565 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5566 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5567 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5568 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 +01005569 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005570 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005572 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5573 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005574 goto cleanup; \
5575 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005576 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5577 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5578 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5579 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5580 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005581 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005582 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5583 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5584 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005585 }
5586
5587 /* apply deviations */
5588 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005589 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5590 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5591 struct ly_err_item *err = NULL;
5592
5593 dflt = NULL;
5594 changed_type = 0;
5595
Radek Krejci327de162019-06-14 12:52:07 +02005596 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5597
Radek Krejcif538ce52019-03-05 10:46:14 +01005598 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5599 /* fix the target pointer in case of RPC's/action's input/output */
5600 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5601 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5602 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5603 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5604 }
5605 }
5606
Radek Krejciccd20f12019-02-15 14:12:27 +01005607 /* not-supported */
5608 if (devs[u]->not_supported) {
5609 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5610 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5611 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5612 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005613
5614#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5615 if (devs[u]->target->parent) { \
5616 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5617 } else { \
5618 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5619 } \
5620 LY_ARRAY_FOR(ARRAY, x) { \
5621 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5622 } \
5623 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5624 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5625 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5626 LY_ARRAY_DECREMENT(ARRAY); \
5627 }
5628
Radek Krejcif538ce52019-03-05 10:46:14 +01005629 if (devs[u]->target->nodetype == LYS_ACTION) {
5630 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5631 /* remove RPC's/action's input */
5632 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5633 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005634 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5635 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005636 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5637 /* remove RPC's/action's output */
5638 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5639 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005640 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5641 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005642 } else {
5643 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005644 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005645 }
5646 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005647 /* remove Notification */
5648 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005649 } else {
5650 /* remove the target node */
5651 lysc_disconnect(devs[u]->target);
5652 lysc_node_free(ctx->ctx, devs[u]->target);
5653 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005654
5655 continue;
5656 }
5657
5658 /* list of deviates (not-supported is not present in the list) */
5659 LY_ARRAY_FOR(devs[u]->deviates, v) {
5660 d = devs[u]->deviates[v];
5661
5662 switch (d->mod) {
5663 case LYS_DEV_ADD:
5664 d_add = (struct lysp_deviate_add*)d;
5665 /* [units-stmt] */
5666 if (d_add->units) {
5667 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5668 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5669
5670 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5671 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5672 }
5673
5674 /* *must-stmt */
5675 if (d_add->musts) {
5676 switch (devs[u]->target->nodetype) {
5677 case LYS_CONTAINER:
5678 case LYS_LIST:
5679 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005680 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005681 break;
5682 case LYS_LEAF:
5683 case LYS_LEAFLIST:
5684 case LYS_ANYDATA:
5685 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005686 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005687 break;
5688 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005689 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005690 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005691 break;
5692 case LYS_ACTION:
5693 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5694 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005695 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005696 break;
5697 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5698 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005699 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005700 break;
5701 }
5702 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005703 default:
5704 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005705 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005706 goto cleanup;
5707 }
5708 }
5709
5710 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005711 if (d_add->uniques) {
5712 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5713 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5714 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005715
5716 /* *default-stmt */
5717 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005718 switch (devs[u]->target->nodetype) {
5719 case LYS_LEAF:
5720 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005721 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 +02005722 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005723 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005724 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5725 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5726 } else {
5727 /* prepare new default value storage */
5728 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005729 }
Radek Krejcia1911222019-07-22 17:24:50 +02005730 dflt = d_add->dflts[0];
5731 /* parsing is done at the end after possible replace of the leaf's type */
5732
Radek Krejci551b12c2019-02-19 16:11:21 +01005733 /* mark the new default values as leaf's own */
5734 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005735 break;
5736 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005737 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005738 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005739 LY_ARRAY_FOR(llist->dflts, x) {
5740 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5741 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5742 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005743 }
Radek Krejcia1911222019-07-22 17:24:50 +02005744 LY_ARRAY_FREE(llist->dflts);
5745 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005746 LY_ARRAY_FREE(llist->dflts_mods);
5747 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005748 }
5749 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005750 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005751 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5752 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005753 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005754 LY_ARRAY_INCREMENT(llist->dflts_mods);
5755 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005756 LY_ARRAY_INCREMENT(llist->dflts);
5757 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5758 llist->dflts[x]->realtype = llist->type;
5759 rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]),
5760 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005761 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, NULL, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005762 llist->dflts[x]->realtype->refcount++;
5763 if (err) {
5764 ly_err_print(err);
5765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5766 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5767 d_add->dflts[x - y], err->msg);
5768 ly_err_free(err);
5769 }
5770 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005771 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005772 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005773 devs[u]->target->flags |= LYS_SET_DFLT;
5774 break;
5775 case LYS_CHOICE:
5776 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5777 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5778 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5779 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005780 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 +01005781 goto cleanup;
5782 }
5783 break;
5784 default:
5785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005786 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005787 goto cleanup;
5788 }
5789 }
5790
5791 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005792 if (d_add->flags & LYS_CONFIG_MASK) {
5793 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005795 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5796 goto cleanup;
5797 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005798 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005799 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5800 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005801 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005802 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005804 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5805 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005806 goto cleanup;
5807 }
Radek Krejci327de162019-06-14 12:52:07 +02005808 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005809 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005810
5811 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005812 if (d_add->flags & LYS_MAND_MASK) {
5813 if (devs[u]->target->flags & LYS_MAND_MASK) {
5814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005815 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5816 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005817 goto cleanup;
5818 }
Radek Krejci327de162019-06-14 12:52:07 +02005819 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005820 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005821
5822 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005823 if (d_add->flags & LYS_SET_MIN) {
5824 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5825 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5826 /* change value */
5827 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5828 } else if (devs[u]->target->nodetype == LYS_LIST) {
5829 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5830 /* change value */
5831 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5832 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005834 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5835 goto cleanup;
5836 }
5837 if (d_add->min) {
5838 devs[u]->target->flags |= LYS_MAND_TRUE;
5839 }
5840 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005841
5842 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005843 if (d_add->flags & LYS_SET_MAX) {
5844 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5845 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5846 /* change value */
5847 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5848 } else if (devs[u]->target->nodetype == LYS_LIST) {
5849 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5850 /* change value */
5851 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5852 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005853 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005854 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5855 goto cleanup;
5856 }
5857 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005858
5859 break;
5860 case LYS_DEV_DELETE:
5861 d_del = (struct lysp_deviate_del*)d;
5862
5863 /* [units-stmt] */
5864 if (d_del->units) {
5865 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005866 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5867 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5869 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5870 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5871 goto cleanup;
5872 }
5873 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5874 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005875 }
5876
5877 /* *must-stmt */
5878 if (d_del->musts) {
5879 switch (devs[u]->target->nodetype) {
5880 case LYS_CONTAINER:
5881 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005882 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005883 break;
5884 case LYS_LEAF:
5885 case LYS_LEAFLIST:
5886 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005887 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005888 break;
5889 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005890 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005891 break;
5892 case LYS_ACTION:
5893 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5894 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5895 break;
5896 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5897 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5898 break;
5899 }
5900 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005901 default:
5902 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005903 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005904 goto cleanup;
5905 }
5906 }
5907
5908 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005909 if (d_del->uniques) {
5910 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5911 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5912 LY_ARRAY_FOR(d_del->uniques, x) {
5913 LY_ARRAY_FOR(list->uniques, z) {
5914 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5915 nodeid = strpbrk(name, " \t\n");
5916 if (nodeid) {
5917 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5918 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5919 break;
5920 }
5921 while (isspace(*nodeid)) {
5922 ++nodeid;
5923 }
5924 } else {
5925 if (strcmp(name, list->uniques[z][y]->name)) {
5926 break;
5927 }
5928 }
5929 }
5930 if (!name) {
5931 /* complete match - remove the unique */
5932 LY_ARRAY_DECREMENT(list->uniques);
5933 LY_ARRAY_FREE(list->uniques[z]);
5934 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5935 --z;
5936 break;
5937 }
5938 }
5939 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005941 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5942 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01005943 goto cleanup;
5944 }
5945 }
5946 if (!LY_ARRAY_SIZE(list->uniques)) {
5947 LY_ARRAY_FREE(list->uniques);
5948 list->uniques = NULL;
5949 }
5950 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005951
5952 /* *default-stmt */
5953 if (d_del->dflts) {
5954 switch (devs[u]->target->nodetype) {
5955 case LYS_LEAF:
5956 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5957 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5958 dflt, "deleting", "default", d_del->dflts[0]);
5959
Radek Krejcia1911222019-07-22 17:24:50 +02005960 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005961 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02005962 if (strcmp(dflt, d_del->dflts[0])) {
5963 if (i) {
5964 free((char*)dflt);
5965 }
5966 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5967 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
5968 d_del->dflts[0], dflt);
5969 goto cleanup;
5970 }
5971 if (i) {
5972 free((char*)dflt);
5973 }
5974 dflt = NULL;
5975
5976 /* remove the default specification */
5977 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5978 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5979 free(leaf->dflt);
5980 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005981 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01005982 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005983 break;
5984 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005985 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
5986 LY_ARRAY_FOR(d_del->dflts, x) {
5987 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005988 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 +02005989 if (!strcmp(dflt, d_del->dflts[x])) {
5990 if (i) {
5991 free((char*)dflt);
5992 }
5993 dflt = NULL;
5994 break;
5995 }
5996 if (i) {
5997 free((char*)dflt);
5998 }
5999 dflt = NULL;
6000 }
6001 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6003 "which does not match any of the target's property values.", d_del->dflts[x]);
6004 goto cleanup;
6005 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006006 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006007 LY_ARRAY_DECREMENT(llist->dflts);
6008 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6009 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6010 free(llist->dflts[y]);
6011 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006012 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 +02006013 }
6014 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006015 LY_ARRAY_FREE(llist->dflts_mods);
6016 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006017 LY_ARRAY_FREE(llist->dflts);
6018 llist->dflts = NULL;
6019 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006020 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006021 break;
6022 case LYS_CHOICE:
6023 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6024 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6025 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006026 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006027 if (prefix) {
6028 /* use module prefixes from the deviation module to match the module of the default case */
6029 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6030 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006031 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6032 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006033 goto cleanup;
6034 }
6035 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6036 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006037 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6038 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006039 goto cleanup;
6040 }
6041 }
6042 /* else {
6043 * strictly, the default prefix would point to the deviation module, but the value should actually
6044 * match the default string in the original module (usually unprefixed), so in this case we do not check
6045 * the module of the default case, just matching its name */
6046 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6047 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006048 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6049 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006050 goto cleanup;
6051 }
6052 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6053 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6054 break;
6055 default:
6056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006057 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006058 goto cleanup;
6059 }
6060 }
6061
6062 break;
6063 case LYS_DEV_REPLACE:
6064 d_rpl = (struct lysp_deviate_rpl*)d;
6065
6066 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006067 if (d_rpl->type) {
6068 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6069 /* type is mandatory, so checking for its presence is not necessary */
6070 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006071
6072 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6073 /* the target has default from the previous type - remove it */
6074 if (devs[u]->target->nodetype == LYS_LEAF) {
6075 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6076 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6077 free(leaf->dflt);
6078 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006079 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006080 } else { /* LYS_LEAFLIST */
6081 LY_ARRAY_FOR(llist->dflts, x) {
6082 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6083 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6084 free(llist->dflts[x]);
6085 }
6086 LY_ARRAY_FREE(llist->dflts);
6087 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006088 LY_ARRAY_FREE(llist->dflts_mods);
6089 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006090 }
6091 }
6092 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006093 /* there is no default value, do not set changed_type after type compilation
6094 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006095 changed_type = -1;
6096 }
Radek Krejciec4da802019-05-02 13:02:41 +02006097 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 +02006098 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006099 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006100
6101 /* [units-stmt] */
6102 if (d_rpl->units) {
6103 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6104 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6105 units, "replacing", "units", d_rpl->units);
6106
6107 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6108 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6109 }
6110
6111 /* [default-stmt] */
6112 if (d_rpl->dflt) {
6113 switch (devs[u]->target->nodetype) {
6114 case LYS_LEAF:
6115 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6116 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006117 /* first, remove the default value taken from the type */
6118 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6119 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6120 dflt = d_rpl->dflt;
6121 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006122 break;
6123 case LYS_CHOICE:
6124 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006125 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 +01006126 goto cleanup;
6127 }
6128 break;
6129 default:
6130 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006131 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006132 goto cleanup;
6133 }
6134 }
6135
6136 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006137 if (d_rpl->flags & LYS_CONFIG_MASK) {
6138 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006140 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6141 goto cleanup;
6142 }
6143 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006145 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6146 goto cleanup;
6147 }
Radek Krejci327de162019-06-14 12:52:07 +02006148 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006149 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006150
6151 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006152 if (d_rpl->flags & LYS_MAND_MASK) {
6153 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006155 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6156 goto cleanup;
6157 }
Radek Krejci327de162019-06-14 12:52:07 +02006158 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006159 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006160
6161 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006162 if (d_rpl->flags & LYS_SET_MIN) {
6163 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6164 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6165 /* change value */
6166 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6167 } else if (devs[u]->target->nodetype == LYS_LIST) {
6168 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6169 /* change value */
6170 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6171 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006172 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006173 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6174 goto cleanup;
6175 }
6176 if (d_rpl->min) {
6177 devs[u]->target->flags |= LYS_MAND_TRUE;
6178 }
6179 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006180
6181 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006182 if (d_rpl->flags & LYS_SET_MAX) {
6183 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6184 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6185 /* change value */
6186 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6187 } else if (devs[u]->target->nodetype == LYS_LIST) {
6188 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6189 /* change value */
6190 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6191 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006192 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006193 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6194 goto cleanup;
6195 }
6196 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006197
6198 break;
6199 default:
6200 LOGINT(ctx->ctx);
6201 goto cleanup;
6202 }
6203 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006204
Radek Krejci33f72892019-02-21 10:36:58 +01006205 /* final check when all deviations of a single target node are applied */
6206
Radek Krejci551b12c2019-02-19 16:11:21 +01006207 /* check min-max compatibility */
6208 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6209 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6210 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6211 } else if (devs[u]->target->nodetype == LYS_LIST) {
6212 min = ((struct lysc_node_list*)devs[u]->target)->min;
6213 max = ((struct lysc_node_list*)devs[u]->target)->max;
6214 } else {
6215 min = max = 0;
6216 }
6217 if (min > max) {
6218 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 +02006219 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006220 goto cleanup;
6221 }
6222
Radek Krejcia1911222019-07-22 17:24:50 +02006223 if (dflt) {
6224 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006225 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006226 leaf->dflt->realtype = leaf->type;
6227 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6228 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006229 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, NULL, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006230 leaf->dflt->realtype->refcount++;
6231 if (err) {
6232 ly_err_print(err);
6233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6234 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6235 ly_err_free(err);
6236 }
6237 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006238 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006239 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6240 int dynamic;
6241 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006242 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02006243 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6244 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6245 leaf->dflt->realtype = leaf->type;
6246 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6247 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006248 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, NULL, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006249 leaf->dflt->realtype->refcount++;
6250 if (err) {
6251 ly_err_print(err);
6252 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6253 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6254 ly_err_free(err);
6255 }
6256 if (dynamic) {
6257 free((void*)dflt);
6258 }
6259 LY_CHECK_GOTO(rc, cleanup);
6260 dflt = NULL;
6261 } else { /* LYS_LEAFLIST */
6262 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006263 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 +02006264 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6265 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6266 llist->dflts[x]->realtype = llist->type;
6267 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6268 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006269 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, NULL, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006270 llist->dflts[x]->realtype->refcount++;
6271 if (err) {
6272 ly_err_print(err);
6273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6274 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6275 dflt, err->msg);
6276 ly_err_free(err);
6277 }
6278 if (dynamic) {
6279 free((void*)dflt);
6280 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006281 dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006282 LY_CHECK_GOTO(rc, cleanup);
6283 }
6284 }
6285 }
6286
Radek Krejci551b12c2019-02-19 16:11:21 +01006287 /* check mandatory - default compatibility */
6288 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6289 && (devs[u]->target->flags & LYS_SET_DFLT)
6290 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006292 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006293 goto cleanup;
6294 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6295 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6296 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006297 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 +01006298 goto cleanup;
6299 }
6300 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6301 /* mandatory node under a default case */
6302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006303 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6304 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006305 goto cleanup;
6306 }
Radek Krejci33f72892019-02-21 10:36:58 +01006307
Radek Krejci327de162019-06-14 12:52:07 +02006308 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006309 }
6310
Radek Krejci327de162019-06-14 12:52:07 +02006311 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006312 ret = LY_SUCCESS;
6313
6314cleanup:
6315 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6316 LY_ARRAY_FREE(devs[u]->deviates);
6317 free(devs[u]);
6318 }
6319 free(devs);
6320 ly_set_erase(&targets, NULL);
6321 ly_set_erase(&devs_p, NULL);
6322
6323 return ret;
6324}
6325
Radek Krejcib56c7502019-02-13 14:19:54 +01006326/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006327 * @brief Compile the given YANG submodule into the main module.
6328 * @param[in] ctx Compile context
6329 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006330 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6331 */
6332LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006333lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006334{
6335 unsigned int u;
6336 LY_ERR ret = LY_SUCCESS;
6337 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006338 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006339 struct lysc_module *mainmod = ctx->mod->compiled;
6340
Radek Krejci0af46292019-01-11 16:02:31 +01006341 if (!mainmod->mod->off_features) {
6342 /* features are compiled directly into the compiled module structure,
6343 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6344 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci327de162019-06-14 12:52:07 +02006345 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01006346 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
6347 LY_CHECK_GOTO(ret, error);
6348 }
6349
Radek Krejci327de162019-06-14 12:52:07 +02006350 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006351 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006352 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006353
Radek Krejci8cce8532019-03-05 11:27:45 +01006354 /* TODO data nodes */
6355
Radek Krejciec4da802019-05-02 13:02:41 +02006356 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6357 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006358
Radek Krejcid05cbd92018-12-05 14:26:40 +01006359error:
6360 return ret;
6361}
6362
Radek Krejci19a96102018-11-15 13:38:09 +01006363LY_ERR
6364lys_compile(struct lys_module *mod, int options)
6365{
6366 struct lysc_ctx ctx = {0};
6367 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006368 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006369 struct lysp_module *sp;
6370 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006371 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006372 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006373 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006374 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006375 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006376
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006377 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006378
6379 if (!mod->implemented) {
6380 /* just imported modules are not compiled */
6381 return LY_SUCCESS;
6382 }
6383
Radek Krejci19a96102018-11-15 13:38:09 +01006384 sp = mod->parsed;
6385
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006386 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006387 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006388 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006389 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006390 ctx.path_len = 1;
6391 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006392
6393 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006394 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6395 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006396
Radek Krejciec4da802019-05-02 13:02:41 +02006397 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006398 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006399 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006400 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6401 }
Radek Krejci0af46292019-01-11 16:02:31 +01006402 if (mod->off_features) {
6403 /* there is already precompiled array of features */
6404 mod_c->features = mod->off_features;
6405 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006406 } else {
6407 /* features are compiled directly into the compiled module structure,
6408 * 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 +02006409 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006410 LY_CHECK_GOTO(ret, error);
6411 }
6412 /* finish feature compilation, not only for the main module, but also for the submodules.
6413 * Due to possible forward references, it must be done when all the features (including submodules)
6414 * are present. */
6415 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006416 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006417 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6418 }
Radek Krejci327de162019-06-14 12:52:07 +02006419 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006420 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006421 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006422 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006423 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006424 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6425 }
Radek Krejci327de162019-06-14 12:52:07 +02006426 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006427 }
Radek Krejci327de162019-06-14 12:52:07 +02006428 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006429
Radek Krejci327de162019-06-14 12:52:07 +02006430 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006431 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006432 if (sp->identities) {
6433 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6434 }
Radek Krejci327de162019-06-14 12:52:07 +02006435 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006436
Radek Krejci95710c92019-02-11 15:49:55 +01006437 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006438 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006439 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006440 LY_CHECK_GOTO(ret, error);
6441 }
Radek Krejci95710c92019-02-11 15:49:55 +01006442
Radek Krejciec4da802019-05-02 13:02:41 +02006443 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6444 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006445
Radek Krejci95710c92019-02-11 15:49:55 +01006446 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006447 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006448 LY_CHECK_GOTO(ret, error);
6449 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006450 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006451 LY_CHECK_GOTO(ret, error);
6452 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006453
6454 /* deviations */
Radek Krejciec4da802019-05-02 13:02:41 +02006455 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006456 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006457
Radek Krejciec4da802019-05-02 13:02:41 +02006458 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006459
Radek Krejcia3045382018-11-22 14:30:31 +01006460 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006461 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6462 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6463 * 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 +01006464 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006465 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006466 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6467 if (type->basetype == LY_TYPE_LEAFREF) {
6468 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006469 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 +01006470 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006471 } else if (type->basetype == LY_TYPE_UNION) {
6472 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6473 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6474 /* validate the path */
6475 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6476 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6477 LY_CHECK_GOTO(ret, error);
6478 }
6479 }
Radek Krejcia3045382018-11-22 14:30:31 +01006480 }
6481 }
6482 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006483 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006484 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006485 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6486 if (type->basetype == LY_TYPE_LEAFREF) {
6487 /* store pointer to the real type */
6488 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6489 typeiter->basetype == LY_TYPE_LEAFREF;
6490 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6491 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006492 } else if (type->basetype == LY_TYPE_UNION) {
6493 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6494 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6495 /* store pointer to the real type */
6496 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6497 typeiter->basetype == LY_TYPE_LEAFREF;
6498 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6499 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6500 }
6501 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006502 }
6503 }
6504 }
Radek Krejciec4da802019-05-02 13:02:41 +02006505
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006506 /* validate non-instantiated groupings from the parsed schema,
6507 * without it we would accept even the schemas with invalid grouping specification */
6508 ctx.options |= LYSC_OPT_GROUPING;
6509 LY_ARRAY_FOR(sp->groupings, u) {
6510 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6511 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6512 }
6513 }
6514 LY_LIST_FOR(sp->data, node_p) {
6515 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6516 LY_ARRAY_FOR(grps, u) {
6517 if (!(grps[u].flags & LYS_USED_GRP)) {
6518 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6519 }
6520 }
6521 }
6522
Radek Krejcia3045382018-11-22 14:30:31 +01006523 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006524 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006525 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006526 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006527
Radek Krejciec4da802019-05-02 13:02:41 +02006528 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006529 lysp_module_free(mod->parsed);
6530 ((struct lys_module*)mod)->parsed = NULL;
6531 }
6532
Radek Krejciec4da802019-05-02 13:02:41 +02006533 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006534 /* remove flag of the modules implemented by dependency */
6535 for (u = 0; u < ctx.ctx->list.count; ++u) {
6536 m = ctx.ctx->list.objs[u];
6537 if (m->implemented == 2) {
6538 m->implemented = 1;
6539 }
6540 }
6541 }
6542
Radek Krejci19a96102018-11-15 13:38:09 +01006543 ((struct lys_module*)mod)->compiled = mod_c;
6544 return LY_SUCCESS;
6545
6546error:
Radek Krejci95710c92019-02-11 15:49:55 +01006547 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01006548 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006549 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006550 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006551 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006552 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006553 mod->compiled = NULL;
6554
6555 /* revert compilation of modules implemented by dependency */
6556 for (u = 0; u < ctx.ctx->list.count; ++u) {
6557 m = ctx.ctx->list.objs[u];
6558 if (m->implemented == 2) {
6559 /* revert features list to the precompiled state */
6560 lys_feature_precompile_revert(&ctx, m);
6561 /* mark module as imported-only / not-implemented */
6562 m->implemented = 0;
6563 /* free the compiled version of the module */
6564 lysc_module_free(m->compiled, NULL);
6565 m->compiled = NULL;
6566 }
6567 }
6568
Radek Krejci19a96102018-11-15 13:38:09 +01006569 return ret;
6570}