blob: 37932a756ce90fc21443fb44c578262c3564a001 [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);
407 /* TODO plugins */
408
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 */
2151 if (strncmp(path_key_expr, "current()", 9)) {
2152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2153 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2154 *predicate - start, start);
2155 goto cleanup;
2156 }
2157 path_key_expr += 9;
2158 while (isspace(*path_key_expr)) {
2159 ++path_key_expr;
2160 }
2161
2162 if (*path_key_expr != '/') {
2163 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2164 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2165 *predicate - start, start);
2166 goto cleanup;
2167 }
2168 ++path_key_expr;
2169 while (isspace(*path_key_expr)) {
2170 ++path_key_expr;
2171 }
2172
2173 /* rel-path-keyexpr:
2174 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2175 while (!strncmp(path_key_expr, "..", 2)) {
2176 ++dest_parent_times;
2177 path_key_expr += 2;
2178 while (isspace(*path_key_expr)) {
2179 ++path_key_expr;
2180 }
2181 if (*path_key_expr != '/') {
2182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2183 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2184 *predicate - start, start);
2185 goto cleanup;
2186 }
2187 ++path_key_expr;
2188 while (isspace(*path_key_expr)) {
2189 ++path_key_expr;
2190 }
2191
2192 /* path is supposed to be evaluated in data tree, so we have to skip
2193 * all schema nodes that cannot be instantiated in data tree */
2194 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2195 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2196 *predicate - start, start);
2197 }
2198 if (!dest_parent_times) {
2199 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2200 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2201 *predicate - start, start);
2202 goto cleanup;
2203 }
2204 if (path_key_expr == pke_end) {
2205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2206 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2207 *predicate - start, start);
2208 goto cleanup;
2209 }
2210
2211 while(path_key_expr != pke_end) {
Radek Krejcib4a4a272019-06-10 12:44:52 +02002212 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002213 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002214 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2215 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002216 goto cleanup;
2217 }
2218
2219 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002220 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002221 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002222 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002223 }
2224 if (!mod) {
2225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002226 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002227 *predicate - start, start, dst_len, dst);
2228 goto cleanup;
2229 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002230 if (!mod->implemented) {
2231 /* make the module implemented */
2232 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2233 }
Radek Krejcia3045382018-11-22 14:30:31 +01002234
2235 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2236 if (!dst_node) {
2237 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002238 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002239 *predicate - start, start, path_key_expr - pke_start, pke_start);
2240 goto cleanup;
2241 }
2242 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002243 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 +01002244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002245 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002246 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2247 goto cleanup;
2248 }
2249 }
2250
2251 ret = LY_SUCCESS;
2252cleanup:
2253 ly_set_erase(&keys, NULL);
2254 return ret;
2255}
2256
2257/**
2258 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2259 *
2260 * path-arg = absolute-path / relative-path
2261 * absolute-path = 1*("/" (node-identifier *path-predicate))
2262 * relative-path = 1*(".." "/") descendant-path
2263 *
2264 * @param[in,out] path Path to parse.
2265 * @param[out] prefix Prefix of the token, NULL if there is not any.
2266 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2267 * @param[out] name Name of the token.
2268 * @param[out] nam_len Length of the name.
2269 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2270 * must not be changed between consecutive calls. -1 if the
2271 * path is absolute.
2272 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2273 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2274 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002275LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002276lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2277 int *parent_times, int *has_predicate)
2278{
2279 int par_times = 0;
2280
2281 assert(path && *path);
2282 assert(parent_times);
2283 assert(prefix);
2284 assert(prefix_len);
2285 assert(name);
2286 assert(name_len);
2287 assert(has_predicate);
2288
2289 *prefix = NULL;
2290 *prefix_len = 0;
2291 *name = NULL;
2292 *name_len = 0;
2293 *has_predicate = 0;
2294
2295 if (!*parent_times) {
2296 if (!strncmp(*path, "..", 2)) {
2297 *path += 2;
2298 ++par_times;
2299 while (!strncmp(*path, "/..", 3)) {
2300 *path += 3;
2301 ++par_times;
2302 }
2303 }
2304 if (par_times) {
2305 *parent_times = par_times;
2306 } else {
2307 *parent_times = -1;
2308 }
2309 }
2310
2311 if (**path != '/') {
2312 return LY_EINVAL;
2313 }
2314 /* skip '/' */
2315 ++(*path);
2316
2317 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002318 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002319
2320 if ((**path == '/' && (*path)[1]) || !**path) {
2321 /* path continues by another token or this is the last token */
2322 return LY_SUCCESS;
2323 } else if ((*path)[0] != '[') {
2324 /* unexpected character */
2325 return LY_EINVAL;
2326 } else {
2327 /* predicate starting with [ */
2328 *has_predicate = 1;
2329 return LY_SUCCESS;
2330 }
2331}
2332
2333/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002334 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2335 *
2336 * The set of features used for target must be a subset of features used for the leafref.
2337 * This is not a perfect, we should compare the truth tables but it could require too much resources
2338 * and RFC 7950 does not require it explicitely, so we simplify that.
2339 *
2340 * @param[in] refnode The leafref node.
2341 * @param[in] target Tha target node of the leafref.
2342 * @return LY_SUCCESS or LY_EVALID;
2343 */
2344static LY_ERR
2345lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2346{
2347 LY_ERR ret = LY_EVALID;
2348 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002349 unsigned int u, v, count;
2350 struct ly_set features = {0};
2351
2352 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002353 if (iter->iffeatures) {
2354 LY_ARRAY_FOR(iter->iffeatures, u) {
2355 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2356 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002357 }
2358 }
2359 }
2360 }
2361
2362 /* we should have, in features set, a superset of features applicable to the target node.
2363 * So when adding features applicable to the target into the features set, we should not be
2364 * able to actually add any new feature, otherwise it is not a subset of features applicable
2365 * to the leafref itself. */
2366 count = features.count;
2367 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002368 if (iter->iffeatures) {
2369 LY_ARRAY_FOR(iter->iffeatures, u) {
2370 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2371 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002372 /* new feature was added (or LY_EMEM) */
2373 goto cleanup;
2374 }
2375 }
2376 }
2377 }
2378 }
2379 ret = LY_SUCCESS;
2380
2381cleanup:
2382 ly_set_erase(&features, NULL);
2383 return ret;
2384}
2385
2386/**
Radek Krejcia3045382018-11-22 14:30:31 +01002387 * @brief Validate the leafref path.
2388 * @param[in] ctx Compile context
2389 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002390 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002391 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2392 */
2393static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002394lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002395{
2396 const struct lysc_node *node = NULL, *parent = NULL;
2397 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002398 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002399 const char *id, *prefix, *name;
2400 size_t prefix_len, name_len;
2401 int parent_times = 0, has_predicate;
2402 unsigned int iter, u;
2403 LY_ERR ret = LY_SUCCESS;
Radek Krejci327de162019-06-14 12:52:07 +02002404 char *logpath;
Radek Krejcia3045382018-11-22 14:30:31 +01002405
2406 assert(ctx);
2407 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002408 assert(leafref);
2409
Radek Krejci327de162019-06-14 12:52:07 +02002410 logpath = lysc_path(startnode, LY_PATH_LOG);
2411 strncpy(ctx->path, logpath, LYSC_CTX_BUFSIZE - 1);
2412 ctx->path_len = strlen(logpath);
2413 free(logpath);
2414
Radek Krejcia3045382018-11-22 14:30:31 +01002415 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002416 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002417 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2418 if (!iter) { /* first iteration */
2419 /* precess ".." in relative paths */
2420 if (parent_times > 0) {
2421 /* move from the context node */
2422 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2423 /* path is supposed to be evaluated in data tree, so we have to skip
2424 * all schema nodes that cannot be instantiated in data tree */
2425 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002426 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002427 }
2428 }
2429 }
2430
2431 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002432 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002433 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002434 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002435 }
2436 if (!mod) {
2437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002438 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2439 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002440 return LY_EVALID;
2441 }
Radek Krejci3d929562019-02-21 11:25:55 +01002442 if (!mod->implemented) {
2443 /* make the module implemented */
2444 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2445 }
Radek Krejcia3045382018-11-22 14:30:31 +01002446
2447 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2448 if (!node) {
2449 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002450 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002451 return LY_EVALID;
2452 }
2453 parent = node;
2454
2455 if (has_predicate) {
2456 /* we have predicate, so the current result must be list */
2457 if (node->nodetype != LYS_LIST) {
2458 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2459 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002460 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002461 return LY_EVALID;
2462 }
2463
Radek Krejcibade82a2018-12-05 10:13:48 +01002464 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2465 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002466 }
2467
2468 ++iter;
2469 }
2470 if (ret) {
2471 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002472 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002473 return LY_EVALID;
2474 }
2475
2476 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2478 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002479 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002480 return LY_EVALID;
2481 }
2482
2483 /* check status */
2484 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2485 return LY_EVALID;
2486 }
2487
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002488 /* check config */
2489 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2490 if (node->flags & LYS_CONFIG_R) {
2491 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2492 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2493 leafref->path);
2494 return LY_EVALID;
2495 }
2496 }
2497
Radek Krejci412ddfa2018-11-23 11:44:11 +01002498 /* store the target's type and check for circular chain of leafrefs */
2499 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2500 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2501 if (type == (struct lysc_type*)leafref) {
2502 /* circular chain detected */
2503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2504 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2505 return LY_EVALID;
2506 }
2507 }
2508
Radek Krejci58d171e2018-11-23 13:50:55 +01002509 /* check if leafref and its target are under common if-features */
2510 if (lys_compile_leafref_features_validate(startnode, node)) {
2511 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2512 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2513 leafref->path);
2514 return LY_EVALID;
2515 }
2516
Radek Krejci327de162019-06-14 12:52:07 +02002517 ctx->path_len = 1;
2518 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002519 return LY_SUCCESS;
2520}
2521
Radek Krejcicdfecd92018-11-26 11:27:32 +01002522static 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 +02002523 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002524/**
2525 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2526 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002527 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2528 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2529 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2530 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002531 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002532 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002533 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002534 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2535 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2536 * @param[out] type Newly created type structure with the filled information about the type.
2537 * @return LY_ERR value.
2538 */
Radek Krejci19a96102018-11-15 13:38:09 +01002539static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002540lys_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 +02002541 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002542 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002543{
2544 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002545 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002546 struct lysc_type_bin *bin;
2547 struct lysc_type_num *num;
2548 struct lysc_type_str *str;
2549 struct lysc_type_bits *bits;
2550 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002551 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002552 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002553 struct lysc_type_union *un, *un_aux;
2554 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002555
2556 switch (basetype) {
2557 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002558 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002559
2560 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002561 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002562 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2563 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002564 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002565 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002566 }
2567 }
2568
2569 if (tpdfname) {
2570 type_p->compiled = *type;
2571 *type = calloc(1, sizeof(struct lysc_type_bin));
2572 }
2573 break;
2574 case LY_TYPE_BITS:
2575 /* RFC 7950 9.7 - bits */
2576 bits = (struct lysc_type_bits*)(*type);
2577 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002578 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002579 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2580 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002581 }
2582
Radek Krejci555cb5b2018-11-16 14:54:33 +01002583 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002585 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002587 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002589 free(*type);
2590 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002592 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 }
2594
2595 if (tpdfname) {
2596 type_p->compiled = *type;
2597 *type = calloc(1, sizeof(struct lysc_type_bits));
2598 }
2599 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002600 case LY_TYPE_DEC64:
2601 dec = (struct lysc_type_dec*)(*type);
2602
2603 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002604 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002605 if (!type_p->fraction_digits) {
2606 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002607 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002608 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002610 free(*type);
2611 *type = NULL;
2612 }
2613 return LY_EVALID;
2614 }
2615 } else if (type_p->fraction_digits) {
2616 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002617 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2619 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002620 tpdfname);
2621 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2623 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002624 free(*type);
2625 *type = NULL;
2626 }
2627 return LY_EVALID;
2628 }
2629 dec->fraction_digits = type_p->fraction_digits;
2630
2631 /* RFC 7950 9.2.4 - range */
2632 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002633 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2634 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002635 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002636 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002637 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002638 }
2639
2640 if (tpdfname) {
2641 type_p->compiled = *type;
2642 *type = calloc(1, sizeof(struct lysc_type_dec));
2643 }
2644 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002645 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002646 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002647
2648 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002649 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002650 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2651 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002652 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002653 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002654 }
2655 } else if (base && ((struct lysc_type_str*)base)->length) {
2656 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2657 }
2658
2659 /* RFC 7950 9.4.5 - pattern */
2660 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002661 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002662 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002663 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2664 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2665 }
2666
2667 if (tpdfname) {
2668 type_p->compiled = *type;
2669 *type = calloc(1, sizeof(struct lysc_type_str));
2670 }
2671 break;
2672 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002673 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002674
2675 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002676 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002677 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002678 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002679 }
2680
Radek Krejci555cb5b2018-11-16 14:54:33 +01002681 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002682 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002683 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002685 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002687 free(*type);
2688 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002689 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002690 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002691 }
2692
2693 if (tpdfname) {
2694 type_p->compiled = *type;
2695 *type = calloc(1, sizeof(struct lysc_type_enum));
2696 }
2697 break;
2698 case LY_TYPE_INT8:
2699 case LY_TYPE_UINT8:
2700 case LY_TYPE_INT16:
2701 case LY_TYPE_UINT16:
2702 case LY_TYPE_INT32:
2703 case LY_TYPE_UINT32:
2704 case LY_TYPE_INT64:
2705 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002706 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002707
2708 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002709 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002710 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2711 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002712 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002713 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002714 }
2715 }
2716
2717 if (tpdfname) {
2718 type_p->compiled = *type;
2719 *type = calloc(1, sizeof(struct lysc_type_num));
2720 }
2721 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002722 case LY_TYPE_IDENT:
2723 idref = (struct lysc_type_identityref*)(*type);
2724
2725 /* RFC 7950 9.10.2 - base */
2726 if (type_p->bases) {
2727 if (base) {
2728 /* only the directly derived identityrefs can contain base specification */
2729 if (tpdfname) {
2730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002731 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002732 tpdfname);
2733 } else {
2734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002735 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002736 free(*type);
2737 *type = NULL;
2738 }
2739 return LY_EVALID;
2740 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002741 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002742 }
2743
2744 if (!base && !type_p->flags) {
2745 /* type derived from identityref built-in type must contain at least one base */
2746 if (tpdfname) {
2747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2748 } else {
2749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2750 free(*type);
2751 *type = NULL;
2752 }
2753 return LY_EVALID;
2754 }
2755
2756 if (tpdfname) {
2757 type_p->compiled = *type;
2758 *type = calloc(1, sizeof(struct lysc_type_identityref));
2759 }
2760 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002761 case LY_TYPE_LEAFREF:
2762 /* RFC 7950 9.9.3 - require-instance */
2763 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002764 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002765 if (tpdfname) {
2766 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2767 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2768 } else {
2769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2770 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2771 free(*type);
2772 *type = NULL;
2773 }
2774 return LY_EVALID;
2775 }
Radek Krejcia3045382018-11-22 14:30:31 +01002776 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002777 } else if (base) {
2778 /* inherit */
2779 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002780 } else {
2781 /* default is true */
2782 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2783 }
2784 if (type_p->path) {
2785 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002786 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002787 } else if (base) {
2788 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002789 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002790 } else if (tpdfname) {
2791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2792 return LY_EVALID;
2793 } else {
2794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2795 free(*type);
2796 *type = NULL;
2797 return LY_EVALID;
2798 }
2799 if (tpdfname) {
2800 type_p->compiled = *type;
2801 *type = calloc(1, sizeof(struct lysc_type_leafref));
2802 }
2803 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002804 case LY_TYPE_INST:
2805 /* RFC 7950 9.9.3 - require-instance */
2806 if (type_p->flags & LYS_SET_REQINST) {
2807 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2808 } else {
2809 /* default is true */
2810 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2811 }
2812
2813 if (tpdfname) {
2814 type_p->compiled = *type;
2815 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2816 }
2817 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002818 case LY_TYPE_UNION:
2819 un = (struct lysc_type_union*)(*type);
2820
2821 /* RFC 7950 7.4 - type */
2822 if (type_p->types) {
2823 if (base) {
2824 /* only the directly derived union can contain types specification */
2825 if (tpdfname) {
2826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2827 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2828 tpdfname);
2829 } else {
2830 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2831 "Invalid type substatement for the type not directly derived from union built-in type.");
2832 free(*type);
2833 *type = NULL;
2834 }
2835 return LY_EVALID;
2836 }
2837 /* compile the type */
2838 additional = 0;
2839 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2840 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002841 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 +01002842 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2843 /* add space for additional types from the union subtype */
2844 un_aux = (struct lysc_type_union *)un->types[u + additional];
2845 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)));
2846 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2847 un->types = (void*)((uint32_t*)(p) + 1);
2848
2849 /* copy subtypes of the subtype union */
2850 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2851 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2852 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2853 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2854 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2855 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2856 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2857 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2858 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2859 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2860 /* TODO extensions */
2861
2862 } else {
2863 un->types[u + additional] = un_aux->types[v];
2864 ++un_aux->types[v]->refcount;
2865 }
2866 ++additional;
2867 LY_ARRAY_INCREMENT(un->types);
2868 }
2869 /* compensate u increment in main loop */
2870 --additional;
2871
2872 /* free the replaced union subtype */
2873 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2874 } else {
2875 LY_ARRAY_INCREMENT(un->types);
2876 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002877 }
2878 }
2879
2880 if (!base && !type_p->flags) {
2881 /* type derived from union built-in type must contain at least one type */
2882 if (tpdfname) {
2883 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2884 } else {
2885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2886 free(*type);
2887 *type = NULL;
2888 }
2889 return LY_EVALID;
2890 }
2891
2892 if (tpdfname) {
2893 type_p->compiled = *type;
2894 *type = calloc(1, sizeof(struct lysc_type_union));
2895 }
2896 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002897 case LY_TYPE_BOOL:
2898 case LY_TYPE_EMPTY:
2899 case LY_TYPE_UNKNOWN: /* just to complete switch */
2900 break;
2901 }
2902 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2903done:
2904 return ret;
2905}
2906
Radek Krejcia3045382018-11-22 14:30:31 +01002907/**
2908 * @brief Compile information about the leaf/leaf-list's type.
2909 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002910 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2911 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2912 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2913 * @param[in] context_name Name of the context node or referencing typedef for logging.
2914 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002915 * @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 +01002916 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002917 * @return LY_ERR value.
2918 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002919static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002920lys_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 +02002921 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002922{
2923 LY_ERR ret = LY_SUCCESS;
2924 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002925 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002926 struct type_context {
2927 const struct lysp_tpdf *tpdf;
2928 struct lysp_node *node;
2929 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002930 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002931 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002932 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002933 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002934 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002935
2936 (*type) = NULL;
2937
2938 tctx = calloc(1, sizeof *tctx);
2939 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002940 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002941 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2942 ret == LY_SUCCESS;
2943 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2944 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2945 if (basetype) {
2946 break;
2947 }
2948
2949 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002950 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2951 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002952 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2953
Radek Krejcicdfecd92018-11-26 11:27:32 +01002954 if (units && !*units) {
2955 /* inherit units */
2956 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2957 }
Radek Krejci01342af2019-01-03 15:18:08 +01002958 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002959 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002960 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002961 }
Radek Krejci01342af2019-01-03 15:18:08 +01002962 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002963 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2964 break;
2965 }
2966
Radek Krejci19a96102018-11-15 13:38:09 +01002967 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002968 /* it is not necessary to continue, the rest of the chain was already compiled,
2969 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002970 basetype = tctx->tpdf->type.compiled->basetype;
2971 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002972 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002973 dummyloops = 1;
2974 goto preparenext;
2975 } else {
2976 tctx = NULL;
2977 break;
2978 }
Radek Krejci19a96102018-11-15 13:38:09 +01002979 }
2980
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002981 /* circular typedef reference detection */
2982 for (u = 0; u < tpdf_chain.count; u++) {
2983 /* local part */
2984 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2985 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2987 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2988 free(tctx);
2989 ret = LY_EVALID;
2990 goto cleanup;
2991 }
2992 }
2993 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2994 /* global part for unions corner case */
2995 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2996 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2998 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2999 free(tctx);
3000 ret = LY_EVALID;
3001 goto cleanup;
3002 }
3003 }
3004
Radek Krejci19a96102018-11-15 13:38:09 +01003005 /* store information for the following processing */
3006 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3007
Radek Krejcicdfecd92018-11-26 11:27:32 +01003008preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003009 /* prepare next loop */
3010 tctx_prev = tctx;
3011 tctx = calloc(1, sizeof *tctx);
3012 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3013 }
3014 free(tctx);
3015
3016 /* allocate type according to the basetype */
3017 switch (basetype) {
3018 case LY_TYPE_BINARY:
3019 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003020 break;
3021 case LY_TYPE_BITS:
3022 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003023 break;
3024 case LY_TYPE_BOOL:
3025 case LY_TYPE_EMPTY:
3026 *type = calloc(1, sizeof(struct lysc_type));
3027 break;
3028 case LY_TYPE_DEC64:
3029 *type = calloc(1, sizeof(struct lysc_type_dec));
3030 break;
3031 case LY_TYPE_ENUM:
3032 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003033 break;
3034 case LY_TYPE_IDENT:
3035 *type = calloc(1, sizeof(struct lysc_type_identityref));
3036 break;
3037 case LY_TYPE_INST:
3038 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3039 break;
3040 case LY_TYPE_LEAFREF:
3041 *type = calloc(1, sizeof(struct lysc_type_leafref));
3042 break;
3043 case LY_TYPE_STRING:
3044 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003045 break;
3046 case LY_TYPE_UNION:
3047 *type = calloc(1, sizeof(struct lysc_type_union));
3048 break;
3049 case LY_TYPE_INT8:
3050 case LY_TYPE_UINT8:
3051 case LY_TYPE_INT16:
3052 case LY_TYPE_UINT16:
3053 case LY_TYPE_INT32:
3054 case LY_TYPE_UINT32:
3055 case LY_TYPE_INT64:
3056 case LY_TYPE_UINT64:
3057 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003058 break;
3059 case LY_TYPE_UNKNOWN:
3060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3061 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3062 ret = LY_EVALID;
3063 goto cleanup;
3064 }
3065 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003066 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3068 ly_data_type2str[basetype]);
3069 free(*type);
3070 (*type) = NULL;
3071 ret = LY_EVALID;
3072 goto cleanup;
3073 }
3074
3075 /* get restrictions from the referred typedefs */
3076 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3077 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003078
3079 /* remember the typedef context for circular check */
3080 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3081
Radek Krejci43699232018-11-23 14:59:46 +01003082 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003083 base = tctx->tpdf->type.compiled;
3084 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003085 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003086 /* no change, just use the type information from the base */
3087 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3088 ++base->refcount;
3089 continue;
3090 }
3091
3092 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003093 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3095 tctx->tpdf->name, ly_data_type2str[basetype]);
3096 ret = LY_EVALID;
3097 goto cleanup;
3098 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3099 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3100 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3101 tctx->tpdf->name, tctx->tpdf->dflt);
3102 ret = LY_EVALID;
3103 goto cleanup;
3104 }
3105
Radek Krejci19a96102018-11-15 13:38:09 +01003106 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003107 /* TODO user type plugins */
3108 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003109 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003110 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 +01003111 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003112 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003113 LY_CHECK_GOTO(ret, cleanup);
3114 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003115 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003116 /* remove the processed typedef contexts from the stack for circular check */
3117 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003118
Radek Krejcic5c27e52018-11-15 14:38:11 +01003119 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003120 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003121 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003122 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003123 /* TODO user type plugins */
3124 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003125 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003126 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 +01003127 LY_CHECK_GOTO(ret, cleanup);
3128 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003129 /* no specific restriction in leaf's type definition, copy from the base */
3130 free(*type);
3131 (*type) = base;
3132 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003133 }
Radek Krejci01342af2019-01-03 15:18:08 +01003134 if (!(*type)->dflt) {
3135 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3136 }
Radek Krejci19a96102018-11-15 13:38:09 +01003137
Radek Krejciec4da802019-05-02 13:02:41 +02003138 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003139
3140cleanup:
3141 ly_set_erase(&tpdf_chain, free);
3142 return ret;
3143}
3144
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003145/**
3146 * @brief Compile status information of the given node.
3147 *
3148 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3149 * has the status correctly set during the compilation.
3150 *
3151 * @param[in] ctx Compile context
3152 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3153 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3154 * the compatibility with the parent's status value.
3155 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3156 * @return LY_ERR value.
3157 */
3158static LY_ERR
3159lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3160{
3161 /* status - it is not inherited by specification, but it does not make sense to have
3162 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3163 if (!((*node_flags) & LYS_STATUS_MASK)) {
3164 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3165 if ((parent_flags & 0x3) != 0x3) {
3166 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3167 * combination of bits (0x3) which marks the uses_status value */
3168 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3169 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3170 }
3171 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3172 } else {
3173 (*node_flags) |= LYS_STATUS_CURR;
3174 }
3175 } else if (parent_flags & LYS_STATUS_MASK) {
3176 /* check status compatibility with the parent */
3177 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3178 if ((*node_flags) & LYS_STATUS_CURR) {
3179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3180 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3181 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3182 } else { /* LYS_STATUS_DEPRC */
3183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3184 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3185 }
3186 return LY_EVALID;
3187 }
3188 }
3189 return LY_SUCCESS;
3190}
3191
Radek Krejci8cce8532019-03-05 11:27:45 +01003192/**
3193 * @brief Check uniqness of the node/action/notification name.
3194 *
3195 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3196 * structures, but they share the namespace so we need to check their name collisions.
3197 *
3198 * @param[in] ctx Compile context.
3199 * @param[in] children List (linked list) of data nodes to go through.
3200 * @param[in] actions List (sized array) of actions or RPCs to go through.
3201 * @param[in] notifs List (sized array) of Notifications to go through.
3202 * @param[in] name Name of the item to find in the given lists.
3203 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3204 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3205 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3206 */
3207static LY_ERR
3208lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3209 const struct lysc_action *actions, const struct lysc_notif *notifs,
3210 const char *name, void *exclude)
3211{
3212 const struct lysc_node *iter;
3213 unsigned int u;
3214
3215 LY_LIST_FOR(children, iter) {
3216 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3217 goto error;
3218 }
3219 }
3220 LY_ARRAY_FOR(actions, u) {
3221 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3222 goto error;
3223 }
3224 }
3225 LY_ARRAY_FOR(notifs, u) {
3226 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3227 goto error;
3228 }
3229 }
3230 return LY_SUCCESS;
3231error:
3232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3233 return LY_EEXIST;
3234}
3235
Radek Krejciec4da802019-05-02 13:02:41 +02003236static 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 +01003237
Radek Krejcia3045382018-11-22 14:30:31 +01003238/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003239 * @brief Compile parsed RPC/action schema node information.
3240 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003241 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003242 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003243 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3244 * @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).
3245 * Zero means no uses, non-zero value with no status bit set mean the default status.
3246 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3247 */
3248static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003249lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003250 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3251{
3252 LY_ERR ret = LY_SUCCESS;
3253 struct lysp_node *child_p;
3254 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003255 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003256
Radek Krejci327de162019-06-14 12:52:07 +02003257 lysc_update_path(ctx, parent, action_p->name);
3258
Radek Krejci8cce8532019-03-05 11:27:45 +01003259 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3260 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3261 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3262 action_p->name, action)) {
3263 return LY_EVALID;
3264 }
3265
Radek Krejciec4da802019-05-02 13:02:41 +02003266 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003268 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003269 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003270 return LY_EVALID;
3271 }
3272
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003273 action->nodetype = LYS_ACTION;
3274 action->module = ctx->mod;
3275 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003276 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003277 action->sp = action_p;
3278 }
3279 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3280
3281 /* status - it is not inherited by specification, but it does not make sense to have
3282 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3283 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3284
3285 DUP_STRING(ctx->ctx, action_p->name, action->name);
3286 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3287 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003288 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3289 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003290
3291 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003292 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003293 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3294 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3295 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003296 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003297 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003298 }
Radek Krejci327de162019-06-14 12:52:07 +02003299 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003300 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003301
3302 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003303 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003304 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3305 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3306 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003307 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003308 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003309 }
Radek Krejci327de162019-06-14 12:52:07 +02003310 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003311
Radek Krejci327de162019-06-14 12:52:07 +02003312 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003313cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003314 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003315 return ret;
3316}
3317
3318/**
Radek Krejci43981a32019-04-12 09:44:11 +02003319 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003320 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003321 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003322 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3323 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3324 * @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 +02003325 * Zero means no uses, non-zero value with no status bit set mean the default status.
3326 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3327 */
3328static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003329lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003330 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3331{
3332 LY_ERR ret = LY_SUCCESS;
3333 struct lysp_node *child_p;
3334 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003335 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003336
Radek Krejci327de162019-06-14 12:52:07 +02003337 lysc_update_path(ctx, parent, notif_p->name);
3338
Radek Krejcifc11bd72019-04-11 16:00:05 +02003339 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3340 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3341 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3342 notif_p->name, notif)) {
3343 return LY_EVALID;
3344 }
3345
Radek Krejciec4da802019-05-02 13:02:41 +02003346 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3348 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003349 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003350 return LY_EVALID;
3351 }
3352
3353 notif->nodetype = LYS_NOTIF;
3354 notif->module = ctx->mod;
3355 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003356 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003357 notif->sp = notif_p;
3358 }
3359 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3360
3361 /* status - it is not inherited by specification, but it does not make sense to have
3362 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3363 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3364
3365 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3366 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3367 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003368 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3369 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3370 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003371
Radek Krejciec4da802019-05-02 13:02:41 +02003372 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003373 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003374 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003375 }
3376
Radek Krejci327de162019-06-14 12:52:07 +02003377 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003378cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003379 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003380 return ret;
3381}
3382
3383/**
Radek Krejcia3045382018-11-22 14:30:31 +01003384 * @brief Compile parsed container node information.
3385 * @param[in] ctx Compile context
3386 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003387 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3388 * is enriched with the container-specific information.
3389 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3390 */
Radek Krejci19a96102018-11-15 13:38:09 +01003391static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003392lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003393{
3394 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3395 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3396 struct lysp_node *child_p;
3397 unsigned int u;
3398 LY_ERR ret = LY_SUCCESS;
3399
Radek Krejcife909632019-02-12 15:34:42 +01003400 if (cont_p->presence) {
3401 cont->flags |= LYS_PRESENCE;
3402 }
3403
Radek Krejci19a96102018-11-15 13:38:09 +01003404 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003405 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003406 }
3407
Radek Krejciec4da802019-05-02 13:02:41 +02003408 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3409 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3410 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003411
3412done:
3413 return ret;
3414}
3415
Radek Krejci33f72892019-02-21 10:36:58 +01003416/*
3417 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3418 * @param[in] ctx Compile context.
3419 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3420 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003421 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3422 * @return LY_ERR value.
3423 */
3424static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003425lys_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 +01003426{
3427 unsigned int u, v;
3428 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3429
Radek Krejciec4da802019-05-02 13:02:41 +02003430 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 +01003431 leaf->units ? NULL : &leaf->units));
3432 if (leaf->nodetype == LYS_LEAFLIST) {
3433 if (llist->type->dflt && !llist->dflts && !llist->min) {
3434 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3435 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3436 LY_ARRAY_INCREMENT(llist->dflts);
3437 }
3438 } else {
3439 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3440 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3441 }
3442 }
3443 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3444 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3445 ly_set_add(&ctx->unres, leaf, 0);
3446 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3447 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3448 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3449 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3450 ly_set_add(&ctx->unres, leaf, 0);
3451 }
3452 }
3453 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3454 if (leaf->flags & LYS_SET_DFLT) {
3455 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3456 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3457 return LY_EVALID;
3458 }
3459 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3460 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3461 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3462 return LY_EVALID;
3463 }
3464 }
3465
3466 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3467 /* configuration data values must be unique - so check the default values */
3468 LY_ARRAY_FOR(llist->dflts, u) {
3469 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3470 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3471 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3472 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3473 return LY_EVALID;
3474 }
3475 }
3476 }
3477 }
3478
3479 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3480
3481 return LY_SUCCESS;
3482}
3483
Radek Krejcia3045382018-11-22 14:30:31 +01003484/**
3485 * @brief Compile parsed leaf node information.
3486 * @param[in] ctx Compile context
3487 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003488 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3489 * is enriched with the leaf-specific information.
3490 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3491 */
Radek Krejci19a96102018-11-15 13:38:09 +01003492static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003493lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003494{
3495 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3496 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3497 unsigned int u;
3498 LY_ERR ret = LY_SUCCESS;
3499
Radek Krejciec4da802019-05-02 13:02:41 +02003500 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003501 if (leaf_p->units) {
3502 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3503 leaf->flags |= LYS_SET_UNITS;
3504 }
3505 if (leaf_p->dflt) {
3506 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003507 leaf->flags |= LYS_SET_DFLT;
3508 }
Radek Krejci43699232018-11-23 14:59:46 +01003509
Radek Krejciec4da802019-05-02 13:02:41 +02003510 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003511
Radek Krejci19a96102018-11-15 13:38:09 +01003512done:
3513 return ret;
3514}
3515
Radek Krejcia3045382018-11-22 14:30:31 +01003516/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003517 * @brief Compile parsed leaf-list node information.
3518 * @param[in] ctx Compile context
3519 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003520 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3521 * is enriched with the leaf-list-specific information.
3522 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3523 */
3524static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003525lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003526{
3527 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3528 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003529 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003530 LY_ERR ret = LY_SUCCESS;
3531
Radek Krejciec4da802019-05-02 13:02:41 +02003532 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003533 if (llist_p->units) {
3534 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3535 llist->flags |= LYS_SET_UNITS;
3536 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003537
3538 if (llist_p->dflts) {
3539 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3540 LY_ARRAY_FOR(llist_p->dflts, u) {
3541 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3542 LY_ARRAY_INCREMENT(llist->dflts);
3543 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003544 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003545 }
3546
3547 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003548 if (llist->min) {
3549 llist->flags |= LYS_MAND_TRUE;
3550 }
Radek Krejcib7408632018-11-28 17:12:11 +01003551 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003552
Radek Krejciec4da802019-05-02 13:02:41 +02003553 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003554
3555done:
3556 return ret;
3557}
3558
3559/**
Radek Krejci7af64242019-02-18 13:07:53 +01003560 * @brief Compile information about list's uniques.
3561 * @param[in] ctx Compile context.
3562 * @param[in] context_module Module where the prefixes are going to be resolved.
3563 * @param[in] uniques Sized array list of unique statements.
3564 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3565 * @return LY_ERR value.
3566 */
3567static LY_ERR
3568lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3569{
3570 LY_ERR ret = LY_SUCCESS;
3571 struct lysc_node_leaf **key, ***unique;
3572 const char *keystr, *delim;
3573 size_t len;
3574 unsigned int v;
3575 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003576 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003577
3578 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3579 config = -1;
3580 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3581 keystr = uniques[v];
3582 while (keystr) {
3583 delim = strpbrk(keystr, " \t\n");
3584 if (delim) {
3585 len = delim - keystr;
3586 while (isspace(*delim)) {
3587 ++delim;
3588 }
3589 } else {
3590 len = strlen(keystr);
3591 }
3592
3593 /* unique node must be present */
3594 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003595 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3596 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003597 if (ret != LY_SUCCESS) {
3598 if (ret == LY_EDENIED) {
3599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003600 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003601 len, keystr, lys_nodetype2str((*key)->nodetype));
3602 }
3603 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003604 } else if (flags) {
3605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3606 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3607 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3608 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003609 }
3610
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003611
Radek Krejci7af64242019-02-18 13:07:53 +01003612 /* all referenced leafs must be of the same config type */
3613 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3615 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3616 return LY_EVALID;
3617 } else if ((*key)->flags & LYS_CONFIG_W) {
3618 config = 1;
3619 } else { /* LYS_CONFIG_R */
3620 config = 0;
3621 }
3622
3623 /* check status */
3624 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3625 (*key)->flags, (*key)->module, (*key)->name));
3626
3627 /* mark leaf as unique */
3628 (*key)->flags |= LYS_UNIQUE;
3629
3630 /* next unique value in line */
3631 keystr = delim;
3632 }
3633 /* next unique definition */
3634 }
3635
3636 return LY_SUCCESS;
3637}
3638
3639/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003640 * @brief Compile parsed list node information.
3641 * @param[in] ctx Compile context
3642 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003643 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3644 * is enriched with the list-specific information.
3645 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3646 */
3647static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003648lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003649{
3650 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3651 struct lysc_node_list *list = (struct lysc_node_list*)node;
3652 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003653 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003654 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003655 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003656 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003657 LY_ERR ret = LY_SUCCESS;
3658
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003659 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003660 if (list->min) {
3661 list->flags |= LYS_MAND_TRUE;
3662 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003663 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3664
3665 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003666 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003667 }
3668
Radek Krejciec4da802019-05-02 13:02:41 +02003669 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003670
3671 /* keys */
3672 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3674 return LY_EVALID;
3675 }
3676
3677 /* find all the keys (must be direct children) */
3678 keystr = list_p->key;
3679 while (keystr) {
3680 delim = strpbrk(keystr, " \t\n");
3681 if (delim) {
3682 len = delim - keystr;
3683 while (isspace(*delim)) {
3684 ++delim;
3685 }
3686 } else {
3687 len = strlen(keystr);
3688 }
3689
3690 /* key node must be present */
3691 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3692 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3693 if (!(*key)) {
3694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3695 "The list's key \"%.*s\" not found.", len, keystr);
3696 return LY_EVALID;
3697 }
3698 /* keys must be unique */
3699 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3700 if (*key == list->keys[u]) {
3701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3702 "Duplicated key identifier \"%.*s\".", len, keystr);
3703 return LY_EVALID;
3704 }
3705 }
Radek Krejci327de162019-06-14 12:52:07 +02003706 lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003707 /* key must have the same config flag as the list itself */
3708 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3710 return LY_EVALID;
3711 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003712 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003713 /* YANG 1.0 denies key to be of empty type */
3714 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003716 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003717 return LY_EVALID;
3718 }
3719 } else {
3720 /* when and if-feature are illegal on list keys */
3721 if ((*key)->when) {
3722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003723 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003724 return LY_EVALID;
3725 }
3726 if ((*key)->iffeatures) {
3727 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003728 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003729 return LY_EVALID;
3730 }
3731 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003732
3733 /* check status */
3734 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3735 (*key)->flags, (*key)->module, (*key)->name));
3736
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003737 /* ignore default values of the key */
3738 if ((*key)->dflt) {
3739 lydict_remove(ctx->ctx, (*key)->dflt);
3740 (*key)->dflt = NULL;
3741 }
3742 /* mark leaf as key */
3743 (*key)->flags |= LYS_KEY;
3744
3745 /* next key value */
3746 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003747 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003748 }
3749
3750 /* uniques */
3751 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003752 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003753 }
3754
Radek Krejciec4da802019-05-02 13:02:41 +02003755 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3756 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003757
3758done:
3759 return ret;
3760}
3761
Radek Krejcib56c7502019-02-13 14:19:54 +01003762/**
3763 * @brief Do some checks and set the default choice's case.
3764 *
3765 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3766 *
3767 * @param[in] ctx Compile context.
3768 * @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,
3769 * not the reference to the imported module.
3770 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3771 * @return LY_ERR value.
3772 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003773static LY_ERR
3774lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3775{
3776 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3777 const char *prefix = NULL, *name;
3778 size_t prefix_len = 0;
3779
3780 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3781 name = strchr(dflt, ':');
3782 if (name) {
3783 prefix = dflt;
3784 prefix_len = name - prefix;
3785 ++name;
3786 } else {
3787 name = dflt;
3788 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003789 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003790 /* prefixed default case make sense only for the prefix of the schema itself */
3791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3792 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3793 prefix_len, prefix);
3794 return LY_EVALID;
3795 }
3796 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3797 if (!ch->dflt) {
3798 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3799 "Default case \"%s\" not found.", dflt);
3800 return LY_EVALID;
3801 }
3802 /* no mandatory nodes directly under the default case */
3803 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003804 if (iter->parent != (struct lysc_node*)ch->dflt) {
3805 break;
3806 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003807 if (iter->flags & LYS_MAND_TRUE) {
3808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3809 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3810 return LY_EVALID;
3811 }
3812 }
Radek Krejci01342af2019-01-03 15:18:08 +01003813 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003814 return LY_SUCCESS;
3815}
3816
Radek Krejciccd20f12019-02-15 14:12:27 +01003817static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003818lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003819{
3820 struct lys_module *mod;
3821 const char *prefix = NULL, *name;
3822 size_t prefix_len = 0;
3823 struct lysc_node_case *cs;
3824 struct lysc_node *node;
3825
3826 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3827 name = strchr(dflt, ':');
3828 if (name) {
3829 prefix = dflt;
3830 prefix_len = name - prefix;
3831 ++name;
3832 } else {
3833 name = dflt;
3834 }
3835 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3836 if (prefix) {
3837 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3838 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003839 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3840 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003841 return LY_EVALID;
3842 }
3843 } else {
3844 mod = ctx->mod;
3845 }
3846 /* get the default case */
3847 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3848 if (!cs) {
3849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003850 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003851 return LY_EVALID;
3852 }
3853
3854 /* check that there is no mandatory node */
3855 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003856 if (node->parent != (struct lysc_node*)cs) {
3857 break;
3858 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003859 if (node->flags & LYS_MAND_TRUE) {
3860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003861 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3862 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003863 return LY_EVALID;
3864 }
3865 }
3866
3867 /* set the default case in choice */
3868 ch->dflt = cs;
3869 cs->flags |= LYS_SET_DFLT;
3870
3871 return LY_SUCCESS;
3872}
3873
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003874/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003875 * @brief Compile parsed choice node information.
3876 * @param[in] ctx Compile context
3877 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003878 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003879 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003880 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3881 */
3882static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003883lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003884{
3885 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3886 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3887 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003888 LY_ERR ret = LY_SUCCESS;
3889
Radek Krejci056d0a82018-12-06 16:57:25 +01003890 LY_LIST_FOR(ch_p->child, child_p) {
3891 if (child_p->nodetype == LYS_CASE) {
3892 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003893 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003894 }
3895 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003896 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003897 }
3898 }
3899
3900 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003901 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003902 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003903 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003904
Radek Krejci9800fb82018-12-13 14:26:23 +01003905 return ret;
3906}
3907
3908/**
3909 * @brief Compile parsed anydata or anyxml node information.
3910 * @param[in] ctx Compile context
3911 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01003912 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3913 * is enriched with the any-specific information.
3914 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3915 */
3916static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003917lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01003918{
3919 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3920 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3921 unsigned int u;
3922 LY_ERR ret = LY_SUCCESS;
3923
Radek Krejciec4da802019-05-02 13:02:41 +02003924 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01003925
3926 if (any->flags & LYS_CONFIG_W) {
3927 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3928 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3929 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003930done:
3931 return ret;
3932}
3933
Radek Krejcib56c7502019-02-13 14:19:54 +01003934/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003935 * @brief Connect the node into the siblings list and check its name uniqueness.
3936 *
3937 * @param[in] ctx Compile context
3938 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3939 * the choice itself is expected instead of a specific case node.
3940 * @param[in] node Schema node to connect into the list.
3941 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3942 */
3943static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003944lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003945{
3946 struct lysc_node **children;
3947
3948 if (node->nodetype == LYS_CASE) {
3949 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3950 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003951 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003952 }
3953 if (children) {
3954 if (!(*children)) {
3955 /* first child */
3956 *children = node;
3957 } else if (*children != node) {
3958 /* by the condition in previous branch we cover the choice/case children
3959 * - the children list is shared by the choice and the the first case, in addition
3960 * the first child of each case must be referenced from the case node. So the node is
3961 * actually always already inserted in case it is the first children - so here such
3962 * a situation actually corresponds to the first branch */
3963 /* insert at the end of the parent's children list */
3964 (*children)->prev->next = node;
3965 node->prev = (*children)->prev;
3966 (*children)->prev = node;
3967
3968 /* check the name uniqueness */
3969 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3970 lysc_node_notifs(parent), node->name, node)) {
3971 return LY_EEXIST;
3972 }
3973 }
3974 }
3975 return LY_SUCCESS;
3976}
3977
Radek Krejci95710c92019-02-11 15:49:55 +01003978/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003979 * @brief Get the XPath context node for the given schema node.
3980 * @param[in] start The schema node where the XPath expression appears.
3981 * @return The context node to evaluate XPath expression in given schema node.
3982 * @return NULL in case the context node is the root node.
3983 */
3984static struct lysc_node *
3985lysc_xpath_context(struct lysc_node *start)
3986{
3987 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3988 start = start->parent);
3989 return start;
3990}
3991
3992/**
3993 * @brief Prepare the case structure in choice node for the new data node.
3994 *
3995 * 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
3996 * created in the choice when the first child was processed.
3997 *
3998 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003999 * @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,
4000 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004001 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4002 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4003 * @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,
4004 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004005 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004006static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004007lys_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 +01004008{
4009 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004010 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004011 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004012 unsigned int u;
4013 LY_ERR ret;
4014
Radek Krejci95710c92019-02-11 15:49:55 +01004015#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004016 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004017 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4019 return NULL; \
4020 } \
4021 }
4022
Radek Krejci95710c92019-02-11 15:49:55 +01004023 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4024 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004025
4026 /* we have to add an implicit case node into the parent choice */
4027 cs = calloc(1, sizeof(struct lysc_node_case));
4028 DUP_STRING(ctx->ctx, child->name, cs->name);
4029 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004030 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004031 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4032 /* the case is already present since the child is not its first children */
4033 return (struct lysc_node_case*)ch->cases->prev;
4034 }
Radek Krejci95710c92019-02-11 15:49:55 +01004035 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004036
4037 /* explicit parent case is not present (this is its first child) */
4038 cs = calloc(1, sizeof(struct lysc_node_case));
4039 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4040 cs->flags = LYS_STATUS_MASK & node_p->flags;
4041 cs->sp = node_p;
4042
Radek Krejcib1b59152019-01-07 13:21:56 +01004043 /* 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 +02004044 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004045
4046 if (node_p->when) {
4047 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004048 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004049 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004050 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004051 }
Radek Krejciec4da802019-05-02 13:02:41 +02004052 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004053 } else {
4054 LOGINT(ctx->ctx);
4055 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004056 }
4057 cs->module = ctx->mod;
4058 cs->prev = (struct lysc_node*)cs;
4059 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004060 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004061 cs->parent = (struct lysc_node*)ch;
4062 cs->child = child;
4063
4064 return cs;
4065error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004066 if (cs) {
4067 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4068 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004069 return NULL;
4070
4071#undef UNIQUE_CHECK
4072}
4073
Radek Krejcib56c7502019-02-13 14:19:54 +01004074/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004075 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004076 *
4077 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004078 * @param[in] node Target node where the config is supposed to be changed.
4079 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004080 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4081 * 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 +01004082 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4083 * @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 +01004084 * @return LY_ERR value.
4085 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004086static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004087lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004088 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004089{
4090 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004091 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004092
4093 if (config == (node->flags & LYS_CONFIG_MASK)) {
4094 /* nothing to do */
4095 return LY_SUCCESS;
4096 }
4097
4098 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004099 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004100 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004102 "Invalid %s of config - configuration node cannot be child of any state data node.",
4103 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004104 return LY_EVALID;
4105 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004106 node->flags |= LYS_SET_CONFIG;
4107 } else {
4108 if (node->flags & LYS_SET_CONFIG) {
4109 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4110 /* setting config flags, but have node with explicit config true */
4111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004112 "Invalid %s of config - configuration node cannot be child of any state data node.",
4113 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004114 return LY_EVALID;
4115 }
4116 /* do not change config on nodes where the config is explicitely set, this does not apply to
4117 * nodes, which are being changed explicitly (targets of refine or deviation) */
4118 return LY_SUCCESS;
4119 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004120 }
4121 node->flags &= ~LYS_CONFIG_MASK;
4122 node->flags |= config;
4123
4124 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004125 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004126 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004127 }
4128
Radek Krejci76b3e962018-12-14 17:01:25 +01004129 return LY_SUCCESS;
4130}
4131
Radek Krejcib56c7502019-02-13 14:19:54 +01004132/**
4133 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4134 *
4135 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4136 * the flag to such parents from a mandatory children.
4137 *
4138 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4139 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4140 * (mandatory children was removed).
4141 */
Radek Krejcife909632019-02-12 15:34:42 +01004142void
4143lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4144{
4145 struct lysc_node *iter;
4146
4147 if (add) { /* set flag */
4148 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4149 parent = parent->parent) {
4150 parent->flags |= LYS_MAND_TRUE;
4151 }
4152 } else { /* unset flag */
4153 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004154 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004155 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004156 /* there is another mandatory node */
4157 return;
4158 }
4159 }
4160 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4161 parent->flags &= ~LYS_MAND_TRUE;
4162 }
4163 }
4164}
4165
Radek Krejci056d0a82018-12-06 16:57:25 +01004166/**
Radek Krejci3641f562019-02-13 15:38:40 +01004167 * @brief Internal sorting process for the lys_compile_augment_sort().
4168 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4169 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4170 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4171 */
4172static void
4173lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4174{
4175 unsigned int v;
4176 size_t len;
4177
4178 len = strlen(aug_p->nodeid);
4179 LY_ARRAY_FOR(result, v) {
4180 if (strlen(result[v]->nodeid) <= len) {
4181 continue;
4182 }
4183 if (v < LY_ARRAY_SIZE(result)) {
4184 /* move the rest of array */
4185 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4186 break;
4187 }
4188 }
4189 result[v] = aug_p;
4190 LY_ARRAY_INCREMENT(result);
4191}
4192
4193/**
4194 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4195 *
4196 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4197 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4198 *
4199 * @param[in] ctx Compile context.
4200 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4201 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4202 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4203 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4204 * @return LY_ERR value.
4205 */
4206LY_ERR
4207lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4208{
4209 struct lysp_augment **result = NULL;
4210 unsigned int u, v;
4211 size_t count = 0;
4212
4213 assert(augments);
4214
4215 /* get count of the augments in module and all its submodules */
4216 if (aug_p) {
4217 count += LY_ARRAY_SIZE(aug_p);
4218 }
4219 LY_ARRAY_FOR(inc_p, u) {
4220 if (inc_p[u].submodule->augments) {
4221 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4222 }
4223 }
4224
4225 if (!count) {
4226 *augments = NULL;
4227 return LY_SUCCESS;
4228 }
4229 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4230
4231 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4232 * together, so there can be even /z/y betwwen them. */
4233 LY_ARRAY_FOR(aug_p, u) {
4234 lys_compile_augment_sort_(&aug_p[u], result);
4235 }
4236 LY_ARRAY_FOR(inc_p, u) {
4237 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4238 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4239 }
4240 }
4241
4242 *augments = result;
4243 return LY_SUCCESS;
4244}
4245
4246/**
4247 * @brief Compile the parsed augment connecting it into its target.
4248 *
4249 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4250 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4251 * are already implemented and compiled.
4252 *
4253 * @param[in] ctx Compile context.
4254 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004255 * @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
4256 * children in case of the augmenting uses data.
4257 * @return LY_SUCCESS on success.
4258 * @return LY_EVALID on failure.
4259 */
4260LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004261lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004262{
4263 LY_ERR ret = LY_SUCCESS;
4264 struct lysp_node *node_p, *case_node_p;
4265 struct lysc_node *target; /* target target of the augment */
4266 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004267 struct lysc_when **when, *when_shared;
4268 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004269 uint16_t flags = 0;
4270 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004271 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004272
Radek Krejci327de162019-06-14 12:52:07 +02004273 lysc_update_path(ctx, NULL, "{augment}");
4274 lysc_update_path(ctx, NULL, aug_p->nodeid);
4275
Radek Krejci7af64242019-02-18 13:07:53 +01004276 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004277 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004278 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004279 if (ret != LY_SUCCESS) {
4280 if (ret == LY_EDENIED) {
4281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4282 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4283 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4284 }
4285 return LY_EVALID;
4286 }
4287
4288 /* check for mandatory nodes
4289 * - new cases augmenting some choice can have mandatory nodes
4290 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4291 */
Radek Krejci733988a2019-02-15 15:12:44 +01004292 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004293 allow_mandatory = 1;
4294 }
4295
4296 when_shared = NULL;
4297 LY_LIST_FOR(aug_p->child, node_p) {
4298 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4299 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4300 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004301 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4302 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004304 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4305 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004306 return LY_EVALID;
4307 }
4308
4309 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004310 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004311 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004312 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004313 } else {
4314 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004315 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004316 }
4317 }
Radek Krejciec4da802019-05-02 13:02:41 +02004318 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004319
Radek Krejcife13da42019-02-15 14:51:01 +01004320 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4321 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004322 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004323 /* 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 +01004324 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 +01004325 } else if (target->nodetype == LYS_CHOICE) {
4326 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4327 node = ((struct lysc_node_choice*)target)->cases->prev;
4328 } else {
4329 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004330 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004331 }
4332
Radek Krejci733988a2019-02-15 15:12:44 +01004333 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004334 node->flags &= ~LYS_MAND_TRUE;
4335 lys_compile_mandatory_parents(target, 0);
4336 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004337 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004338 return LY_EVALID;
4339 }
4340
4341 /* pass augment's when to all the children */
4342 if (aug_p->when) {
4343 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4344 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004345 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004346 LY_CHECK_GOTO(ret, error);
4347 (*when)->context = lysc_xpath_context(target);
4348 when_shared = *when;
4349 } else {
4350 ++when_shared->refcount;
4351 (*when) = when_shared;
4352 }
4353 }
4354 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004355
Radek Krejciec4da802019-05-02 13:02:41 +02004356 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004357 switch (target->nodetype) {
4358 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004359 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004360 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004361 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004362 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004363 break;
4364 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004365 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004366 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004367 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004368 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004369 break;
4370 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004371 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004372 if (aug_p->actions) {
4373 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004374 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4375 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004376 return LY_EVALID;
4377 }
4378 if (aug_p->notifs) {
4379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004380 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4381 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004382 return LY_EVALID;
4383 }
4384 }
Radek Krejci3641f562019-02-13 15:38:40 +01004385
Radek Krejci327de162019-06-14 12:52:07 +02004386 lysc_update_path(ctx, NULL, NULL);
4387 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004388error:
Radek Krejciec4da802019-05-02 13:02:41 +02004389 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004390 return ret;
4391}
4392
4393/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004394 * @brief Apply refined or deviated mandatory flag to the target node.
4395 *
4396 * @param[in] ctx Compile context.
4397 * @param[in] node Target node where the mandatory property is supposed to be changed.
4398 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004399 * @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 +01004400 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4401 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4402 * 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 +01004403 * @return LY_ERR value.
4404 */
4405static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004406lys_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 +01004407{
4408 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004410 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4411 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004412 return LY_EVALID;
4413 }
4414
4415 if (mandatory_flag & LYS_MAND_TRUE) {
4416 /* check if node has default value */
4417 if (node->nodetype & LYS_LEAF) {
4418 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004419 if (refine_flag) {
4420 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004421 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004422 return LY_EVALID;
4423 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004424 } else {
4425 /* remove the default value taken from the leaf's type */
4426 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4427 ((struct lysc_node_leaf*)node)->dflt = NULL;
4428 }
4429 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004430 if (refine_flag) {
4431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004432 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004433 return LY_EVALID;
4434 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004435 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004436 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004437 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 +01004438 return LY_EVALID;
4439 }
4440
4441 node->flags &= ~LYS_MAND_FALSE;
4442 node->flags |= LYS_MAND_TRUE;
4443 lys_compile_mandatory_parents(node->parent, 1);
4444 } else {
4445 /* make mandatory false */
4446 node->flags &= ~LYS_MAND_TRUE;
4447 node->flags |= LYS_MAND_FALSE;
4448 lys_compile_mandatory_parents(node->parent, 0);
4449 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4450 /* get the type's default value if any */
4451 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4452 }
4453 }
4454 return LY_SUCCESS;
4455}
4456
4457/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004458 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4459 * If present, also apply uses's modificators.
4460 *
4461 * @param[in] ctx Compile context
4462 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004463 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4464 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4465 * the compile context.
4466 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4467 */
4468static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004469lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004470{
4471 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004472 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004473 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004474 struct lysc_node_container context_node_fake =
4475 {.nodetype = LYS_CONTAINER,
4476 .module = ctx->mod,
4477 .flags = parent ? parent->flags : 0,
4478 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004479 .prev = (struct lysc_node*)&context_node_fake,
4480 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004481 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004482 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004483 int found;
4484 const char *id, *name, *prefix;
4485 size_t prefix_len, name_len;
4486 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004487 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004488 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004489 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004490 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004491 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004492 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004493 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004494 unsigned int actions_index, notifs_index;
4495 struct lysc_notif **notifs = NULL;
4496 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004497
4498 /* search for the grouping definition */
4499 found = 0;
4500 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004501 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004502 if (prefix) {
4503 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4504 if (!mod) {
4505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004506 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004507 return LY_EVALID;
4508 }
4509 } else {
4510 mod = ctx->mod_def;
4511 }
4512 if (mod == ctx->mod_def) {
4513 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004514 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004515 LY_ARRAY_FOR(grp, u) {
4516 if (!strcmp(grp[u].name, name)) {
4517 grp = &grp[u];
4518 found = 1;
4519 break;
4520 }
4521 }
4522 }
4523 }
4524 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004525 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004526 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004527 if (grp) {
4528 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4529 if (!strcmp(grp[u].name, name)) {
4530 grp = &grp[u];
4531 found = 1;
4532 }
4533 }
4534 }
4535 if (!found && mod->parsed->includes) {
4536 /* ... and all the submodules */
4537 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4538 grp = mod->parsed->includes[u].submodule->groupings;
4539 if (grp) {
4540 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4541 if (!strcmp(grp[v].name, name)) {
4542 grp = &grp[v];
4543 found = 1;
4544 }
4545 }
4546 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004547 }
4548 }
4549 }
4550 if (!found) {
4551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4552 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4553 return LY_EVALID;
4554 }
4555
4556 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4557 grp_stack_count = ctx->groupings.count;
4558 ly_set_add(&ctx->groupings, (void*)grp, 0);
4559 if (grp_stack_count == ctx->groupings.count) {
4560 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4561 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4562 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4563 return LY_EVALID;
4564 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004565 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4566 /* remember that the grouping is instantiated to avoid its standalone validation */
4567 grp->flags |= LYS_USED_GRP;
4568 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004569
4570 /* switch context's mod_def */
4571 mod_old = ctx->mod_def;
4572 ctx->mod_def = mod;
4573
4574 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004575 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 +01004576
Radek Krejcifc11bd72019-04-11 16:00:05 +02004577 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004578 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004579 /* 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 +02004580 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 +01004581
4582 /* some preparation for applying refines */
4583 if (grp->data == node_p) {
4584 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004585 if (parent) {
4586 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4587 } else if (ctx->mod->compiled->data) {
4588 child = ctx->mod->compiled->data;
4589 } else {
4590 child = NULL;
4591 }
4592 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004593 }
4594 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004595 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004596 LY_LIST_FOR(context_node_fake.child, child) {
4597 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004598
Radek Krejcifc11bd72019-04-11 16:00:05 +02004599 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004600 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004601 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004602 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004603 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004604 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004605 when_shared = *when;
4606 } else {
4607 ++when_shared->refcount;
4608 (*when) = when_shared;
4609 }
4610 }
Radek Krejci01342af2019-01-03 15:18:08 +01004611 }
4612 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004613 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004614 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004615 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004616 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 +01004617 }
4618
Radek Krejcifc11bd72019-04-11 16:00:05 +02004619 /* compile actions */
4620 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4621 if (actions) {
4622 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004623 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004624 if (*actions && (uses_p->augments || uses_p->refines)) {
4625 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4626 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4627 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4628 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4629 }
4630 }
4631
4632 /* compile notifications */
4633 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4634 if (notifs) {
4635 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004636 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004637 if (*notifs && (uses_p->augments || uses_p->refines)) {
4638 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4639 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4640 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4641 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4642 }
4643 }
4644
4645
Radek Krejci3641f562019-02-13 15:38:40 +01004646 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004647 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004648 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004649 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004650 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004651
Radek Krejcif0089082019-01-07 16:42:01 +01004652 /* reload previous context's mod_def */
4653 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004654 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004655
Radek Krejci76b3e962018-12-14 17:01:25 +01004656 /* apply refine */
4657 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004658 lysc_update_path(ctx, NULL, rfn->nodeid);
4659
Radek Krejci7af64242019-02-18 13:07:53 +01004660 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 +01004661 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004662 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004663 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004664
4665 /* default value */
4666 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004667 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004669 "Invalid refine of default - %s cannot hold %d default values.",
4670 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004671 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004672 }
4673 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004675 "Invalid refine of default - %s cannot hold default value(s).",
4676 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004677 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004678 }
4679 if (node->nodetype == LYS_LEAF) {
4680 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4681 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004682 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004683 /* TODO check the default value according to type */
4684 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004685 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4687 "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 +02004688 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004689 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004690 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4691 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4692 }
4693 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004694 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004695 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004696 LY_ARRAY_FOR(rfn->dflts, u) {
4697 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4698 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4699 }
4700 /* TODO check the default values according to type */
4701 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004702 if (((struct lysc_node_choice*)node)->dflt) {
4703 /* unset LYS_SET_DFLT from the current default case */
4704 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4705 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004706 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 +01004707 }
4708 }
4709
Radek Krejci12fb9142019-01-08 09:45:30 +01004710 /* description */
4711 if (rfn->dsc) {
4712 FREE_STRING(ctx->ctx, node->dsc);
4713 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4714 }
4715
4716 /* reference */
4717 if (rfn->ref) {
4718 FREE_STRING(ctx->ctx, node->ref);
4719 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4720 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004721
4722 /* config */
4723 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004724 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004725 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004726 } else {
4727 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4728 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4729 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004730 }
4731
4732 /* mandatory */
4733 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004734 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004735 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004736
4737 /* presence */
4738 if (rfn->presence) {
4739 if (node->nodetype != LYS_CONTAINER) {
4740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004741 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4742 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004743 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004744 }
4745 node->flags |= LYS_PRESENCE;
4746 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004747
4748 /* must */
4749 if (rfn->musts) {
4750 switch (node->nodetype) {
4751 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004752 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 +01004753 break;
4754 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004755 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 +01004756 break;
4757 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004758 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 +01004759 break;
4760 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004761 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 +01004762 break;
4763 case LYS_ANYXML:
4764 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004765 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 +01004766 break;
4767 default:
4768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004769 "Invalid refine of must statement - %s cannot hold any must statement.",
4770 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004771 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004772 }
4773 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004774
4775 /* min/max-elements */
4776 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4777 switch (node->nodetype) {
4778 case LYS_LEAFLIST:
4779 if (rfn->flags & LYS_SET_MAX) {
4780 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4781 }
4782 if (rfn->flags & LYS_SET_MIN) {
4783 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004784 if (rfn->min) {
4785 node->flags |= LYS_MAND_TRUE;
4786 lys_compile_mandatory_parents(node->parent, 1);
4787 } else {
4788 node->flags &= ~LYS_MAND_TRUE;
4789 lys_compile_mandatory_parents(node->parent, 0);
4790 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004791 }
4792 break;
4793 case LYS_LIST:
4794 if (rfn->flags & LYS_SET_MAX) {
4795 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4796 }
4797 if (rfn->flags & LYS_SET_MIN) {
4798 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004799 if (rfn->min) {
4800 node->flags |= LYS_MAND_TRUE;
4801 lys_compile_mandatory_parents(node->parent, 1);
4802 } else {
4803 node->flags &= ~LYS_MAND_TRUE;
4804 lys_compile_mandatory_parents(node->parent, 0);
4805 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004806 }
4807 break;
4808 default:
4809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004810 "Invalid refine of %s statement - %s cannot hold this statement.",
4811 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004812 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004813 }
4814 }
Radek Krejcif0089082019-01-07 16:42:01 +01004815
4816 /* if-feature */
4817 if (rfn->iffeatures) {
4818 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004819 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004820 }
Radek Krejci327de162019-06-14 12:52:07 +02004821
4822 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004823 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004824
Radek Krejcif2271f12019-01-07 16:42:23 +01004825 /* do some additional checks of the changed nodes when all the refines are applied */
4826 for (u = 0; u < refined.count; ++u) {
4827 node = (struct lysc_node*)refined.objs[u];
4828 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02004829 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004830
4831 /* check possible conflict with default value (default added, mandatory left true) */
4832 if ((node->flags & LYS_MAND_TRUE) &&
4833 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4834 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004836 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004837 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004838 }
4839
4840 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4841 if (node->nodetype == LYS_LIST) {
4842 min = ((struct lysc_node_list*)node)->min;
4843 max = ((struct lysc_node_list*)node)->max;
4844 } else {
4845 min = ((struct lysc_node_leaflist*)node)->min;
4846 max = ((struct lysc_node_leaflist*)node)->max;
4847 }
4848 if (min > max) {
4849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004850 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
4851 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004852 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004853 }
4854 }
4855 }
4856
Radek Krejci327de162019-06-14 12:52:07 +02004857 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004858 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004859
4860cleanup:
4861 /* fix connection of the children nodes from fake context node back into the parent */
4862 if (context_node_fake.child) {
4863 context_node_fake.child->prev = child;
4864 }
4865 LY_LIST_FOR(context_node_fake.child, child) {
4866 child->parent = parent;
4867 }
4868
4869 if (uses_p->augments || uses_p->refines) {
4870 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004871 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004872 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4873 LY_ARRAY_FREE(context_node_fake.actions);
4874 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004875 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004876 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4877 LY_ARRAY_FREE(context_node_fake.notifs);
4878 }
4879 }
4880
Radek Krejcie86bf772018-12-14 11:39:53 +01004881 /* reload previous context's mod_def */
4882 ctx->mod_def = mod_old;
4883 /* remove the grouping from the stack for circular groupings dependency check */
4884 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4885 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004886 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004887 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004888
4889 return ret;
4890}
4891
Radek Krejci327de162019-06-14 12:52:07 +02004892static int
4893lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
4894{
4895 struct lysp_node *iter;
4896 int len = 0;
4897
4898 *path = NULL;
4899 for (iter = node; iter && len >= 0; iter = iter->parent) {
4900 char *s = *path;
4901 char *id;
4902
4903 switch (iter->nodetype) {
4904 case LYS_USES:
4905 asprintf(&id, "{uses='%s'}", iter->name);
4906 break;
4907 case LYS_GROUPING:
4908 asprintf(&id, "{grouping='%s'}", iter->name);
4909 break;
4910 case LYS_AUGMENT:
4911 asprintf(&id, "{augment='%s'}", iter->name);
4912 break;
4913 default:
4914 id = strdup(iter->name);
4915 break;
4916 }
4917
4918 if (!iter->parent) {
4919 /* print prefix */
4920 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
4921 } else {
4922 /* prefix is the same as in parent */
4923 len = asprintf(path, "/%s%s", id, s ? s : "");
4924 }
4925 free(s);
4926 free(id);
4927 }
4928
4929 if (len < 0) {
4930 free(*path);
4931 *path = NULL;
4932 } else if (len == 0) {
4933 *path = strdup("/");
4934 len = 1;
4935 }
4936 return len;
4937}
4938
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004939/**
4940 * @brief Validate groupings that were defined but not directly used in the schema itself.
4941 *
4942 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
4943 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
4944 */
4945static LY_ERR
4946lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
4947{
4948 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02004949 char *path;
4950 int len;
4951
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004952 struct lysp_node_uses fake_uses = {
4953 .parent = node_p,
4954 .nodetype = LYS_USES,
4955 .flags = 0, .next = NULL,
4956 .name = grp->name,
4957 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4958 .refines = NULL, .augments = NULL
4959 };
4960 struct lysc_node_container fake_container = {
4961 .nodetype = LYS_CONTAINER,
4962 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
4963 .module = ctx->mod,
4964 .sp = NULL, .parent = NULL, .next = NULL,
4965 .prev = (struct lysc_node*)&fake_container,
4966 .name = "fake",
4967 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
4968 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4969 };
4970
4971 if (grp->parent) {
4972 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4973 }
Radek Krejci327de162019-06-14 12:52:07 +02004974
4975 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4976 if (len < 0) {
4977 LOGMEM(ctx->ctx);
4978 return LY_EMEM;
4979 }
4980 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4981 ctx->path_len = (uint16_t)len;
4982 free(path);
4983
4984 lysc_update_path(ctx, NULL, "{grouping}");
4985 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004986 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02004987 lysc_update_path(ctx, NULL, NULL);
4988 lysc_update_path(ctx, NULL, NULL);
4989
4990 ctx->path_len = 1;
4991 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004992
4993 /* cleanup */
4994 lysc_node_container_free(ctx->ctx, &fake_container);
4995
4996 return ret;
4997}
Radek Krejcife909632019-02-12 15:34:42 +01004998
Radek Krejcie86bf772018-12-14 11:39:53 +01004999/**
Radek Krejcia3045382018-11-22 14:30:31 +01005000 * @brief Compile parsed schema node information.
5001 * @param[in] ctx Compile context
5002 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005003 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5004 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5005 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005006 * @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).
5007 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005008 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5009 */
Radek Krejci19a96102018-11-15 13:38:09 +01005010static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005011lys_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 +01005012{
5013 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005014 struct lysc_node *node;
5015 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005016 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005017 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005018 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005019
Radek Krejci327de162019-06-14 12:52:07 +02005020 if (node_p->nodetype != LYS_USES) {
5021 lysc_update_path(ctx, parent, node_p->name);
5022 } else {
5023 lysc_update_path(ctx, NULL, "{uses}");
5024 lysc_update_path(ctx, NULL, node_p->name);
5025 }
5026
Radek Krejci19a96102018-11-15 13:38:09 +01005027 switch (node_p->nodetype) {
5028 case LYS_CONTAINER:
5029 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5030 node_compile_spec = lys_compile_node_container;
5031 break;
5032 case LYS_LEAF:
5033 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5034 node_compile_spec = lys_compile_node_leaf;
5035 break;
5036 case LYS_LIST:
5037 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005038 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005039 break;
5040 case LYS_LEAFLIST:
5041 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005042 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005043 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005044 case LYS_CHOICE:
5045 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005046 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005047 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005048 case LYS_ANYXML:
5049 case LYS_ANYDATA:
5050 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005051 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005052 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005053 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005054 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5055 lysc_update_path(ctx, NULL, NULL);
5056 lysc_update_path(ctx, NULL, NULL);
5057 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005058 default:
5059 LOGINT(ctx->ctx);
5060 return LY_EINT;
5061 }
5062 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5063 node->nodetype = node_p->nodetype;
5064 node->module = ctx->mod;
5065 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005066 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005067
5068 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005069 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005070 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005071 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005072 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5073 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005074 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005075 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005076 node->flags |= LYS_CONFIG_R;
5077 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005078 /* config not explicitely set, inherit it from parent */
5079 if (parent) {
5080 node->flags |= parent->flags & LYS_CONFIG_MASK;
5081 } else {
5082 /* default is config true */
5083 node->flags |= LYS_CONFIG_W;
5084 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005085 } else {
5086 /* config set explicitely */
5087 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005088 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005089 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5090 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5091 "Configuration node cannot be child of any state data node.");
5092 goto error;
5093 }
Radek Krejci19a96102018-11-15 13:38:09 +01005094
Radek Krejcia6d57732018-11-29 13:40:37 +01005095 /* *list ordering */
5096 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5097 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005098 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005099 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5100 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005101 node->flags &= ~LYS_ORDBY_MASK;
5102 node->flags |= LYS_ORDBY_SYSTEM;
5103 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5104 /* default ordering is system */
5105 node->flags |= LYS_ORDBY_SYSTEM;
5106 }
5107 }
5108
Radek Krejci19a96102018-11-15 13:38:09 +01005109 /* status - it is not inherited by specification, but it does not make sense to have
5110 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005111 if (!parent || parent->nodetype != LYS_CHOICE) {
5112 /* in case of choice/case's children, postpone the check to the moment we know if
5113 * 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 +01005114 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 +01005115 }
5116
Radek Krejciec4da802019-05-02 13:02:41 +02005117 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005118 node->sp = node_p;
5119 }
5120 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005121 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5122 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005123 if (node_p->when) {
5124 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005125 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005126 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005127 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005128 }
Radek Krejciec4da802019-05-02 13:02:41 +02005129 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
5130 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005131
5132 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005133 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005134
Radek Krejcife909632019-02-12 15:34:42 +01005135 /* inherit LYS_MAND_TRUE in parent containers */
5136 if (node->flags & LYS_MAND_TRUE) {
5137 lys_compile_mandatory_parents(parent, 1);
5138 }
5139
Radek Krejci327de162019-06-14 12:52:07 +02005140 lysc_update_path(ctx, NULL, NULL);
5141
Radek Krejci19a96102018-11-15 13:38:09 +01005142 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005143 if (parent) {
5144 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005145 if (node_p->parent->nodetype == LYS_CASE) {
5146 lysc_update_path(ctx, parent, node_p->parent->name);
5147 } else {
5148 lysc_update_path(ctx, parent, node->name);
5149 }
Radek Krejciec4da802019-05-02 13:02:41 +02005150 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005151 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005152 if (uses_status) {
5153
5154 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005155 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005156 * it directly gets the same status flags as the choice;
5157 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005158 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005159 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005160 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005161 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005162 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005163 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005164 }
Radek Krejciec4da802019-05-02 13:02:41 +02005165 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 +02005166
5167 if (parent->nodetype == LYS_CHOICE) {
5168 lysc_update_path(ctx, NULL, NULL);
5169 }
Radek Krejci19a96102018-11-15 13:38:09 +01005170 } else {
5171 /* top-level element */
5172 if (!ctx->mod->compiled->data) {
5173 ctx->mod->compiled->data = node;
5174 } else {
5175 /* insert at the end of the module's top-level nodes list */
5176 ctx->mod->compiled->data->prev->next = node;
5177 node->prev = ctx->mod->compiled->data->prev;
5178 ctx->mod->compiled->data->prev = node;
5179 }
Radek Krejci327de162019-06-14 12:52:07 +02005180 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005181 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5182 ctx->mod->compiled->notifs, node->name, node)) {
5183 return LY_EVALID;
5184 }
Radek Krejci19a96102018-11-15 13:38:09 +01005185 }
Radek Krejci327de162019-06-14 12:52:07 +02005186 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005187
5188 return LY_SUCCESS;
5189
5190error:
5191 lysc_node_free(ctx->ctx, node);
5192 return ret;
5193}
5194
Radek Krejciccd20f12019-02-15 14:12:27 +01005195static void
5196lysc_disconnect(struct lysc_node *node)
5197{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005198 struct lysc_node *parent, *child, *prev = NULL, *next;
5199 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005200 int remove_cs = 0;
5201
5202 parent = node->parent;
5203
5204 /* parent's first child */
5205 if (!parent) {
5206 return;
5207 }
5208 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005209 cs = (struct lysc_node_case*)node;
5210 } else if (parent->nodetype == LYS_CASE) {
5211 /* disconnecting some node in a case */
5212 cs = (struct lysc_node_case*)parent;
5213 parent = cs->parent;
5214 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5215 if (child == node) {
5216 if (cs->child == child) {
5217 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5218 /* case with a single child -> remove also the case */
5219 child->parent = NULL;
5220 remove_cs = 1;
5221 } else {
5222 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005223 }
5224 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005225 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005226 }
5227 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005228 if (!remove_cs) {
5229 cs = NULL;
5230 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005231 } else if (lysc_node_children(parent, node->flags) == node) {
5232 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005233 }
5234
5235 if (cs) {
5236 if (remove_cs) {
5237 /* cs has only one child which is being also removed */
5238 lysc_disconnect((struct lysc_node*)cs);
5239 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5240 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005241 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5242 /* default case removed */
5243 ((struct lysc_node_choice*)parent)->dflt = NULL;
5244 }
5245 if (((struct lysc_node_choice*)parent)->cases == cs) {
5246 /* first case removed */
5247 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5248 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005249 if (cs->child) {
5250 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5251 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5252 prev = cs->child->prev;
5253 } /* else all the children are under a single case */
5254 LY_LIST_FOR_SAFE(cs->child, next, child) {
5255 if (child->parent != (struct lysc_node*)cs) {
5256 break;
5257 }
5258 lysc_node_free(node->module->ctx, child);
5259 }
5260 if (prev) {
5261 if (prev->next) {
5262 prev->next = child;
5263 }
5264 if (child) {
5265 child->prev = prev;
5266 } else {
5267 /* link from the first child under the cases */
5268 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5269 }
5270 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005271 }
5272 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005273 }
5274
5275 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005276 if (node->prev->next) {
5277 node->prev->next = node->next;
5278 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005279 if (node->next) {
5280 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005281 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005282 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005283 if (child) {
5284 child->prev = node->prev;
5285 }
5286 } else if (((struct lysc_node_choice*)parent)->cases) {
5287 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005288 }
5289}
5290
5291LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005292lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005293{
5294 LY_ERR ret = LY_EVALID;
5295 struct ly_set devs_p = {0};
5296 struct ly_set targets = {0};
5297 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005298 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005299 struct lysc_action *rpcs;
5300 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005301 struct lysp_deviation *dev;
5302 struct lysp_deviate *d, **dp_new;
5303 struct lysp_deviate_add *d_add;
5304 struct lysp_deviate_del *d_del;
5305 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005306 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005307 struct lysc_deviation {
5308 const char *nodeid;
5309 struct lysc_node *target; /* target node of the deviation */
5310 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005311 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005312 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5313 } **devs = NULL;
5314 int i;
5315 size_t prefix_len, name_len;
5316 const char *prefix, *name, *nodeid;
5317 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005318 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005319 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005320
5321 /* get all deviations from the module and all its submodules ... */
5322 LY_ARRAY_FOR(mod_p->deviations, u) {
5323 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5324 }
5325 LY_ARRAY_FOR(mod_p->includes, v) {
5326 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5327 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5328 }
5329 }
5330 if (!devs_p.count) {
5331 /* nothing to do */
5332 return LY_SUCCESS;
5333 }
5334
Radek Krejci327de162019-06-14 12:52:07 +02005335 lysc_update_path(ctx, NULL, "{deviation}");
5336
Radek Krejciccd20f12019-02-15 14:12:27 +01005337 /* ... and group them by the target node */
5338 devs = calloc(devs_p.count, sizeof *devs);
5339 for (u = 0; u < devs_p.count; ++u) {
5340 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005341 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005342
5343 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005344 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5345 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005346 if (target->nodetype == LYS_ACTION) {
5347 /* move the target pointer to input/output to make them different from the action and
5348 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5349 * back to the RPC/action node due to a better compatibility and decision code in this function.
5350 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5351 if (flags & LYSC_OPT_RPC_INPUT) {
5352 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5353 flags |= LYSC_OPT_INTERNAL;
5354 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5355 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5356 flags |= LYSC_OPT_INTERNAL;
5357 }
5358 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005359 /* insert into the set of targets with duplicity detection */
5360 i = ly_set_add(&targets, target, 0);
5361 if (!devs[i]) {
5362 /* new record */
5363 devs[i] = calloc(1, sizeof **devs);
5364 devs[i]->target = target;
5365 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005366 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005367 }
5368 /* add deviates into the deviation's list of deviates */
5369 for (d = dev->deviates; d; d = d->next) {
5370 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5371 *dp_new = d;
5372 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5373 devs[i]->not_supported = 1;
5374 }
5375 }
Radek Krejci327de162019-06-14 12:52:07 +02005376
5377 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005378 }
5379
5380 /* MACROS for deviates checking */
5381#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5382 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005383 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 +01005384 goto cleanup; \
5385 }
5386
5387#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5388 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005389 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5390 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005391 goto cleanup; \
5392 }
5393
5394#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005395 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005397 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5398 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005399 goto cleanup; \
5400 }
5401
Radek Krejci551b12c2019-02-19 16:11:21 +01005402#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5403 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5404 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005405 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5406 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005407 goto cleanup; \
5408 }
5409
Radek Krejciccd20f12019-02-15 14:12:27 +01005410#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5411 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005413 goto cleanup; \
5414 }
5415
Radek Krejci551b12c2019-02-19 16:11:21 +01005416#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5417 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5418 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005419 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005420 goto cleanup; \
5421 }
5422
Radek Krejciccd20f12019-02-15 14:12:27 +01005423#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5424 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5425 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5426 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005427 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5428 PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005429 goto cleanup; \
5430 } \
5431 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5432 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5433
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005434#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5435 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5436 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5437 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5438 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 +01005439 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005440 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005442 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5443 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005444 goto cleanup; \
5445 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005446 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5447 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5448 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5449 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5450 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005451 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005452 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5453 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5454 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005455 }
5456
5457 /* apply deviations */
5458 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejci327de162019-06-14 12:52:07 +02005459 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5460
Radek Krejcif538ce52019-03-05 10:46:14 +01005461 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5462 /* fix the target pointer in case of RPC's/action's input/output */
5463 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5464 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5465 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5466 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5467 }
5468 }
5469
Radek Krejciccd20f12019-02-15 14:12:27 +01005470 /* not-supported */
5471 if (devs[u]->not_supported) {
5472 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5473 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5474 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5475 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005476
5477#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5478 if (devs[u]->target->parent) { \
5479 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5480 } else { \
5481 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5482 } \
5483 LY_ARRAY_FOR(ARRAY, x) { \
5484 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5485 } \
5486 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5487 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5488 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5489 LY_ARRAY_DECREMENT(ARRAY); \
5490 }
5491
Radek Krejcif538ce52019-03-05 10:46:14 +01005492 if (devs[u]->target->nodetype == LYS_ACTION) {
5493 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5494 /* remove RPC's/action's input */
5495 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5496 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005497 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5498 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005499 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5500 /* remove RPC's/action's output */
5501 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5502 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005503 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5504 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005505 } else {
5506 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005507 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005508 }
5509 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005510 /* remove Notification */
5511 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005512 } else {
5513 /* remove the target node */
5514 lysc_disconnect(devs[u]->target);
5515 lysc_node_free(ctx->ctx, devs[u]->target);
5516 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005517
5518 continue;
5519 }
5520
5521 /* list of deviates (not-supported is not present in the list) */
5522 LY_ARRAY_FOR(devs[u]->deviates, v) {
5523 d = devs[u]->deviates[v];
5524
5525 switch (d->mod) {
5526 case LYS_DEV_ADD:
5527 d_add = (struct lysp_deviate_add*)d;
5528 /* [units-stmt] */
5529 if (d_add->units) {
5530 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5531 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5532
5533 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5534 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5535 }
5536
5537 /* *must-stmt */
5538 if (d_add->musts) {
5539 switch (devs[u]->target->nodetype) {
5540 case LYS_CONTAINER:
5541 case LYS_LIST:
5542 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005543 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005544 break;
5545 case LYS_LEAF:
5546 case LYS_LEAFLIST:
5547 case LYS_ANYDATA:
5548 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005549 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005550 break;
5551 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005552 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005553 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005554 break;
5555 case LYS_ACTION:
5556 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5557 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005558 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005559 break;
5560 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5561 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005562 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005563 break;
5564 }
5565 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005566 default:
5567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005568 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005569 goto cleanup;
5570 }
5571 }
5572
5573 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005574 if (d_add->uniques) {
5575 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5576 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5577 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005578
5579 /* *default-stmt */
5580 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005581 switch (devs[u]->target->nodetype) {
5582 case LYS_LEAF:
5583 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5584 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5585 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5586 /* first, remove the default value taken from the type */
5587 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5588 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5589 }
5590 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005591 /* mark the new default values as leaf's own */
5592 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005593 break;
5594 case LYS_LEAFLIST:
5595 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5596 /* first, remove the default value taken from the type */
5597 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5598 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5599 }
5600 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5601 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5602 }
5603 /* add new default value(s) */
5604 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5605 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5606 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5607 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5608 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5609 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5610 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005611 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005612 devs[u]->target->flags |= LYS_SET_DFLT;
5613 break;
5614 case LYS_CHOICE:
5615 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5616 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5617 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5618 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005619 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 +01005620 goto cleanup;
5621 }
5622 break;
5623 default:
5624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005625 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005626 goto cleanup;
5627 }
5628 }
5629
5630 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005631 if (d_add->flags & LYS_CONFIG_MASK) {
5632 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005634 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5635 goto cleanup;
5636 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005637 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005638 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5639 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005640 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005641 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005643 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5644 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005645 goto cleanup;
5646 }
Radek Krejci327de162019-06-14 12:52:07 +02005647 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005648 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005649
5650 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005651 if (d_add->flags & LYS_MAND_MASK) {
5652 if (devs[u]->target->flags & LYS_MAND_MASK) {
5653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005654 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5655 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005656 goto cleanup;
5657 }
Radek Krejci327de162019-06-14 12:52:07 +02005658 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005659 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005660
5661 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005662 if (d_add->flags & LYS_SET_MIN) {
5663 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5664 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5665 /* change value */
5666 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5667 } else if (devs[u]->target->nodetype == LYS_LIST) {
5668 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5669 /* change value */
5670 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5671 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005673 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5674 goto cleanup;
5675 }
5676 if (d_add->min) {
5677 devs[u]->target->flags |= LYS_MAND_TRUE;
5678 }
5679 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005680
5681 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005682 if (d_add->flags & LYS_SET_MAX) {
5683 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5684 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5685 /* change value */
5686 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5687 } else if (devs[u]->target->nodetype == LYS_LIST) {
5688 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5689 /* change value */
5690 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5691 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005693 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5694 goto cleanup;
5695 }
5696 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005697
5698 break;
5699 case LYS_DEV_DELETE:
5700 d_del = (struct lysp_deviate_del*)d;
5701
5702 /* [units-stmt] */
5703 if (d_del->units) {
5704 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5705 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5706 }
5707
5708 /* *must-stmt */
5709 if (d_del->musts) {
5710 switch (devs[u]->target->nodetype) {
5711 case LYS_CONTAINER:
5712 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005713 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005714 break;
5715 case LYS_LEAF:
5716 case LYS_LEAFLIST:
5717 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005718 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005719 break;
5720 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005721 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005722 break;
5723 case LYS_ACTION:
5724 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5725 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5726 break;
5727 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5728 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5729 break;
5730 }
5731 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005732 default:
5733 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005734 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005735 goto cleanup;
5736 }
5737 }
5738
5739 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005740 if (d_del->uniques) {
5741 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5742 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5743 LY_ARRAY_FOR(d_del->uniques, x) {
5744 LY_ARRAY_FOR(list->uniques, z) {
5745 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5746 nodeid = strpbrk(name, " \t\n");
5747 if (nodeid) {
5748 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5749 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5750 break;
5751 }
5752 while (isspace(*nodeid)) {
5753 ++nodeid;
5754 }
5755 } else {
5756 if (strcmp(name, list->uniques[z][y]->name)) {
5757 break;
5758 }
5759 }
5760 }
5761 if (!name) {
5762 /* complete match - remove the unique */
5763 LY_ARRAY_DECREMENT(list->uniques);
5764 LY_ARRAY_FREE(list->uniques[z]);
5765 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5766 --z;
5767 break;
5768 }
5769 }
5770 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005772 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5773 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01005774 goto cleanup;
5775 }
5776 }
5777 if (!LY_ARRAY_SIZE(list->uniques)) {
5778 LY_ARRAY_FREE(list->uniques);
5779 list->uniques = NULL;
5780 }
5781 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005782
5783 /* *default-stmt */
5784 if (d_del->dflts) {
5785 switch (devs[u]->target->nodetype) {
5786 case LYS_LEAF:
5787 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5788 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5789 dflt, "deleting", "default", d_del->dflts[0]);
5790
5791 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005792 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005793 break;
5794 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005795 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005796 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5797 devs[u]->target->flags &= ~LYS_SET_DFLT;
5798 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005799 break;
5800 case LYS_CHOICE:
5801 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5802 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5803 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02005804 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005805 if (prefix) {
5806 /* use module prefixes from the deviation module to match the module of the default case */
5807 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5808 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005809 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
5810 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005811 goto cleanup;
5812 }
5813 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5814 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005815 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
5816 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005817 goto cleanup;
5818 }
5819 }
5820 /* else {
5821 * strictly, the default prefix would point to the deviation module, but the value should actually
5822 * match the default string in the original module (usually unprefixed), so in this case we do not check
5823 * the module of the default case, just matching its name */
5824 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5825 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005826 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5827 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01005828 goto cleanup;
5829 }
5830 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5831 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5832 break;
5833 default:
5834 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005835 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005836 goto cleanup;
5837 }
5838 }
5839
5840 break;
5841 case LYS_DEV_REPLACE:
5842 d_rpl = (struct lysp_deviate_rpl*)d;
5843
5844 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005845 if (d_rpl->type) {
5846 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5847 /* type is mandatory, so checking for its presence is not necessary */
5848 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejciec4da802019-05-02 13:02:41 +02005849 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup);
Radek Krejci33f72892019-02-21 10:36:58 +01005850 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005851
5852 /* [units-stmt] */
5853 if (d_rpl->units) {
5854 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5855 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5856 units, "replacing", "units", d_rpl->units);
5857
5858 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5859 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5860 }
5861
5862 /* [default-stmt] */
5863 if (d_rpl->dflt) {
5864 switch (devs[u]->target->nodetype) {
5865 case LYS_LEAF:
5866 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5867 dflt, "replacing", "default", d_rpl->dflt);
5868
5869 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5870 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5871 break;
5872 case LYS_CHOICE:
5873 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02005874 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 +01005875 goto cleanup;
5876 }
5877 break;
5878 default:
5879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005880 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005881 goto cleanup;
5882 }
5883 }
5884
5885 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005886 if (d_rpl->flags & LYS_CONFIG_MASK) {
5887 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005888 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005889 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5890 goto cleanup;
5891 }
5892 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02005893 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01005894 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5895 goto cleanup;
5896 }
Radek Krejci327de162019-06-14 12:52:07 +02005897 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005898 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005899
5900 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005901 if (d_rpl->flags & LYS_MAND_MASK) {
5902 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02005903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01005904 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5905 goto cleanup;
5906 }
Radek Krejci327de162019-06-14 12:52:07 +02005907 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005908 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005909
5910 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005911 if (d_rpl->flags & LYS_SET_MIN) {
5912 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5913 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5914 /* change value */
5915 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5916 } else if (devs[u]->target->nodetype == LYS_LIST) {
5917 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5918 /* change value */
5919 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5920 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005922 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5923 goto cleanup;
5924 }
5925 if (d_rpl->min) {
5926 devs[u]->target->flags |= LYS_MAND_TRUE;
5927 }
5928 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005929
5930 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005931 if (d_rpl->flags & LYS_SET_MAX) {
5932 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5933 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5934 /* change value */
5935 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5936 } else if (devs[u]->target->nodetype == LYS_LIST) {
5937 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5938 /* change value */
5939 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5940 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005942 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5943 goto cleanup;
5944 }
5945 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005946
5947 break;
5948 default:
5949 LOGINT(ctx->ctx);
5950 goto cleanup;
5951 }
5952 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005953
Radek Krejci33f72892019-02-21 10:36:58 +01005954 /* final check when all deviations of a single target node are applied */
5955
Radek Krejci551b12c2019-02-19 16:11:21 +01005956 /* check min-max compatibility */
5957 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5958 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5959 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5960 } else if (devs[u]->target->nodetype == LYS_LIST) {
5961 min = ((struct lysc_node_list*)devs[u]->target)->min;
5962 max = ((struct lysc_node_list*)devs[u]->target)->max;
5963 } else {
5964 min = max = 0;
5965 }
5966 if (min > max) {
5967 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 +02005968 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01005969 goto cleanup;
5970 }
5971
5972 /* check mandatory - default compatibility */
5973 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5974 && (devs[u]->target->flags & LYS_SET_DFLT)
5975 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5976 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005977 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01005978 goto cleanup;
5979 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5980 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5981 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02005982 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 +01005983 goto cleanup;
5984 }
5985 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5986 /* mandatory node under a default case */
5987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005988 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5989 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01005990 goto cleanup;
5991 }
Radek Krejci33f72892019-02-21 10:36:58 +01005992
5993 /* TODO check default value(s) according to the type */
Radek Krejci327de162019-06-14 12:52:07 +02005994
5995 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005996 }
5997
Radek Krejci327de162019-06-14 12:52:07 +02005998 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005999 ret = LY_SUCCESS;
6000
6001cleanup:
6002 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6003 LY_ARRAY_FREE(devs[u]->deviates);
6004 free(devs[u]);
6005 }
6006 free(devs);
6007 ly_set_erase(&targets, NULL);
6008 ly_set_erase(&devs_p, NULL);
6009
6010 return ret;
6011}
6012
Radek Krejcib56c7502019-02-13 14:19:54 +01006013/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006014 * @brief Compile the given YANG submodule into the main module.
6015 * @param[in] ctx Compile context
6016 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006017 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6018 */
6019LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006020lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006021{
6022 unsigned int u;
6023 LY_ERR ret = LY_SUCCESS;
6024 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006025 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006026 struct lysc_module *mainmod = ctx->mod->compiled;
6027
Radek Krejci0af46292019-01-11 16:02:31 +01006028 if (!mainmod->mod->off_features) {
6029 /* features are compiled directly into the compiled module structure,
6030 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6031 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci327de162019-06-14 12:52:07 +02006032 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01006033 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
6034 LY_CHECK_GOTO(ret, error);
6035 }
6036
Radek Krejci327de162019-06-14 12:52:07 +02006037 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006038 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006039 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006040
Radek Krejci8cce8532019-03-05 11:27:45 +01006041 /* TODO data nodes */
6042
Radek Krejciec4da802019-05-02 13:02:41 +02006043 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6044 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006045
Radek Krejcid05cbd92018-12-05 14:26:40 +01006046error:
6047 return ret;
6048}
6049
Radek Krejci19a96102018-11-15 13:38:09 +01006050LY_ERR
6051lys_compile(struct lys_module *mod, int options)
6052{
6053 struct lysc_ctx ctx = {0};
6054 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006055 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006056 struct lysp_module *sp;
6057 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006058 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006059 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006060 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006061 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006062 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006063
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006064 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006065
6066 if (!mod->implemented) {
6067 /* just imported modules are not compiled */
6068 return LY_SUCCESS;
6069 }
6070
Radek Krejci19a96102018-11-15 13:38:09 +01006071 sp = mod->parsed;
6072
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006073 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006074 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006075 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006076 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006077 ctx.path_len = 1;
6078 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006079
6080 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006081 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6082 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006083
Radek Krejciec4da802019-05-02 13:02:41 +02006084 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006085 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006086 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006087 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6088 }
Radek Krejci0af46292019-01-11 16:02:31 +01006089 if (mod->off_features) {
6090 /* there is already precompiled array of features */
6091 mod_c->features = mod->off_features;
6092 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006093 } else {
6094 /* features are compiled directly into the compiled module structure,
6095 * 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 +02006096 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006097 LY_CHECK_GOTO(ret, error);
6098 }
6099 /* finish feature compilation, not only for the main module, but also for the submodules.
6100 * Due to possible forward references, it must be done when all the features (including submodules)
6101 * are present. */
6102 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006103 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006104 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6105 }
Radek Krejci327de162019-06-14 12:52:07 +02006106 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006107 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006108 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006109 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006110 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006111 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6112 }
Radek Krejci327de162019-06-14 12:52:07 +02006113 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006114 }
Radek Krejci327de162019-06-14 12:52:07 +02006115 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006116
Radek Krejci327de162019-06-14 12:52:07 +02006117 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006118 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006119 if (sp->identities) {
6120 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6121 }
Radek Krejci327de162019-06-14 12:52:07 +02006122 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006123
Radek Krejci95710c92019-02-11 15:49:55 +01006124 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006125 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006126 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006127 LY_CHECK_GOTO(ret, error);
6128 }
Radek Krejci95710c92019-02-11 15:49:55 +01006129
Radek Krejciec4da802019-05-02 13:02:41 +02006130 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6131 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006132
Radek Krejci95710c92019-02-11 15:49:55 +01006133 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006134 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006135 LY_CHECK_GOTO(ret, error);
6136 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006137 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006138 LY_CHECK_GOTO(ret, error);
6139 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006140
6141 /* deviations */
Radek Krejciec4da802019-05-02 13:02:41 +02006142 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006143 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006144
Radek Krejciec4da802019-05-02 13:02:41 +02006145 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006146
Radek Krejcia3045382018-11-22 14:30:31 +01006147 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006148 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6149 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6150 * 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 +01006151 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006152 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006153 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6154 if (type->basetype == LY_TYPE_LEAFREF) {
6155 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006156 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 +01006157 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006158 } else if (type->basetype == LY_TYPE_UNION) {
6159 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6160 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6161 /* validate the path */
6162 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6163 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6164 LY_CHECK_GOTO(ret, error);
6165 }
6166 }
Radek Krejcia3045382018-11-22 14:30:31 +01006167 }
6168 }
6169 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006170 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006171 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006172 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6173 if (type->basetype == LY_TYPE_LEAFREF) {
6174 /* store pointer to the real type */
6175 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6176 typeiter->basetype == LY_TYPE_LEAFREF;
6177 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6178 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006179 } else if (type->basetype == LY_TYPE_UNION) {
6180 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6181 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6182 /* store pointer to the real type */
6183 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6184 typeiter->basetype == LY_TYPE_LEAFREF;
6185 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6186 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6187 }
6188 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006189 }
6190 }
6191 }
Radek Krejciec4da802019-05-02 13:02:41 +02006192
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006193 /* validate non-instantiated groupings from the parsed schema,
6194 * without it we would accept even the schemas with invalid grouping specification */
6195 ctx.options |= LYSC_OPT_GROUPING;
6196 LY_ARRAY_FOR(sp->groupings, u) {
6197 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6198 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6199 }
6200 }
6201 LY_LIST_FOR(sp->data, node_p) {
6202 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6203 LY_ARRAY_FOR(grps, u) {
6204 if (!(grps[u].flags & LYS_USED_GRP)) {
6205 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6206 }
6207 }
6208 }
6209
Radek Krejcia3045382018-11-22 14:30:31 +01006210 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006211 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006212 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006213 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006214
Radek Krejciec4da802019-05-02 13:02:41 +02006215 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006216 lysp_module_free(mod->parsed);
6217 ((struct lys_module*)mod)->parsed = NULL;
6218 }
6219
Radek Krejciec4da802019-05-02 13:02:41 +02006220 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006221 /* remove flag of the modules implemented by dependency */
6222 for (u = 0; u < ctx.ctx->list.count; ++u) {
6223 m = ctx.ctx->list.objs[u];
6224 if (m->implemented == 2) {
6225 m->implemented = 1;
6226 }
6227 }
6228 }
6229
Radek Krejci19a96102018-11-15 13:38:09 +01006230 ((struct lys_module*)mod)->compiled = mod_c;
6231 return LY_SUCCESS;
6232
6233error:
Radek Krejci95710c92019-02-11 15:49:55 +01006234 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01006235 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006236 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006237 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006238 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006239 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006240 mod->compiled = NULL;
6241
6242 /* revert compilation of modules implemented by dependency */
6243 for (u = 0; u < ctx.ctx->list.count; ++u) {
6244 m = ctx.ctx->list.objs[u];
6245 if (m->implemented == 2) {
6246 /* revert features list to the precompiled state */
6247 lys_feature_precompile_revert(&ctx, m);
6248 /* mark module as imported-only / not-implemented */
6249 m->implemented = 0;
6250 /* free the compiled version of the module */
6251 lysc_module_free(m->compiled, NULL);
6252 m->compiled = NULL;
6253 }
6254 }
6255
Radek Krejci19a96102018-11-15 13:38:09 +01006256 return ret;
6257}