blob: 68c219faf686e4ad5eed0e619cccf43e47f7c2c7 [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/**
112 * @brief Duplicate the compiled pattern structure.
113 *
114 * Instead of duplicating memory, the reference counter in the @p orig is increased.
115 *
116 * @param[in] orig The pattern structure to duplicate.
117 * @return The duplicated structure to use.
118 */
Radek Krejci19a96102018-11-15 13:38:09 +0100119static struct lysc_pattern*
120lysc_pattern_dup(struct lysc_pattern *orig)
121{
122 ++orig->refcount;
123 return orig;
124}
125
Radek Krejcib56c7502019-02-13 14:19:54 +0100126/**
127 * @brief Duplicate the array of compiled patterns.
128 *
129 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
130 *
131 * @param[in] ctx Libyang context for logging.
132 * @param[in] orig The patterns sized array to duplicate.
133 * @return New sized array as a copy of @p orig.
134 * @return NULL in case of memory allocation error.
135 */
Radek Krejci19a96102018-11-15 13:38:09 +0100136static struct lysc_pattern**
137lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
138{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100139 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100140 unsigned int u;
141
Radek Krejcib56c7502019-02-13 14:19:54 +0100142 assert(orig);
143
Radek Krejci19a96102018-11-15 13:38:09 +0100144 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
145 LY_ARRAY_FOR(orig, u) {
146 dup[u] = lysc_pattern_dup(orig[u]);
147 LY_ARRAY_INCREMENT(dup);
148 }
149 return dup;
150}
151
Radek Krejcib56c7502019-02-13 14:19:54 +0100152/**
153 * @brief Duplicate compiled range structure.
154 *
155 * @param[in] ctx Libyang context for logging.
156 * @param[in] orig The range structure to be duplicated.
157 * @return New compiled range structure as a copy of @p orig.
158 * @return NULL in case of memory allocation error.
159 */
Radek Krejci19a96102018-11-15 13:38:09 +0100160struct lysc_range*
161lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
162{
163 struct lysc_range *dup;
164 LY_ERR ret;
165
Radek Krejcib56c7502019-02-13 14:19:54 +0100166 assert(orig);
167
Radek Krejci19a96102018-11-15 13:38:09 +0100168 dup = calloc(1, sizeof *dup);
169 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
170 if (orig->parts) {
171 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
172 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
173 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
174 }
175 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
176 DUP_STRING(ctx, orig->emsg, dup->emsg);
177 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
178
179 return dup;
180cleanup:
181 free(dup);
182 (void) ret; /* set but not used due to the return type */
183 return NULL;
184}
185
Radek Krejcib56c7502019-02-13 14:19:54 +0100186/**
187 * @brief Stack for processing if-feature expressions.
188 */
Radek Krejci19a96102018-11-15 13:38:09 +0100189struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100190 int size; /**< number of items in the stack */
191 int index; /**< first empty item */
192 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100193};
194
Radek Krejcib56c7502019-02-13 14:19:54 +0100195/**
196 * @brief Add @ref ifftokens into the stack.
197 * @param[in] stack The if-feature stack to use.
198 * @param[in] value One of the @ref ifftokens to store in the stack.
199 * @return LY_EMEM in case of memory allocation error
200 * @return LY_ESUCCESS if the value successfully stored.
201 */
Radek Krejci19a96102018-11-15 13:38:09 +0100202static LY_ERR
203iff_stack_push(struct iff_stack *stack, uint8_t value)
204{
205 if (stack->index == stack->size) {
206 stack->size += 4;
207 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
208 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
209 }
210 stack->stack[stack->index++] = value;
211 return LY_SUCCESS;
212}
213
Radek Krejcib56c7502019-02-13 14:19:54 +0100214/**
215 * @brief Get (and remove) the last item form the stack.
216 * @param[in] stack The if-feature stack to use.
217 * @return The value from the top of the stack.
218 */
Radek Krejci19a96102018-11-15 13:38:09 +0100219static uint8_t
220iff_stack_pop(struct iff_stack *stack)
221{
Radek Krejcib56c7502019-02-13 14:19:54 +0100222 assert(stack && stack->index);
223
Radek Krejci19a96102018-11-15 13:38:09 +0100224 stack->index--;
225 return stack->stack[stack->index];
226}
227
Radek Krejcib56c7502019-02-13 14:19:54 +0100228/**
229 * @brief Clean up the stack.
230 * @param[in] stack The if-feature stack to use.
231 */
Radek Krejci19a96102018-11-15 13:38:09 +0100232static void
233iff_stack_clean(struct iff_stack *stack)
234{
235 stack->size = 0;
236 free(stack->stack);
237}
238
Radek Krejcib56c7502019-02-13 14:19:54 +0100239/**
240 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
241 * (libyang format of the if-feature expression).
242 * @param[in,out] list The 2bits array to modify.
243 * @param[in] op The operand (@ref ifftokens) to store.
244 * @param[in] pos Position (0-based) where to store the given @p op.
245 */
Radek Krejci19a96102018-11-15 13:38:09 +0100246static void
247iff_setop(uint8_t *list, uint8_t op, int pos)
248{
249 uint8_t *item;
250 uint8_t mask = 3;
251
252 assert(pos >= 0);
253 assert(op <= 3); /* max 2 bits */
254
255 item = &list[pos / 4];
256 mask = mask << 2 * (pos % 4);
257 *item = (*item) & ~mask;
258 *item = (*item) | (op << 2 * (pos % 4));
259}
260
Radek Krejcib56c7502019-02-13 14:19:54 +0100261#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
262#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100263
Radek Krejci0af46292019-01-11 16:02:31 +0100264/**
265 * @brief Find a feature of the given name and referenced in the given module.
266 *
267 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
268 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
269 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
270 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
271 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
272 * assigned till that time will be still valid.
273 *
274 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
275 * @param[in] name Name of the feature including possible prefix.
276 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
277 * @return Pointer to the feature structure if found, NULL otherwise.
278 */
Radek Krejci19a96102018-11-15 13:38:09 +0100279static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100280lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100281{
282 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100283 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100284
285 for (i = 0; i < len; ++i) {
286 if (name[i] == ':') {
287 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100288 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100289 LY_CHECK_RET(!mod, NULL);
290
291 name = &name[i + 1];
292 len = len - i - 1;
293 }
294 }
295
296 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100297 if (mod->implemented) {
298 /* module is implemented so there is already the compiled schema */
299 flist = mod->compiled->features;
300 } else {
301 flist = mod->off_features;
302 }
303 LY_ARRAY_FOR(flist, i) {
304 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100305 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
306 return f;
307 }
308 }
309
310 return NULL;
311}
312
313static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200314lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext)
Radek Krejci19a96102018-11-15 13:38:09 +0100315{
316 const char *name;
317 unsigned int u;
318 const struct lys_module *mod;
319 struct lysp_ext *edef = NULL;
320
321 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
322 ext->insubstmt = ext_p->insubstmt;
323 ext->insubstmt_index = ext_p->insubstmt_index;
324
325 /* get module where the extension definition should be placed */
326 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100327 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100328 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
329 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
330 LY_EVALID);
331 LY_CHECK_ERR_RET(!mod->parsed->extensions,
332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
333 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100334 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100335 LY_EVALID);
336 name = &ext_p->name[u + 1];
337 /* find the extension definition there */
338 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
339 if (!strcmp(name, mod->parsed->extensions[u].name)) {
340 edef = &mod->parsed->extensions[u];
341 break;
342 }
343 }
344 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
345 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
346 LY_EVALID);
347 /* TODO plugins */
348
349 return LY_SUCCESS;
350}
351
Radek Krejcib56c7502019-02-13 14:19:54 +0100352/**
353 * @brief Compile information from the if-feature statement
354 * @param[in] ctx Compile context.
355 * @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 +0100356 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
357 * @return LY_ERR value.
358 */
Radek Krejci19a96102018-11-15 13:38:09 +0100359static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200360lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100361{
362 const char *c = *value;
363 int r, rc = EXIT_FAILURE;
364 int i, j, last_not, checkversion = 0;
365 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
366 uint8_t op;
367 struct iff_stack stack = {0, 0, NULL};
368 struct lysc_feature *f;
369
370 assert(c);
371
372 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
373 for (i = j = last_not = 0; c[i]; i++) {
374 if (c[i] == '(') {
375 j++;
376 checkversion = 1;
377 continue;
378 } else if (c[i] == ')') {
379 j--;
380 continue;
381 } else if (isspace(c[i])) {
382 checkversion = 1;
383 continue;
384 }
385
386 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
387 if (c[i + r] == '\0') {
388 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
389 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
390 return LY_EVALID;
391 } else if (!isspace(c[i + r])) {
392 /* feature name starting with the not/and/or */
393 last_not = 0;
394 f_size++;
395 } else if (c[i] == 'n') { /* not operation */
396 if (last_not) {
397 /* double not */
398 expr_size = expr_size - 2;
399 last_not = 0;
400 } else {
401 last_not = 1;
402 }
403 } else { /* and, or */
404 f_exp++;
405 /* not a not operation */
406 last_not = 0;
407 }
408 i += r;
409 } else {
410 f_size++;
411 last_not = 0;
412 }
413 expr_size++;
414
415 while (!isspace(c[i])) {
416 if (!c[i] || c[i] == ')') {
417 i--;
418 break;
419 }
420 i++;
421 }
422 }
423 if (j || f_exp != f_size) {
424 /* not matching count of ( and ) */
425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
426 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
427 return LY_EVALID;
428 }
429
430 if (checkversion || expr_size > 1) {
431 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100432 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100433 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
434 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
435 return LY_EVALID;
436 }
437 }
438
439 /* allocate the memory */
440 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
441 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
442 stack.stack = malloc(expr_size * sizeof *stack.stack);
443 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
444
445 stack.size = expr_size;
446 f_size--; expr_size--; /* used as indexes from now */
447
448 for (i--; i >= 0; i--) {
449 if (c[i] == ')') {
450 /* push it on stack */
451 iff_stack_push(&stack, LYS_IFF_RP);
452 continue;
453 } else if (c[i] == '(') {
454 /* pop from the stack into result all operators until ) */
455 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
456 iff_setop(iff->expr, op, expr_size--);
457 }
458 continue;
459 } else if (isspace(c[i])) {
460 continue;
461 }
462
463 /* end of operator or operand -> find beginning and get what is it */
464 j = i + 1;
465 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
466 i--;
467 }
468 i++; /* go back by one step */
469
470 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
471 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
472 /* double not */
473 iff_stack_pop(&stack);
474 } else {
475 /* not has the highest priority, so do not pop from the stack
476 * as in case of AND and OR */
477 iff_stack_push(&stack, LYS_IFF_NOT);
478 }
479 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
480 /* as for OR - pop from the stack all operators with the same or higher
481 * priority and store them to the result, then push the AND to the stack */
482 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
483 op = iff_stack_pop(&stack);
484 iff_setop(iff->expr, op, expr_size--);
485 }
486 iff_stack_push(&stack, LYS_IFF_AND);
487 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
488 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
489 op = iff_stack_pop(&stack);
490 iff_setop(iff->expr, op, expr_size--);
491 }
492 iff_stack_push(&stack, LYS_IFF_OR);
493 } else {
494 /* feature name, length is j - i */
495
496 /* add it to the expression */
497 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
498
499 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100500 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
501 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
502 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
503 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100504 iff->features[f_size] = f;
505 LY_ARRAY_INCREMENT(iff->features);
506 f_size--;
507 }
508 }
509 while (stack.index) {
510 op = iff_stack_pop(&stack);
511 iff_setop(iff->expr, op, expr_size--);
512 }
513
514 if (++expr_size || ++f_size) {
515 /* not all expected operators and operands found */
516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
517 "Invalid value \"%s\" of if-feature - processing error.", *value);
518 rc = LY_EINT;
519 } else {
520 rc = LY_SUCCESS;
521 }
522
523error:
524 /* cleanup */
525 iff_stack_clean(&stack);
526
527 return rc;
528}
529
Radek Krejcib56c7502019-02-13 14:19:54 +0100530/**
531 * @brief Compile information from the when statement
532 * @param[in] ctx Compile context.
533 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100534 * @param[out] when Pointer where to store pointer to the created compiled when structure.
535 * @return LY_ERR value.
536 */
Radek Krejci19a96102018-11-15 13:38:09 +0100537static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200538lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100539{
540 unsigned int u;
541 LY_ERR ret = LY_SUCCESS;
542
Radek Krejci00b874b2019-02-12 10:54:50 +0100543 *when = calloc(1, sizeof **when);
544 (*when)->refcount = 1;
545 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
546 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
547 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
548 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejciec4da802019-05-02 13:02:41 +0200549 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100550
551done:
552 return ret;
553}
554
Radek Krejcib56c7502019-02-13 14:19:54 +0100555/**
556 * @brief Compile information from the must statement
557 * @param[in] ctx Compile context.
558 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100559 * @param[in,out] must Prepared (empty) compiled must structure to fill.
560 * @return LY_ERR value.
561 */
Radek Krejci19a96102018-11-15 13:38:09 +0100562static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200563lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100564{
565 unsigned int u;
566 LY_ERR ret = LY_SUCCESS;
567
568 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
569 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
570
571 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
572 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100573 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
574 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejciec4da802019-05-02 13:02:41 +0200575 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100576
577done:
578 return ret;
579}
580
Radek Krejcib56c7502019-02-13 14:19:54 +0100581/**
582 * @brief Compile information from the import statement
583 * @param[in] ctx Compile context.
584 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100585 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
586 * @return LY_ERR value.
587 */
Radek Krejci19a96102018-11-15 13:38:09 +0100588static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200589lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100590{
591 unsigned int u;
592 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100593 LY_ERR ret = LY_SUCCESS;
594
595 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejciec4da802019-05-02 13:02:41 +0200596 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100597 imp->module = imp_p->module;
598
Radek Krejci7f2a5362018-11-28 13:05:37 +0100599 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
600 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100601 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100602 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100603 /* try to use filepath if present */
604 if (imp->module->filepath) {
605 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
606 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100607 if (mod != imp->module) {
608 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100609 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100610 mod = NULL;
611 }
612 }
613 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100614 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100615 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 +0100616 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100617 return LY_ENOTFOUND;
618 }
619 }
Radek Krejci19a96102018-11-15 13:38:09 +0100620 }
621
622done:
623 return ret;
624}
625
Radek Krejcib56c7502019-02-13 14:19:54 +0100626/**
627 * @brief Compile information from the identity statement
628 *
629 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
630 *
631 * @param[in] ctx Compile context.
632 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100633 * @param[in] idents List of so far compiled identities to check the name uniqueness.
634 * @param[in,out] ident Prepared (empty) compiled identity 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_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 +0100639{
640 unsigned int u;
641 LY_ERR ret = LY_SUCCESS;
642
Radek Krejcid05cbd92018-12-05 14:26:40 +0100643 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100644 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200645 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
646 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200647 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200648 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100649 /* 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 +0200650 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100651 ident->flags = ident_p->flags;
652
653done:
654 return ret;
655}
656
Radek Krejcib56c7502019-02-13 14:19:54 +0100657/**
658 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
659 *
660 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
661 *
662 * @param[in] ctx Compile context for logging.
663 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
664 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
665 * @return LY_SUCCESS if everything is ok.
666 * @return LY_EVALID if the identity is derived from itself.
667 */
Radek Krejci38222632019-02-12 16:55:05 +0100668static LY_ERR
669lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
670{
671 LY_ERR ret = LY_EVALID;
672 unsigned int u, v;
673 struct ly_set recursion = {0};
674 struct lysc_ident *drv;
675
676 if (!derived) {
677 return LY_SUCCESS;
678 }
679
680 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
681 if (ident == derived[u]) {
682 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
683 "Identity \"%s\" is indirectly derived from itself.", ident->name);
684 goto cleanup;
685 }
686 ly_set_add(&recursion, derived[u], 0);
687 }
688
689 for (v = 0; v < recursion.count; ++v) {
690 drv = recursion.objs[v];
691 if (!drv->derived) {
692 continue;
693 }
694 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
695 if (ident == drv->derived[u]) {
696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
697 "Identity \"%s\" is indirectly derived from itself.", ident->name);
698 goto cleanup;
699 }
700 ly_set_add(&recursion, drv->derived[u], 0);
701 }
702 }
703 ret = LY_SUCCESS;
704
705cleanup:
706 ly_set_erase(&recursion, NULL);
707 return ret;
708}
709
Radek Krejcia3045382018-11-22 14:30:31 +0100710/**
711 * @brief Find and process the referenced base identities from another identity or identityref
712 *
Radek Krejciaca74032019-06-04 08:53:06 +0200713 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100714 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
715 * to distinguish these two use cases.
716 *
717 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
718 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
719 * @param[in] ident Referencing identity to work with.
720 * @param[in] bases Array of bases of identityref to fill in.
721 * @return LY_ERR value.
722 */
Radek Krejci19a96102018-11-15 13:38:09 +0100723static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100724lys_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 +0100725{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100726 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100727 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100728 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100729 struct lysc_ident **idref;
730
731 assert(ident || bases);
732
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100733 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
735 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
736 return LY_EVALID;
737 }
738
739 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
740 s = strchr(bases_p[u], ':');
741 if (s) {
742 /* prefixed identity */
743 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100744 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100745 } else {
746 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100747 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100748 }
749 if (!mod) {
750 if (ident) {
751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
752 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
753 } else {
754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
755 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
756 }
757 return LY_EVALID;
758 }
759 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100760 if (mod->compiled && mod->compiled->identities) {
761 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
762 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100763 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100764 if (ident == &mod->compiled->identities[v]) {
765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
766 "Identity \"%s\" is derived from itself.", ident->name);
767 return LY_EVALID;
768 }
769 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100770 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100771 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100772 *idref = ident;
773 } else {
774 /* we have match! store the found identity */
775 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100776 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100777 }
778 break;
779 }
780 }
781 }
782 if (!idref || !(*idref)) {
783 if (ident) {
784 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
785 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
786 } else {
787 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
788 "Unable to find base (%s) of identityref.", bases_p[u]);
789 }
790 return LY_EVALID;
791 }
792 }
793 return LY_SUCCESS;
794}
795
Radek Krejcia3045382018-11-22 14:30:31 +0100796/**
797 * @brief For the given array of identities, set the backlinks from all their base identities.
798 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
799 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
800 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
801 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
802 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100803static LY_ERR
804lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
805{
806 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100807
808 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
809 if (!idents_p[i].bases) {
810 continue;
811 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100812 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100813 }
814 return LY_SUCCESS;
815}
816
Radek Krejci0af46292019-01-11 16:02:31 +0100817LY_ERR
Radek Krejci693262f2019-04-29 15:23:20 +0200818lys_feature_precompile(struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +0100819{
820 unsigned int offset = 0, u;
821 struct lysc_ctx context = {0};
822
823 assert(ctx);
824 context.ctx = ctx;
825
826 if (!features_p) {
827 return LY_SUCCESS;
828 }
829 if (*features) {
830 offset = LY_ARRAY_SIZE(*features);
831 }
832
833 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
834 LY_ARRAY_FOR(features_p, u) {
835 LY_ARRAY_INCREMENT(*features);
836 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
837 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
838 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
839 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
840 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci693262f2019-04-29 15:23:20 +0200841 (*features)[offset + u].module = module;
Radek Krejci0af46292019-01-11 16:02:31 +0100842 }
843
844 return LY_SUCCESS;
845}
846
Radek Krejcia3045382018-11-22 14:30:31 +0100847/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100848 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100849 *
850 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
851 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100852 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100853 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
854 * being currently processed).
855 * @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 +0100856 * @return LY_SUCCESS if everything is ok.
857 * @return LY_EVALID if the feature references indirectly itself.
858 */
859static LY_ERR
860lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
861{
862 LY_ERR ret = LY_EVALID;
863 unsigned int u, v;
864 struct ly_set recursion = {0};
865 struct lysc_feature *drv;
866
867 if (!depfeatures) {
868 return LY_SUCCESS;
869 }
870
871 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
872 if (feature == depfeatures[u]) {
873 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
874 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
875 goto cleanup;
876 }
877 ly_set_add(&recursion, depfeatures[u], 0);
878 }
879
880 for (v = 0; v < recursion.count; ++v) {
881 drv = recursion.objs[v];
882 if (!drv->depfeatures) {
883 continue;
884 }
885 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
886 if (feature == drv->depfeatures[u]) {
887 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
888 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
889 goto cleanup;
890 }
891 ly_set_add(&recursion, drv->depfeatures[u], 0);
892 }
893 }
894 ret = LY_SUCCESS;
895
896cleanup:
897 ly_set_erase(&recursion, NULL);
898 return ret;
899}
900
901/**
Radek Krejci0af46292019-01-11 16:02:31 +0100902 * @brief Create pre-compiled features array.
903 *
904 * See lys_feature_precompile() for more details.
905 *
Radek Krejcia3045382018-11-22 14:30:31 +0100906 * @param[in] ctx Compile context.
907 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +0100908 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100909 * @return LY_ERR value.
910 */
Radek Krejci19a96102018-11-15 13:38:09 +0100911static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200912lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +0100913{
Radek Krejci0af46292019-01-11 16:02:31 +0100914 unsigned int u, v, x;
915 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100916 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100917
Radek Krejci0af46292019-01-11 16:02:31 +0100918 /* find the preprecompiled feature */
919 LY_ARRAY_FOR(features, x) {
920 if (strcmp(features[x].name, feature_p->name)) {
921 continue;
922 }
923 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100924
Radek Krejci0af46292019-01-11 16:02:31 +0100925 /* finish compilation started in lys_feature_precompile() */
Radek Krejciec4da802019-05-02 13:02:41 +0200926 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done);
927 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +0100928 if (feature->iffeatures) {
929 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
930 if (feature->iffeatures[u].features) {
931 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100932 /* check for circular dependency - direct reference first,... */
933 if (feature == feature->iffeatures[u].features[v]) {
934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
935 "Feature \"%s\" is referenced from itself.", feature->name);
936 return LY_EVALID;
937 }
938 /* ... and indirect circular reference */
939 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
940
Radek Krejci0af46292019-01-11 16:02:31 +0100941 /* add itself into the dependants list */
942 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
943 *df = feature;
944 }
Radek Krejci19a96102018-11-15 13:38:09 +0100945 }
Radek Krejci19a96102018-11-15 13:38:09 +0100946 }
947 }
Radek Krejci0af46292019-01-11 16:02:31 +0100948 done:
949 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100950 }
Radek Krejci0af46292019-01-11 16:02:31 +0100951
952 LOGINT(ctx->ctx);
953 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100954}
955
Radek Krejcib56c7502019-02-13 14:19:54 +0100956/**
957 * @brief Revert compiled list of features back to the precompiled state.
958 *
959 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
960 * The features are supposed to be stored again as off_features in ::lys_module structure.
961 *
962 * @param[in] ctx Compilation context.
963 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
964 * and supposed to hold the off_features list.
965 */
Radek Krejci95710c92019-02-11 15:49:55 +0100966static void
967lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
968{
969 unsigned int u, v;
970
971 /* keep the off_features list until the complete lys_module is freed */
972 mod->off_features = mod->compiled->features;
973 mod->compiled->features = NULL;
974
975 /* in the off_features list, remove all the parts (from finished compiling process)
976 * which may points into the data being freed here */
977 LY_ARRAY_FOR(mod->off_features, u) {
978 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
979 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
980 }
981 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
982 mod->off_features[u].iffeatures = NULL;
983
984 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
985 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
986 }
987 LY_ARRAY_FREE(mod->off_features[u].exts);
988 mod->off_features[u].exts = NULL;
989 }
990}
991
Radek Krejcia3045382018-11-22 14:30:31 +0100992/**
993 * @brief Validate and normalize numeric value from a range definition.
994 * @param[in] ctx Compile context.
995 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
996 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
997 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
998 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
999 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1000 * @param[in] value String value of the range boundary.
1001 * @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.
1002 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1003 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1004 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001005LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001006range_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 +01001007{
Radek Krejci6cba4292018-11-15 17:33:29 +01001008 size_t fraction = 0, size;
1009
Radek Krejci19a96102018-11-15 13:38:09 +01001010 *len = 0;
1011
1012 assert(value);
1013 /* parse value */
1014 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1015 return LY_EVALID;
1016 }
1017
1018 if ((value[*len] == '-') || (value[*len] == '+')) {
1019 ++(*len);
1020 }
1021
1022 while (isdigit(value[*len])) {
1023 ++(*len);
1024 }
1025
1026 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001027 if (basetype == LY_TYPE_DEC64) {
1028 goto decimal;
1029 } else {
1030 *valcopy = strndup(value, *len);
1031 return LY_SUCCESS;
1032 }
Radek Krejci19a96102018-11-15 13:38:09 +01001033 }
1034 fraction = *len;
1035
1036 ++(*len);
1037 while (isdigit(value[*len])) {
1038 ++(*len);
1039 }
1040
Radek Krejci6cba4292018-11-15 17:33:29 +01001041 if (basetype == LY_TYPE_DEC64) {
1042decimal:
1043 assert(frdigits);
1044 if (*len - 1 - fraction > frdigits) {
1045 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1046 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1047 *len, value, frdigits);
1048 return LY_EINVAL;
1049 }
1050 if (fraction) {
1051 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1052 } else {
1053 size = (*len) + frdigits + 1;
1054 }
1055 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001056 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1057
Radek Krejci6cba4292018-11-15 17:33:29 +01001058 (*valcopy)[size - 1] = '\0';
1059 if (fraction) {
1060 memcpy(&(*valcopy)[0], &value[0], fraction);
1061 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1062 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1063 } else {
1064 memcpy(&(*valcopy)[0], &value[0], *len);
1065 memset(&(*valcopy)[*len], '0', frdigits);
1066 }
Radek Krejci19a96102018-11-15 13:38:09 +01001067 }
1068 return LY_SUCCESS;
1069}
1070
Radek Krejcia3045382018-11-22 14:30:31 +01001071/**
1072 * @brief Check that values in range are in ascendant order.
1073 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001074 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1075 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001076 * @param[in] value Current value to check.
1077 * @param[in] prev_value The last seen value.
1078 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1079 */
Radek Krejci19a96102018-11-15 13:38:09 +01001080static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001081range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001082{
1083 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001084 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001085 return LY_EEXIST;
1086 }
1087 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001088 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001089 return LY_EEXIST;
1090 }
1091 }
1092 return LY_SUCCESS;
1093}
1094
Radek Krejcia3045382018-11-22 14:30:31 +01001095/**
1096 * @brief Set min/max value of the range part.
1097 * @param[in] ctx Compile context.
1098 * @param[in] part Range part structure to fill.
1099 * @param[in] max Flag to distinguish if storing min or max value.
1100 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1101 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1102 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1103 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1104 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001105 * @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 +01001106 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1107 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1108 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1109 * frdigits value), LY_EMEM.
1110 */
Radek Krejci19a96102018-11-15 13:38:09 +01001111static LY_ERR
1112range_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 +01001113 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001114{
1115 LY_ERR ret = LY_SUCCESS;
1116 char *valcopy = NULL;
1117 size_t len;
1118
1119 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001120 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001121 LY_CHECK_GOTO(ret, finalize);
1122 }
1123 if (!valcopy && base_range) {
1124 if (max) {
1125 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1126 } else {
1127 part->min_64 = base_range->parts[0].min_64;
1128 }
1129 if (!first) {
1130 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1131 }
1132 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001133 }
1134
1135 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001136 case LY_TYPE_INT8: /* range */
1137 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001138 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 +01001139 } else if (max) {
1140 part->max_64 = INT64_C(127);
1141 } else {
1142 part->min_64 = INT64_C(-128);
1143 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001144 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001145 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001146 }
1147 break;
1148 case LY_TYPE_INT16: /* range */
1149 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001150 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 +01001151 } else if (max) {
1152 part->max_64 = INT64_C(32767);
1153 } else {
1154 part->min_64 = INT64_C(-32768);
1155 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001156 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001157 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001158 }
1159 break;
1160 case LY_TYPE_INT32: /* range */
1161 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001162 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 +01001163 } else if (max) {
1164 part->max_64 = INT64_C(2147483647);
1165 } else {
1166 part->min_64 = INT64_C(-2147483648);
1167 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001168 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001169 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001170 }
1171 break;
1172 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001173 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001174 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001175 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001176 max ? &part->max_64 : &part->min_64);
1177 } else if (max) {
1178 part->max_64 = INT64_C(9223372036854775807);
1179 } else {
1180 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1181 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001182 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001183 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001184 }
1185 break;
1186 case LY_TYPE_UINT8: /* range */
1187 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001188 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 +01001189 } else if (max) {
1190 part->max_u64 = UINT64_C(255);
1191 } else {
1192 part->min_u64 = UINT64_C(0);
1193 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001194 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001195 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001196 }
1197 break;
1198 case LY_TYPE_UINT16: /* range */
1199 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001200 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 +01001201 } else if (max) {
1202 part->max_u64 = UINT64_C(65535);
1203 } else {
1204 part->min_u64 = UINT64_C(0);
1205 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001206 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001207 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001208 }
1209 break;
1210 case LY_TYPE_UINT32: /* range */
1211 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001212 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 +01001213 } else if (max) {
1214 part->max_u64 = UINT64_C(4294967295);
1215 } else {
1216 part->min_u64 = UINT64_C(0);
1217 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001218 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001219 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001220 }
1221 break;
1222 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001223 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001224 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001225 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001226 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 +01001227 } else if (max) {
1228 part->max_u64 = UINT64_C(18446744073709551615);
1229 } else {
1230 part->min_u64 = UINT64_C(0);
1231 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001232 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001233 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001234 }
1235 break;
1236 default:
1237 LOGINT(ctx->ctx);
1238 ret = LY_EINT;
1239 }
1240
Radek Krejci5969f272018-11-23 10:03:58 +01001241finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001242 if (ret == LY_EDENIED) {
1243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1244 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1245 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1246 } else if (ret == LY_EVALID) {
1247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1248 "Invalid %s restriction - invalid value \"%s\".",
1249 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1250 } else if (ret == LY_EEXIST) {
1251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1252 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001253 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001254 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001255 } else if (!ret && value) {
1256 *value = *value + len;
1257 }
1258 free(valcopy);
1259 return ret;
1260}
1261
Radek Krejcia3045382018-11-22 14:30:31 +01001262/**
1263 * @brief Compile the parsed range restriction.
1264 * @param[in] ctx Compile context.
1265 * @param[in] range_p Parsed range structure to compile.
1266 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1267 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1268 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1269 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1270 * range restriction must be more restrictive than the base_range.
1271 * @param[in,out] range Pointer to the created current range structure.
1272 * @return LY_ERR value.
1273 */
Radek Krejci19a96102018-11-15 13:38:09 +01001274static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001275lys_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 +01001276 struct lysc_range *base_range, struct lysc_range **range)
1277{
1278 LY_ERR ret = LY_EVALID;
1279 const char *expr;
1280 struct lysc_range_part *parts = NULL, *part;
1281 int range_expected = 0, uns;
1282 unsigned int parts_done = 0, u, v;
1283
1284 assert(range);
1285 assert(range_p);
1286
1287 expr = range_p->arg;
1288 while(1) {
1289 if (isspace(*expr)) {
1290 ++expr;
1291 } else if (*expr == '\0') {
1292 if (range_expected) {
1293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1294 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1295 length_restr ? "length" : "range", range_p->arg);
1296 goto cleanup;
1297 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1299 "Invalid %s restriction - unexpected end of the expression (%s).",
1300 length_restr ? "length" : "range", range_p->arg);
1301 goto cleanup;
1302 }
1303 parts_done++;
1304 break;
1305 } else if (!strncmp(expr, "min", 3)) {
1306 if (parts) {
1307 /* min cannot be used elsewhere than in the first part */
1308 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1309 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1310 expr - range_p->arg, range_p->arg);
1311 goto cleanup;
1312 }
1313 expr += 3;
1314
1315 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001316 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 +01001317 part->max_64 = part->min_64;
1318 } else if (*expr == '|') {
1319 if (!parts || range_expected) {
1320 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1321 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1322 goto cleanup;
1323 }
1324 expr++;
1325 parts_done++;
1326 /* process next part of the expression */
1327 } else if (!strncmp(expr, "..", 2)) {
1328 expr += 2;
1329 while (isspace(*expr)) {
1330 expr++;
1331 }
1332 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1333 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1334 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1335 goto cleanup;
1336 }
1337 /* continue expecting the upper boundary */
1338 range_expected = 1;
1339 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1340 /* number */
1341 if (range_expected) {
1342 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001343 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 +01001344 range_expected = 0;
1345 } else {
1346 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1347 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 +01001348 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001349 part->max_64 = part->min_64;
1350 }
1351
1352 /* continue with possible another expression part */
1353 } else if (!strncmp(expr, "max", 3)) {
1354 expr += 3;
1355 while (isspace(*expr)) {
1356 expr++;
1357 }
1358 if (*expr != '\0') {
1359 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1360 length_restr ? "length" : "range", expr);
1361 goto cleanup;
1362 }
1363 if (range_expected) {
1364 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001365 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 +01001366 range_expected = 0;
1367 } else {
1368 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1369 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 +01001370 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001371 part->min_64 = part->max_64;
1372 }
1373 } else {
1374 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1375 length_restr ? "length" : "range", expr);
1376 goto cleanup;
1377 }
1378 }
1379
1380 /* check with the previous range/length restriction */
1381 if (base_range) {
1382 switch (basetype) {
1383 case LY_TYPE_BINARY:
1384 case LY_TYPE_UINT8:
1385 case LY_TYPE_UINT16:
1386 case LY_TYPE_UINT32:
1387 case LY_TYPE_UINT64:
1388 case LY_TYPE_STRING:
1389 uns = 1;
1390 break;
1391 case LY_TYPE_DEC64:
1392 case LY_TYPE_INT8:
1393 case LY_TYPE_INT16:
1394 case LY_TYPE_INT32:
1395 case LY_TYPE_INT64:
1396 uns = 0;
1397 break;
1398 default:
1399 LOGINT(ctx->ctx);
1400 ret = LY_EINT;
1401 goto cleanup;
1402 }
1403 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1404 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1405 goto baseerror;
1406 }
1407 /* current lower bound is not lower than the base */
1408 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1409 /* base has single value */
1410 if (base_range->parts[v].min_64 == parts[u].min_64) {
1411 /* both lower bounds are the same */
1412 if (parts[u].min_64 != parts[u].max_64) {
1413 /* current continues with a range */
1414 goto baseerror;
1415 } else {
1416 /* equal single values, move both forward */
1417 ++v;
1418 continue;
1419 }
1420 } else {
1421 /* base is single value lower than current range, so the
1422 * value from base range is removed in the current,
1423 * move only base and repeat checking */
1424 ++v;
1425 --u;
1426 continue;
1427 }
1428 } else {
1429 /* base is the range */
1430 if (parts[u].min_64 == parts[u].max_64) {
1431 /* current is a single value */
1432 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1433 /* current is behind the base range, so base range is omitted,
1434 * move the base and keep the current for further check */
1435 ++v;
1436 --u;
1437 } /* else it is within the base range, so move the current, but keep the base */
1438 continue;
1439 } else {
1440 /* both are ranges - check the higher bound, the lower was already checked */
1441 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1442 /* higher bound is higher than the current higher bound */
1443 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1444 /* but the current lower bound is also higher, so the base range is omitted,
1445 * continue with the same current, but move the base */
1446 --u;
1447 ++v;
1448 continue;
1449 }
1450 /* current range starts within the base range but end behind it */
1451 goto baseerror;
1452 } else {
1453 /* current range is smaller than the base,
1454 * move current, but stay with the base */
1455 continue;
1456 }
1457 }
1458 }
1459 }
1460 if (u != parts_done) {
1461baseerror:
1462 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1463 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1464 length_restr ? "length" : "range", range_p->arg);
1465 goto cleanup;
1466 }
1467 }
1468
1469 if (!(*range)) {
1470 *range = calloc(1, sizeof **range);
1471 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1472 }
1473
Radek Krejcic8b31002019-01-08 10:24:45 +01001474 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001475 if (range_p->eapptag) {
1476 lydict_remove(ctx->ctx, (*range)->eapptag);
1477 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1478 }
1479 if (range_p->emsg) {
1480 lydict_remove(ctx->ctx, (*range)->emsg);
1481 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1482 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001483 if (range_p->dsc) {
1484 lydict_remove(ctx->ctx, (*range)->dsc);
1485 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1486 }
1487 if (range_p->ref) {
1488 lydict_remove(ctx->ctx, (*range)->ref);
1489 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1490 }
Radek Krejci19a96102018-11-15 13:38:09 +01001491 /* extensions are taken only from the last range by the caller */
1492
1493 (*range)->parts = parts;
1494 parts = NULL;
1495 ret = LY_SUCCESS;
1496cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001497 LY_ARRAY_FREE(parts);
1498
1499 return ret;
1500}
1501
1502/**
1503 * @brief Checks pattern syntax.
1504 *
Radek Krejcia3045382018-11-22 14:30:31 +01001505 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001506 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001507 * @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 +01001508 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001509 */
1510static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001511lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001512{
Radek Krejci54579462019-04-30 12:47:06 +02001513 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001514 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001515 int err_code;
1516 const char *orig_ptr;
1517 PCRE2_SIZE err_offset;
1518 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001519#define URANGE_LEN 19
1520 char *ublock2urange[][2] = {
1521 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1522 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1523 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1524 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1525 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1526 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1527 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1528 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1529 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1530 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1531 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1532 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1533 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1534 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1535 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1536 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1537 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1538 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1539 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1540 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1541 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1542 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1543 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1544 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1545 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1546 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1547 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1548 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1549 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1550 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1551 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1552 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1553 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1554 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1555 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1556 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1557 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1558 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1559 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1560 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1561 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1562 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1563 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1564 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1565 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1566 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1567 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1568 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1569 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1570 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1571 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1572 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1573 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1574 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1575 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1576 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1577 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1578 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1579 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1580 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1581 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1582 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1583 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1584 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1585 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1586 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1587 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1588 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1589 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1590 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1591 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1592 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1593 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1594 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1595 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1596 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1597 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1598 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1599 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1600 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1601 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1602 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1603 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1604 {NULL, NULL}
1605 };
1606
1607 /* adjust the expression to a Perl equivalent
1608 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1609
1610 /* we need to replace all "$" with "\$", count them now */
1611 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1612
1613 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1614 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1615 perl_regex[0] = '\0';
1616
1617 ptr = perl_regex;
1618
Radek Krejci19a96102018-11-15 13:38:09 +01001619 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1620 if (orig_ptr[0] == '$') {
1621 ptr += sprintf(ptr, "\\$");
1622 } else if (orig_ptr[0] == '^') {
1623 ptr += sprintf(ptr, "\\^");
1624 } else {
1625 ptr[0] = orig_ptr[0];
1626 ++ptr;
1627 }
1628 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001629 ptr[0] = '\0';
1630 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001631
1632 /* substitute Unicode Character Blocks with exact Character Ranges */
1633 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1634 start = ptr - perl_regex;
1635
1636 ptr = strchr(ptr, '}');
1637 if (!ptr) {
1638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1639 pattern, perl_regex + start + 2, "unterminated character property");
1640 free(perl_regex);
1641 return LY_EVALID;
1642 }
1643 end = (ptr - perl_regex) + 1;
1644
1645 /* need more space */
1646 if (end - start < URANGE_LEN) {
1647 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1648 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1649 }
1650
1651 /* find our range */
1652 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1653 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1654 break;
1655 }
1656 }
1657 if (!ublock2urange[idx][0]) {
1658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1659 pattern, perl_regex + start + 5, "unknown block name");
1660 free(perl_regex);
1661 return LY_EVALID;
1662 }
1663
1664 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1665 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1666 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1667 ++count;
1668 }
1669 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1670 --count;
1671 }
1672 }
1673 if (count) {
1674 /* skip brackets */
1675 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1676 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1677 } else {
1678 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1679 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1680 }
1681 }
1682
1683 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001684 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1685 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001686 &err_code, &err_offset, NULL);
1687 if (!code_local) {
1688 PCRE2_UCHAR err_msg[256] = {0};
1689 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1691 free(perl_regex);
1692 return LY_EVALID;
1693 }
1694 free(perl_regex);
1695
Radek Krejci54579462019-04-30 12:47:06 +02001696 if (code) {
1697 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001698 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001699 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001700 }
1701
1702 return LY_SUCCESS;
1703
1704#undef URANGE_LEN
1705}
1706
Radek Krejcia3045382018-11-22 14:30:31 +01001707/**
1708 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1709 * @param[in] ctx Compile context.
1710 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001711 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1712 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1713 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1714 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1715 */
Radek Krejci19a96102018-11-15 13:38:09 +01001716static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001717lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001718 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1719{
1720 struct lysc_pattern **pattern;
1721 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001722 LY_ERR ret = LY_SUCCESS;
1723
1724 /* first, copy the patterns from the base type */
1725 if (base_patterns) {
1726 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1727 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1728 }
1729
1730 LY_ARRAY_FOR(patterns_p, u) {
1731 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1732 *pattern = calloc(1, sizeof **pattern);
1733 ++(*pattern)->refcount;
1734
Radek Krejci54579462019-04-30 12:47:06 +02001735 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001736 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001737
1738 if (patterns_p[u].arg[0] == 0x15) {
1739 (*pattern)->inverted = 1;
1740 }
Radek Krejci54579462019-04-30 12:47:06 +02001741 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001742 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1743 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001744 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1745 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02001746 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001747 }
1748done:
1749 return ret;
1750}
1751
Radek Krejcia3045382018-11-22 14:30:31 +01001752/**
1753 * @brief map of the possible restrictions combination for the specific built-in type.
1754 */
Radek Krejci19a96102018-11-15 13:38:09 +01001755static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1756 0 /* LY_TYPE_UNKNOWN */,
1757 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001758 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1759 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1760 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1761 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1762 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001763 LYS_SET_BIT /* LY_TYPE_BITS */,
1764 0 /* LY_TYPE_BOOL */,
1765 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1766 0 /* LY_TYPE_EMPTY */,
1767 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1768 LYS_SET_BASE /* LY_TYPE_IDENT */,
1769 LYS_SET_REQINST /* LY_TYPE_INST */,
1770 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001771 LYS_SET_TYPE /* LY_TYPE_UNION */,
1772 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001773 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001774 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001775 LYS_SET_RANGE /* LY_TYPE_INT64 */
1776};
1777
1778/**
1779 * @brief stringification of the YANG built-in data types
1780 */
1781const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1782 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1783 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001784};
1785
Radek Krejcia3045382018-11-22 14:30:31 +01001786/**
1787 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1788 * @param[in] ctx Compile context.
1789 * @param[in] enums_p Array of the parsed enum structures to compile.
1790 * @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 +01001791 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1792 * @param[out] enums Newly created array of the compiled enums information for the current type.
1793 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1794 */
Radek Krejci19a96102018-11-15 13:38:09 +01001795static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001796lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02001797 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01001798{
1799 LY_ERR ret = LY_SUCCESS;
1800 unsigned int u, v, match;
1801 int32_t value = 0;
1802 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001803 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01001804
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001805 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001806 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1807 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1808 return LY_EVALID;
1809 }
1810
1811 LY_ARRAY_FOR(enums_p, u) {
1812 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1813 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001814 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1815 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02001816 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01001817 if (base_enums) {
1818 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1819 LY_ARRAY_FOR(base_enums, v) {
1820 if (!strcmp(e->name, base_enums[v].name)) {
1821 break;
1822 }
1823 }
1824 if (v == LY_ARRAY_SIZE(base_enums)) {
1825 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1826 "Invalid %s - derived type adds new item \"%s\".",
1827 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1828 return LY_EVALID;
1829 }
1830 match = v;
1831 }
1832
1833 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02001834 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01001835 if (enums_p[u].flags & LYS_SET_VALUE) {
1836 e->value = (int32_t)enums_p[u].value;
1837 if (!u || e->value >= value) {
1838 value = e->value + 1;
1839 }
1840 /* check collision with other values */
1841 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1842 if (e->value == (*enums)[v].value) {
1843 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1844 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1845 e->value, e->name, (*enums)[v].name);
1846 return LY_EVALID;
1847 }
1848 }
1849 } else if (base_enums) {
1850 /* inherit the assigned value */
1851 e->value = base_enums[match].value;
1852 if (!u || e->value >= value) {
1853 value = e->value + 1;
1854 }
1855 } else {
1856 /* assign value automatically */
1857 if (u && value == INT32_MIN) {
1858 /* counter overflow */
1859 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1860 "Invalid enumeration - it is not possible to auto-assign enum value for "
1861 "\"%s\" since the highest value is already 2147483647.", e->name);
1862 return LY_EVALID;
1863 }
1864 e->value = value++;
1865 }
1866 } else { /* LY_TYPE_BITS */
1867 if (enums_p[u].flags & LYS_SET_VALUE) {
1868 e->value = (int32_t)enums_p[u].value;
1869 if (!u || (uint32_t)e->value >= position) {
1870 position = (uint32_t)e->value + 1;
1871 }
1872 /* check collision with other values */
1873 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1874 if (e->value == (*enums)[v].value) {
1875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1876 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1877 (uint32_t)e->value, e->name, (*enums)[v].name);
1878 return LY_EVALID;
1879 }
1880 }
1881 } else if (base_enums) {
1882 /* inherit the assigned value */
1883 e->value = base_enums[match].value;
1884 if (!u || (uint32_t)e->value >= position) {
1885 position = (uint32_t)e->value + 1;
1886 }
1887 } else {
1888 /* assign value automatically */
1889 if (u && position == 0) {
1890 /* counter overflow */
1891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1892 "Invalid bits - it is not possible to auto-assign bit position for "
1893 "\"%s\" since the highest value is already 4294967295.", e->name);
1894 return LY_EVALID;
1895 }
1896 e->value = position++;
1897 }
1898 }
1899
1900 if (base_enums) {
1901 /* the assigned values must not change from the derived type */
1902 if (e->value != base_enums[match].value) {
1903 if (basetype == LY_TYPE_ENUM) {
1904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1905 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1906 e->name, base_enums[match].value, e->value);
1907 } else {
1908 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1909 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1910 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1911 }
1912 return LY_EVALID;
1913 }
1914 }
1915
Radek Krejciec4da802019-05-02 13:02:41 +02001916 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
1917 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001918
1919 if (basetype == LY_TYPE_BITS) {
1920 /* keep bits ordered by position */
1921 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1922 if (v != u) {
1923 memcpy(&storage, e, sizeof *e);
1924 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1925 memcpy(&(*enums)[v], &storage, sizeof storage);
1926 }
1927 }
1928 }
1929
1930done:
1931 return ret;
1932}
1933
Radek Krejcia3045382018-11-22 14:30:31 +01001934#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1935 for ((NODE) = (NODE)->parent; \
1936 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1937 (NODE) = (NODE)->parent); \
1938 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1940 TERM; \
1941 }
1942
1943/**
1944 * @brief Validate the predicate(s) from the leafref path.
1945 * @param[in] ctx Compile context
1946 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1947 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001948 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001949 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001950 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001951 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1952 */
1953static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001954lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001955 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001956{
1957 LY_ERR ret = LY_EVALID;
1958 const struct lys_module *mod;
1959 const struct lysc_node *src_node, *dst_node;
1960 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1961 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001962 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001963 const char *start, *end, *pke_end;
1964 struct ly_set keys = {0};
1965 int i;
1966
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001967 assert(path_context);
1968
Radek Krejcia3045382018-11-22 14:30:31 +01001969 while (**predicate == '[') {
1970 start = (*predicate)++;
1971
1972 while (isspace(**predicate)) {
1973 ++(*predicate);
1974 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02001975 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01001976 while (isspace(**predicate)) {
1977 ++(*predicate);
1978 }
1979 if (**predicate != '=') {
1980 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001981 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1982 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001983 goto cleanup;
1984 }
1985 ++(*predicate);
1986 while (isspace(**predicate)) {
1987 ++(*predicate);
1988 }
1989
1990 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1991 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1992 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1993 goto cleanup;
1994 }
1995 --pke_end;
1996 while (isspace(*pke_end)) {
1997 --pke_end;
1998 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001999 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002000 /* localize path-key-expr */
2001 pke_start = path_key_expr = *predicate;
2002 /* move after the current predicate */
2003 *predicate = end + 1;
2004
2005 /* source (must be leaf or leaf-list) */
2006 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002007 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002008 if (!mod) {
2009 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2010 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002011 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002012 goto cleanup;
2013 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002014 if (!mod->implemented) {
2015 /* make the module implemented */
2016 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2017 }
Radek Krejcia3045382018-11-22 14:30:31 +01002018 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002019 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002020 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002021 src_node = NULL;
2022 if (context_node->keys) {
2023 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2024 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2025 src_node = (const struct lysc_node*)context_node->keys[u];
2026 break;
2027 }
2028 }
2029 }
Radek Krejcia3045382018-11-22 14:30:31 +01002030 if (!src_node) {
2031 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002032 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002033 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002034 goto cleanup;
2035 }
Radek Krejcia3045382018-11-22 14:30:31 +01002036
2037 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002038 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002039 i = ly_set_add(&keys, (void*)src_node, 0);
2040 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002041 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002043 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002044 *predicate - start, start, src_node->name);
2045 goto cleanup;
2046 }
2047
2048 /* destination */
2049 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002050 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002051
2052 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2053 if (strncmp(path_key_expr, "current()", 9)) {
2054 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2055 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2056 *predicate - start, start);
2057 goto cleanup;
2058 }
2059 path_key_expr += 9;
2060 while (isspace(*path_key_expr)) {
2061 ++path_key_expr;
2062 }
2063
2064 if (*path_key_expr != '/') {
2065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2066 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2067 *predicate - start, start);
2068 goto cleanup;
2069 }
2070 ++path_key_expr;
2071 while (isspace(*path_key_expr)) {
2072 ++path_key_expr;
2073 }
2074
2075 /* rel-path-keyexpr:
2076 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2077 while (!strncmp(path_key_expr, "..", 2)) {
2078 ++dest_parent_times;
2079 path_key_expr += 2;
2080 while (isspace(*path_key_expr)) {
2081 ++path_key_expr;
2082 }
2083 if (*path_key_expr != '/') {
2084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2085 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2086 *predicate - start, start);
2087 goto cleanup;
2088 }
2089 ++path_key_expr;
2090 while (isspace(*path_key_expr)) {
2091 ++path_key_expr;
2092 }
2093
2094 /* path is supposed to be evaluated in data tree, so we have to skip
2095 * all schema nodes that cannot be instantiated in data tree */
2096 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2097 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2098 *predicate - start, start);
2099 }
2100 if (!dest_parent_times) {
2101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2102 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2103 *predicate - start, start);
2104 goto cleanup;
2105 }
2106 if (path_key_expr == pke_end) {
2107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2108 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2109 *predicate - start, start);
2110 goto cleanup;
2111 }
2112
2113 while(path_key_expr != pke_end) {
Radek Krejcib4a4a272019-06-10 12:44:52 +02002114 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002116 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2117 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002118 goto cleanup;
2119 }
2120
2121 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002122 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002123 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002124 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002125 }
2126 if (!mod) {
2127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002128 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002129 *predicate - start, start, dst_len, dst);
2130 goto cleanup;
2131 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002132 if (!mod->implemented) {
2133 /* make the module implemented */
2134 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2135 }
Radek Krejcia3045382018-11-22 14:30:31 +01002136
2137 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2138 if (!dst_node) {
2139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002140 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002141 *predicate - start, start, path_key_expr - pke_start, pke_start);
2142 goto cleanup;
2143 }
2144 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002145 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 +01002146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002147 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002148 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2149 goto cleanup;
2150 }
2151 }
2152
2153 ret = LY_SUCCESS;
2154cleanup:
2155 ly_set_erase(&keys, NULL);
2156 return ret;
2157}
2158
2159/**
2160 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2161 *
2162 * path-arg = absolute-path / relative-path
2163 * absolute-path = 1*("/" (node-identifier *path-predicate))
2164 * relative-path = 1*(".." "/") descendant-path
2165 *
2166 * @param[in,out] path Path to parse.
2167 * @param[out] prefix Prefix of the token, NULL if there is not any.
2168 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2169 * @param[out] name Name of the token.
2170 * @param[out] nam_len Length of the name.
2171 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2172 * must not be changed between consecutive calls. -1 if the
2173 * path is absolute.
2174 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2175 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2176 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002177LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002178lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2179 int *parent_times, int *has_predicate)
2180{
2181 int par_times = 0;
2182
2183 assert(path && *path);
2184 assert(parent_times);
2185 assert(prefix);
2186 assert(prefix_len);
2187 assert(name);
2188 assert(name_len);
2189 assert(has_predicate);
2190
2191 *prefix = NULL;
2192 *prefix_len = 0;
2193 *name = NULL;
2194 *name_len = 0;
2195 *has_predicate = 0;
2196
2197 if (!*parent_times) {
2198 if (!strncmp(*path, "..", 2)) {
2199 *path += 2;
2200 ++par_times;
2201 while (!strncmp(*path, "/..", 3)) {
2202 *path += 3;
2203 ++par_times;
2204 }
2205 }
2206 if (par_times) {
2207 *parent_times = par_times;
2208 } else {
2209 *parent_times = -1;
2210 }
2211 }
2212
2213 if (**path != '/') {
2214 return LY_EINVAL;
2215 }
2216 /* skip '/' */
2217 ++(*path);
2218
2219 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002220 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002221
2222 if ((**path == '/' && (*path)[1]) || !**path) {
2223 /* path continues by another token or this is the last token */
2224 return LY_SUCCESS;
2225 } else if ((*path)[0] != '[') {
2226 /* unexpected character */
2227 return LY_EINVAL;
2228 } else {
2229 /* predicate starting with [ */
2230 *has_predicate = 1;
2231 return LY_SUCCESS;
2232 }
2233}
2234
2235/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002236 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2237 *
2238 * The set of features used for target must be a subset of features used for the leafref.
2239 * This is not a perfect, we should compare the truth tables but it could require too much resources
2240 * and RFC 7950 does not require it explicitely, so we simplify that.
2241 *
2242 * @param[in] refnode The leafref node.
2243 * @param[in] target Tha target node of the leafref.
2244 * @return LY_SUCCESS or LY_EVALID;
2245 */
2246static LY_ERR
2247lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2248{
2249 LY_ERR ret = LY_EVALID;
2250 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002251 unsigned int u, v, count;
2252 struct ly_set features = {0};
2253
2254 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002255 if (iter->iffeatures) {
2256 LY_ARRAY_FOR(iter->iffeatures, u) {
2257 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2258 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002259 }
2260 }
2261 }
2262 }
2263
2264 /* we should have, in features set, a superset of features applicable to the target node.
2265 * So when adding features applicable to the target into the features set, we should not be
2266 * able to actually add any new feature, otherwise it is not a subset of features applicable
2267 * to the leafref itself. */
2268 count = features.count;
2269 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002270 if (iter->iffeatures) {
2271 LY_ARRAY_FOR(iter->iffeatures, u) {
2272 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2273 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002274 /* new feature was added (or LY_EMEM) */
2275 goto cleanup;
2276 }
2277 }
2278 }
2279 }
2280 }
2281 ret = LY_SUCCESS;
2282
2283cleanup:
2284 ly_set_erase(&features, NULL);
2285 return ret;
2286}
2287
2288/**
Radek Krejcia3045382018-11-22 14:30:31 +01002289 * @brief Validate the leafref path.
2290 * @param[in] ctx Compile context
2291 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002292 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002293 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2294 */
2295static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002296lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002297{
2298 const struct lysc_node *node = NULL, *parent = NULL;
2299 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002300 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002301 const char *id, *prefix, *name;
2302 size_t prefix_len, name_len;
2303 int parent_times = 0, has_predicate;
2304 unsigned int iter, u;
2305 LY_ERR ret = LY_SUCCESS;
2306
2307 assert(ctx);
2308 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002309 assert(leafref);
2310
Radek Krejcia3045382018-11-22 14:30:31 +01002311 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002312 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002313 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2314 if (!iter) { /* first iteration */
2315 /* precess ".." in relative paths */
2316 if (parent_times > 0) {
2317 /* move from the context node */
2318 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2319 /* path is supposed to be evaluated in data tree, so we have to skip
2320 * all schema nodes that cannot be instantiated in data tree */
2321 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002322 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002323 }
2324 }
2325 }
2326
2327 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002328 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002329 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002330 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002331 }
2332 if (!mod) {
2333 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002334 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2335 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002336 return LY_EVALID;
2337 }
Radek Krejci3d929562019-02-21 11:25:55 +01002338 if (!mod->implemented) {
2339 /* make the module implemented */
2340 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2341 }
Radek Krejcia3045382018-11-22 14:30:31 +01002342
2343 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2344 if (!node) {
2345 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002346 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002347 return LY_EVALID;
2348 }
2349 parent = node;
2350
2351 if (has_predicate) {
2352 /* we have predicate, so the current result must be list */
2353 if (node->nodetype != LYS_LIST) {
2354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2355 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002356 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002357 return LY_EVALID;
2358 }
2359
Radek Krejcibade82a2018-12-05 10:13:48 +01002360 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2361 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002362 }
2363
2364 ++iter;
2365 }
2366 if (ret) {
2367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002368 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002369 return LY_EVALID;
2370 }
2371
2372 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2373 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2374 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002375 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002376 return LY_EVALID;
2377 }
2378
2379 /* check status */
2380 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2381 return LY_EVALID;
2382 }
2383
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002384 /* check config */
2385 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2386 if (node->flags & LYS_CONFIG_R) {
2387 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2388 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2389 leafref->path);
2390 return LY_EVALID;
2391 }
2392 }
2393
Radek Krejci412ddfa2018-11-23 11:44:11 +01002394 /* store the target's type and check for circular chain of leafrefs */
2395 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2396 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2397 if (type == (struct lysc_type*)leafref) {
2398 /* circular chain detected */
2399 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2400 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2401 return LY_EVALID;
2402 }
2403 }
2404
Radek Krejci58d171e2018-11-23 13:50:55 +01002405 /* check if leafref and its target are under common if-features */
2406 if (lys_compile_leafref_features_validate(startnode, node)) {
2407 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2408 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2409 leafref->path);
2410 return LY_EVALID;
2411 }
2412
Radek Krejcia3045382018-11-22 14:30:31 +01002413 return LY_SUCCESS;
2414}
2415
Radek Krejcicdfecd92018-11-26 11:27:32 +01002416static 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 +02002417 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002418/**
2419 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2420 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002421 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2422 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2423 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2424 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002425 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002426 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002427 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002428 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2429 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2430 * @param[out] type Newly created type structure with the filled information about the type.
2431 * @return LY_ERR value.
2432 */
Radek Krejci19a96102018-11-15 13:38:09 +01002433static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002434lys_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 +02002435 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002436 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002437{
2438 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002439 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002440 struct lysc_type_bin *bin;
2441 struct lysc_type_num *num;
2442 struct lysc_type_str *str;
2443 struct lysc_type_bits *bits;
2444 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002445 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002446 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002447 struct lysc_type_union *un, *un_aux;
2448 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002449
2450 switch (basetype) {
2451 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002452 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002453
2454 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002455 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002456 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2457 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002458 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002459 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002460 }
2461 }
2462
2463 if (tpdfname) {
2464 type_p->compiled = *type;
2465 *type = calloc(1, sizeof(struct lysc_type_bin));
2466 }
2467 break;
2468 case LY_TYPE_BITS:
2469 /* RFC 7950 9.7 - bits */
2470 bits = (struct lysc_type_bits*)(*type);
2471 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002472 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002473 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2474 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002475 }
2476
Radek Krejci555cb5b2018-11-16 14:54:33 +01002477 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002478 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002479 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002480 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002481 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002483 free(*type);
2484 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002485 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002486 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002487 }
2488
2489 if (tpdfname) {
2490 type_p->compiled = *type;
2491 *type = calloc(1, sizeof(struct lysc_type_bits));
2492 }
2493 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002494 case LY_TYPE_DEC64:
2495 dec = (struct lysc_type_dec*)(*type);
2496
2497 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002498 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002499 if (!type_p->fraction_digits) {
2500 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002502 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002504 free(*type);
2505 *type = NULL;
2506 }
2507 return LY_EVALID;
2508 }
2509 } else if (type_p->fraction_digits) {
2510 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002511 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2513 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002514 tpdfname);
2515 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2517 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002518 free(*type);
2519 *type = NULL;
2520 }
2521 return LY_EVALID;
2522 }
2523 dec->fraction_digits = type_p->fraction_digits;
2524
2525 /* RFC 7950 9.2.4 - range */
2526 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002527 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2528 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002529 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002530 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002531 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002532 }
2533
2534 if (tpdfname) {
2535 type_p->compiled = *type;
2536 *type = calloc(1, sizeof(struct lysc_type_dec));
2537 }
2538 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002539 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002540 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002541
2542 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002543 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002544 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2545 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002546 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002547 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002548 }
2549 } else if (base && ((struct lysc_type_str*)base)->length) {
2550 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2551 }
2552
2553 /* RFC 7950 9.4.5 - pattern */
2554 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002555 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002556 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002557 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2558 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2559 }
2560
2561 if (tpdfname) {
2562 type_p->compiled = *type;
2563 *type = calloc(1, sizeof(struct lysc_type_str));
2564 }
2565 break;
2566 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002567 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002568
2569 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002570 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002571 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002572 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002573 }
2574
Radek Krejci555cb5b2018-11-16 14:54:33 +01002575 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002576 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002577 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002579 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002580 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002581 free(*type);
2582 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002583 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002584 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002585 }
2586
2587 if (tpdfname) {
2588 type_p->compiled = *type;
2589 *type = calloc(1, sizeof(struct lysc_type_enum));
2590 }
2591 break;
2592 case LY_TYPE_INT8:
2593 case LY_TYPE_UINT8:
2594 case LY_TYPE_INT16:
2595 case LY_TYPE_UINT16:
2596 case LY_TYPE_INT32:
2597 case LY_TYPE_UINT32:
2598 case LY_TYPE_INT64:
2599 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002600 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002601
2602 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002603 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002604 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2605 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002606 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002607 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002608 }
2609 }
2610
2611 if (tpdfname) {
2612 type_p->compiled = *type;
2613 *type = calloc(1, sizeof(struct lysc_type_num));
2614 }
2615 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002616 case LY_TYPE_IDENT:
2617 idref = (struct lysc_type_identityref*)(*type);
2618
2619 /* RFC 7950 9.10.2 - base */
2620 if (type_p->bases) {
2621 if (base) {
2622 /* only the directly derived identityrefs can contain base specification */
2623 if (tpdfname) {
2624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002625 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002626 tpdfname);
2627 } else {
2628 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002629 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002630 free(*type);
2631 *type = NULL;
2632 }
2633 return LY_EVALID;
2634 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002635 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002636 }
2637
2638 if (!base && !type_p->flags) {
2639 /* type derived from identityref built-in type must contain at least one base */
2640 if (tpdfname) {
2641 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2642 } else {
2643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2644 free(*type);
2645 *type = NULL;
2646 }
2647 return LY_EVALID;
2648 }
2649
2650 if (tpdfname) {
2651 type_p->compiled = *type;
2652 *type = calloc(1, sizeof(struct lysc_type_identityref));
2653 }
2654 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002655 case LY_TYPE_LEAFREF:
2656 /* RFC 7950 9.9.3 - require-instance */
2657 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002658 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002659 if (tpdfname) {
2660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2661 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2662 } else {
2663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2664 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2665 free(*type);
2666 *type = NULL;
2667 }
2668 return LY_EVALID;
2669 }
Radek Krejcia3045382018-11-22 14:30:31 +01002670 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002671 } else if (base) {
2672 /* inherit */
2673 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002674 } else {
2675 /* default is true */
2676 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2677 }
2678 if (type_p->path) {
2679 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002680 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002681 } else if (base) {
2682 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002683 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002684 } else if (tpdfname) {
2685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2686 return LY_EVALID;
2687 } else {
2688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2689 free(*type);
2690 *type = NULL;
2691 return LY_EVALID;
2692 }
2693 if (tpdfname) {
2694 type_p->compiled = *type;
2695 *type = calloc(1, sizeof(struct lysc_type_leafref));
2696 }
2697 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002698 case LY_TYPE_INST:
2699 /* RFC 7950 9.9.3 - require-instance */
2700 if (type_p->flags & LYS_SET_REQINST) {
2701 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2702 } else {
2703 /* default is true */
2704 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2705 }
2706
2707 if (tpdfname) {
2708 type_p->compiled = *type;
2709 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2710 }
2711 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002712 case LY_TYPE_UNION:
2713 un = (struct lysc_type_union*)(*type);
2714
2715 /* RFC 7950 7.4 - type */
2716 if (type_p->types) {
2717 if (base) {
2718 /* only the directly derived union can contain types specification */
2719 if (tpdfname) {
2720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2721 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2722 tpdfname);
2723 } else {
2724 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2725 "Invalid type substatement for the type not directly derived from union built-in type.");
2726 free(*type);
2727 *type = NULL;
2728 }
2729 return LY_EVALID;
2730 }
2731 /* compile the type */
2732 additional = 0;
2733 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2734 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002735 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 +01002736 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2737 /* add space for additional types from the union subtype */
2738 un_aux = (struct lysc_type_union *)un->types[u + additional];
2739 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)));
2740 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2741 un->types = (void*)((uint32_t*)(p) + 1);
2742
2743 /* copy subtypes of the subtype union */
2744 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2745 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2746 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2747 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2748 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2749 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2750 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2751 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2752 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2753 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2754 /* TODO extensions */
2755
2756 } else {
2757 un->types[u + additional] = un_aux->types[v];
2758 ++un_aux->types[v]->refcount;
2759 }
2760 ++additional;
2761 LY_ARRAY_INCREMENT(un->types);
2762 }
2763 /* compensate u increment in main loop */
2764 --additional;
2765
2766 /* free the replaced union subtype */
2767 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2768 } else {
2769 LY_ARRAY_INCREMENT(un->types);
2770 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002771 }
2772 }
2773
2774 if (!base && !type_p->flags) {
2775 /* type derived from union built-in type must contain at least one type */
2776 if (tpdfname) {
2777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2778 } else {
2779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2780 free(*type);
2781 *type = NULL;
2782 }
2783 return LY_EVALID;
2784 }
2785
2786 if (tpdfname) {
2787 type_p->compiled = *type;
2788 *type = calloc(1, sizeof(struct lysc_type_union));
2789 }
2790 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002791 case LY_TYPE_BOOL:
2792 case LY_TYPE_EMPTY:
2793 case LY_TYPE_UNKNOWN: /* just to complete switch */
2794 break;
2795 }
2796 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2797done:
2798 return ret;
2799}
2800
Radek Krejcia3045382018-11-22 14:30:31 +01002801/**
2802 * @brief Compile information about the leaf/leaf-list's type.
2803 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002804 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2805 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2806 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2807 * @param[in] context_name Name of the context node or referencing typedef for logging.
2808 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002809 * @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 +01002810 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002811 * @return LY_ERR value.
2812 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002813static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002814lys_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 +02002815 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002816{
2817 LY_ERR ret = LY_SUCCESS;
2818 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002819 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002820 struct type_context {
2821 const struct lysp_tpdf *tpdf;
2822 struct lysp_node *node;
2823 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002824 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002825 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002826 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002827 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002828 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002829
2830 (*type) = NULL;
2831
2832 tctx = calloc(1, sizeof *tctx);
2833 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002834 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002835 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2836 ret == LY_SUCCESS;
2837 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2838 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2839 if (basetype) {
2840 break;
2841 }
2842
2843 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002844 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2845 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002846 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2847
Radek Krejcicdfecd92018-11-26 11:27:32 +01002848 if (units && !*units) {
2849 /* inherit units */
2850 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2851 }
Radek Krejci01342af2019-01-03 15:18:08 +01002852 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002853 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002854 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002855 }
Radek Krejci01342af2019-01-03 15:18:08 +01002856 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002857 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2858 break;
2859 }
2860
Radek Krejci19a96102018-11-15 13:38:09 +01002861 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002862 /* it is not necessary to continue, the rest of the chain was already compiled,
2863 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002864 basetype = tctx->tpdf->type.compiled->basetype;
2865 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002866 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002867 dummyloops = 1;
2868 goto preparenext;
2869 } else {
2870 tctx = NULL;
2871 break;
2872 }
Radek Krejci19a96102018-11-15 13:38:09 +01002873 }
2874
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002875 /* circular typedef reference detection */
2876 for (u = 0; u < tpdf_chain.count; u++) {
2877 /* local part */
2878 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2879 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2880 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2881 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2882 free(tctx);
2883 ret = LY_EVALID;
2884 goto cleanup;
2885 }
2886 }
2887 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2888 /* global part for unions corner case */
2889 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2890 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2892 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2893 free(tctx);
2894 ret = LY_EVALID;
2895 goto cleanup;
2896 }
2897 }
2898
Radek Krejci19a96102018-11-15 13:38:09 +01002899 /* store information for the following processing */
2900 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2901
Radek Krejcicdfecd92018-11-26 11:27:32 +01002902preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002903 /* prepare next loop */
2904 tctx_prev = tctx;
2905 tctx = calloc(1, sizeof *tctx);
2906 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2907 }
2908 free(tctx);
2909
2910 /* allocate type according to the basetype */
2911 switch (basetype) {
2912 case LY_TYPE_BINARY:
2913 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002914 break;
2915 case LY_TYPE_BITS:
2916 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002917 break;
2918 case LY_TYPE_BOOL:
2919 case LY_TYPE_EMPTY:
2920 *type = calloc(1, sizeof(struct lysc_type));
2921 break;
2922 case LY_TYPE_DEC64:
2923 *type = calloc(1, sizeof(struct lysc_type_dec));
2924 break;
2925 case LY_TYPE_ENUM:
2926 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002927 break;
2928 case LY_TYPE_IDENT:
2929 *type = calloc(1, sizeof(struct lysc_type_identityref));
2930 break;
2931 case LY_TYPE_INST:
2932 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2933 break;
2934 case LY_TYPE_LEAFREF:
2935 *type = calloc(1, sizeof(struct lysc_type_leafref));
2936 break;
2937 case LY_TYPE_STRING:
2938 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002939 break;
2940 case LY_TYPE_UNION:
2941 *type = calloc(1, sizeof(struct lysc_type_union));
2942 break;
2943 case LY_TYPE_INT8:
2944 case LY_TYPE_UINT8:
2945 case LY_TYPE_INT16:
2946 case LY_TYPE_UINT16:
2947 case LY_TYPE_INT32:
2948 case LY_TYPE_UINT32:
2949 case LY_TYPE_INT64:
2950 case LY_TYPE_UINT64:
2951 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002952 break;
2953 case LY_TYPE_UNKNOWN:
2954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2955 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2956 ret = LY_EVALID;
2957 goto cleanup;
2958 }
2959 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002960 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002961 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2962 ly_data_type2str[basetype]);
2963 free(*type);
2964 (*type) = NULL;
2965 ret = LY_EVALID;
2966 goto cleanup;
2967 }
2968
2969 /* get restrictions from the referred typedefs */
2970 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2971 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002972
2973 /* remember the typedef context for circular check */
2974 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2975
Radek Krejci43699232018-11-23 14:59:46 +01002976 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002977 base = tctx->tpdf->type.compiled;
2978 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002979 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002980 /* no change, just use the type information from the base */
2981 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2982 ++base->refcount;
2983 continue;
2984 }
2985
2986 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002987 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2989 tctx->tpdf->name, ly_data_type2str[basetype]);
2990 ret = LY_EVALID;
2991 goto cleanup;
2992 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2994 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2995 tctx->tpdf->name, tctx->tpdf->dflt);
2996 ret = LY_EVALID;
2997 goto cleanup;
2998 }
2999
Radek Krejci19a96102018-11-15 13:38:09 +01003000 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003001 /* TODO user type plugins */
3002 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003003 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003004 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 +01003005 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003006 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003007 LY_CHECK_GOTO(ret, cleanup);
3008 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003009 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003010 /* remove the processed typedef contexts from the stack for circular check */
3011 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003012
Radek Krejcic5c27e52018-11-15 14:38:11 +01003013 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003014 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003015 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003016 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003017 /* TODO user type plugins */
3018 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003019 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003020 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 +01003021 LY_CHECK_GOTO(ret, cleanup);
3022 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003023 /* no specific restriction in leaf's type definition, copy from the base */
3024 free(*type);
3025 (*type) = base;
3026 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003027 }
Radek Krejci01342af2019-01-03 15:18:08 +01003028 if (!(*type)->dflt) {
3029 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3030 }
Radek Krejci19a96102018-11-15 13:38:09 +01003031
Radek Krejciec4da802019-05-02 13:02:41 +02003032 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003033
3034cleanup:
3035 ly_set_erase(&tpdf_chain, free);
3036 return ret;
3037}
3038
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003039/**
3040 * @brief Compile status information of the given node.
3041 *
3042 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3043 * has the status correctly set during the compilation.
3044 *
3045 * @param[in] ctx Compile context
3046 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3047 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3048 * the compatibility with the parent's status value.
3049 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3050 * @return LY_ERR value.
3051 */
3052static LY_ERR
3053lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3054{
3055 /* status - it is not inherited by specification, but it does not make sense to have
3056 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3057 if (!((*node_flags) & LYS_STATUS_MASK)) {
3058 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3059 if ((parent_flags & 0x3) != 0x3) {
3060 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3061 * combination of bits (0x3) which marks the uses_status value */
3062 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3063 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3064 }
3065 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3066 } else {
3067 (*node_flags) |= LYS_STATUS_CURR;
3068 }
3069 } else if (parent_flags & LYS_STATUS_MASK) {
3070 /* check status compatibility with the parent */
3071 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3072 if ((*node_flags) & LYS_STATUS_CURR) {
3073 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3074 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3075 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3076 } else { /* LYS_STATUS_DEPRC */
3077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3078 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3079 }
3080 return LY_EVALID;
3081 }
3082 }
3083 return LY_SUCCESS;
3084}
3085
Radek Krejci8cce8532019-03-05 11:27:45 +01003086/**
3087 * @brief Check uniqness of the node/action/notification name.
3088 *
3089 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3090 * structures, but they share the namespace so we need to check their name collisions.
3091 *
3092 * @param[in] ctx Compile context.
3093 * @param[in] children List (linked list) of data nodes to go through.
3094 * @param[in] actions List (sized array) of actions or RPCs to go through.
3095 * @param[in] notifs List (sized array) of Notifications to go through.
3096 * @param[in] name Name of the item to find in the given lists.
3097 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3098 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3099 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3100 */
3101static LY_ERR
3102lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3103 const struct lysc_action *actions, const struct lysc_notif *notifs,
3104 const char *name, void *exclude)
3105{
3106 const struct lysc_node *iter;
3107 unsigned int u;
3108
3109 LY_LIST_FOR(children, iter) {
3110 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3111 goto error;
3112 }
3113 }
3114 LY_ARRAY_FOR(actions, u) {
3115 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3116 goto error;
3117 }
3118 }
3119 LY_ARRAY_FOR(notifs, u) {
3120 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3121 goto error;
3122 }
3123 }
3124 return LY_SUCCESS;
3125error:
3126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3127 return LY_EEXIST;
3128}
3129
Radek Krejciec4da802019-05-02 13:02:41 +02003130static 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 +01003131
Radek Krejcia3045382018-11-22 14:30:31 +01003132/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003133 * @brief Compile parsed RPC/action schema node information.
3134 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003135 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003136 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003137 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3138 * @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).
3139 * Zero means no uses, non-zero value with no status bit set mean the default status.
3140 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3141 */
3142static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003143lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003144 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3145{
3146 LY_ERR ret = LY_SUCCESS;
3147 struct lysp_node *child_p;
3148 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003149 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003150
Radek Krejci8cce8532019-03-05 11:27:45 +01003151 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3152 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3153 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3154 action_p->name, action)) {
3155 return LY_EVALID;
3156 }
3157
Radek Krejciec4da802019-05-02 13:02:41 +02003158 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003159 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003160 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003161 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003162 return LY_EVALID;
3163 }
3164
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003165 action->nodetype = LYS_ACTION;
3166 action->module = ctx->mod;
3167 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003168 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003169 action->sp = action_p;
3170 }
3171 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3172
3173 /* status - it is not inherited by specification, but it does not make sense to have
3174 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3175 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3176
3177 DUP_STRING(ctx->ctx, action_p->name, action->name);
3178 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3179 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003180 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3181 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003182
3183 /* input */
Radek Krejciec4da802019-05-02 13:02:41 +02003184 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3185 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3186 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003187 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003188 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003189 }
Radek Krejciec4da802019-05-02 13:02:41 +02003190 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003191
3192 /* output */
Radek Krejciec4da802019-05-02 13:02:41 +02003193 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3194 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3195 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003196 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003197 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003198 }
3199
3200cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003201 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003202 return ret;
3203}
3204
3205/**
Radek Krejci43981a32019-04-12 09:44:11 +02003206 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003207 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003208 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003209 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3210 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3211 * @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 +02003212 * Zero means no uses, non-zero value with no status bit set mean the default status.
3213 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3214 */
3215static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003216lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003217 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3218{
3219 LY_ERR ret = LY_SUCCESS;
3220 struct lysp_node *child_p;
3221 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003222 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003223
3224 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3225 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3226 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3227 notif_p->name, notif)) {
3228 return LY_EVALID;
3229 }
3230
Radek Krejciec4da802019-05-02 13:02:41 +02003231 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3233 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003234 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003235 return LY_EVALID;
3236 }
3237
3238 notif->nodetype = LYS_NOTIF;
3239 notif->module = ctx->mod;
3240 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003241 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003242 notif->sp = notif_p;
3243 }
3244 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3245
3246 /* status - it is not inherited by specification, but it does not make sense to have
3247 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3248 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3249
3250 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3251 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3252 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003253 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3254 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3255 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003256
Radek Krejciec4da802019-05-02 13:02:41 +02003257 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003258 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003259 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003260 }
3261
3262cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003263 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003264 return ret;
3265}
3266
3267/**
Radek Krejcia3045382018-11-22 14:30:31 +01003268 * @brief Compile parsed container node information.
3269 * @param[in] ctx Compile context
3270 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003271 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3272 * is enriched with the container-specific information.
3273 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3274 */
Radek Krejci19a96102018-11-15 13:38:09 +01003275static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003276lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003277{
3278 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3279 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3280 struct lysp_node *child_p;
3281 unsigned int u;
3282 LY_ERR ret = LY_SUCCESS;
3283
Radek Krejcife909632019-02-12 15:34:42 +01003284 if (cont_p->presence) {
3285 cont->flags |= LYS_PRESENCE;
3286 }
3287
Radek Krejci19a96102018-11-15 13:38:09 +01003288 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003289 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003290 }
3291
Radek Krejciec4da802019-05-02 13:02:41 +02003292 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3293 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3294 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003295
3296done:
3297 return ret;
3298}
3299
Radek Krejci33f72892019-02-21 10:36:58 +01003300/*
3301 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3302 * @param[in] ctx Compile context.
3303 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3304 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003305 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3306 * @return LY_ERR value.
3307 */
3308static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003309lys_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 +01003310{
3311 unsigned int u, v;
3312 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3313
Radek Krejciec4da802019-05-02 13:02:41 +02003314 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 +01003315 leaf->units ? NULL : &leaf->units));
3316 if (leaf->nodetype == LYS_LEAFLIST) {
3317 if (llist->type->dflt && !llist->dflts && !llist->min) {
3318 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3319 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3320 LY_ARRAY_INCREMENT(llist->dflts);
3321 }
3322 } else {
3323 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3324 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3325 }
3326 }
3327 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3328 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3329 ly_set_add(&ctx->unres, leaf, 0);
3330 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3331 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3332 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3333 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3334 ly_set_add(&ctx->unres, leaf, 0);
3335 }
3336 }
3337 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3338 if (leaf->flags & LYS_SET_DFLT) {
3339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3340 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3341 return LY_EVALID;
3342 }
3343 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3344 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3345 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3346 return LY_EVALID;
3347 }
3348 }
3349
3350 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3351 /* configuration data values must be unique - so check the default values */
3352 LY_ARRAY_FOR(llist->dflts, u) {
3353 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3354 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3355 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3356 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3357 return LY_EVALID;
3358 }
3359 }
3360 }
3361 }
3362
3363 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3364
3365 return LY_SUCCESS;
3366}
3367
Radek Krejcia3045382018-11-22 14:30:31 +01003368/**
3369 * @brief Compile parsed leaf node information.
3370 * @param[in] ctx Compile context
3371 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003372 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3373 * is enriched with the leaf-specific information.
3374 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3375 */
Radek Krejci19a96102018-11-15 13:38:09 +01003376static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003377lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003378{
3379 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3380 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3381 unsigned int u;
3382 LY_ERR ret = LY_SUCCESS;
3383
Radek Krejciec4da802019-05-02 13:02:41 +02003384 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003385 if (leaf_p->units) {
3386 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3387 leaf->flags |= LYS_SET_UNITS;
3388 }
3389 if (leaf_p->dflt) {
3390 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003391 leaf->flags |= LYS_SET_DFLT;
3392 }
Radek Krejci43699232018-11-23 14:59:46 +01003393
Radek Krejciec4da802019-05-02 13:02:41 +02003394 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003395
Radek Krejci19a96102018-11-15 13:38:09 +01003396done:
3397 return ret;
3398}
3399
Radek Krejcia3045382018-11-22 14:30:31 +01003400/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003401 * @brief Compile parsed leaf-list node information.
3402 * @param[in] ctx Compile context
3403 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003404 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3405 * is enriched with the leaf-list-specific information.
3406 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3407 */
3408static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003409lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003410{
3411 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3412 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003413 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003414 LY_ERR ret = LY_SUCCESS;
3415
Radek Krejciec4da802019-05-02 13:02:41 +02003416 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003417 if (llist_p->units) {
3418 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3419 llist->flags |= LYS_SET_UNITS;
3420 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003421
3422 if (llist_p->dflts) {
3423 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3424 LY_ARRAY_FOR(llist_p->dflts, u) {
3425 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3426 LY_ARRAY_INCREMENT(llist->dflts);
3427 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003428 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003429 }
3430
3431 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003432 if (llist->min) {
3433 llist->flags |= LYS_MAND_TRUE;
3434 }
Radek Krejcib7408632018-11-28 17:12:11 +01003435 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003436
Radek Krejciec4da802019-05-02 13:02:41 +02003437 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003438
3439done:
3440 return ret;
3441}
3442
3443/**
Radek Krejci7af64242019-02-18 13:07:53 +01003444 * @brief Compile information about list's uniques.
3445 * @param[in] ctx Compile context.
3446 * @param[in] context_module Module where the prefixes are going to be resolved.
3447 * @param[in] uniques Sized array list of unique statements.
3448 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3449 * @return LY_ERR value.
3450 */
3451static LY_ERR
3452lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3453{
3454 LY_ERR ret = LY_SUCCESS;
3455 struct lysc_node_leaf **key, ***unique;
3456 const char *keystr, *delim;
3457 size_t len;
3458 unsigned int v;
3459 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003460 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003461
3462 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3463 config = -1;
3464 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3465 keystr = uniques[v];
3466 while (keystr) {
3467 delim = strpbrk(keystr, " \t\n");
3468 if (delim) {
3469 len = delim - keystr;
3470 while (isspace(*delim)) {
3471 ++delim;
3472 }
3473 } else {
3474 len = strlen(keystr);
3475 }
3476
3477 /* unique node must be present */
3478 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003479 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3480 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003481 if (ret != LY_SUCCESS) {
3482 if (ret == LY_EDENIED) {
3483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003484 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003485 len, keystr, lys_nodetype2str((*key)->nodetype));
3486 }
3487 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003488 } else if (flags) {
3489 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3490 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3491 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3492 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003493 }
3494
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003495
Radek Krejci7af64242019-02-18 13:07:53 +01003496 /* all referenced leafs must be of the same config type */
3497 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3499 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3500 return LY_EVALID;
3501 } else if ((*key)->flags & LYS_CONFIG_W) {
3502 config = 1;
3503 } else { /* LYS_CONFIG_R */
3504 config = 0;
3505 }
3506
3507 /* check status */
3508 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3509 (*key)->flags, (*key)->module, (*key)->name));
3510
3511 /* mark leaf as unique */
3512 (*key)->flags |= LYS_UNIQUE;
3513
3514 /* next unique value in line */
3515 keystr = delim;
3516 }
3517 /* next unique definition */
3518 }
3519
3520 return LY_SUCCESS;
3521}
3522
3523/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003524 * @brief Compile parsed list node information.
3525 * @param[in] ctx Compile context
3526 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003527 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3528 * is enriched with the 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_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003533{
3534 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3535 struct lysc_node_list *list = (struct lysc_node_list*)node;
3536 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003537 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003538 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003539 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003540 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003541 LY_ERR ret = LY_SUCCESS;
3542
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003543 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003544 if (list->min) {
3545 list->flags |= LYS_MAND_TRUE;
3546 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003547 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3548
3549 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003550 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003551 }
3552
Radek Krejciec4da802019-05-02 13:02:41 +02003553 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003554
3555 /* keys */
3556 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3557 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3558 return LY_EVALID;
3559 }
3560
3561 /* find all the keys (must be direct children) */
3562 keystr = list_p->key;
3563 while (keystr) {
3564 delim = strpbrk(keystr, " \t\n");
3565 if (delim) {
3566 len = delim - keystr;
3567 while (isspace(*delim)) {
3568 ++delim;
3569 }
3570 } else {
3571 len = strlen(keystr);
3572 }
3573
3574 /* key node must be present */
3575 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3576 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3577 if (!(*key)) {
3578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3579 "The list's key \"%.*s\" not found.", len, keystr);
3580 return LY_EVALID;
3581 }
3582 /* keys must be unique */
3583 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3584 if (*key == list->keys[u]) {
3585 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3586 "Duplicated key identifier \"%.*s\".", len, keystr);
3587 return LY_EVALID;
3588 }
3589 }
3590 /* key must have the same config flag as the list itself */
3591 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3592 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3593 return LY_EVALID;
3594 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003595 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003596 /* YANG 1.0 denies key to be of empty type */
3597 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3599 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3600 return LY_EVALID;
3601 }
3602 } else {
3603 /* when and if-feature are illegal on list keys */
3604 if ((*key)->when) {
3605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3606 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3607 return LY_EVALID;
3608 }
3609 if ((*key)->iffeatures) {
3610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3611 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3612 return LY_EVALID;
3613 }
3614 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003615
3616 /* check status */
3617 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3618 (*key)->flags, (*key)->module, (*key)->name));
3619
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003620 /* ignore default values of the key */
3621 if ((*key)->dflt) {
3622 lydict_remove(ctx->ctx, (*key)->dflt);
3623 (*key)->dflt = NULL;
3624 }
3625 /* mark leaf as key */
3626 (*key)->flags |= LYS_KEY;
3627
3628 /* next key value */
3629 keystr = delim;
3630 }
3631
3632 /* uniques */
3633 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003634 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003635 }
3636
Radek Krejciec4da802019-05-02 13:02:41 +02003637 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3638 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003639
3640done:
3641 return ret;
3642}
3643
Radek Krejcib56c7502019-02-13 14:19:54 +01003644/**
3645 * @brief Do some checks and set the default choice's case.
3646 *
3647 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3648 *
3649 * @param[in] ctx Compile context.
3650 * @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,
3651 * not the reference to the imported module.
3652 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3653 * @return LY_ERR value.
3654 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003655static LY_ERR
3656lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3657{
3658 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3659 const char *prefix = NULL, *name;
3660 size_t prefix_len = 0;
3661
3662 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3663 name = strchr(dflt, ':');
3664 if (name) {
3665 prefix = dflt;
3666 prefix_len = name - prefix;
3667 ++name;
3668 } else {
3669 name = dflt;
3670 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003671 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003672 /* prefixed default case make sense only for the prefix of the schema itself */
3673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3674 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3675 prefix_len, prefix);
3676 return LY_EVALID;
3677 }
3678 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3679 if (!ch->dflt) {
3680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3681 "Default case \"%s\" not found.", dflt);
3682 return LY_EVALID;
3683 }
3684 /* no mandatory nodes directly under the default case */
3685 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003686 if (iter->parent != (struct lysc_node*)ch->dflt) {
3687 break;
3688 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003689 if (iter->flags & LYS_MAND_TRUE) {
3690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3691 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3692 return LY_EVALID;
3693 }
3694 }
Radek Krejci01342af2019-01-03 15:18:08 +01003695 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003696 return LY_SUCCESS;
3697}
3698
Radek Krejciccd20f12019-02-15 14:12:27 +01003699static LY_ERR
3700lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3701{
3702 struct lys_module *mod;
3703 const char *prefix = NULL, *name;
3704 size_t prefix_len = 0;
3705 struct lysc_node_case *cs;
3706 struct lysc_node *node;
3707
3708 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3709 name = strchr(dflt, ':');
3710 if (name) {
3711 prefix = dflt;
3712 prefix_len = name - prefix;
3713 ++name;
3714 } else {
3715 name = dflt;
3716 }
3717 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3718 if (prefix) {
3719 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3721 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3722 "The prefix does not match any imported module of the deviation module.",
3723 devnodeid, dflt);
3724 return LY_EVALID;
3725 }
3726 } else {
3727 mod = ctx->mod;
3728 }
3729 /* get the default case */
3730 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3731 if (!cs) {
3732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3733 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3734 devnodeid, dflt);
3735 return LY_EVALID;
3736 }
3737
3738 /* check that there is no mandatory node */
3739 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003740 if (node->parent != (struct lysc_node*)cs) {
3741 break;
3742 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003743 if (node->flags & LYS_MAND_TRUE) {
3744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3745 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3746 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3747 return LY_EVALID;
3748 }
3749 }
3750
3751 /* set the default case in choice */
3752 ch->dflt = cs;
3753 cs->flags |= LYS_SET_DFLT;
3754
3755 return LY_SUCCESS;
3756}
3757
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003758/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003759 * @brief Compile parsed choice node information.
3760 * @param[in] ctx Compile context
3761 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003762 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003763 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003764 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3765 */
3766static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003767lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003768{
3769 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3770 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3771 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003772 LY_ERR ret = LY_SUCCESS;
3773
Radek Krejci056d0a82018-12-06 16:57:25 +01003774 LY_LIST_FOR(ch_p->child, child_p) {
3775 if (child_p->nodetype == LYS_CASE) {
3776 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003777 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003778 }
3779 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003780 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003781 }
3782 }
3783
3784 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003785 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003786 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003787 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003788
Radek Krejci9800fb82018-12-13 14:26:23 +01003789 return ret;
3790}
3791
3792/**
3793 * @brief Compile parsed anydata or anyxml node information.
3794 * @param[in] ctx Compile context
3795 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01003796 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3797 * is enriched with the any-specific information.
3798 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3799 */
3800static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003801lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01003802{
3803 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3804 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3805 unsigned int u;
3806 LY_ERR ret = LY_SUCCESS;
3807
Radek Krejciec4da802019-05-02 13:02:41 +02003808 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01003809
3810 if (any->flags & LYS_CONFIG_W) {
3811 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3812 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3813 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003814done:
3815 return ret;
3816}
3817
Radek Krejcib56c7502019-02-13 14:19:54 +01003818/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003819 * @brief Connect the node into the siblings list and check its name uniqueness.
3820 *
3821 * @param[in] ctx Compile context
3822 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3823 * the choice itself is expected instead of a specific case node.
3824 * @param[in] node Schema node to connect into the list.
3825 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3826 */
3827static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003828lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003829{
3830 struct lysc_node **children;
3831
3832 if (node->nodetype == LYS_CASE) {
3833 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3834 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003835 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003836 }
3837 if (children) {
3838 if (!(*children)) {
3839 /* first child */
3840 *children = node;
3841 } else if (*children != node) {
3842 /* by the condition in previous branch we cover the choice/case children
3843 * - the children list is shared by the choice and the the first case, in addition
3844 * the first child of each case must be referenced from the case node. So the node is
3845 * actually always already inserted in case it is the first children - so here such
3846 * a situation actually corresponds to the first branch */
3847 /* insert at the end of the parent's children list */
3848 (*children)->prev->next = node;
3849 node->prev = (*children)->prev;
3850 (*children)->prev = node;
3851
3852 /* check the name uniqueness */
3853 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3854 lysc_node_notifs(parent), node->name, node)) {
3855 return LY_EEXIST;
3856 }
3857 }
3858 }
3859 return LY_SUCCESS;
3860}
3861
Radek Krejci95710c92019-02-11 15:49:55 +01003862/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003863 * @brief Get the XPath context node for the given schema node.
3864 * @param[in] start The schema node where the XPath expression appears.
3865 * @return The context node to evaluate XPath expression in given schema node.
3866 * @return NULL in case the context node is the root node.
3867 */
3868static struct lysc_node *
3869lysc_xpath_context(struct lysc_node *start)
3870{
3871 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3872 start = start->parent);
3873 return start;
3874}
3875
3876/**
3877 * @brief Prepare the case structure in choice node for the new data node.
3878 *
3879 * 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
3880 * created in the choice when the first child was processed.
3881 *
3882 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003883 * @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,
3884 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003885 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3886 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3887 * @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,
3888 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003889 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003890static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02003891lys_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 +01003892{
3893 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02003894 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01003895 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003896 unsigned int u;
3897 LY_ERR ret;
3898
Radek Krejci95710c92019-02-11 15:49:55 +01003899#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003900 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003901 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003902 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3903 return NULL; \
3904 } \
3905 }
3906
Radek Krejci95710c92019-02-11 15:49:55 +01003907 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3908 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003909
3910 /* we have to add an implicit case node into the parent choice */
3911 cs = calloc(1, sizeof(struct lysc_node_case));
3912 DUP_STRING(ctx->ctx, child->name, cs->name);
3913 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003914 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003915 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3916 /* the case is already present since the child is not its first children */
3917 return (struct lysc_node_case*)ch->cases->prev;
3918 }
Radek Krejci95710c92019-02-11 15:49:55 +01003919 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003920
3921 /* explicit parent case is not present (this is its first child) */
3922 cs = calloc(1, sizeof(struct lysc_node_case));
3923 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3924 cs->flags = LYS_STATUS_MASK & node_p->flags;
3925 cs->sp = node_p;
3926
Radek Krejcib1b59152019-01-07 13:21:56 +01003927 /* 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 +02003928 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01003929
3930 if (node_p->when) {
3931 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02003932 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01003933 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003934 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003935 }
Radek Krejciec4da802019-05-02 13:02:41 +02003936 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003937 } else {
3938 LOGINT(ctx->ctx);
3939 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003940 }
3941 cs->module = ctx->mod;
3942 cs->prev = (struct lysc_node*)cs;
3943 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02003944 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01003945 cs->parent = (struct lysc_node*)ch;
3946 cs->child = child;
3947
3948 return cs;
3949error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02003950 if (cs) {
3951 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
3952 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003953 return NULL;
3954
3955#undef UNIQUE_CHECK
3956}
3957
Radek Krejcib56c7502019-02-13 14:19:54 +01003958/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003959 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003960 *
3961 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003962 * @param[in] node Target node where the config is supposed to be changed.
3963 * @param[in] config_flag Node's config flag to be applied to the @p node.
3964 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003965 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3966 * 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 +01003967 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3968 * @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 +01003969 * @return LY_ERR value.
3970 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003971static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003972lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3973 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003974{
3975 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003976 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003977
3978 if (config == (node->flags & LYS_CONFIG_MASK)) {
3979 /* nothing to do */
3980 return LY_SUCCESS;
3981 }
3982
3983 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003984 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003985 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003987 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3988 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003989 return LY_EVALID;
3990 }
Radek Krejci93dcc392019-02-19 10:43:38 +01003991 node->flags |= LYS_SET_CONFIG;
3992 } else {
3993 if (node->flags & LYS_SET_CONFIG) {
3994 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
3995 /* setting config flags, but have node with explicit config true */
3996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3997 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3998 refine_flag ? "refine" : "deviation", nodeid);
3999 return LY_EVALID;
4000 }
4001 /* do not change config on nodes where the config is explicitely set, this does not apply to
4002 * nodes, which are being changed explicitly (targets of refine or deviation) */
4003 return LY_SUCCESS;
4004 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004005 }
4006 node->flags &= ~LYS_CONFIG_MASK;
4007 node->flags |= config;
4008
4009 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004010 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004011 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004012 }
4013
Radek Krejci76b3e962018-12-14 17:01:25 +01004014 return LY_SUCCESS;
4015}
4016
Radek Krejcib56c7502019-02-13 14:19:54 +01004017/**
4018 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4019 *
4020 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4021 * the flag to such parents from a mandatory children.
4022 *
4023 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4024 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4025 * (mandatory children was removed).
4026 */
Radek Krejcife909632019-02-12 15:34:42 +01004027void
4028lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4029{
4030 struct lysc_node *iter;
4031
4032 if (add) { /* set flag */
4033 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4034 parent = parent->parent) {
4035 parent->flags |= LYS_MAND_TRUE;
4036 }
4037 } else { /* unset flag */
4038 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004039 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004040 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004041 /* there is another mandatory node */
4042 return;
4043 }
4044 }
4045 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4046 parent->flags &= ~LYS_MAND_TRUE;
4047 }
4048 }
4049}
4050
Radek Krejci056d0a82018-12-06 16:57:25 +01004051/**
Radek Krejci3641f562019-02-13 15:38:40 +01004052 * @brief Internal sorting process for the lys_compile_augment_sort().
4053 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4054 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4055 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4056 */
4057static void
4058lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4059{
4060 unsigned int v;
4061 size_t len;
4062
4063 len = strlen(aug_p->nodeid);
4064 LY_ARRAY_FOR(result, v) {
4065 if (strlen(result[v]->nodeid) <= len) {
4066 continue;
4067 }
4068 if (v < LY_ARRAY_SIZE(result)) {
4069 /* move the rest of array */
4070 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4071 break;
4072 }
4073 }
4074 result[v] = aug_p;
4075 LY_ARRAY_INCREMENT(result);
4076}
4077
4078/**
4079 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4080 *
4081 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4082 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4083 *
4084 * @param[in] ctx Compile context.
4085 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4086 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4087 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4088 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4089 * @return LY_ERR value.
4090 */
4091LY_ERR
4092lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4093{
4094 struct lysp_augment **result = NULL;
4095 unsigned int u, v;
4096 size_t count = 0;
4097
4098 assert(augments);
4099
4100 /* get count of the augments in module and all its submodules */
4101 if (aug_p) {
4102 count += LY_ARRAY_SIZE(aug_p);
4103 }
4104 LY_ARRAY_FOR(inc_p, u) {
4105 if (inc_p[u].submodule->augments) {
4106 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4107 }
4108 }
4109
4110 if (!count) {
4111 *augments = NULL;
4112 return LY_SUCCESS;
4113 }
4114 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4115
4116 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4117 * together, so there can be even /z/y betwwen them. */
4118 LY_ARRAY_FOR(aug_p, u) {
4119 lys_compile_augment_sort_(&aug_p[u], result);
4120 }
4121 LY_ARRAY_FOR(inc_p, u) {
4122 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4123 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4124 }
4125 }
4126
4127 *augments = result;
4128 return LY_SUCCESS;
4129}
4130
4131/**
4132 * @brief Compile the parsed augment connecting it into its target.
4133 *
4134 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4135 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4136 * are already implemented and compiled.
4137 *
4138 * @param[in] ctx Compile context.
4139 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004140 * @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
4141 * children in case of the augmenting uses data.
4142 * @return LY_SUCCESS on success.
4143 * @return LY_EVALID on failure.
4144 */
4145LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004146lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004147{
4148 LY_ERR ret = LY_SUCCESS;
4149 struct lysp_node *node_p, *case_node_p;
4150 struct lysc_node *target; /* target target of the augment */
4151 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004152 struct lysc_when **when, *when_shared;
4153 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004154 uint16_t flags = 0;
4155 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004156 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004157
Radek Krejci7af64242019-02-18 13:07:53 +01004158 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004159 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004160 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004161 if (ret != LY_SUCCESS) {
4162 if (ret == LY_EDENIED) {
4163 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4164 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4165 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4166 }
4167 return LY_EVALID;
4168 }
4169
4170 /* check for mandatory nodes
4171 * - new cases augmenting some choice can have mandatory nodes
4172 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4173 */
Radek Krejci733988a2019-02-15 15:12:44 +01004174 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004175 allow_mandatory = 1;
4176 }
4177
4178 when_shared = NULL;
4179 LY_LIST_FOR(aug_p->child, node_p) {
4180 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4181 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4182 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004183 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4184 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004185 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4186 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4187 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4188 return LY_EVALID;
4189 }
4190
4191 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004192 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004193 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004194 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004195 } else {
4196 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004197 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004198 }
4199 }
Radek Krejciec4da802019-05-02 13:02:41 +02004200 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004201
Radek Krejcife13da42019-02-15 14:51:01 +01004202 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4203 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004204 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004205 /* 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 +01004206 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 +01004207 } else if (target->nodetype == LYS_CHOICE) {
4208 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4209 node = ((struct lysc_node_choice*)target)->cases->prev;
4210 } else {
4211 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004212 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004213 }
4214
Radek Krejci733988a2019-02-15 15:12:44 +01004215 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004216 node->flags &= ~LYS_MAND_TRUE;
4217 lys_compile_mandatory_parents(target, 0);
4218 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4219 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4220 aug_p->nodeid, node->name);
4221 return LY_EVALID;
4222 }
4223
4224 /* pass augment's when to all the children */
4225 if (aug_p->when) {
4226 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4227 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004228 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004229 LY_CHECK_GOTO(ret, error);
4230 (*when)->context = lysc_xpath_context(target);
4231 when_shared = *when;
4232 } else {
4233 ++when_shared->refcount;
4234 (*when) = when_shared;
4235 }
4236 }
4237 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004238
Radek Krejciec4da802019-05-02 13:02:41 +02004239 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004240 switch (target->nodetype) {
4241 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004242 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004243 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004244 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004245 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004246 break;
4247 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004248 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004249 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004250 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004251 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004252 break;
4253 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004254 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004255 if (aug_p->actions) {
4256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4257 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4258 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4259 return LY_EVALID;
4260 }
4261 if (aug_p->notifs) {
4262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4263 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4264 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4265 return LY_EVALID;
4266 }
4267 }
Radek Krejci3641f562019-02-13 15:38:40 +01004268
4269error:
Radek Krejciec4da802019-05-02 13:02:41 +02004270 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004271 return ret;
4272}
4273
4274/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004275 * @brief Apply refined or deviated mandatory flag to the target node.
4276 *
4277 * @param[in] ctx Compile context.
4278 * @param[in] node Target node where the mandatory property is supposed to be changed.
4279 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4280 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4281 * @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 +01004282 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4283 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4284 * 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 +01004285 * @return LY_ERR value.
4286 */
4287static LY_ERR
4288lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4289{
4290 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4292 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4293 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4294 return LY_EVALID;
4295 }
4296
4297 if (mandatory_flag & LYS_MAND_TRUE) {
4298 /* check if node has default value */
4299 if (node->nodetype & LYS_LEAF) {
4300 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004301 if (refine_flag) {
4302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4303 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4304 return LY_EVALID;
4305 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004306 } else {
4307 /* remove the default value taken from the leaf's type */
4308 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4309 ((struct lysc_node_leaf*)node)->dflt = NULL;
4310 }
4311 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004312 if (refine_flag) {
4313 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4314 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4315 return LY_EVALID;
4316 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004317 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004318 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004320 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004321 return LY_EVALID;
4322 }
4323
4324 node->flags &= ~LYS_MAND_FALSE;
4325 node->flags |= LYS_MAND_TRUE;
4326 lys_compile_mandatory_parents(node->parent, 1);
4327 } else {
4328 /* make mandatory false */
4329 node->flags &= ~LYS_MAND_TRUE;
4330 node->flags |= LYS_MAND_FALSE;
4331 lys_compile_mandatory_parents(node->parent, 0);
4332 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4333 /* get the type's default value if any */
4334 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4335 }
4336 }
4337 return LY_SUCCESS;
4338}
4339
4340/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004341 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4342 * If present, also apply uses's modificators.
4343 *
4344 * @param[in] ctx Compile context
4345 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004346 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4347 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4348 * the compile context.
4349 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4350 */
4351static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004352lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004353{
4354 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004355 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004356 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004357 struct lysc_node_container context_node_fake =
4358 {.nodetype = LYS_CONTAINER,
4359 .module = ctx->mod,
4360 .flags = parent ? parent->flags : 0,
4361 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004362 .prev = (struct lysc_node*)&context_node_fake,
4363 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004364 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004365 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004366 int found;
4367 const char *id, *name, *prefix;
4368 size_t prefix_len, name_len;
4369 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004370 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004371 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004372 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004373 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004374 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004375 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004376 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004377 unsigned int actions_index, notifs_index;
4378 struct lysc_notif **notifs = NULL;
4379 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004380
4381 /* search for the grouping definition */
4382 found = 0;
4383 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004384 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004385 if (prefix) {
4386 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4387 if (!mod) {
4388 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4389 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4390 return LY_EVALID;
4391 }
4392 } else {
4393 mod = ctx->mod_def;
4394 }
4395 if (mod == ctx->mod_def) {
4396 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004397 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004398 LY_ARRAY_FOR(grp, u) {
4399 if (!strcmp(grp[u].name, name)) {
4400 grp = &grp[u];
4401 found = 1;
4402 break;
4403 }
4404 }
4405 }
4406 }
4407 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004408 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004409 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004410 if (grp) {
4411 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4412 if (!strcmp(grp[u].name, name)) {
4413 grp = &grp[u];
4414 found = 1;
4415 }
4416 }
4417 }
4418 if (!found && mod->parsed->includes) {
4419 /* ... and all the submodules */
4420 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4421 grp = mod->parsed->includes[u].submodule->groupings;
4422 if (grp) {
4423 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4424 if (!strcmp(grp[v].name, name)) {
4425 grp = &grp[v];
4426 found = 1;
4427 }
4428 }
4429 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004430 }
4431 }
4432 }
4433 if (!found) {
4434 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4435 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4436 return LY_EVALID;
4437 }
4438
4439 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4440 grp_stack_count = ctx->groupings.count;
4441 ly_set_add(&ctx->groupings, (void*)grp, 0);
4442 if (grp_stack_count == ctx->groupings.count) {
4443 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4445 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4446 return LY_EVALID;
4447 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004448 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4449 /* remember that the grouping is instantiated to avoid its standalone validation */
4450 grp->flags |= LYS_USED_GRP;
4451 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004452
4453 /* switch context's mod_def */
4454 mod_old = ctx->mod_def;
4455 ctx->mod_def = mod;
4456
4457 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004458 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 +01004459
Radek Krejcifc11bd72019-04-11 16:00:05 +02004460 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004461 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004462 /* 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 +02004463 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 +01004464
4465 /* some preparation for applying refines */
4466 if (grp->data == node_p) {
4467 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004468 if (parent) {
4469 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4470 } else if (ctx->mod->compiled->data) {
4471 child = ctx->mod->compiled->data;
4472 } else {
4473 child = NULL;
4474 }
4475 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004476 }
4477 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004478 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004479 LY_LIST_FOR(context_node_fake.child, child) {
4480 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004481
Radek Krejcifc11bd72019-04-11 16:00:05 +02004482 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004483 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004484 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004485 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004486 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004487 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004488 when_shared = *when;
4489 } else {
4490 ++when_shared->refcount;
4491 (*when) = when_shared;
4492 }
4493 }
Radek Krejci01342af2019-01-03 15:18:08 +01004494 }
4495 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004496 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004497 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004498 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004499 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 +01004500 }
4501
Radek Krejcifc11bd72019-04-11 16:00:05 +02004502 /* compile actions */
4503 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4504 if (actions) {
4505 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004506 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004507 if (*actions && (uses_p->augments || uses_p->refines)) {
4508 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4509 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4510 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4511 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4512 }
4513 }
4514
4515 /* compile notifications */
4516 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4517 if (notifs) {
4518 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004519 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004520 if (*notifs && (uses_p->augments || uses_p->refines)) {
4521 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4522 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4523 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4524 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4525 }
4526 }
4527
4528
Radek Krejci3641f562019-02-13 15:38:40 +01004529 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004530 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004531 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004532 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004533 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004534
Radek Krejcif0089082019-01-07 16:42:01 +01004535 /* reload previous context's mod_def */
4536 ctx->mod_def = mod_old;
4537
Radek Krejci76b3e962018-12-14 17:01:25 +01004538 /* apply refine */
4539 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004540 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 +01004541 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004542 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004543 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004544
4545 /* default value */
4546 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004547 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004548 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4549 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4550 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004551 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004552 }
4553 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4555 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4556 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004557 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004558 }
4559 if (node->nodetype == LYS_LEAF) {
4560 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4561 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004562 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004563 /* TODO check the default value according to type */
4564 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004565 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4567 "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 +02004568 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004569 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004570 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4571 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4572 }
4573 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004574 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004575 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 +01004576 LY_ARRAY_FOR(rfn->dflts, u) {
4577 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4578 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4579 }
4580 /* TODO check the default values according to type */
4581 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004582 if (((struct lysc_node_choice*)node)->dflt) {
4583 /* unset LYS_SET_DFLT from the current default case */
4584 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4585 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004586 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 +01004587 }
4588 }
4589
Radek Krejci12fb9142019-01-08 09:45:30 +01004590 /* description */
4591 if (rfn->dsc) {
4592 FREE_STRING(ctx->ctx, node->dsc);
4593 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4594 }
4595
4596 /* reference */
4597 if (rfn->ref) {
4598 FREE_STRING(ctx->ctx, node->ref);
4599 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4600 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004601
4602 /* config */
4603 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004604 if (!flags) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004605 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004606 } else {
4607 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4608 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4609 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004610 }
4611
4612 /* mandatory */
4613 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004614 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004615 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004616
4617 /* presence */
4618 if (rfn->presence) {
4619 if (node->nodetype != LYS_CONTAINER) {
4620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4621 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4622 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004623 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004624 }
4625 node->flags |= LYS_PRESENCE;
4626 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004627
4628 /* must */
4629 if (rfn->musts) {
4630 switch (node->nodetype) {
4631 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004632 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 +01004633 break;
4634 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004635 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 +01004636 break;
4637 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004638 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 +01004639 break;
4640 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004641 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 +01004642 break;
4643 case LYS_ANYXML:
4644 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004645 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 +01004646 break;
4647 default:
4648 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4649 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4650 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004651 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004652 }
4653 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004654
4655 /* min/max-elements */
4656 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4657 switch (node->nodetype) {
4658 case LYS_LEAFLIST:
4659 if (rfn->flags & LYS_SET_MAX) {
4660 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4661 }
4662 if (rfn->flags & LYS_SET_MIN) {
4663 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004664 if (rfn->min) {
4665 node->flags |= LYS_MAND_TRUE;
4666 lys_compile_mandatory_parents(node->parent, 1);
4667 } else {
4668 node->flags &= ~LYS_MAND_TRUE;
4669 lys_compile_mandatory_parents(node->parent, 0);
4670 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004671 }
4672 break;
4673 case LYS_LIST:
4674 if (rfn->flags & LYS_SET_MAX) {
4675 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4676 }
4677 if (rfn->flags & LYS_SET_MIN) {
4678 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004679 if (rfn->min) {
4680 node->flags |= LYS_MAND_TRUE;
4681 lys_compile_mandatory_parents(node->parent, 1);
4682 } else {
4683 node->flags &= ~LYS_MAND_TRUE;
4684 lys_compile_mandatory_parents(node->parent, 0);
4685 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004686 }
4687 break;
4688 default:
4689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4690 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4691 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004692 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004693 }
4694 }
Radek Krejcif0089082019-01-07 16:42:01 +01004695
4696 /* if-feature */
4697 if (rfn->iffeatures) {
4698 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004699 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004700 }
Radek Krejci01342af2019-01-03 15:18:08 +01004701 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004702
Radek Krejcif2271f12019-01-07 16:42:23 +01004703 /* do some additional checks of the changed nodes when all the refines are applied */
4704 for (u = 0; u < refined.count; ++u) {
4705 node = (struct lysc_node*)refined.objs[u];
4706 rfn = &uses_p->refines[u];
4707
4708 /* check possible conflict with default value (default added, mandatory left true) */
4709 if ((node->flags & LYS_MAND_TRUE) &&
4710 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4711 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4713 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004714 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004715 }
4716
4717 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4718 if (node->nodetype == LYS_LIST) {
4719 min = ((struct lysc_node_list*)node)->min;
4720 max = ((struct lysc_node_list*)node)->max;
4721 } else {
4722 min = ((struct lysc_node_leaflist*)node)->min;
4723 max = ((struct lysc_node_leaflist*)node)->max;
4724 }
4725 if (min > max) {
4726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4727 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4728 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004729 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004730 }
4731 }
4732 }
4733
Radek Krejcie86bf772018-12-14 11:39:53 +01004734 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004735
4736cleanup:
4737 /* fix connection of the children nodes from fake context node back into the parent */
4738 if (context_node_fake.child) {
4739 context_node_fake.child->prev = child;
4740 }
4741 LY_LIST_FOR(context_node_fake.child, child) {
4742 child->parent = parent;
4743 }
4744
4745 if (uses_p->augments || uses_p->refines) {
4746 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004747 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004748 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4749 LY_ARRAY_FREE(context_node_fake.actions);
4750 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004751 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004752 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4753 LY_ARRAY_FREE(context_node_fake.notifs);
4754 }
4755 }
4756
Radek Krejcie86bf772018-12-14 11:39:53 +01004757 /* reload previous context's mod_def */
4758 ctx->mod_def = mod_old;
4759 /* remove the grouping from the stack for circular groupings dependency check */
4760 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4761 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004762 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004763 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004764
4765 return ret;
4766}
4767
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004768/**
4769 * @brief Validate groupings that were defined but not directly used in the schema itself.
4770 *
4771 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
4772 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
4773 */
4774static LY_ERR
4775lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
4776{
4777 LY_ERR ret;
4778 struct lysp_node_uses fake_uses = {
4779 .parent = node_p,
4780 .nodetype = LYS_USES,
4781 .flags = 0, .next = NULL,
4782 .name = grp->name,
4783 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4784 .refines = NULL, .augments = NULL
4785 };
4786 struct lysc_node_container fake_container = {
4787 .nodetype = LYS_CONTAINER,
4788 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
4789 .module = ctx->mod,
4790 .sp = NULL, .parent = NULL, .next = NULL,
4791 .prev = (struct lysc_node*)&fake_container,
4792 .name = "fake",
4793 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
4794 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4795 };
4796
4797 if (grp->parent) {
4798 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4799 }
4800 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
4801
4802 /* cleanup */
4803 lysc_node_container_free(ctx->ctx, &fake_container);
4804
4805 return ret;
4806}
Radek Krejcife909632019-02-12 15:34:42 +01004807
Radek Krejcie86bf772018-12-14 11:39:53 +01004808/**
Radek Krejcia3045382018-11-22 14:30:31 +01004809 * @brief Compile parsed schema node information.
4810 * @param[in] ctx Compile context
4811 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01004812 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4813 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4814 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004815 * @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).
4816 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004817 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4818 */
Radek Krejci19a96102018-11-15 13:38:09 +01004819static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004820lys_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 +01004821{
4822 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004823 struct lysc_node *node;
4824 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004825 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004826 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004827 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01004828
4829 switch (node_p->nodetype) {
4830 case LYS_CONTAINER:
4831 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4832 node_compile_spec = lys_compile_node_container;
4833 break;
4834 case LYS_LEAF:
4835 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4836 node_compile_spec = lys_compile_node_leaf;
4837 break;
4838 case LYS_LIST:
4839 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004840 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004841 break;
4842 case LYS_LEAFLIST:
4843 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004844 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004845 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004846 case LYS_CHOICE:
4847 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004848 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004849 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004850 case LYS_ANYXML:
4851 case LYS_ANYDATA:
4852 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004853 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004854 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004855 case LYS_USES:
Radek Krejciec4da802019-05-02 13:02:41 +02004856 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004857 default:
4858 LOGINT(ctx->ctx);
4859 return LY_EINT;
4860 }
4861 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4862 node->nodetype = node_p->nodetype;
4863 node->module = ctx->mod;
4864 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004865 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004866
4867 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02004868 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004869 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004870 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02004871 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4872 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004873 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004874 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004875 node->flags |= LYS_CONFIG_R;
4876 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004877 /* config not explicitely set, inherit it from parent */
4878 if (parent) {
4879 node->flags |= parent->flags & LYS_CONFIG_MASK;
4880 } else {
4881 /* default is config true */
4882 node->flags |= LYS_CONFIG_W;
4883 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004884 } else {
4885 /* config set explicitely */
4886 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004887 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004888 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4889 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4890 "Configuration node cannot be child of any state data node.");
4891 goto error;
4892 }
Radek Krejci19a96102018-11-15 13:38:09 +01004893
Radek Krejcia6d57732018-11-29 13:40:37 +01004894 /* *list ordering */
4895 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4896 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004897 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02004898 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
4899 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01004900 node->flags &= ~LYS_ORDBY_MASK;
4901 node->flags |= LYS_ORDBY_SYSTEM;
4902 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4903 /* default ordering is system */
4904 node->flags |= LYS_ORDBY_SYSTEM;
4905 }
4906 }
4907
Radek Krejci19a96102018-11-15 13:38:09 +01004908 /* status - it is not inherited by specification, but it does not make sense to have
4909 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004910 if (!parent || parent->nodetype != LYS_CHOICE) {
4911 /* in case of choice/case's children, postpone the check to the moment we know if
4912 * 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 +01004913 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 +01004914 }
4915
Radek Krejciec4da802019-05-02 13:02:41 +02004916 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004917 node->sp = node_p;
4918 }
4919 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004920 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4921 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004922 if (node_p->when) {
4923 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004924 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004925 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004926 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004927 }
Radek Krejciec4da802019-05-02 13:02:41 +02004928 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
4929 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004930
4931 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02004932 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01004933
Radek Krejcife909632019-02-12 15:34:42 +01004934 /* inherit LYS_MAND_TRUE in parent containers */
4935 if (node->flags & LYS_MAND_TRUE) {
4936 lys_compile_mandatory_parents(parent, 1);
4937 }
4938
Radek Krejci19a96102018-11-15 13:38:09 +01004939 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004940 if (parent) {
4941 if (parent->nodetype == LYS_CHOICE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004942 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01004943 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004944 if (uses_status) {
4945
4946 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004947 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004948 * it directly gets the same status flags as the choice;
4949 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004950 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004951 node->parent = (struct lysc_node*)cs;
4952 } else { /* other than choice */
4953 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004954 }
Radek Krejciec4da802019-05-02 13:02:41 +02004955 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci19a96102018-11-15 13:38:09 +01004956 } else {
4957 /* top-level element */
4958 if (!ctx->mod->compiled->data) {
4959 ctx->mod->compiled->data = node;
4960 } else {
4961 /* insert at the end of the module's top-level nodes list */
4962 ctx->mod->compiled->data->prev->next = node;
4963 node->prev = ctx->mod->compiled->data->prev;
4964 ctx->mod->compiled->data->prev = node;
4965 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004966 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4967 ctx->mod->compiled->notifs, node->name, node)) {
4968 return LY_EVALID;
4969 }
Radek Krejci19a96102018-11-15 13:38:09 +01004970 }
4971
4972 return LY_SUCCESS;
4973
4974error:
4975 lysc_node_free(ctx->ctx, node);
4976 return ret;
4977}
4978
Radek Krejciccd20f12019-02-15 14:12:27 +01004979static void
4980lysc_disconnect(struct lysc_node *node)
4981{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004982 struct lysc_node *parent, *child, *prev = NULL, *next;
4983 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004984 int remove_cs = 0;
4985
4986 parent = node->parent;
4987
4988 /* parent's first child */
4989 if (!parent) {
4990 return;
4991 }
4992 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004993 cs = (struct lysc_node_case*)node;
4994 } else if (parent->nodetype == LYS_CASE) {
4995 /* disconnecting some node in a case */
4996 cs = (struct lysc_node_case*)parent;
4997 parent = cs->parent;
4998 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4999 if (child == node) {
5000 if (cs->child == child) {
5001 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5002 /* case with a single child -> remove also the case */
5003 child->parent = NULL;
5004 remove_cs = 1;
5005 } else {
5006 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005007 }
5008 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005009 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005010 }
5011 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005012 if (!remove_cs) {
5013 cs = NULL;
5014 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005015 } else if (lysc_node_children(parent, node->flags) == node) {
5016 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005017 }
5018
5019 if (cs) {
5020 if (remove_cs) {
5021 /* cs has only one child which is being also removed */
5022 lysc_disconnect((struct lysc_node*)cs);
5023 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5024 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005025 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5026 /* default case removed */
5027 ((struct lysc_node_choice*)parent)->dflt = NULL;
5028 }
5029 if (((struct lysc_node_choice*)parent)->cases == cs) {
5030 /* first case removed */
5031 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5032 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005033 if (cs->child) {
5034 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5035 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5036 prev = cs->child->prev;
5037 } /* else all the children are under a single case */
5038 LY_LIST_FOR_SAFE(cs->child, next, child) {
5039 if (child->parent != (struct lysc_node*)cs) {
5040 break;
5041 }
5042 lysc_node_free(node->module->ctx, child);
5043 }
5044 if (prev) {
5045 if (prev->next) {
5046 prev->next = child;
5047 }
5048 if (child) {
5049 child->prev = prev;
5050 } else {
5051 /* link from the first child under the cases */
5052 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5053 }
5054 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005055 }
5056 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005057 }
5058
5059 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005060 if (node->prev->next) {
5061 node->prev->next = node->next;
5062 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005063 if (node->next) {
5064 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005065 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005066 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005067 if (child) {
5068 child->prev = node->prev;
5069 }
5070 } else if (((struct lysc_node_choice*)parent)->cases) {
5071 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005072 }
5073}
5074
5075LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005076lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005077{
5078 LY_ERR ret = LY_EVALID;
5079 struct ly_set devs_p = {0};
5080 struct ly_set targets = {0};
5081 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005082 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005083 struct lysc_action *rpcs;
5084 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005085 struct lysp_deviation *dev;
5086 struct lysp_deviate *d, **dp_new;
5087 struct lysp_deviate_add *d_add;
5088 struct lysp_deviate_del *d_del;
5089 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005090 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005091 struct lysc_deviation {
5092 const char *nodeid;
5093 struct lysc_node *target; /* target node of the deviation */
5094 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005095 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005096 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5097 } **devs = NULL;
5098 int i;
5099 size_t prefix_len, name_len;
5100 const char *prefix, *name, *nodeid;
5101 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005102 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005103 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005104
5105 /* get all deviations from the module and all its submodules ... */
5106 LY_ARRAY_FOR(mod_p->deviations, u) {
5107 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5108 }
5109 LY_ARRAY_FOR(mod_p->includes, v) {
5110 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5111 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5112 }
5113 }
5114 if (!devs_p.count) {
5115 /* nothing to do */
5116 return LY_SUCCESS;
5117 }
5118
5119 /* ... and group them by the target node */
5120 devs = calloc(devs_p.count, sizeof *devs);
5121 for (u = 0; u < devs_p.count; ++u) {
5122 dev = devs_p.objs[u];
5123
5124 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005125 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5126 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005127 if (target->nodetype == LYS_ACTION) {
5128 /* move the target pointer to input/output to make them different from the action and
5129 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5130 * back to the RPC/action node due to a better compatibility and decision code in this function.
5131 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5132 if (flags & LYSC_OPT_RPC_INPUT) {
5133 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5134 flags |= LYSC_OPT_INTERNAL;
5135 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5136 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5137 flags |= LYSC_OPT_INTERNAL;
5138 }
5139 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005140 /* insert into the set of targets with duplicity detection */
5141 i = ly_set_add(&targets, target, 0);
5142 if (!devs[i]) {
5143 /* new record */
5144 devs[i] = calloc(1, sizeof **devs);
5145 devs[i]->target = target;
5146 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005147 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005148 }
5149 /* add deviates into the deviation's list of deviates */
5150 for (d = dev->deviates; d; d = d->next) {
5151 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5152 *dp_new = d;
5153 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5154 devs[i]->not_supported = 1;
5155 }
5156 }
5157 }
5158
5159 /* MACROS for deviates checking */
5160#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5161 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
5162 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
5163 goto cleanup; \
5164 }
5165
5166#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5167 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
5168 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
5169 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
5170 goto cleanup; \
5171 }
5172
5173#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005174 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005175 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5176 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
5177 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5178 goto cleanup; \
5179 }
5180
Radek Krejci551b12c2019-02-19 16:11:21 +01005181#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5182 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5184 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
5185 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5186 goto cleanup; \
5187 }
5188
Radek Krejciccd20f12019-02-15 14:12:27 +01005189#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5190 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5192 goto cleanup; \
5193 }
5194
Radek Krejci551b12c2019-02-19 16:11:21 +01005195#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5196 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5197 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5198 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5199 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5200 goto cleanup; \
5201 }
5202
Radek Krejciccd20f12019-02-15 14:12:27 +01005203#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5204 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5205 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5206 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5207 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5208 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5209 goto cleanup; \
5210 } \
5211 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5212 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5213
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005214#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5215 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5216 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5217 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5218 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 +01005219 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005220 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5222 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005223 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005224 goto cleanup; \
5225 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005226 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5227 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5228 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5229 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5230 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005231 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005232 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5233 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5234 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005235 }
5236
5237 /* apply deviations */
5238 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005239 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5240 /* fix the target pointer in case of RPC's/action's input/output */
5241 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5242 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5243 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5244 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5245 }
5246 }
5247
Radek Krejciccd20f12019-02-15 14:12:27 +01005248 /* not-supported */
5249 if (devs[u]->not_supported) {
5250 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5251 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5252 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5253 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005254
5255#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5256 if (devs[u]->target->parent) { \
5257 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5258 } else { \
5259 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5260 } \
5261 LY_ARRAY_FOR(ARRAY, x) { \
5262 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5263 } \
5264 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5265 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5266 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5267 LY_ARRAY_DECREMENT(ARRAY); \
5268 }
5269
Radek Krejcif538ce52019-03-05 10:46:14 +01005270 if (devs[u]->target->nodetype == LYS_ACTION) {
5271 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5272 /* remove RPC's/action's input */
5273 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5274 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005275 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5276 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005277 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5278 /* remove RPC's/action's output */
5279 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5280 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005281 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5282 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005283 } else {
5284 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005285 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005286 }
5287 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005288 /* remove Notification */
5289 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005290 } else {
5291 /* remove the target node */
5292 lysc_disconnect(devs[u]->target);
5293 lysc_node_free(ctx->ctx, devs[u]->target);
5294 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005295
5296 continue;
5297 }
5298
5299 /* list of deviates (not-supported is not present in the list) */
5300 LY_ARRAY_FOR(devs[u]->deviates, v) {
5301 d = devs[u]->deviates[v];
5302
5303 switch (d->mod) {
5304 case LYS_DEV_ADD:
5305 d_add = (struct lysp_deviate_add*)d;
5306 /* [units-stmt] */
5307 if (d_add->units) {
5308 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5309 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5310
5311 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5312 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5313 }
5314
5315 /* *must-stmt */
5316 if (d_add->musts) {
5317 switch (devs[u]->target->nodetype) {
5318 case LYS_CONTAINER:
5319 case LYS_LIST:
5320 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005321 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005322 break;
5323 case LYS_LEAF:
5324 case LYS_LEAFLIST:
5325 case LYS_ANYDATA:
5326 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005327 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005328 break;
5329 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005330 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005331 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005332 break;
5333 case LYS_ACTION:
5334 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5335 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005336 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005337 break;
5338 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5339 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005340 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005341 break;
5342 }
5343 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005344 default:
5345 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005346 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005347 goto cleanup;
5348 }
5349 }
5350
5351 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005352 if (d_add->uniques) {
5353 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5354 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5355 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005356
5357 /* *default-stmt */
5358 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005359 switch (devs[u]->target->nodetype) {
5360 case LYS_LEAF:
5361 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5362 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5363 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5364 /* first, remove the default value taken from the type */
5365 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5366 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5367 }
5368 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005369 /* mark the new default values as leaf's own */
5370 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005371 break;
5372 case LYS_LEAFLIST:
5373 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5374 /* first, remove the default value taken from the type */
5375 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5376 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5377 }
5378 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5379 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5380 }
5381 /* add new default value(s) */
5382 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5383 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5384 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5385 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5386 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5387 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5388 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005389 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005390 devs[u]->target->flags |= LYS_SET_DFLT;
5391 break;
5392 case LYS_CHOICE:
5393 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5394 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5395 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5396 * to allow making the default case even the augmented case from the deviating module */
5397 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5398 (struct lysc_node_choice*)devs[u]->target)) {
5399 goto cleanup;
5400 }
5401 break;
5402 default:
5403 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005404 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005405 goto cleanup;
5406 }
5407 }
5408
5409 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005410 if (d_add->flags & LYS_CONFIG_MASK) {
5411 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5413 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5414 goto cleanup;
5415 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005416 if (devs[u]->flags) {
5417 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5418 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5419 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005420 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5422 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5423 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5424 goto cleanup;
5425 }
5426 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5427 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005428
5429 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005430 if (d_add->flags & LYS_MAND_MASK) {
5431 if (devs[u]->target->flags & LYS_MAND_MASK) {
5432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5433 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5434 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5435 goto cleanup;
5436 }
5437 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5438 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005439
5440 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005441 if (d_add->flags & LYS_SET_MIN) {
5442 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5443 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5444 /* change value */
5445 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5446 } else if (devs[u]->target->nodetype == LYS_LIST) {
5447 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5448 /* change value */
5449 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5450 } else {
5451 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5452 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5453 goto cleanup;
5454 }
5455 if (d_add->min) {
5456 devs[u]->target->flags |= LYS_MAND_TRUE;
5457 }
5458 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005459
5460 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005461 if (d_add->flags & LYS_SET_MAX) {
5462 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5463 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5464 /* change value */
5465 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5466 } else if (devs[u]->target->nodetype == LYS_LIST) {
5467 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5468 /* change value */
5469 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5470 } else {
5471 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5472 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5473 goto cleanup;
5474 }
5475 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005476
5477 break;
5478 case LYS_DEV_DELETE:
5479 d_del = (struct lysp_deviate_del*)d;
5480
5481 /* [units-stmt] */
5482 if (d_del->units) {
5483 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5484 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5485 }
5486
5487 /* *must-stmt */
5488 if (d_del->musts) {
5489 switch (devs[u]->target->nodetype) {
5490 case LYS_CONTAINER:
5491 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005492 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005493 break;
5494 case LYS_LEAF:
5495 case LYS_LEAFLIST:
5496 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005497 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005498 break;
5499 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005500 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005501 break;
5502 case LYS_ACTION:
5503 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5504 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5505 break;
5506 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5507 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5508 break;
5509 }
5510 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005511 default:
5512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005513 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005514 goto cleanup;
5515 }
5516 }
5517
5518 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005519 if (d_del->uniques) {
5520 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5521 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5522 LY_ARRAY_FOR(d_del->uniques, x) {
5523 LY_ARRAY_FOR(list->uniques, z) {
5524 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5525 nodeid = strpbrk(name, " \t\n");
5526 if (nodeid) {
5527 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5528 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5529 break;
5530 }
5531 while (isspace(*nodeid)) {
5532 ++nodeid;
5533 }
5534 } else {
5535 if (strcmp(name, list->uniques[z][y]->name)) {
5536 break;
5537 }
5538 }
5539 }
5540 if (!name) {
5541 /* complete match - remove the unique */
5542 LY_ARRAY_DECREMENT(list->uniques);
5543 LY_ARRAY_FREE(list->uniques[z]);
5544 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5545 --z;
5546 break;
5547 }
5548 }
5549 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5551 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5552 devs[u]->nodeid, d_del->uniques[x]);
5553 goto cleanup;
5554 }
5555 }
5556 if (!LY_ARRAY_SIZE(list->uniques)) {
5557 LY_ARRAY_FREE(list->uniques);
5558 list->uniques = NULL;
5559 }
5560 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005561
5562 /* *default-stmt */
5563 if (d_del->dflts) {
5564 switch (devs[u]->target->nodetype) {
5565 case LYS_LEAF:
5566 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5567 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5568 dflt, "deleting", "default", d_del->dflts[0]);
5569
5570 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005571 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005572 break;
5573 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005574 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005575 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5576 devs[u]->target->flags &= ~LYS_SET_DFLT;
5577 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005578 break;
5579 case LYS_CHOICE:
5580 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5581 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5582 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02005583 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005584 if (prefix) {
5585 /* use module prefixes from the deviation module to match the module of the default case */
5586 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5587 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5588 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5589 "The prefix does not match any imported module of the deviation module.",
5590 devs[u]->nodeid, d_del->dflts[0]);
5591 goto cleanup;
5592 }
5593 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5594 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5595 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5596 "The prefix does not match the default case's module.",
5597 devs[u]->nodeid, d_del->dflts[0]);
5598 goto cleanup;
5599 }
5600 }
5601 /* else {
5602 * strictly, the default prefix would point to the deviation module, but the value should actually
5603 * match the default string in the original module (usually unprefixed), so in this case we do not check
5604 * the module of the default case, just matching its name */
5605 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5607 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5608 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5609 goto cleanup;
5610 }
5611 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5612 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5613 break;
5614 default:
5615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005616 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005617 goto cleanup;
5618 }
5619 }
5620
5621 break;
5622 case LYS_DEV_REPLACE:
5623 d_rpl = (struct lysp_deviate_rpl*)d;
5624
5625 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005626 if (d_rpl->type) {
5627 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5628 /* type is mandatory, so checking for its presence is not necessary */
5629 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejciec4da802019-05-02 13:02:41 +02005630 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 +01005631 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005632
5633 /* [units-stmt] */
5634 if (d_rpl->units) {
5635 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5636 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5637 units, "replacing", "units", d_rpl->units);
5638
5639 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5640 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5641 }
5642
5643 /* [default-stmt] */
5644 if (d_rpl->dflt) {
5645 switch (devs[u]->target->nodetype) {
5646 case LYS_LEAF:
5647 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5648 dflt, "replacing", "default", d_rpl->dflt);
5649
5650 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5651 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5652 break;
5653 case LYS_CHOICE:
5654 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5655 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5656 (struct lysc_node_choice*)devs[u]->target)) {
5657 goto cleanup;
5658 }
5659 break;
5660 default:
5661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005662 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005663 goto cleanup;
5664 }
5665 }
5666
5667 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005668 if (d_rpl->flags & LYS_CONFIG_MASK) {
5669 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5671 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5672 goto cleanup;
5673 }
5674 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5676 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5677 goto cleanup;
5678 }
5679 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5680 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005681
5682 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005683 if (d_rpl->flags & LYS_MAND_MASK) {
5684 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5686 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5687 goto cleanup;
5688 }
5689 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5690 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005691
5692 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005693 if (d_rpl->flags & LYS_SET_MIN) {
5694 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5695 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5696 /* change value */
5697 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5698 } else if (devs[u]->target->nodetype == LYS_LIST) {
5699 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5700 /* change value */
5701 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5702 } else {
5703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5704 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5705 goto cleanup;
5706 }
5707 if (d_rpl->min) {
5708 devs[u]->target->flags |= LYS_MAND_TRUE;
5709 }
5710 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005711
5712 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005713 if (d_rpl->flags & LYS_SET_MAX) {
5714 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5715 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5716 /* change value */
5717 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5718 } else if (devs[u]->target->nodetype == LYS_LIST) {
5719 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5720 /* change value */
5721 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5722 } else {
5723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5724 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5725 goto cleanup;
5726 }
5727 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005728
5729 break;
5730 default:
5731 LOGINT(ctx->ctx);
5732 goto cleanup;
5733 }
5734 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005735
Radek Krejci33f72892019-02-21 10:36:58 +01005736 /* final check when all deviations of a single target node are applied */
5737
Radek Krejci551b12c2019-02-19 16:11:21 +01005738 /* check min-max compatibility */
5739 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5740 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5741 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5742 } else if (devs[u]->target->nodetype == LYS_LIST) {
5743 min = ((struct lysc_node_list*)devs[u]->target)->min;
5744 max = ((struct lysc_node_list*)devs[u]->target)->max;
5745 } else {
5746 min = max = 0;
5747 }
5748 if (min > max) {
5749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5750 "after deviation (%s): min value %u is bigger than max value %u.",
5751 devs[u]->nodeid, min, max);
5752 goto cleanup;
5753 }
5754
5755 /* check mandatory - default compatibility */
5756 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5757 && (devs[u]->target->flags & LYS_SET_DFLT)
5758 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5760 "Invalid deviation (%s) combining default value and mandatory %s.",
5761 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5762 goto cleanup;
5763 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5764 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5765 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5766 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5767 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5768 goto cleanup;
5769 }
5770 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5771 /* mandatory node under a default case */
5772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5773 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5774 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5775 goto cleanup;
5776 }
Radek Krejci33f72892019-02-21 10:36:58 +01005777
5778 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005779 }
5780
5781 ret = LY_SUCCESS;
5782
5783cleanup:
5784 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5785 LY_ARRAY_FREE(devs[u]->deviates);
5786 free(devs[u]);
5787 }
5788 free(devs);
5789 ly_set_erase(&targets, NULL);
5790 ly_set_erase(&devs_p, NULL);
5791
5792 return ret;
5793}
5794
Radek Krejcib56c7502019-02-13 14:19:54 +01005795/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005796 * @brief Compile the given YANG submodule into the main module.
5797 * @param[in] ctx Compile context
5798 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01005799 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5800 */
5801LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005802lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01005803{
5804 unsigned int u;
5805 LY_ERR ret = LY_SUCCESS;
5806 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005807 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005808 struct lysc_module *mainmod = ctx->mod->compiled;
5809
Radek Krejci0af46292019-01-11 16:02:31 +01005810 if (!mainmod->mod->off_features) {
5811 /* features are compiled directly into the compiled module structure,
5812 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5813 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci693262f2019-04-29 15:23:20 +02005814 ret = lys_feature_precompile(ctx->ctx, ctx->mod, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01005815 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5816 LY_CHECK_GOTO(ret, error);
5817 }
5818
Radek Krejciec4da802019-05-02 13:02:41 +02005819 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005820
Radek Krejci8cce8532019-03-05 11:27:45 +01005821 /* TODO data nodes */
5822
Radek Krejciec4da802019-05-02 13:02:41 +02005823 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
5824 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01005825
Radek Krejcid05cbd92018-12-05 14:26:40 +01005826error:
5827 return ret;
5828}
5829
Radek Krejci19a96102018-11-15 13:38:09 +01005830LY_ERR
5831lys_compile(struct lys_module *mod, int options)
5832{
5833 struct lysc_ctx ctx = {0};
5834 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005835 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005836 struct lysp_module *sp;
5837 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005838 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005839 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01005840 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005841 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005842 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005843
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005844 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005845
5846 if (!mod->implemented) {
5847 /* just imported modules are not compiled */
5848 return LY_SUCCESS;
5849 }
5850
Radek Krejci19a96102018-11-15 13:38:09 +01005851 sp = mod->parsed;
5852
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005853 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005854 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005855 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02005856 ctx.options = options;
Radek Krejci19a96102018-11-15 13:38:09 +01005857
5858 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005859 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5860 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005861
Radek Krejciec4da802019-05-02 13:02:41 +02005862 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005863 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005864 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005865 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5866 }
Radek Krejci0af46292019-01-11 16:02:31 +01005867 if (mod->off_features) {
5868 /* there is already precompiled array of features */
5869 mod_c->features = mod->off_features;
5870 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005871 } else {
5872 /* features are compiled directly into the compiled module structure,
5873 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci693262f2019-04-29 15:23:20 +02005874 ret = lys_feature_precompile(ctx.ctx, ctx.mod, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005875 LY_CHECK_GOTO(ret, error);
5876 }
5877 /* finish feature compilation, not only for the main module, but also for the submodules.
5878 * Due to possible forward references, it must be done when all the features (including submodules)
5879 * are present. */
5880 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005881 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005882 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5883 }
5884 LY_ARRAY_FOR(sp->includes, v) {
5885 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005886 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005887 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5888 }
5889 }
5890
Radek Krejciec4da802019-05-02 13:02:41 +02005891 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005892 if (sp->identities) {
5893 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5894 }
5895
Radek Krejci95710c92019-02-11 15:49:55 +01005896 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005897 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02005898 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005899 LY_CHECK_GOTO(ret, error);
5900 }
Radek Krejci95710c92019-02-11 15:49:55 +01005901
Radek Krejciec4da802019-05-02 13:02:41 +02005902 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
5903 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01005904
Radek Krejci95710c92019-02-11 15:49:55 +01005905 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005906 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005907 LY_CHECK_GOTO(ret, error);
5908 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005909 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005910 LY_CHECK_GOTO(ret, error);
5911 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005912
5913 /* deviations */
Radek Krejciec4da802019-05-02 13:02:41 +02005914 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01005915 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005916
Radek Krejciec4da802019-05-02 13:02:41 +02005917 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005918
Radek Krejcia3045382018-11-22 14:30:31 +01005919 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005920 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5921 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5922 * 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 +01005923 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005924 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005925 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5926 if (type->basetype == LY_TYPE_LEAFREF) {
5927 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005928 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 +01005929 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005930 } else if (type->basetype == LY_TYPE_UNION) {
5931 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5932 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5933 /* validate the path */
5934 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5935 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5936 LY_CHECK_GOTO(ret, error);
5937 }
5938 }
Radek Krejcia3045382018-11-22 14:30:31 +01005939 }
5940 }
5941 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005942 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005943 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005944 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5945 if (type->basetype == LY_TYPE_LEAFREF) {
5946 /* store pointer to the real type */
5947 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5948 typeiter->basetype == LY_TYPE_LEAFREF;
5949 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5950 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005951 } else if (type->basetype == LY_TYPE_UNION) {
5952 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5953 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5954 /* store pointer to the real type */
5955 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5956 typeiter->basetype == LY_TYPE_LEAFREF;
5957 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5958 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5959 }
5960 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005961 }
5962 }
5963 }
Radek Krejciec4da802019-05-02 13:02:41 +02005964
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005965 /* validate non-instantiated groupings from the parsed schema,
5966 * without it we would accept even the schemas with invalid grouping specification */
5967 ctx.options |= LYSC_OPT_GROUPING;
5968 LY_ARRAY_FOR(sp->groupings, u) {
5969 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
5970 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
5971 }
5972 }
5973 LY_LIST_FOR(sp->data, node_p) {
5974 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
5975 LY_ARRAY_FOR(grps, u) {
5976 if (!(grps[u].flags & LYS_USED_GRP)) {
5977 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
5978 }
5979 }
5980 }
5981
Radek Krejcia3045382018-11-22 14:30:31 +01005982 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005983 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02005984 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005985 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005986
Radek Krejciec4da802019-05-02 13:02:41 +02005987 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01005988 lysp_module_free(mod->parsed);
5989 ((struct lys_module*)mod)->parsed = NULL;
5990 }
5991
Radek Krejciec4da802019-05-02 13:02:41 +02005992 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01005993 /* remove flag of the modules implemented by dependency */
5994 for (u = 0; u < ctx.ctx->list.count; ++u) {
5995 m = ctx.ctx->list.objs[u];
5996 if (m->implemented == 2) {
5997 m->implemented = 1;
5998 }
5999 }
6000 }
6001
Radek Krejci19a96102018-11-15 13:38:09 +01006002 ((struct lys_module*)mod)->compiled = mod_c;
6003 return LY_SUCCESS;
6004
6005error:
Radek Krejci95710c92019-02-11 15:49:55 +01006006 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01006007 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006008 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006009 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006010 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006011 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006012 mod->compiled = NULL;
6013
6014 /* revert compilation of modules implemented by dependency */
6015 for (u = 0; u < ctx.ctx->list.count; ++u) {
6016 m = ctx.ctx->list.objs[u];
6017 if (m->implemented == 2) {
6018 /* revert features list to the precompiled state */
6019 lys_feature_precompile_revert(&ctx, m);
6020 /* mark module as imported-only / not-implemented */
6021 m->implemented = 0;
6022 /* free the compiled version of the module */
6023 lysc_module_free(m->compiled, NULL);
6024 m->compiled = NULL;
6025 }
6026 }
6027
Radek Krejci19a96102018-11-15 13:38:09 +01006028 return ret;
6029}