blob: 8204bf4e2c8a65ffae9011b4870fa053d8392a60 [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 */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002151 if (strncmp(path_key_expr, "current", 7)) {
2152error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2154 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2155 *predicate - start, start);
2156 goto cleanup;
2157 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002158 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2159 if (*path_key_expr != '(') {
2160 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002161 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002162 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2163 if (*path_key_expr != ')') {
2164 goto error_current_function_invocation;
2165 }
2166 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002167
2168 if (*path_key_expr != '/') {
2169 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2170 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2171 *predicate - start, start);
2172 goto cleanup;
2173 }
2174 ++path_key_expr;
2175 while (isspace(*path_key_expr)) {
2176 ++path_key_expr;
2177 }
2178
2179 /* rel-path-keyexpr:
2180 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2181 while (!strncmp(path_key_expr, "..", 2)) {
2182 ++dest_parent_times;
2183 path_key_expr += 2;
2184 while (isspace(*path_key_expr)) {
2185 ++path_key_expr;
2186 }
2187 if (*path_key_expr != '/') {
2188 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2189 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2190 *predicate - start, start);
2191 goto cleanup;
2192 }
2193 ++path_key_expr;
2194 while (isspace(*path_key_expr)) {
2195 ++path_key_expr;
2196 }
2197
2198 /* path is supposed to be evaluated in data tree, so we have to skip
2199 * all schema nodes that cannot be instantiated in data tree */
2200 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2201 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2202 *predicate - start, start);
2203 }
2204 if (!dest_parent_times) {
2205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2206 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2207 *predicate - start, start);
2208 goto cleanup;
2209 }
2210 if (path_key_expr == pke_end) {
2211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2212 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2213 *predicate - start, start);
2214 goto cleanup;
2215 }
2216
2217 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002218 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002219 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002220 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002221 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2222 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002223 goto cleanup;
2224 }
2225
2226 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002227 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002228 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002229 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002230 }
2231 if (!mod) {
2232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002233 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002234 *predicate - start, start, dst_len, dst);
2235 goto cleanup;
2236 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002237 if (!mod->implemented) {
2238 /* make the module implemented */
2239 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2240 }
Radek Krejcia3045382018-11-22 14:30:31 +01002241
2242 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2243 if (!dst_node) {
2244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002245 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002246 *predicate - start, start, path_key_expr - pke_start, pke_start);
2247 goto cleanup;
2248 }
2249 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002250 if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcia3045382018-11-22 14:30:31 +01002251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002252 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002253 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2254 goto cleanup;
2255 }
2256 }
2257
2258 ret = LY_SUCCESS;
2259cleanup:
2260 ly_set_erase(&keys, NULL);
2261 return ret;
2262}
2263
2264/**
2265 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2266 *
2267 * path-arg = absolute-path / relative-path
2268 * absolute-path = 1*("/" (node-identifier *path-predicate))
2269 * relative-path = 1*(".." "/") descendant-path
2270 *
2271 * @param[in,out] path Path to parse.
2272 * @param[out] prefix Prefix of the token, NULL if there is not any.
2273 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2274 * @param[out] name Name of the token.
2275 * @param[out] nam_len Length of the name.
2276 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2277 * must not be changed between consecutive calls. -1 if the
2278 * path is absolute.
2279 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2280 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2281 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002282LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002283lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2284 int *parent_times, int *has_predicate)
2285{
2286 int par_times = 0;
2287
2288 assert(path && *path);
2289 assert(parent_times);
2290 assert(prefix);
2291 assert(prefix_len);
2292 assert(name);
2293 assert(name_len);
2294 assert(has_predicate);
2295
2296 *prefix = NULL;
2297 *prefix_len = 0;
2298 *name = NULL;
2299 *name_len = 0;
2300 *has_predicate = 0;
2301
2302 if (!*parent_times) {
2303 if (!strncmp(*path, "..", 2)) {
2304 *path += 2;
2305 ++par_times;
2306 while (!strncmp(*path, "/..", 3)) {
2307 *path += 3;
2308 ++par_times;
2309 }
2310 }
2311 if (par_times) {
2312 *parent_times = par_times;
2313 } else {
2314 *parent_times = -1;
2315 }
2316 }
2317
2318 if (**path != '/') {
2319 return LY_EINVAL;
2320 }
2321 /* skip '/' */
2322 ++(*path);
2323
2324 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002325 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002326
2327 if ((**path == '/' && (*path)[1]) || !**path) {
2328 /* path continues by another token or this is the last token */
2329 return LY_SUCCESS;
2330 } else if ((*path)[0] != '[') {
2331 /* unexpected character */
2332 return LY_EINVAL;
2333 } else {
2334 /* predicate starting with [ */
2335 *has_predicate = 1;
2336 return LY_SUCCESS;
2337 }
2338}
2339
2340/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002341 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2342 *
2343 * The set of features used for target must be a subset of features used for the leafref.
2344 * This is not a perfect, we should compare the truth tables but it could require too much resources
2345 * and RFC 7950 does not require it explicitely, so we simplify that.
2346 *
2347 * @param[in] refnode The leafref node.
2348 * @param[in] target Tha target node of the leafref.
2349 * @return LY_SUCCESS or LY_EVALID;
2350 */
2351static LY_ERR
2352lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2353{
2354 LY_ERR ret = LY_EVALID;
2355 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002356 unsigned int u, v, count;
2357 struct ly_set features = {0};
2358
2359 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002360 if (iter->iffeatures) {
2361 LY_ARRAY_FOR(iter->iffeatures, u) {
2362 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2363 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002364 }
2365 }
2366 }
2367 }
2368
2369 /* we should have, in features set, a superset of features applicable to the target node.
2370 * So when adding features applicable to the target into the features set, we should not be
2371 * able to actually add any new feature, otherwise it is not a subset of features applicable
2372 * to the leafref itself. */
2373 count = features.count;
2374 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002375 if (iter->iffeatures) {
2376 LY_ARRAY_FOR(iter->iffeatures, u) {
2377 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2378 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002379 /* new feature was added (or LY_EMEM) */
2380 goto cleanup;
2381 }
2382 }
2383 }
2384 }
2385 }
2386 ret = LY_SUCCESS;
2387
2388cleanup:
2389 ly_set_erase(&features, NULL);
2390 return ret;
2391}
2392
2393/**
Radek Krejcia3045382018-11-22 14:30:31 +01002394 * @brief Validate the leafref path.
2395 * @param[in] ctx Compile context
2396 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002397 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002398 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2399 */
2400static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002401lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002402{
2403 const struct lysc_node *node = NULL, *parent = NULL;
2404 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002405 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002406 const char *id, *prefix, *name;
2407 size_t prefix_len, name_len;
2408 int parent_times = 0, has_predicate;
2409 unsigned int iter, u;
2410 LY_ERR ret = LY_SUCCESS;
Radek Krejci327de162019-06-14 12:52:07 +02002411 char *logpath;
Radek Krejcia3045382018-11-22 14:30:31 +01002412
2413 assert(ctx);
2414 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002415 assert(leafref);
2416
Radek Krejci327de162019-06-14 12:52:07 +02002417 logpath = lysc_path(startnode, LY_PATH_LOG);
2418 strncpy(ctx->path, logpath, LYSC_CTX_BUFSIZE - 1);
2419 ctx->path_len = strlen(logpath);
2420 free(logpath);
2421
Radek Krejcia3045382018-11-22 14:30:31 +01002422 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002423 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002424 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2425 if (!iter) { /* first iteration */
2426 /* precess ".." in relative paths */
2427 if (parent_times > 0) {
2428 /* move from the context node */
2429 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2430 /* path is supposed to be evaluated in data tree, so we have to skip
2431 * all schema nodes that cannot be instantiated in data tree */
2432 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002433 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002434 }
2435 }
2436 }
2437
2438 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002439 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002440 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002441 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002442 }
2443 if (!mod) {
2444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002445 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2446 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002447 return LY_EVALID;
2448 }
Radek Krejci3d929562019-02-21 11:25:55 +01002449 if (!mod->implemented) {
2450 /* make the module implemented */
2451 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2452 }
Radek Krejcia3045382018-11-22 14:30:31 +01002453
2454 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2455 if (!node) {
2456 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002457 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002458 return LY_EVALID;
2459 }
2460 parent = node;
2461
2462 if (has_predicate) {
2463 /* we have predicate, so the current result must be list */
2464 if (node->nodetype != LYS_LIST) {
2465 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2466 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002467 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002468 return LY_EVALID;
2469 }
2470
Radek Krejcibade82a2018-12-05 10:13:48 +01002471 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2472 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002473 }
2474
2475 ++iter;
2476 }
2477 if (ret) {
2478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002479 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002480 return LY_EVALID;
2481 }
2482
2483 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2485 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002486 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002487 return LY_EVALID;
2488 }
2489
2490 /* check status */
2491 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2492 return LY_EVALID;
2493 }
2494
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002495 /* check config */
2496 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2497 if (node->flags & LYS_CONFIG_R) {
2498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2499 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2500 leafref->path);
2501 return LY_EVALID;
2502 }
2503 }
2504
Radek Krejci412ddfa2018-11-23 11:44:11 +01002505 /* store the target's type and check for circular chain of leafrefs */
2506 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2507 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2508 if (type == (struct lysc_type*)leafref) {
2509 /* circular chain detected */
2510 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2511 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2512 return LY_EVALID;
2513 }
2514 }
2515
Radek Krejci58d171e2018-11-23 13:50:55 +01002516 /* check if leafref and its target are under common if-features */
2517 if (lys_compile_leafref_features_validate(startnode, node)) {
2518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2519 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2520 leafref->path);
2521 return LY_EVALID;
2522 }
2523
Radek Krejci327de162019-06-14 12:52:07 +02002524 ctx->path_len = 1;
2525 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002526 return LY_SUCCESS;
2527}
2528
Radek Krejcicdfecd92018-11-26 11:27:32 +01002529static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002530 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002531/**
2532 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2533 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002534 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2535 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2536 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2537 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002538 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002539 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002540 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002541 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2542 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2543 * @param[out] type Newly created type structure with the filled information about the type.
2544 * @return LY_ERR value.
2545 */
Radek Krejci19a96102018-11-15 13:38:09 +01002546static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002547lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002548 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002549 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002550{
2551 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002552 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002553 struct lysc_type_bin *bin;
2554 struct lysc_type_num *num;
2555 struct lysc_type_str *str;
2556 struct lysc_type_bits *bits;
2557 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002558 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002559 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002560 struct lysc_type_union *un, *un_aux;
2561 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002562
2563 switch (basetype) {
2564 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002566
2567 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002568 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002569 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2570 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002571 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002572 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002573 }
2574 }
2575
2576 if (tpdfname) {
2577 type_p->compiled = *type;
2578 *type = calloc(1, sizeof(struct lysc_type_bin));
2579 }
2580 break;
2581 case LY_TYPE_BITS:
2582 /* RFC 7950 9.7 - bits */
2583 bits = (struct lysc_type_bits*)(*type);
2584 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002585 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002586 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2587 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002588 }
2589
Radek Krejci555cb5b2018-11-16 14:54:33 +01002590 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002592 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002594 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002596 free(*type);
2597 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002598 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002599 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002600 }
2601
2602 if (tpdfname) {
2603 type_p->compiled = *type;
2604 *type = calloc(1, sizeof(struct lysc_type_bits));
2605 }
2606 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002607 case LY_TYPE_DEC64:
2608 dec = (struct lysc_type_dec*)(*type);
2609
2610 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002611 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002612 if (!type_p->fraction_digits) {
2613 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002615 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002617 free(*type);
2618 *type = NULL;
2619 }
2620 return LY_EVALID;
2621 }
2622 } else if (type_p->fraction_digits) {
2623 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002624 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2626 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002627 tpdfname);
2628 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2630 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002631 free(*type);
2632 *type = NULL;
2633 }
2634 return LY_EVALID;
2635 }
2636 dec->fraction_digits = type_p->fraction_digits;
2637
2638 /* RFC 7950 9.2.4 - range */
2639 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002640 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2641 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002642 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002643 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002644 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002645 }
2646
2647 if (tpdfname) {
2648 type_p->compiled = *type;
2649 *type = calloc(1, sizeof(struct lysc_type_dec));
2650 }
2651 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002652 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002653 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002654
2655 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002656 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002657 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2658 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002659 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002660 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002661 }
2662 } else if (base && ((struct lysc_type_str*)base)->length) {
2663 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2664 }
2665
2666 /* RFC 7950 9.4.5 - pattern */
2667 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002668 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002669 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002670 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2671 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2672 }
2673
2674 if (tpdfname) {
2675 type_p->compiled = *type;
2676 *type = calloc(1, sizeof(struct lysc_type_str));
2677 }
2678 break;
2679 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002680 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002681
2682 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002683 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002684 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002685 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002686 }
2687
Radek Krejci555cb5b2018-11-16 14:54:33 +01002688 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002689 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002690 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002692 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002694 free(*type);
2695 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002696 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002697 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002698 }
2699
2700 if (tpdfname) {
2701 type_p->compiled = *type;
2702 *type = calloc(1, sizeof(struct lysc_type_enum));
2703 }
2704 break;
2705 case LY_TYPE_INT8:
2706 case LY_TYPE_UINT8:
2707 case LY_TYPE_INT16:
2708 case LY_TYPE_UINT16:
2709 case LY_TYPE_INT32:
2710 case LY_TYPE_UINT32:
2711 case LY_TYPE_INT64:
2712 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002713 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002714
2715 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002716 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002717 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2718 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002719 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002720 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002721 }
2722 }
2723
2724 if (tpdfname) {
2725 type_p->compiled = *type;
2726 *type = calloc(1, sizeof(struct lysc_type_num));
2727 }
2728 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002729 case LY_TYPE_IDENT:
2730 idref = (struct lysc_type_identityref*)(*type);
2731
2732 /* RFC 7950 9.10.2 - base */
2733 if (type_p->bases) {
2734 if (base) {
2735 /* only the directly derived identityrefs can contain base specification */
2736 if (tpdfname) {
2737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002738 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002739 tpdfname);
2740 } else {
2741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002742 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002743 free(*type);
2744 *type = NULL;
2745 }
2746 return LY_EVALID;
2747 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002748 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002749 }
2750
2751 if (!base && !type_p->flags) {
2752 /* type derived from identityref built-in type must contain at least one base */
2753 if (tpdfname) {
2754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2755 } else {
2756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2757 free(*type);
2758 *type = NULL;
2759 }
2760 return LY_EVALID;
2761 }
2762
2763 if (tpdfname) {
2764 type_p->compiled = *type;
2765 *type = calloc(1, sizeof(struct lysc_type_identityref));
2766 }
2767 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002768 case LY_TYPE_LEAFREF:
2769 /* RFC 7950 9.9.3 - require-instance */
2770 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002771 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002772 if (tpdfname) {
2773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2774 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2775 } else {
2776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2777 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2778 free(*type);
2779 *type = NULL;
2780 }
2781 return LY_EVALID;
2782 }
Radek Krejcia3045382018-11-22 14:30:31 +01002783 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002784 } else if (base) {
2785 /* inherit */
2786 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002787 } else {
2788 /* default is true */
2789 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2790 }
2791 if (type_p->path) {
2792 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002793 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002794 } else if (base) {
2795 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002796 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002797 } else if (tpdfname) {
2798 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2799 return LY_EVALID;
2800 } else {
2801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2802 free(*type);
2803 *type = NULL;
2804 return LY_EVALID;
2805 }
2806 if (tpdfname) {
2807 type_p->compiled = *type;
2808 *type = calloc(1, sizeof(struct lysc_type_leafref));
2809 }
2810 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002811 case LY_TYPE_INST:
2812 /* RFC 7950 9.9.3 - require-instance */
2813 if (type_p->flags & LYS_SET_REQINST) {
2814 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2815 } else {
2816 /* default is true */
2817 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2818 }
2819
2820 if (tpdfname) {
2821 type_p->compiled = *type;
2822 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2823 }
2824 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002825 case LY_TYPE_UNION:
2826 un = (struct lysc_type_union*)(*type);
2827
2828 /* RFC 7950 7.4 - type */
2829 if (type_p->types) {
2830 if (base) {
2831 /* only the directly derived union can contain types specification */
2832 if (tpdfname) {
2833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2834 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2835 tpdfname);
2836 } else {
2837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2838 "Invalid type substatement for the type not directly derived from union built-in type.");
2839 free(*type);
2840 *type = NULL;
2841 }
2842 return LY_EVALID;
2843 }
2844 /* compile the type */
2845 additional = 0;
2846 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2847 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002848 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01002849 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2850 /* add space for additional types from the union subtype */
2851 un_aux = (struct lysc_type_union *)un->types[u + additional];
2852 p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types)));
2853 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2854 un->types = (void*)((uint32_t*)(p) + 1);
2855
2856 /* copy subtypes of the subtype union */
2857 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2858 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2859 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2860 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2861 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2862 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2863 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2864 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2865 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2866 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2867 /* TODO extensions */
2868
2869 } else {
2870 un->types[u + additional] = un_aux->types[v];
2871 ++un_aux->types[v]->refcount;
2872 }
2873 ++additional;
2874 LY_ARRAY_INCREMENT(un->types);
2875 }
2876 /* compensate u increment in main loop */
2877 --additional;
2878
2879 /* free the replaced union subtype */
2880 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2881 } else {
2882 LY_ARRAY_INCREMENT(un->types);
2883 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002884 }
2885 }
2886
2887 if (!base && !type_p->flags) {
2888 /* type derived from union built-in type must contain at least one type */
2889 if (tpdfname) {
2890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2891 } else {
2892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2893 free(*type);
2894 *type = NULL;
2895 }
2896 return LY_EVALID;
2897 }
2898
2899 if (tpdfname) {
2900 type_p->compiled = *type;
2901 *type = calloc(1, sizeof(struct lysc_type_union));
2902 }
2903 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002904 case LY_TYPE_BOOL:
2905 case LY_TYPE_EMPTY:
2906 case LY_TYPE_UNKNOWN: /* just to complete switch */
2907 break;
2908 }
2909 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2910done:
2911 return ret;
2912}
2913
Radek Krejcia3045382018-11-22 14:30:31 +01002914/**
2915 * @brief Compile information about the leaf/leaf-list's type.
2916 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002917 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2918 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2919 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2920 * @param[in] context_name Name of the context node or referencing typedef for logging.
2921 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002922 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002923 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002924 * @return LY_ERR value.
2925 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002926static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002927lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002928 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002929{
2930 LY_ERR ret = LY_SUCCESS;
2931 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002932 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002933 struct type_context {
2934 const struct lysp_tpdf *tpdf;
2935 struct lysp_node *node;
2936 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002937 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002938 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002939 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002940 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002941 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002942
2943 (*type) = NULL;
2944
2945 tctx = calloc(1, sizeof *tctx);
2946 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002947 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002948 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2949 ret == LY_SUCCESS;
2950 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2951 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2952 if (basetype) {
2953 break;
2954 }
2955
2956 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002957 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2958 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002959 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2960
Radek Krejcicdfecd92018-11-26 11:27:32 +01002961 if (units && !*units) {
2962 /* inherit units */
2963 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2964 }
Radek Krejci01342af2019-01-03 15:18:08 +01002965 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002966 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002967 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002968 }
Radek Krejci01342af2019-01-03 15:18:08 +01002969 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002970 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2971 break;
2972 }
2973
Radek Krejci19a96102018-11-15 13:38:09 +01002974 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002975 /* it is not necessary to continue, the rest of the chain was already compiled,
2976 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002977 basetype = tctx->tpdf->type.compiled->basetype;
2978 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002979 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002980 dummyloops = 1;
2981 goto preparenext;
2982 } else {
2983 tctx = NULL;
2984 break;
2985 }
Radek Krejci19a96102018-11-15 13:38:09 +01002986 }
2987
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002988 /* circular typedef reference detection */
2989 for (u = 0; u < tpdf_chain.count; u++) {
2990 /* local part */
2991 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2992 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2994 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2995 free(tctx);
2996 ret = LY_EVALID;
2997 goto cleanup;
2998 }
2999 }
3000 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3001 /* global part for unions corner case */
3002 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3003 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3004 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3005 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3006 free(tctx);
3007 ret = LY_EVALID;
3008 goto cleanup;
3009 }
3010 }
3011
Radek Krejci19a96102018-11-15 13:38:09 +01003012 /* store information for the following processing */
3013 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3014
Radek Krejcicdfecd92018-11-26 11:27:32 +01003015preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003016 /* prepare next loop */
3017 tctx_prev = tctx;
3018 tctx = calloc(1, sizeof *tctx);
3019 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3020 }
3021 free(tctx);
3022
3023 /* allocate type according to the basetype */
3024 switch (basetype) {
3025 case LY_TYPE_BINARY:
3026 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003027 break;
3028 case LY_TYPE_BITS:
3029 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003030 break;
3031 case LY_TYPE_BOOL:
3032 case LY_TYPE_EMPTY:
3033 *type = calloc(1, sizeof(struct lysc_type));
3034 break;
3035 case LY_TYPE_DEC64:
3036 *type = calloc(1, sizeof(struct lysc_type_dec));
3037 break;
3038 case LY_TYPE_ENUM:
3039 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003040 break;
3041 case LY_TYPE_IDENT:
3042 *type = calloc(1, sizeof(struct lysc_type_identityref));
3043 break;
3044 case LY_TYPE_INST:
3045 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3046 break;
3047 case LY_TYPE_LEAFREF:
3048 *type = calloc(1, sizeof(struct lysc_type_leafref));
3049 break;
3050 case LY_TYPE_STRING:
3051 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003052 break;
3053 case LY_TYPE_UNION:
3054 *type = calloc(1, sizeof(struct lysc_type_union));
3055 break;
3056 case LY_TYPE_INT8:
3057 case LY_TYPE_UINT8:
3058 case LY_TYPE_INT16:
3059 case LY_TYPE_UINT16:
3060 case LY_TYPE_INT32:
3061 case LY_TYPE_UINT32:
3062 case LY_TYPE_INT64:
3063 case LY_TYPE_UINT64:
3064 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003065 break;
3066 case LY_TYPE_UNKNOWN:
3067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3068 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3069 ret = LY_EVALID;
3070 goto cleanup;
3071 }
3072 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003073 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3075 ly_data_type2str[basetype]);
3076 free(*type);
3077 (*type) = NULL;
3078 ret = LY_EVALID;
3079 goto cleanup;
3080 }
3081
3082 /* get restrictions from the referred typedefs */
3083 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3084 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003085
3086 /* remember the typedef context for circular check */
3087 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3088
Radek Krejci43699232018-11-23 14:59:46 +01003089 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003090 base = tctx->tpdf->type.compiled;
3091 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003092 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003093 /* no change, just use the type information from the base */
3094 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3095 ++base->refcount;
3096 continue;
3097 }
3098
3099 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003100 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3102 tctx->tpdf->name, ly_data_type2str[basetype]);
3103 ret = LY_EVALID;
3104 goto cleanup;
3105 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3106 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3107 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3108 tctx->tpdf->name, tctx->tpdf->dflt);
3109 ret = LY_EVALID;
3110 goto cleanup;
3111 }
3112
Radek Krejci19a96102018-11-15 13:38:09 +01003113 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003114 /* TODO user type plugins */
3115 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003116 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003117 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003118 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003119 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003120 LY_CHECK_GOTO(ret, cleanup);
3121 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003122 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003123 /* remove the processed typedef contexts from the stack for circular check */
3124 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003125
Radek Krejcic5c27e52018-11-15 14:38:11 +01003126 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003127 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003128 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003129 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003130 /* TODO user type plugins */
3131 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003132 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003133 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003134 LY_CHECK_GOTO(ret, cleanup);
3135 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003136 /* no specific restriction in leaf's type definition, copy from the base */
3137 free(*type);
3138 (*type) = base;
3139 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003140 }
Radek Krejci01342af2019-01-03 15:18:08 +01003141 if (!(*type)->dflt) {
3142 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3143 }
Radek Krejci19a96102018-11-15 13:38:09 +01003144
Radek Krejciec4da802019-05-02 13:02:41 +02003145 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003146
3147cleanup:
3148 ly_set_erase(&tpdf_chain, free);
3149 return ret;
3150}
3151
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003152/**
3153 * @brief Compile status information of the given node.
3154 *
3155 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3156 * has the status correctly set during the compilation.
3157 *
3158 * @param[in] ctx Compile context
3159 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3160 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3161 * the compatibility with the parent's status value.
3162 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3163 * @return LY_ERR value.
3164 */
3165static LY_ERR
3166lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3167{
3168 /* status - it is not inherited by specification, but it does not make sense to have
3169 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3170 if (!((*node_flags) & LYS_STATUS_MASK)) {
3171 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3172 if ((parent_flags & 0x3) != 0x3) {
3173 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3174 * combination of bits (0x3) which marks the uses_status value */
3175 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3176 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3177 }
3178 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3179 } else {
3180 (*node_flags) |= LYS_STATUS_CURR;
3181 }
3182 } else if (parent_flags & LYS_STATUS_MASK) {
3183 /* check status compatibility with the parent */
3184 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3185 if ((*node_flags) & LYS_STATUS_CURR) {
3186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3187 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3188 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3189 } else { /* LYS_STATUS_DEPRC */
3190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3191 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3192 }
3193 return LY_EVALID;
3194 }
3195 }
3196 return LY_SUCCESS;
3197}
3198
Radek Krejci8cce8532019-03-05 11:27:45 +01003199/**
3200 * @brief Check uniqness of the node/action/notification name.
3201 *
3202 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3203 * structures, but they share the namespace so we need to check their name collisions.
3204 *
3205 * @param[in] ctx Compile context.
3206 * @param[in] children List (linked list) of data nodes to go through.
3207 * @param[in] actions List (sized array) of actions or RPCs to go through.
3208 * @param[in] notifs List (sized array) of Notifications to go through.
3209 * @param[in] name Name of the item to find in the given lists.
3210 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3211 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3212 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3213 */
3214static LY_ERR
3215lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3216 const struct lysc_action *actions, const struct lysc_notif *notifs,
3217 const char *name, void *exclude)
3218{
3219 const struct lysc_node *iter;
3220 unsigned int u;
3221
3222 LY_LIST_FOR(children, iter) {
3223 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3224 goto error;
3225 }
3226 }
3227 LY_ARRAY_FOR(actions, u) {
3228 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3229 goto error;
3230 }
3231 }
3232 LY_ARRAY_FOR(notifs, u) {
3233 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3234 goto error;
3235 }
3236 }
3237 return LY_SUCCESS;
3238error:
3239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3240 return LY_EEXIST;
3241}
3242
Radek Krejciec4da802019-05-02 13:02:41 +02003243static 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 +01003244
Radek Krejcia3045382018-11-22 14:30:31 +01003245/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003246 * @brief Compile parsed RPC/action schema node information.
3247 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003248 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003249 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003250 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3251 * @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).
3252 * Zero means no uses, non-zero value with no status bit set mean the default status.
3253 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3254 */
3255static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003256lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003257 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3258{
3259 LY_ERR ret = LY_SUCCESS;
3260 struct lysp_node *child_p;
3261 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003262 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003263
Radek Krejci327de162019-06-14 12:52:07 +02003264 lysc_update_path(ctx, parent, action_p->name);
3265
Radek Krejci8cce8532019-03-05 11:27:45 +01003266 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3267 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3268 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3269 action_p->name, action)) {
3270 return LY_EVALID;
3271 }
3272
Radek Krejciec4da802019-05-02 13:02:41 +02003273 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003275 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003276 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003277 return LY_EVALID;
3278 }
3279
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003280 action->nodetype = LYS_ACTION;
3281 action->module = ctx->mod;
3282 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003283 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003284 action->sp = action_p;
3285 }
3286 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3287
3288 /* status - it is not inherited by specification, but it does not make sense to have
3289 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3290 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3291
3292 DUP_STRING(ctx->ctx, action_p->name, action->name);
3293 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3294 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003295 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3296 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003297
3298 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003299 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003300 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3301 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3302 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003303 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003304 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003305 }
Radek Krejci327de162019-06-14 12:52:07 +02003306 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003307 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003308
3309 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003310 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003311 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3312 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3313 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003314 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003315 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003316 }
Radek Krejci327de162019-06-14 12:52:07 +02003317 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003318
Radek Krejci327de162019-06-14 12:52:07 +02003319 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003320cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003321 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003322 return ret;
3323}
3324
3325/**
Radek Krejci43981a32019-04-12 09:44:11 +02003326 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003327 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003328 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003329 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3330 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3331 * @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 +02003332 * Zero means no uses, non-zero value with no status bit set mean the default status.
3333 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3334 */
3335static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003336lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003337 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3338{
3339 LY_ERR ret = LY_SUCCESS;
3340 struct lysp_node *child_p;
3341 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003342 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003343
Radek Krejci327de162019-06-14 12:52:07 +02003344 lysc_update_path(ctx, parent, notif_p->name);
3345
Radek Krejcifc11bd72019-04-11 16:00:05 +02003346 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3347 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3348 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3349 notif_p->name, notif)) {
3350 return LY_EVALID;
3351 }
3352
Radek Krejciec4da802019-05-02 13:02:41 +02003353 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3355 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003356 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003357 return LY_EVALID;
3358 }
3359
3360 notif->nodetype = LYS_NOTIF;
3361 notif->module = ctx->mod;
3362 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003363 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003364 notif->sp = notif_p;
3365 }
3366 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3367
3368 /* status - it is not inherited by specification, but it does not make sense to have
3369 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3370 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3371
3372 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3373 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3374 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003375 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3376 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3377 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003378
Radek Krejciec4da802019-05-02 13:02:41 +02003379 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003380 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003381 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003382 }
3383
Radek Krejci327de162019-06-14 12:52:07 +02003384 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003385cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003386 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003387 return ret;
3388}
3389
3390/**
Radek Krejcia3045382018-11-22 14:30:31 +01003391 * @brief Compile parsed container node information.
3392 * @param[in] ctx Compile context
3393 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003394 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3395 * is enriched with the container-specific information.
3396 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3397 */
Radek Krejci19a96102018-11-15 13:38:09 +01003398static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003399lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003400{
3401 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3402 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3403 struct lysp_node *child_p;
3404 unsigned int u;
3405 LY_ERR ret = LY_SUCCESS;
3406
Radek Krejcife909632019-02-12 15:34:42 +01003407 if (cont_p->presence) {
3408 cont->flags |= LYS_PRESENCE;
3409 }
3410
Radek Krejci19a96102018-11-15 13:38:09 +01003411 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003412 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003413 }
3414
Radek Krejciec4da802019-05-02 13:02:41 +02003415 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3416 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3417 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003418
3419done:
3420 return ret;
3421}
3422
Radek Krejci33f72892019-02-21 10:36:58 +01003423/*
3424 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3425 * @param[in] ctx Compile context.
3426 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3427 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003428 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3429 * @return LY_ERR value.
3430 */
3431static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003432lys_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 +01003433{
3434 unsigned int u, v;
3435 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3436
Radek Krejciec4da802019-05-02 13:02:41 +02003437 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 +01003438 leaf->units ? NULL : &leaf->units));
3439 if (leaf->nodetype == LYS_LEAFLIST) {
3440 if (llist->type->dflt && !llist->dflts && !llist->min) {
3441 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3442 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3443 LY_ARRAY_INCREMENT(llist->dflts);
3444 }
3445 } else {
3446 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3447 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3448 }
3449 }
3450 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3451 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3452 ly_set_add(&ctx->unres, leaf, 0);
3453 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3454 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3455 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3456 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3457 ly_set_add(&ctx->unres, leaf, 0);
3458 }
3459 }
3460 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3461 if (leaf->flags & LYS_SET_DFLT) {
3462 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3463 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3464 return LY_EVALID;
3465 }
3466 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3468 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3469 return LY_EVALID;
3470 }
3471 }
3472
3473 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3474 /* configuration data values must be unique - so check the default values */
3475 LY_ARRAY_FOR(llist->dflts, u) {
3476 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3477 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3479 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3480 return LY_EVALID;
3481 }
3482 }
3483 }
3484 }
3485
3486 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3487
3488 return LY_SUCCESS;
3489}
3490
Radek Krejcia3045382018-11-22 14:30:31 +01003491/**
3492 * @brief Compile parsed leaf node information.
3493 * @param[in] ctx Compile context
3494 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003495 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3496 * is enriched with the leaf-specific information.
3497 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3498 */
Radek Krejci19a96102018-11-15 13:38:09 +01003499static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003500lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003501{
3502 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3503 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3504 unsigned int u;
3505 LY_ERR ret = LY_SUCCESS;
3506
Radek Krejciec4da802019-05-02 13:02:41 +02003507 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003508 if (leaf_p->units) {
3509 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3510 leaf->flags |= LYS_SET_UNITS;
3511 }
3512 if (leaf_p->dflt) {
3513 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003514 leaf->flags |= LYS_SET_DFLT;
3515 }
Radek Krejci43699232018-11-23 14:59:46 +01003516
Radek Krejciec4da802019-05-02 13:02:41 +02003517 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003518
Radek Krejci19a96102018-11-15 13:38:09 +01003519done:
3520 return ret;
3521}
3522
Radek Krejcia3045382018-11-22 14:30:31 +01003523/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003524 * @brief Compile parsed leaf-list node information.
3525 * @param[in] ctx Compile context
3526 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003527 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3528 * is enriched with the leaf-list-specific information.
3529 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3530 */
3531static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003532lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003533{
3534 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3535 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003536 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003537 LY_ERR ret = LY_SUCCESS;
3538
Radek Krejciec4da802019-05-02 13:02:41 +02003539 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003540 if (llist_p->units) {
3541 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3542 llist->flags |= LYS_SET_UNITS;
3543 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003544
3545 if (llist_p->dflts) {
3546 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3547 LY_ARRAY_FOR(llist_p->dflts, u) {
3548 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3549 LY_ARRAY_INCREMENT(llist->dflts);
3550 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003551 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003552 }
3553
3554 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003555 if (llist->min) {
3556 llist->flags |= LYS_MAND_TRUE;
3557 }
Radek Krejcib7408632018-11-28 17:12:11 +01003558 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003559
Radek Krejciec4da802019-05-02 13:02:41 +02003560 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003561
3562done:
3563 return ret;
3564}
3565
3566/**
Radek Krejci7af64242019-02-18 13:07:53 +01003567 * @brief Compile information about list's uniques.
3568 * @param[in] ctx Compile context.
3569 * @param[in] context_module Module where the prefixes are going to be resolved.
3570 * @param[in] uniques Sized array list of unique statements.
3571 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3572 * @return LY_ERR value.
3573 */
3574static LY_ERR
3575lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3576{
3577 LY_ERR ret = LY_SUCCESS;
3578 struct lysc_node_leaf **key, ***unique;
3579 const char *keystr, *delim;
3580 size_t len;
3581 unsigned int v;
3582 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003583 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003584
3585 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3586 config = -1;
3587 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3588 keystr = uniques[v];
3589 while (keystr) {
3590 delim = strpbrk(keystr, " \t\n");
3591 if (delim) {
3592 len = delim - keystr;
3593 while (isspace(*delim)) {
3594 ++delim;
3595 }
3596 } else {
3597 len = strlen(keystr);
3598 }
3599
3600 /* unique node must be present */
3601 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003602 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3603 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003604 if (ret != LY_SUCCESS) {
3605 if (ret == LY_EDENIED) {
3606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003607 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003608 len, keystr, lys_nodetype2str((*key)->nodetype));
3609 }
3610 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003611 } else if (flags) {
3612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3613 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3614 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3615 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003616 }
3617
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003618
Radek Krejci7af64242019-02-18 13:07:53 +01003619 /* all referenced leafs must be of the same config type */
3620 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3622 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3623 return LY_EVALID;
3624 } else if ((*key)->flags & LYS_CONFIG_W) {
3625 config = 1;
3626 } else { /* LYS_CONFIG_R */
3627 config = 0;
3628 }
3629
3630 /* check status */
3631 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3632 (*key)->flags, (*key)->module, (*key)->name));
3633
3634 /* mark leaf as unique */
3635 (*key)->flags |= LYS_UNIQUE;
3636
3637 /* next unique value in line */
3638 keystr = delim;
3639 }
3640 /* next unique definition */
3641 }
3642
3643 return LY_SUCCESS;
3644}
3645
3646/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003647 * @brief Compile parsed list node information.
3648 * @param[in] ctx Compile context
3649 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003650 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3651 * is enriched with the list-specific information.
3652 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3653 */
3654static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003655lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003656{
3657 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3658 struct lysc_node_list *list = (struct lysc_node_list*)node;
3659 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003660 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003661 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003662 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003663 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003664 LY_ERR ret = LY_SUCCESS;
3665
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003666 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003667 if (list->min) {
3668 list->flags |= LYS_MAND_TRUE;
3669 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003670 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3671
3672 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003673 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003674 }
3675
Radek Krejciec4da802019-05-02 13:02:41 +02003676 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003677
3678 /* keys */
3679 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3681 return LY_EVALID;
3682 }
3683
3684 /* find all the keys (must be direct children) */
3685 keystr = list_p->key;
3686 while (keystr) {
3687 delim = strpbrk(keystr, " \t\n");
3688 if (delim) {
3689 len = delim - keystr;
3690 while (isspace(*delim)) {
3691 ++delim;
3692 }
3693 } else {
3694 len = strlen(keystr);
3695 }
3696
3697 /* key node must be present */
3698 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3699 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3700 if (!(*key)) {
3701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3702 "The list's key \"%.*s\" not found.", len, keystr);
3703 return LY_EVALID;
3704 }
3705 /* keys must be unique */
3706 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3707 if (*key == list->keys[u]) {
3708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3709 "Duplicated key identifier \"%.*s\".", len, keystr);
3710 return LY_EVALID;
3711 }
3712 }
Radek Krejci327de162019-06-14 12:52:07 +02003713 lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003714 /* key must have the same config flag as the list itself */
3715 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3717 return LY_EVALID;
3718 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003719 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003720 /* YANG 1.0 denies key to be of empty type */
3721 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003723 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003724 return LY_EVALID;
3725 }
3726 } else {
3727 /* when and if-feature are illegal on list keys */
3728 if ((*key)->when) {
3729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003730 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003731 return LY_EVALID;
3732 }
3733 if ((*key)->iffeatures) {
3734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003735 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003736 return LY_EVALID;
3737 }
3738 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003739
3740 /* check status */
3741 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3742 (*key)->flags, (*key)->module, (*key)->name));
3743
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003744 /* ignore default values of the key */
3745 if ((*key)->dflt) {
3746 lydict_remove(ctx->ctx, (*key)->dflt);
3747 (*key)->dflt = NULL;
3748 }
3749 /* mark leaf as key */
3750 (*key)->flags |= LYS_KEY;
3751
3752 /* next key value */
3753 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003754 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003755 }
3756
3757 /* uniques */
3758 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003759 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003760 }
3761
Radek Krejciec4da802019-05-02 13:02:41 +02003762 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3763 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003764
3765done:
3766 return ret;
3767}
3768
Radek Krejcib56c7502019-02-13 14:19:54 +01003769/**
3770 * @brief Do some checks and set the default choice's case.
3771 *
3772 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3773 *
3774 * @param[in] ctx Compile context.
3775 * @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,
3776 * not the reference to the imported module.
3777 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3778 * @return LY_ERR value.
3779 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003780static LY_ERR
3781lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3782{
3783 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3784 const char *prefix = NULL, *name;
3785 size_t prefix_len = 0;
3786
3787 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3788 name = strchr(dflt, ':');
3789 if (name) {
3790 prefix = dflt;
3791 prefix_len = name - prefix;
3792 ++name;
3793 } else {
3794 name = dflt;
3795 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003796 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003797 /* prefixed default case make sense only for the prefix of the schema itself */
3798 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3799 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3800 prefix_len, prefix);
3801 return LY_EVALID;
3802 }
3803 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3804 if (!ch->dflt) {
3805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3806 "Default case \"%s\" not found.", dflt);
3807 return LY_EVALID;
3808 }
3809 /* no mandatory nodes directly under the default case */
3810 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003811 if (iter->parent != (struct lysc_node*)ch->dflt) {
3812 break;
3813 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003814 if (iter->flags & LYS_MAND_TRUE) {
3815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3816 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3817 return LY_EVALID;
3818 }
3819 }
Radek Krejci01342af2019-01-03 15:18:08 +01003820 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003821 return LY_SUCCESS;
3822}
3823
Radek Krejciccd20f12019-02-15 14:12:27 +01003824static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003825lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003826{
3827 struct lys_module *mod;
3828 const char *prefix = NULL, *name;
3829 size_t prefix_len = 0;
3830 struct lysc_node_case *cs;
3831 struct lysc_node *node;
3832
3833 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3834 name = strchr(dflt, ':');
3835 if (name) {
3836 prefix = dflt;
3837 prefix_len = name - prefix;
3838 ++name;
3839 } else {
3840 name = dflt;
3841 }
3842 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3843 if (prefix) {
3844 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3845 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003846 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3847 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003848 return LY_EVALID;
3849 }
3850 } else {
3851 mod = ctx->mod;
3852 }
3853 /* get the default case */
3854 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3855 if (!cs) {
3856 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003857 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003858 return LY_EVALID;
3859 }
3860
3861 /* check that there is no mandatory node */
3862 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003863 if (node->parent != (struct lysc_node*)cs) {
3864 break;
3865 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003866 if (node->flags & LYS_MAND_TRUE) {
3867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003868 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3869 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003870 return LY_EVALID;
3871 }
3872 }
3873
3874 /* set the default case in choice */
3875 ch->dflt = cs;
3876 cs->flags |= LYS_SET_DFLT;
3877
3878 return LY_SUCCESS;
3879}
3880
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003881/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003882 * @brief Compile parsed choice node information.
3883 * @param[in] ctx Compile context
3884 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003885 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003886 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003887 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3888 */
3889static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003890lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003891{
3892 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3893 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3894 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003895 LY_ERR ret = LY_SUCCESS;
3896
Radek Krejci056d0a82018-12-06 16:57:25 +01003897 LY_LIST_FOR(ch_p->child, child_p) {
3898 if (child_p->nodetype == LYS_CASE) {
3899 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003900 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003901 }
3902 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003903 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003904 }
3905 }
3906
3907 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003908 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003909 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003910 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003911
Radek Krejci9800fb82018-12-13 14:26:23 +01003912 return ret;
3913}
3914
3915/**
3916 * @brief Compile parsed anydata or anyxml node information.
3917 * @param[in] ctx Compile context
3918 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01003919 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3920 * is enriched with the any-specific information.
3921 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3922 */
3923static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003924lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01003925{
3926 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3927 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3928 unsigned int u;
3929 LY_ERR ret = LY_SUCCESS;
3930
Radek Krejciec4da802019-05-02 13:02:41 +02003931 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01003932
3933 if (any->flags & LYS_CONFIG_W) {
3934 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3935 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3936 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003937done:
3938 return ret;
3939}
3940
Radek Krejcib56c7502019-02-13 14:19:54 +01003941/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003942 * @brief Connect the node into the siblings list and check its name uniqueness.
3943 *
3944 * @param[in] ctx Compile context
3945 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3946 * the choice itself is expected instead of a specific case node.
3947 * @param[in] node Schema node to connect into the list.
3948 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3949 */
3950static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003951lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003952{
3953 struct lysc_node **children;
3954
3955 if (node->nodetype == LYS_CASE) {
3956 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3957 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003958 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003959 }
3960 if (children) {
3961 if (!(*children)) {
3962 /* first child */
3963 *children = node;
3964 } else if (*children != node) {
3965 /* by the condition in previous branch we cover the choice/case children
3966 * - the children list is shared by the choice and the the first case, in addition
3967 * the first child of each case must be referenced from the case node. So the node is
3968 * actually always already inserted in case it is the first children - so here such
3969 * a situation actually corresponds to the first branch */
3970 /* insert at the end of the parent's children list */
3971 (*children)->prev->next = node;
3972 node->prev = (*children)->prev;
3973 (*children)->prev = node;
3974
3975 /* check the name uniqueness */
3976 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3977 lysc_node_notifs(parent), node->name, node)) {
3978 return LY_EEXIST;
3979 }
3980 }
3981 }
3982 return LY_SUCCESS;
3983}
3984
Radek Krejci95710c92019-02-11 15:49:55 +01003985/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003986 * @brief Get the XPath context node for the given schema node.
3987 * @param[in] start The schema node where the XPath expression appears.
3988 * @return The context node to evaluate XPath expression in given schema node.
3989 * @return NULL in case the context node is the root node.
3990 */
3991static struct lysc_node *
3992lysc_xpath_context(struct lysc_node *start)
3993{
3994 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3995 start = start->parent);
3996 return start;
3997}
3998
3999/**
4000 * @brief Prepare the case structure in choice node for the new data node.
4001 *
4002 * 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
4003 * created in the choice when the first child was processed.
4004 *
4005 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004006 * @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,
4007 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004008 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4009 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4010 * @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,
4011 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004012 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004013static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004014lys_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 +01004015{
4016 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004017 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004018 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004019 unsigned int u;
4020 LY_ERR ret;
4021
Radek Krejci95710c92019-02-11 15:49:55 +01004022#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004023 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004024 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004025 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4026 return NULL; \
4027 } \
4028 }
4029
Radek Krejci95710c92019-02-11 15:49:55 +01004030 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4031 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004032
4033 /* we have to add an implicit case node into the parent choice */
4034 cs = calloc(1, sizeof(struct lysc_node_case));
4035 DUP_STRING(ctx->ctx, child->name, cs->name);
4036 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004037 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004038 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4039 /* the case is already present since the child is not its first children */
4040 return (struct lysc_node_case*)ch->cases->prev;
4041 }
Radek Krejci95710c92019-02-11 15:49:55 +01004042 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004043
4044 /* explicit parent case is not present (this is its first child) */
4045 cs = calloc(1, sizeof(struct lysc_node_case));
4046 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4047 cs->flags = LYS_STATUS_MASK & node_p->flags;
4048 cs->sp = node_p;
4049
Radek Krejcib1b59152019-01-07 13:21:56 +01004050 /* 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 +02004051 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004052
4053 if (node_p->when) {
4054 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004055 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004056 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004057 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004058 }
Radek Krejciec4da802019-05-02 13:02:41 +02004059 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004060 } else {
4061 LOGINT(ctx->ctx);
4062 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004063 }
4064 cs->module = ctx->mod;
4065 cs->prev = (struct lysc_node*)cs;
4066 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004067 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004068 cs->parent = (struct lysc_node*)ch;
4069 cs->child = child;
4070
4071 return cs;
4072error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004073 if (cs) {
4074 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4075 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004076 return NULL;
4077
4078#undef UNIQUE_CHECK
4079}
4080
Radek Krejcib56c7502019-02-13 14:19:54 +01004081/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004082 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004083 *
4084 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004085 * @param[in] node Target node where the config is supposed to be changed.
4086 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004087 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4088 * 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 +01004089 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4090 * @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 +01004091 * @return LY_ERR value.
4092 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004093static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004094lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004095 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004096{
4097 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004098 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004099
4100 if (config == (node->flags & LYS_CONFIG_MASK)) {
4101 /* nothing to do */
4102 return LY_SUCCESS;
4103 }
4104
4105 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004106 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004107 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4108 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004109 "Invalid %s of config - configuration node cannot be child of any state data node.",
4110 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004111 return LY_EVALID;
4112 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004113 node->flags |= LYS_SET_CONFIG;
4114 } else {
4115 if (node->flags & LYS_SET_CONFIG) {
4116 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4117 /* setting config flags, but have node with explicit config true */
4118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004119 "Invalid %s of config - configuration node cannot be child of any state data node.",
4120 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004121 return LY_EVALID;
4122 }
4123 /* do not change config on nodes where the config is explicitely set, this does not apply to
4124 * nodes, which are being changed explicitly (targets of refine or deviation) */
4125 return LY_SUCCESS;
4126 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004127 }
4128 node->flags &= ~LYS_CONFIG_MASK;
4129 node->flags |= config;
4130
4131 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004132 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004133 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004134 }
4135
Radek Krejci76b3e962018-12-14 17:01:25 +01004136 return LY_SUCCESS;
4137}
4138
Radek Krejcib56c7502019-02-13 14:19:54 +01004139/**
4140 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4141 *
4142 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4143 * the flag to such parents from a mandatory children.
4144 *
4145 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4146 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4147 * (mandatory children was removed).
4148 */
Radek Krejcife909632019-02-12 15:34:42 +01004149void
4150lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4151{
4152 struct lysc_node *iter;
4153
4154 if (add) { /* set flag */
4155 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4156 parent = parent->parent) {
4157 parent->flags |= LYS_MAND_TRUE;
4158 }
4159 } else { /* unset flag */
4160 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004161 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004162 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004163 /* there is another mandatory node */
4164 return;
4165 }
4166 }
4167 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4168 parent->flags &= ~LYS_MAND_TRUE;
4169 }
4170 }
4171}
4172
Radek Krejci056d0a82018-12-06 16:57:25 +01004173/**
Radek Krejci3641f562019-02-13 15:38:40 +01004174 * @brief Internal sorting process for the lys_compile_augment_sort().
4175 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4176 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4177 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4178 */
4179static void
4180lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4181{
4182 unsigned int v;
4183 size_t len;
4184
4185 len = strlen(aug_p->nodeid);
4186 LY_ARRAY_FOR(result, v) {
4187 if (strlen(result[v]->nodeid) <= len) {
4188 continue;
4189 }
4190 if (v < LY_ARRAY_SIZE(result)) {
4191 /* move the rest of array */
4192 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4193 break;
4194 }
4195 }
4196 result[v] = aug_p;
4197 LY_ARRAY_INCREMENT(result);
4198}
4199
4200/**
4201 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4202 *
4203 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4204 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4205 *
4206 * @param[in] ctx Compile context.
4207 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4208 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4209 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4210 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4211 * @return LY_ERR value.
4212 */
4213LY_ERR
4214lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4215{
4216 struct lysp_augment **result = NULL;
4217 unsigned int u, v;
4218 size_t count = 0;
4219
4220 assert(augments);
4221
4222 /* get count of the augments in module and all its submodules */
4223 if (aug_p) {
4224 count += LY_ARRAY_SIZE(aug_p);
4225 }
4226 LY_ARRAY_FOR(inc_p, u) {
4227 if (inc_p[u].submodule->augments) {
4228 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4229 }
4230 }
4231
4232 if (!count) {
4233 *augments = NULL;
4234 return LY_SUCCESS;
4235 }
4236 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4237
4238 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4239 * together, so there can be even /z/y betwwen them. */
4240 LY_ARRAY_FOR(aug_p, u) {
4241 lys_compile_augment_sort_(&aug_p[u], result);
4242 }
4243 LY_ARRAY_FOR(inc_p, u) {
4244 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4245 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4246 }
4247 }
4248
4249 *augments = result;
4250 return LY_SUCCESS;
4251}
4252
4253/**
4254 * @brief Compile the parsed augment connecting it into its target.
4255 *
4256 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4257 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4258 * are already implemented and compiled.
4259 *
4260 * @param[in] ctx Compile context.
4261 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004262 * @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
4263 * children in case of the augmenting uses data.
4264 * @return LY_SUCCESS on success.
4265 * @return LY_EVALID on failure.
4266 */
4267LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004268lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004269{
4270 LY_ERR ret = LY_SUCCESS;
4271 struct lysp_node *node_p, *case_node_p;
4272 struct lysc_node *target; /* target target of the augment */
4273 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004274 struct lysc_when **when, *when_shared;
4275 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004276 uint16_t flags = 0;
4277 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004278 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004279
Radek Krejci327de162019-06-14 12:52:07 +02004280 lysc_update_path(ctx, NULL, "{augment}");
4281 lysc_update_path(ctx, NULL, aug_p->nodeid);
4282
Radek Krejci7af64242019-02-18 13:07:53 +01004283 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004284 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004285 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004286 if (ret != LY_SUCCESS) {
4287 if (ret == LY_EDENIED) {
4288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4289 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4290 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4291 }
4292 return LY_EVALID;
4293 }
4294
4295 /* check for mandatory nodes
4296 * - new cases augmenting some choice can have mandatory nodes
4297 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4298 */
Radek Krejci733988a2019-02-15 15:12:44 +01004299 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004300 allow_mandatory = 1;
4301 }
4302
4303 when_shared = NULL;
4304 LY_LIST_FOR(aug_p->child, node_p) {
4305 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4306 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4307 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004308 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4309 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004310 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004311 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4312 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004313 return LY_EVALID;
4314 }
4315
4316 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004317 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004318 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004319 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004320 } else {
4321 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004322 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004323 }
4324 }
Radek Krejciec4da802019-05-02 13:02:41 +02004325 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004326
Radek Krejcife13da42019-02-15 14:51:01 +01004327 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4328 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004329 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004330 /* 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 +01004331 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 +01004332 } else if (target->nodetype == LYS_CHOICE) {
4333 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4334 node = ((struct lysc_node_choice*)target)->cases->prev;
4335 } else {
4336 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004337 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004338 }
4339
Radek Krejci733988a2019-02-15 15:12:44 +01004340 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004341 node->flags &= ~LYS_MAND_TRUE;
4342 lys_compile_mandatory_parents(target, 0);
4343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004344 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004345 return LY_EVALID;
4346 }
4347
4348 /* pass augment's when to all the children */
4349 if (aug_p->when) {
4350 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4351 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004352 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004353 LY_CHECK_GOTO(ret, error);
4354 (*when)->context = lysc_xpath_context(target);
4355 when_shared = *when;
4356 } else {
4357 ++when_shared->refcount;
4358 (*when) = when_shared;
4359 }
4360 }
4361 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004362
Radek Krejciec4da802019-05-02 13:02:41 +02004363 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004364 switch (target->nodetype) {
4365 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004366 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004367 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004368 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004369 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004370 break;
4371 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004372 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004373 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004374 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004375 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004376 break;
4377 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004378 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004379 if (aug_p->actions) {
4380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004381 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4382 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004383 return LY_EVALID;
4384 }
4385 if (aug_p->notifs) {
4386 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004387 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4388 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004389 return LY_EVALID;
4390 }
4391 }
Radek Krejci3641f562019-02-13 15:38:40 +01004392
Radek Krejci327de162019-06-14 12:52:07 +02004393 lysc_update_path(ctx, NULL, NULL);
4394 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004395error:
Radek Krejciec4da802019-05-02 13:02:41 +02004396 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004397 return ret;
4398}
4399
4400/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004401 * @brief Apply refined or deviated mandatory flag to the target node.
4402 *
4403 * @param[in] ctx Compile context.
4404 * @param[in] node Target node where the mandatory property is supposed to be changed.
4405 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004406 * @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 +01004407 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4408 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4409 * 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 +01004410 * @return LY_ERR value.
4411 */
4412static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004413lys_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 +01004414{
4415 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004417 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4418 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004419 return LY_EVALID;
4420 }
4421
4422 if (mandatory_flag & LYS_MAND_TRUE) {
4423 /* check if node has default value */
4424 if (node->nodetype & LYS_LEAF) {
4425 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004426 if (refine_flag) {
4427 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004428 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004429 return LY_EVALID;
4430 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004431 } else {
4432 /* remove the default value taken from the leaf's type */
4433 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4434 ((struct lysc_node_leaf*)node)->dflt = NULL;
4435 }
4436 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004437 if (refine_flag) {
4438 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004439 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004440 return LY_EVALID;
4441 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004442 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004443 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004444 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 +01004445 return LY_EVALID;
4446 }
4447
4448 node->flags &= ~LYS_MAND_FALSE;
4449 node->flags |= LYS_MAND_TRUE;
4450 lys_compile_mandatory_parents(node->parent, 1);
4451 } else {
4452 /* make mandatory false */
4453 node->flags &= ~LYS_MAND_TRUE;
4454 node->flags |= LYS_MAND_FALSE;
4455 lys_compile_mandatory_parents(node->parent, 0);
4456 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4457 /* get the type's default value if any */
4458 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4459 }
4460 }
4461 return LY_SUCCESS;
4462}
4463
4464/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004465 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4466 * If present, also apply uses's modificators.
4467 *
4468 * @param[in] ctx Compile context
4469 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004470 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4471 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4472 * the compile context.
4473 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4474 */
4475static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004476lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004477{
4478 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004479 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004480 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004481 struct lysc_node_container context_node_fake =
4482 {.nodetype = LYS_CONTAINER,
4483 .module = ctx->mod,
4484 .flags = parent ? parent->flags : 0,
4485 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004486 .prev = (struct lysc_node*)&context_node_fake,
4487 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004488 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004489 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004490 int found;
4491 const char *id, *name, *prefix;
4492 size_t prefix_len, name_len;
4493 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004494 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004495 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004496 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004497 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004498 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004499 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004500 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004501 unsigned int actions_index, notifs_index;
4502 struct lysc_notif **notifs = NULL;
4503 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004504
4505 /* search for the grouping definition */
4506 found = 0;
4507 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004508 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004509 if (prefix) {
4510 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4511 if (!mod) {
4512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004513 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004514 return LY_EVALID;
4515 }
4516 } else {
4517 mod = ctx->mod_def;
4518 }
4519 if (mod == ctx->mod_def) {
4520 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004521 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004522 LY_ARRAY_FOR(grp, u) {
4523 if (!strcmp(grp[u].name, name)) {
4524 grp = &grp[u];
4525 found = 1;
4526 break;
4527 }
4528 }
4529 }
4530 }
4531 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004532 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004533 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004534 if (grp) {
4535 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4536 if (!strcmp(grp[u].name, name)) {
4537 grp = &grp[u];
4538 found = 1;
4539 }
4540 }
4541 }
4542 if (!found && mod->parsed->includes) {
4543 /* ... and all the submodules */
4544 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4545 grp = mod->parsed->includes[u].submodule->groupings;
4546 if (grp) {
4547 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4548 if (!strcmp(grp[v].name, name)) {
4549 grp = &grp[v];
4550 found = 1;
4551 }
4552 }
4553 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004554 }
4555 }
4556 }
4557 if (!found) {
4558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4559 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4560 return LY_EVALID;
4561 }
4562
4563 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4564 grp_stack_count = ctx->groupings.count;
4565 ly_set_add(&ctx->groupings, (void*)grp, 0);
4566 if (grp_stack_count == ctx->groupings.count) {
4567 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4569 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4570 return LY_EVALID;
4571 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004572 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4573 /* remember that the grouping is instantiated to avoid its standalone validation */
4574 grp->flags |= LYS_USED_GRP;
4575 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004576
4577 /* switch context's mod_def */
4578 mod_old = ctx->mod_def;
4579 ctx->mod_def = mod;
4580
4581 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004582 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 +01004583
Radek Krejcifc11bd72019-04-11 16:00:05 +02004584 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004585 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004586 /* 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 +02004587 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 +01004588
4589 /* some preparation for applying refines */
4590 if (grp->data == node_p) {
4591 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004592 if (parent) {
4593 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4594 } else if (ctx->mod->compiled->data) {
4595 child = ctx->mod->compiled->data;
4596 } else {
4597 child = NULL;
4598 }
4599 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004600 }
4601 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004602 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004603 LY_LIST_FOR(context_node_fake.child, child) {
4604 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004605
Radek Krejcifc11bd72019-04-11 16:00:05 +02004606 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004607 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004608 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004609 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004610 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004611 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004612 when_shared = *when;
4613 } else {
4614 ++when_shared->refcount;
4615 (*when) = when_shared;
4616 }
4617 }
Radek Krejci01342af2019-01-03 15:18:08 +01004618 }
4619 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004620 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004621 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004622 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004623 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 +01004624 }
4625
Radek Krejcifc11bd72019-04-11 16:00:05 +02004626 /* compile actions */
4627 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4628 if (actions) {
4629 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004630 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004631 if (*actions && (uses_p->augments || uses_p->refines)) {
4632 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4633 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4634 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4635 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4636 }
4637 }
4638
4639 /* compile notifications */
4640 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4641 if (notifs) {
4642 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004643 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004644 if (*notifs && (uses_p->augments || uses_p->refines)) {
4645 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4646 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4647 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4648 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4649 }
4650 }
4651
4652
Radek Krejci3641f562019-02-13 15:38:40 +01004653 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004654 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004655 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004656 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004657 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004658
Radek Krejcif0089082019-01-07 16:42:01 +01004659 /* reload previous context's mod_def */
4660 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004661 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004662
Radek Krejci76b3e962018-12-14 17:01:25 +01004663 /* apply refine */
4664 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004665 lysc_update_path(ctx, NULL, rfn->nodeid);
4666
Radek Krejci7af64242019-02-18 13:07:53 +01004667 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 +01004668 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004669 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004670 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004671
4672 /* default value */
4673 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004674 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004676 "Invalid refine of default - %s cannot hold %d default values.",
4677 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004678 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004679 }
4680 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004682 "Invalid refine of default - %s cannot hold default value(s).",
4683 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004684 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004685 }
4686 if (node->nodetype == LYS_LEAF) {
4687 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4688 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004689 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004690 /* TODO check the default value according to type */
4691 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004692 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4694 "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 +02004695 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004696 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004697 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4698 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4699 }
4700 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004701 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004702 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 +01004703 LY_ARRAY_FOR(rfn->dflts, u) {
4704 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4705 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4706 }
4707 /* TODO check the default values according to type */
4708 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004709 if (((struct lysc_node_choice*)node)->dflt) {
4710 /* unset LYS_SET_DFLT from the current default case */
4711 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4712 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004713 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 +01004714 }
4715 }
4716
Radek Krejci12fb9142019-01-08 09:45:30 +01004717 /* description */
4718 if (rfn->dsc) {
4719 FREE_STRING(ctx->ctx, node->dsc);
4720 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4721 }
4722
4723 /* reference */
4724 if (rfn->ref) {
4725 FREE_STRING(ctx->ctx, node->ref);
4726 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4727 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004728
4729 /* config */
4730 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004731 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004732 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004733 } else {
4734 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4735 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4736 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004737 }
4738
4739 /* mandatory */
4740 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004741 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004742 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004743
4744 /* presence */
4745 if (rfn->presence) {
4746 if (node->nodetype != LYS_CONTAINER) {
4747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004748 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4749 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004750 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004751 }
4752 node->flags |= LYS_PRESENCE;
4753 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004754
4755 /* must */
4756 if (rfn->musts) {
4757 switch (node->nodetype) {
4758 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004759 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 +01004760 break;
4761 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004762 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 +01004763 break;
4764 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004765 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 +01004766 break;
4767 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004768 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 +01004769 break;
4770 case LYS_ANYXML:
4771 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004772 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 +01004773 break;
4774 default:
4775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004776 "Invalid refine of must statement - %s cannot hold any must statement.",
4777 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004778 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004779 }
4780 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004781
4782 /* min/max-elements */
4783 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4784 switch (node->nodetype) {
4785 case LYS_LEAFLIST:
4786 if (rfn->flags & LYS_SET_MAX) {
4787 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4788 }
4789 if (rfn->flags & LYS_SET_MIN) {
4790 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004791 if (rfn->min) {
4792 node->flags |= LYS_MAND_TRUE;
4793 lys_compile_mandatory_parents(node->parent, 1);
4794 } else {
4795 node->flags &= ~LYS_MAND_TRUE;
4796 lys_compile_mandatory_parents(node->parent, 0);
4797 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004798 }
4799 break;
4800 case LYS_LIST:
4801 if (rfn->flags & LYS_SET_MAX) {
4802 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4803 }
4804 if (rfn->flags & LYS_SET_MIN) {
4805 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004806 if (rfn->min) {
4807 node->flags |= LYS_MAND_TRUE;
4808 lys_compile_mandatory_parents(node->parent, 1);
4809 } else {
4810 node->flags &= ~LYS_MAND_TRUE;
4811 lys_compile_mandatory_parents(node->parent, 0);
4812 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004813 }
4814 break;
4815 default:
4816 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004817 "Invalid refine of %s statement - %s cannot hold this statement.",
4818 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004819 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004820 }
4821 }
Radek Krejcif0089082019-01-07 16:42:01 +01004822
4823 /* if-feature */
4824 if (rfn->iffeatures) {
4825 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004826 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004827 }
Radek Krejci327de162019-06-14 12:52:07 +02004828
4829 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01004830 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004831
Radek Krejcif2271f12019-01-07 16:42:23 +01004832 /* do some additional checks of the changed nodes when all the refines are applied */
4833 for (u = 0; u < refined.count; ++u) {
4834 node = (struct lysc_node*)refined.objs[u];
4835 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02004836 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01004837
4838 /* check possible conflict with default value (default added, mandatory left true) */
4839 if ((node->flags & LYS_MAND_TRUE) &&
4840 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4841 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4842 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004843 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004844 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004845 }
4846
4847 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4848 if (node->nodetype == LYS_LIST) {
4849 min = ((struct lysc_node_list*)node)->min;
4850 max = ((struct lysc_node_list*)node)->max;
4851 } else {
4852 min = ((struct lysc_node_leaflist*)node)->min;
4853 max = ((struct lysc_node_leaflist*)node)->max;
4854 }
4855 if (min > max) {
4856 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004857 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
4858 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004859 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004860 }
4861 }
4862 }
4863
Radek Krejci327de162019-06-14 12:52:07 +02004864 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004865 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004866
4867cleanup:
4868 /* fix connection of the children nodes from fake context node back into the parent */
4869 if (context_node_fake.child) {
4870 context_node_fake.child->prev = child;
4871 }
4872 LY_LIST_FOR(context_node_fake.child, child) {
4873 child->parent = parent;
4874 }
4875
4876 if (uses_p->augments || uses_p->refines) {
4877 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004878 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004879 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4880 LY_ARRAY_FREE(context_node_fake.actions);
4881 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004882 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004883 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4884 LY_ARRAY_FREE(context_node_fake.notifs);
4885 }
4886 }
4887
Radek Krejcie86bf772018-12-14 11:39:53 +01004888 /* reload previous context's mod_def */
4889 ctx->mod_def = mod_old;
4890 /* remove the grouping from the stack for circular groupings dependency check */
4891 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4892 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004893 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004894 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004895
4896 return ret;
4897}
4898
Radek Krejci327de162019-06-14 12:52:07 +02004899static int
4900lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
4901{
4902 struct lysp_node *iter;
4903 int len = 0;
4904
4905 *path = NULL;
4906 for (iter = node; iter && len >= 0; iter = iter->parent) {
4907 char *s = *path;
4908 char *id;
4909
4910 switch (iter->nodetype) {
4911 case LYS_USES:
4912 asprintf(&id, "{uses='%s'}", iter->name);
4913 break;
4914 case LYS_GROUPING:
4915 asprintf(&id, "{grouping='%s'}", iter->name);
4916 break;
4917 case LYS_AUGMENT:
4918 asprintf(&id, "{augment='%s'}", iter->name);
4919 break;
4920 default:
4921 id = strdup(iter->name);
4922 break;
4923 }
4924
4925 if (!iter->parent) {
4926 /* print prefix */
4927 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
4928 } else {
4929 /* prefix is the same as in parent */
4930 len = asprintf(path, "/%s%s", id, s ? s : "");
4931 }
4932 free(s);
4933 free(id);
4934 }
4935
4936 if (len < 0) {
4937 free(*path);
4938 *path = NULL;
4939 } else if (len == 0) {
4940 *path = strdup("/");
4941 len = 1;
4942 }
4943 return len;
4944}
4945
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004946/**
4947 * @brief Validate groupings that were defined but not directly used in the schema itself.
4948 *
4949 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
4950 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
4951 */
4952static LY_ERR
4953lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
4954{
4955 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02004956 char *path;
4957 int len;
4958
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004959 struct lysp_node_uses fake_uses = {
4960 .parent = node_p,
4961 .nodetype = LYS_USES,
4962 .flags = 0, .next = NULL,
4963 .name = grp->name,
4964 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4965 .refines = NULL, .augments = NULL
4966 };
4967 struct lysc_node_container fake_container = {
4968 .nodetype = LYS_CONTAINER,
4969 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
4970 .module = ctx->mod,
4971 .sp = NULL, .parent = NULL, .next = NULL,
4972 .prev = (struct lysc_node*)&fake_container,
4973 .name = "fake",
4974 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
4975 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4976 };
4977
4978 if (grp->parent) {
4979 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4980 }
Radek Krejci327de162019-06-14 12:52:07 +02004981
4982 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4983 if (len < 0) {
4984 LOGMEM(ctx->ctx);
4985 return LY_EMEM;
4986 }
4987 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4988 ctx->path_len = (uint16_t)len;
4989 free(path);
4990
4991 lysc_update_path(ctx, NULL, "{grouping}");
4992 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004993 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02004994 lysc_update_path(ctx, NULL, NULL);
4995 lysc_update_path(ctx, NULL, NULL);
4996
4997 ctx->path_len = 1;
4998 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004999
5000 /* cleanup */
5001 lysc_node_container_free(ctx->ctx, &fake_container);
5002
5003 return ret;
5004}
Radek Krejcife909632019-02-12 15:34:42 +01005005
Radek Krejcie86bf772018-12-14 11:39:53 +01005006/**
Radek Krejcia3045382018-11-22 14:30:31 +01005007 * @brief Compile parsed schema node information.
5008 * @param[in] ctx Compile context
5009 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005010 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5011 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5012 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005013 * @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).
5014 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005015 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5016 */
Radek Krejci19a96102018-11-15 13:38:09 +01005017static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005018lys_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 +01005019{
5020 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005021 struct lysc_node *node;
5022 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005023 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005024 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005025 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005026
Radek Krejci327de162019-06-14 12:52:07 +02005027 if (node_p->nodetype != LYS_USES) {
5028 lysc_update_path(ctx, parent, node_p->name);
5029 } else {
5030 lysc_update_path(ctx, NULL, "{uses}");
5031 lysc_update_path(ctx, NULL, node_p->name);
5032 }
5033
Radek Krejci19a96102018-11-15 13:38:09 +01005034 switch (node_p->nodetype) {
5035 case LYS_CONTAINER:
5036 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5037 node_compile_spec = lys_compile_node_container;
5038 break;
5039 case LYS_LEAF:
5040 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5041 node_compile_spec = lys_compile_node_leaf;
5042 break;
5043 case LYS_LIST:
5044 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005045 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005046 break;
5047 case LYS_LEAFLIST:
5048 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005049 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005050 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005051 case LYS_CHOICE:
5052 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005053 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005054 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005055 case LYS_ANYXML:
5056 case LYS_ANYDATA:
5057 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005058 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005059 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005060 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005061 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5062 lysc_update_path(ctx, NULL, NULL);
5063 lysc_update_path(ctx, NULL, NULL);
5064 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005065 default:
5066 LOGINT(ctx->ctx);
5067 return LY_EINT;
5068 }
5069 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5070 node->nodetype = node_p->nodetype;
5071 node->module = ctx->mod;
5072 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005073 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005074
5075 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005076 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005077 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005078 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005079 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5080 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005081 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005082 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005083 node->flags |= LYS_CONFIG_R;
5084 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005085 /* config not explicitely set, inherit it from parent */
5086 if (parent) {
5087 node->flags |= parent->flags & LYS_CONFIG_MASK;
5088 } else {
5089 /* default is config true */
5090 node->flags |= LYS_CONFIG_W;
5091 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005092 } else {
5093 /* config set explicitely */
5094 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005095 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005096 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5097 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5098 "Configuration node cannot be child of any state data node.");
5099 goto error;
5100 }
Radek Krejci19a96102018-11-15 13:38:09 +01005101
Radek Krejcia6d57732018-11-29 13:40:37 +01005102 /* *list ordering */
5103 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5104 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005105 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005106 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5107 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005108 node->flags &= ~LYS_ORDBY_MASK;
5109 node->flags |= LYS_ORDBY_SYSTEM;
5110 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5111 /* default ordering is system */
5112 node->flags |= LYS_ORDBY_SYSTEM;
5113 }
5114 }
5115
Radek Krejci19a96102018-11-15 13:38:09 +01005116 /* status - it is not inherited by specification, but it does not make sense to have
5117 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005118 if (!parent || parent->nodetype != LYS_CHOICE) {
5119 /* in case of choice/case's children, postpone the check to the moment we know if
5120 * 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 +01005121 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 +01005122 }
5123
Radek Krejciec4da802019-05-02 13:02:41 +02005124 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005125 node->sp = node_p;
5126 }
5127 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005128 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5129 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005130 if (node_p->when) {
5131 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005132 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005133 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005134 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005135 }
Radek Krejciec4da802019-05-02 13:02:41 +02005136 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
5137 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005138
5139 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005140 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005141
Radek Krejcife909632019-02-12 15:34:42 +01005142 /* inherit LYS_MAND_TRUE in parent containers */
5143 if (node->flags & LYS_MAND_TRUE) {
5144 lys_compile_mandatory_parents(parent, 1);
5145 }
5146
Radek Krejci327de162019-06-14 12:52:07 +02005147 lysc_update_path(ctx, NULL, NULL);
5148
Radek Krejci19a96102018-11-15 13:38:09 +01005149 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005150 if (parent) {
5151 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005152 if (node_p->parent->nodetype == LYS_CASE) {
5153 lysc_update_path(ctx, parent, node_p->parent->name);
5154 } else {
5155 lysc_update_path(ctx, parent, node->name);
5156 }
Radek Krejciec4da802019-05-02 13:02:41 +02005157 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005158 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005159 if (uses_status) {
5160
5161 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005162 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005163 * it directly gets the same status flags as the choice;
5164 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005165 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005166 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005167 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005168 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005169 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005170 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005171 }
Radek Krejciec4da802019-05-02 13:02:41 +02005172 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 +02005173
5174 if (parent->nodetype == LYS_CHOICE) {
5175 lysc_update_path(ctx, NULL, NULL);
5176 }
Radek Krejci19a96102018-11-15 13:38:09 +01005177 } else {
5178 /* top-level element */
5179 if (!ctx->mod->compiled->data) {
5180 ctx->mod->compiled->data = node;
5181 } else {
5182 /* insert at the end of the module's top-level nodes list */
5183 ctx->mod->compiled->data->prev->next = node;
5184 node->prev = ctx->mod->compiled->data->prev;
5185 ctx->mod->compiled->data->prev = node;
5186 }
Radek Krejci327de162019-06-14 12:52:07 +02005187 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005188 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5189 ctx->mod->compiled->notifs, node->name, node)) {
5190 return LY_EVALID;
5191 }
Radek Krejci19a96102018-11-15 13:38:09 +01005192 }
Radek Krejci327de162019-06-14 12:52:07 +02005193 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005194
5195 return LY_SUCCESS;
5196
5197error:
5198 lysc_node_free(ctx->ctx, node);
5199 return ret;
5200}
5201
Radek Krejciccd20f12019-02-15 14:12:27 +01005202static void
5203lysc_disconnect(struct lysc_node *node)
5204{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005205 struct lysc_node *parent, *child, *prev = NULL, *next;
5206 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005207 int remove_cs = 0;
5208
5209 parent = node->parent;
5210
5211 /* parent's first child */
5212 if (!parent) {
5213 return;
5214 }
5215 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005216 cs = (struct lysc_node_case*)node;
5217 } else if (parent->nodetype == LYS_CASE) {
5218 /* disconnecting some node in a case */
5219 cs = (struct lysc_node_case*)parent;
5220 parent = cs->parent;
5221 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5222 if (child == node) {
5223 if (cs->child == child) {
5224 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5225 /* case with a single child -> remove also the case */
5226 child->parent = NULL;
5227 remove_cs = 1;
5228 } else {
5229 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005230 }
5231 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005232 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005233 }
5234 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005235 if (!remove_cs) {
5236 cs = NULL;
5237 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005238 } else if (lysc_node_children(parent, node->flags) == node) {
5239 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005240 }
5241
5242 if (cs) {
5243 if (remove_cs) {
5244 /* cs has only one child which is being also removed */
5245 lysc_disconnect((struct lysc_node*)cs);
5246 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5247 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005248 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5249 /* default case removed */
5250 ((struct lysc_node_choice*)parent)->dflt = NULL;
5251 }
5252 if (((struct lysc_node_choice*)parent)->cases == cs) {
5253 /* first case removed */
5254 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5255 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005256 if (cs->child) {
5257 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5258 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5259 prev = cs->child->prev;
5260 } /* else all the children are under a single case */
5261 LY_LIST_FOR_SAFE(cs->child, next, child) {
5262 if (child->parent != (struct lysc_node*)cs) {
5263 break;
5264 }
5265 lysc_node_free(node->module->ctx, child);
5266 }
5267 if (prev) {
5268 if (prev->next) {
5269 prev->next = child;
5270 }
5271 if (child) {
5272 child->prev = prev;
5273 } else {
5274 /* link from the first child under the cases */
5275 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5276 }
5277 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005278 }
5279 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005280 }
5281
5282 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005283 if (node->prev->next) {
5284 node->prev->next = node->next;
5285 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005286 if (node->next) {
5287 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005288 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005289 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005290 if (child) {
5291 child->prev = node->prev;
5292 }
5293 } else if (((struct lysc_node_choice*)parent)->cases) {
5294 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005295 }
5296}
5297
5298LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005299lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005300{
5301 LY_ERR ret = LY_EVALID;
5302 struct ly_set devs_p = {0};
5303 struct ly_set targets = {0};
5304 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005305 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005306 struct lysc_action *rpcs;
5307 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005308 struct lysp_deviation *dev;
5309 struct lysp_deviate *d, **dp_new;
5310 struct lysp_deviate_add *d_add;
5311 struct lysp_deviate_del *d_del;
5312 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005313 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005314 struct lysc_deviation {
5315 const char *nodeid;
5316 struct lysc_node *target; /* target node of the deviation */
5317 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005318 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005319 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5320 } **devs = NULL;
5321 int i;
5322 size_t prefix_len, name_len;
5323 const char *prefix, *name, *nodeid;
5324 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005325 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005326 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005327
5328 /* get all deviations from the module and all its submodules ... */
5329 LY_ARRAY_FOR(mod_p->deviations, u) {
5330 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5331 }
5332 LY_ARRAY_FOR(mod_p->includes, v) {
5333 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5334 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5335 }
5336 }
5337 if (!devs_p.count) {
5338 /* nothing to do */
5339 return LY_SUCCESS;
5340 }
5341
Radek Krejci327de162019-06-14 12:52:07 +02005342 lysc_update_path(ctx, NULL, "{deviation}");
5343
Radek Krejciccd20f12019-02-15 14:12:27 +01005344 /* ... and group them by the target node */
5345 devs = calloc(devs_p.count, sizeof *devs);
5346 for (u = 0; u < devs_p.count; ++u) {
5347 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005348 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005349
5350 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005351 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5352 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005353 if (target->nodetype == LYS_ACTION) {
5354 /* move the target pointer to input/output to make them different from the action and
5355 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5356 * back to the RPC/action node due to a better compatibility and decision code in this function.
5357 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5358 if (flags & LYSC_OPT_RPC_INPUT) {
5359 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5360 flags |= LYSC_OPT_INTERNAL;
5361 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5362 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5363 flags |= LYSC_OPT_INTERNAL;
5364 }
5365 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005366 /* insert into the set of targets with duplicity detection */
5367 i = ly_set_add(&targets, target, 0);
5368 if (!devs[i]) {
5369 /* new record */
5370 devs[i] = calloc(1, sizeof **devs);
5371 devs[i]->target = target;
5372 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005373 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005374 }
5375 /* add deviates into the deviation's list of deviates */
5376 for (d = dev->deviates; d; d = d->next) {
5377 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5378 *dp_new = d;
5379 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5380 devs[i]->not_supported = 1;
5381 }
5382 }
Radek Krejci327de162019-06-14 12:52:07 +02005383
5384 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005385 }
5386
5387 /* MACROS for deviates checking */
5388#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5389 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005390 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 +01005391 goto cleanup; \
5392 }
5393
5394#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5395 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5397 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005398 goto cleanup; \
5399 }
5400
5401#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005402 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005403 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005404 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5405 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005406 goto cleanup; \
5407 }
5408
Radek Krejci551b12c2019-02-19 16:11:21 +01005409#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5410 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5411 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005412 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5413 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005414 goto cleanup; \
5415 }
5416
Radek Krejciccd20f12019-02-15 14:12:27 +01005417#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5418 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005419 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005420 goto cleanup; \
5421 }
5422
Radek Krejci551b12c2019-02-19 16:11:21 +01005423#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5424 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005426 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005427 goto cleanup; \
5428 }
5429
Radek Krejciccd20f12019-02-15 14:12:27 +01005430#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5431 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5432 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5433 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005434 "Invalid deviation deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5435 PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005436 goto cleanup; \
5437 } \
5438 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5439 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5440
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005441#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5442 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5443 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5444 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5445 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 +01005446 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005447 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005448 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005449 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5450 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005451 goto cleanup; \
5452 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005453 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5454 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5455 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5456 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5457 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005458 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005459 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5460 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5461 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005462 }
5463
5464 /* apply deviations */
5465 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejci327de162019-06-14 12:52:07 +02005466 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5467
Radek Krejcif538ce52019-03-05 10:46:14 +01005468 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5469 /* fix the target pointer in case of RPC's/action's input/output */
5470 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5471 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5472 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5473 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5474 }
5475 }
5476
Radek Krejciccd20f12019-02-15 14:12:27 +01005477 /* not-supported */
5478 if (devs[u]->not_supported) {
5479 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5480 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5481 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5482 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005483
5484#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5485 if (devs[u]->target->parent) { \
5486 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5487 } else { \
5488 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5489 } \
5490 LY_ARRAY_FOR(ARRAY, x) { \
5491 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5492 } \
5493 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5494 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5495 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5496 LY_ARRAY_DECREMENT(ARRAY); \
5497 }
5498
Radek Krejcif538ce52019-03-05 10:46:14 +01005499 if (devs[u]->target->nodetype == LYS_ACTION) {
5500 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5501 /* remove RPC's/action's input */
5502 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5503 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005504 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5505 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005506 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5507 /* remove RPC's/action's output */
5508 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5509 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005510 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5511 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005512 } else {
5513 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005514 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005515 }
5516 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005517 /* remove Notification */
5518 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005519 } else {
5520 /* remove the target node */
5521 lysc_disconnect(devs[u]->target);
5522 lysc_node_free(ctx->ctx, devs[u]->target);
5523 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005524
5525 continue;
5526 }
5527
5528 /* list of deviates (not-supported is not present in the list) */
5529 LY_ARRAY_FOR(devs[u]->deviates, v) {
5530 d = devs[u]->deviates[v];
5531
5532 switch (d->mod) {
5533 case LYS_DEV_ADD:
5534 d_add = (struct lysp_deviate_add*)d;
5535 /* [units-stmt] */
5536 if (d_add->units) {
5537 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5538 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5539
5540 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5541 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5542 }
5543
5544 /* *must-stmt */
5545 if (d_add->musts) {
5546 switch (devs[u]->target->nodetype) {
5547 case LYS_CONTAINER:
5548 case LYS_LIST:
5549 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005550 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005551 break;
5552 case LYS_LEAF:
5553 case LYS_LEAFLIST:
5554 case LYS_ANYDATA:
5555 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005556 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005557 break;
5558 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005559 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005560 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005561 break;
5562 case LYS_ACTION:
5563 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5564 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005565 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005566 break;
5567 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5568 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005569 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005570 break;
5571 }
5572 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005573 default:
5574 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005575 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005576 goto cleanup;
5577 }
5578 }
5579
5580 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005581 if (d_add->uniques) {
5582 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5583 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5584 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005585
5586 /* *default-stmt */
5587 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005588 switch (devs[u]->target->nodetype) {
5589 case LYS_LEAF:
5590 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5591 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5592 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5593 /* first, remove the default value taken from the type */
5594 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5595 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5596 }
5597 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005598 /* mark the new default values as leaf's own */
5599 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005600 break;
5601 case LYS_LEAFLIST:
5602 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5603 /* first, remove the default value taken from the type */
5604 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5605 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5606 }
5607 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5608 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5609 }
5610 /* add new default value(s) */
5611 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5612 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5613 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5614 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5615 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5616 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5617 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005618 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005619 devs[u]->target->flags |= LYS_SET_DFLT;
5620 break;
5621 case LYS_CHOICE:
5622 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5623 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5624 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5625 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005626 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 +01005627 goto cleanup;
5628 }
5629 break;
5630 default:
5631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005632 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005633 goto cleanup;
5634 }
5635 }
5636
5637 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005638 if (d_add->flags & LYS_CONFIG_MASK) {
5639 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005641 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5642 goto cleanup;
5643 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005644 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005645 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5646 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005647 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005648 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005650 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5651 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005652 goto cleanup;
5653 }
Radek Krejci327de162019-06-14 12:52:07 +02005654 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005655 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005656
5657 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005658 if (d_add->flags & LYS_MAND_MASK) {
5659 if (devs[u]->target->flags & LYS_MAND_MASK) {
5660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005661 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5662 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005663 goto cleanup;
5664 }
Radek Krejci327de162019-06-14 12:52:07 +02005665 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005666 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005667
5668 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005669 if (d_add->flags & LYS_SET_MIN) {
5670 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5671 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5672 /* change value */
5673 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5674 } else if (devs[u]->target->nodetype == LYS_LIST) {
5675 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5676 /* change value */
5677 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5678 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005680 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5681 goto cleanup;
5682 }
5683 if (d_add->min) {
5684 devs[u]->target->flags |= LYS_MAND_TRUE;
5685 }
5686 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005687
5688 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005689 if (d_add->flags & LYS_SET_MAX) {
5690 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5691 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5692 /* change value */
5693 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5694 } else if (devs[u]->target->nodetype == LYS_LIST) {
5695 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5696 /* change value */
5697 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5698 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005700 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5701 goto cleanup;
5702 }
5703 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005704
5705 break;
5706 case LYS_DEV_DELETE:
5707 d_del = (struct lysp_deviate_del*)d;
5708
5709 /* [units-stmt] */
5710 if (d_del->units) {
5711 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5712 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5713 }
5714
5715 /* *must-stmt */
5716 if (d_del->musts) {
5717 switch (devs[u]->target->nodetype) {
5718 case LYS_CONTAINER:
5719 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005720 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005721 break;
5722 case LYS_LEAF:
5723 case LYS_LEAFLIST:
5724 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005725 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005726 break;
5727 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005728 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005729 break;
5730 case LYS_ACTION:
5731 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5732 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5733 break;
5734 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5735 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5736 break;
5737 }
5738 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005739 default:
5740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005741 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005742 goto cleanup;
5743 }
5744 }
5745
5746 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005747 if (d_del->uniques) {
5748 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5749 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5750 LY_ARRAY_FOR(d_del->uniques, x) {
5751 LY_ARRAY_FOR(list->uniques, z) {
5752 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5753 nodeid = strpbrk(name, " \t\n");
5754 if (nodeid) {
5755 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5756 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5757 break;
5758 }
5759 while (isspace(*nodeid)) {
5760 ++nodeid;
5761 }
5762 } else {
5763 if (strcmp(name, list->uniques[z][y]->name)) {
5764 break;
5765 }
5766 }
5767 }
5768 if (!name) {
5769 /* complete match - remove the unique */
5770 LY_ARRAY_DECREMENT(list->uniques);
5771 LY_ARRAY_FREE(list->uniques[z]);
5772 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5773 --z;
5774 break;
5775 }
5776 }
5777 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005779 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5780 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01005781 goto cleanup;
5782 }
5783 }
5784 if (!LY_ARRAY_SIZE(list->uniques)) {
5785 LY_ARRAY_FREE(list->uniques);
5786 list->uniques = NULL;
5787 }
5788 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005789
5790 /* *default-stmt */
5791 if (d_del->dflts) {
5792 switch (devs[u]->target->nodetype) {
5793 case LYS_LEAF:
5794 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5795 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5796 dflt, "deleting", "default", d_del->dflts[0]);
5797
5798 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005799 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005800 break;
5801 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005802 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005803 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5804 devs[u]->target->flags &= ~LYS_SET_DFLT;
5805 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005806 break;
5807 case LYS_CHOICE:
5808 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5809 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5810 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02005811 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005812 if (prefix) {
5813 /* use module prefixes from the deviation module to match the module of the default case */
5814 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5815 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005816 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
5817 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005818 goto cleanup;
5819 }
5820 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5821 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005822 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
5823 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005824 goto cleanup;
5825 }
5826 }
5827 /* else {
5828 * strictly, the default prefix would point to the deviation module, but the value should actually
5829 * match the default string in the original module (usually unprefixed), so in this case we do not check
5830 * the module of the default case, just matching its name */
5831 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005833 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5834 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01005835 goto cleanup;
5836 }
5837 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5838 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5839 break;
5840 default:
5841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005842 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005843 goto cleanup;
5844 }
5845 }
5846
5847 break;
5848 case LYS_DEV_REPLACE:
5849 d_rpl = (struct lysp_deviate_rpl*)d;
5850
5851 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005852 if (d_rpl->type) {
5853 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5854 /* type is mandatory, so checking for its presence is not necessary */
5855 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejciec4da802019-05-02 13:02:41 +02005856 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 +01005857 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005858
5859 /* [units-stmt] */
5860 if (d_rpl->units) {
5861 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5862 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5863 units, "replacing", "units", d_rpl->units);
5864
5865 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5866 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5867 }
5868
5869 /* [default-stmt] */
5870 if (d_rpl->dflt) {
5871 switch (devs[u]->target->nodetype) {
5872 case LYS_LEAF:
5873 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5874 dflt, "replacing", "default", d_rpl->dflt);
5875
5876 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5877 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5878 break;
5879 case LYS_CHOICE:
5880 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02005881 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 +01005882 goto cleanup;
5883 }
5884 break;
5885 default:
5886 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005887 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005888 goto cleanup;
5889 }
5890 }
5891
5892 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005893 if (d_rpl->flags & LYS_CONFIG_MASK) {
5894 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005895 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005896 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5897 goto cleanup;
5898 }
5899 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02005900 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01005901 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5902 goto cleanup;
5903 }
Radek Krejci327de162019-06-14 12:52:07 +02005904 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005905 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005906
5907 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005908 if (d_rpl->flags & LYS_MAND_MASK) {
5909 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02005910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01005911 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5912 goto cleanup;
5913 }
Radek Krejci327de162019-06-14 12:52:07 +02005914 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005915 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005916
5917 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005918 if (d_rpl->flags & LYS_SET_MIN) {
5919 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5920 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5921 /* change value */
5922 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5923 } else if (devs[u]->target->nodetype == LYS_LIST) {
5924 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5925 /* change value */
5926 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5927 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005928 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005929 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5930 goto cleanup;
5931 }
5932 if (d_rpl->min) {
5933 devs[u]->target->flags |= LYS_MAND_TRUE;
5934 }
5935 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005936
5937 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005938 if (d_rpl->flags & LYS_SET_MAX) {
5939 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5940 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5941 /* change value */
5942 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5943 } else if (devs[u]->target->nodetype == LYS_LIST) {
5944 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5945 /* change value */
5946 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5947 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005949 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5950 goto cleanup;
5951 }
5952 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005953
5954 break;
5955 default:
5956 LOGINT(ctx->ctx);
5957 goto cleanup;
5958 }
5959 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005960
Radek Krejci33f72892019-02-21 10:36:58 +01005961 /* final check when all deviations of a single target node are applied */
5962
Radek Krejci551b12c2019-02-19 16:11:21 +01005963 /* check min-max compatibility */
5964 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5965 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5966 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5967 } else if (devs[u]->target->nodetype == LYS_LIST) {
5968 min = ((struct lysc_node_list*)devs[u]->target)->min;
5969 max = ((struct lysc_node_list*)devs[u]->target)->max;
5970 } else {
5971 min = max = 0;
5972 }
5973 if (min > max) {
5974 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 +02005975 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01005976 goto cleanup;
5977 }
5978
5979 /* check mandatory - default compatibility */
5980 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5981 && (devs[u]->target->flags & LYS_SET_DFLT)
5982 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5983 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005984 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01005985 goto cleanup;
5986 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5987 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5988 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02005989 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 +01005990 goto cleanup;
5991 }
5992 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5993 /* mandatory node under a default case */
5994 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005995 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5996 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01005997 goto cleanup;
5998 }
Radek Krejci33f72892019-02-21 10:36:58 +01005999
6000 /* TODO check default value(s) according to the type */
Radek Krejci327de162019-06-14 12:52:07 +02006001
6002 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006003 }
6004
Radek Krejci327de162019-06-14 12:52:07 +02006005 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006006 ret = LY_SUCCESS;
6007
6008cleanup:
6009 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6010 LY_ARRAY_FREE(devs[u]->deviates);
6011 free(devs[u]);
6012 }
6013 free(devs);
6014 ly_set_erase(&targets, NULL);
6015 ly_set_erase(&devs_p, NULL);
6016
6017 return ret;
6018}
6019
Radek Krejcib56c7502019-02-13 14:19:54 +01006020/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006021 * @brief Compile the given YANG submodule into the main module.
6022 * @param[in] ctx Compile context
6023 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006024 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6025 */
6026LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006027lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006028{
6029 unsigned int u;
6030 LY_ERR ret = LY_SUCCESS;
6031 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006032 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006033 struct lysc_module *mainmod = ctx->mod->compiled;
6034
Radek Krejci0af46292019-01-11 16:02:31 +01006035 if (!mainmod->mod->off_features) {
6036 /* features are compiled directly into the compiled module structure,
6037 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6038 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci327de162019-06-14 12:52:07 +02006039 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01006040 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
6041 LY_CHECK_GOTO(ret, error);
6042 }
6043
Radek Krejci327de162019-06-14 12:52:07 +02006044 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006045 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006046 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006047
Radek Krejci8cce8532019-03-05 11:27:45 +01006048 /* TODO data nodes */
6049
Radek Krejciec4da802019-05-02 13:02:41 +02006050 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6051 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006052
Radek Krejcid05cbd92018-12-05 14:26:40 +01006053error:
6054 return ret;
6055}
6056
Radek Krejci19a96102018-11-15 13:38:09 +01006057LY_ERR
6058lys_compile(struct lys_module *mod, int options)
6059{
6060 struct lysc_ctx ctx = {0};
6061 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006062 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006063 struct lysp_module *sp;
6064 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006065 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006066 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006067 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006068 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006069 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006070
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006071 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006072
6073 if (!mod->implemented) {
6074 /* just imported modules are not compiled */
6075 return LY_SUCCESS;
6076 }
6077
Radek Krejci19a96102018-11-15 13:38:09 +01006078 sp = mod->parsed;
6079
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006080 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006081 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006082 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006083 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006084 ctx.path_len = 1;
6085 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006086
6087 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006088 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6089 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006090
Radek Krejciec4da802019-05-02 13:02:41 +02006091 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006092 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006093 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006094 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6095 }
Radek Krejci0af46292019-01-11 16:02:31 +01006096 if (mod->off_features) {
6097 /* there is already precompiled array of features */
6098 mod_c->features = mod->off_features;
6099 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006100 } else {
6101 /* features are compiled directly into the compiled module structure,
6102 * 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 +02006103 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006104 LY_CHECK_GOTO(ret, error);
6105 }
6106 /* finish feature compilation, not only for the main module, but also for the submodules.
6107 * Due to possible forward references, it must be done when all the features (including submodules)
6108 * are present. */
6109 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006110 ret = lys_feature_precompile_finish(&ctx, &sp->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, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006114 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006115 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006116 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006117 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006118 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6119 }
Radek Krejci327de162019-06-14 12:52:07 +02006120 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006121 }
Radek Krejci327de162019-06-14 12:52:07 +02006122 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006123
Radek Krejci327de162019-06-14 12:52:07 +02006124 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006125 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006126 if (sp->identities) {
6127 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6128 }
Radek Krejci327de162019-06-14 12:52:07 +02006129 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006130
Radek Krejci95710c92019-02-11 15:49:55 +01006131 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006132 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006133 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006134 LY_CHECK_GOTO(ret, error);
6135 }
Radek Krejci95710c92019-02-11 15:49:55 +01006136
Radek Krejciec4da802019-05-02 13:02:41 +02006137 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6138 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006139
Radek Krejci95710c92019-02-11 15:49:55 +01006140 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006141 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006142 LY_CHECK_GOTO(ret, error);
6143 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006144 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006145 LY_CHECK_GOTO(ret, error);
6146 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006147
6148 /* deviations */
Radek Krejciec4da802019-05-02 13:02:41 +02006149 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006150 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006151
Radek Krejciec4da802019-05-02 13:02:41 +02006152 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006153
Radek Krejcia3045382018-11-22 14:30:31 +01006154 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006155 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6156 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6157 * 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 +01006158 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006159 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006160 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6161 if (type->basetype == LY_TYPE_LEAFREF) {
6162 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006163 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 +01006164 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006165 } else if (type->basetype == LY_TYPE_UNION) {
6166 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6167 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6168 /* validate the path */
6169 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6170 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6171 LY_CHECK_GOTO(ret, error);
6172 }
6173 }
Radek Krejcia3045382018-11-22 14:30:31 +01006174 }
6175 }
6176 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006177 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006178 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006179 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6180 if (type->basetype == LY_TYPE_LEAFREF) {
6181 /* store pointer to the real type */
6182 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6183 typeiter->basetype == LY_TYPE_LEAFREF;
6184 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6185 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006186 } else if (type->basetype == LY_TYPE_UNION) {
6187 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6188 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6189 /* store pointer to the real type */
6190 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6191 typeiter->basetype == LY_TYPE_LEAFREF;
6192 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6193 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6194 }
6195 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006196 }
6197 }
6198 }
Radek Krejciec4da802019-05-02 13:02:41 +02006199
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006200 /* validate non-instantiated groupings from the parsed schema,
6201 * without it we would accept even the schemas with invalid grouping specification */
6202 ctx.options |= LYSC_OPT_GROUPING;
6203 LY_ARRAY_FOR(sp->groupings, u) {
6204 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6205 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6206 }
6207 }
6208 LY_LIST_FOR(sp->data, node_p) {
6209 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6210 LY_ARRAY_FOR(grps, u) {
6211 if (!(grps[u].flags & LYS_USED_GRP)) {
6212 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6213 }
6214 }
6215 }
6216
Radek Krejcia3045382018-11-22 14:30:31 +01006217 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006218 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006219 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006220 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006221
Radek Krejciec4da802019-05-02 13:02:41 +02006222 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006223 lysp_module_free(mod->parsed);
6224 ((struct lys_module*)mod)->parsed = NULL;
6225 }
6226
Radek Krejciec4da802019-05-02 13:02:41 +02006227 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006228 /* remove flag of the modules implemented by dependency */
6229 for (u = 0; u < ctx.ctx->list.count; ++u) {
6230 m = ctx.ctx->list.objs[u];
6231 if (m->implemented == 2) {
6232 m->implemented = 1;
6233 }
6234 }
6235 }
6236
Radek Krejci19a96102018-11-15 13:38:09 +01006237 ((struct lys_module*)mod)->compiled = mod_c;
6238 return LY_SUCCESS;
6239
6240error:
Radek Krejci95710c92019-02-11 15:49:55 +01006241 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01006242 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006243 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006244 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006245 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006246 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006247 mod->compiled = NULL;
6248
6249 /* revert compilation of modules implemented by dependency */
6250 for (u = 0; u < ctx.ctx->list.count; ++u) {
6251 m = ctx.ctx->list.objs[u];
6252 if (m->implemented == 2) {
6253 /* revert features list to the precompiled state */
6254 lys_feature_precompile_revert(&ctx, m);
6255 /* mark module as imported-only / not-implemented */
6256 m->implemented = 0;
6257 /* free the compiled version of the module */
6258 lysc_module_free(m->compiled, NULL);
6259 m->compiled = NULL;
6260 }
6261 }
6262
Radek Krejci19a96102018-11-15 13:38:09 +01006263 return ret;
6264}