blob: 97eed855be5630df63175722b5b50901a9d066ea [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 *
713 * For bases in identity se backlinks to them from the base identities. For identityref, store
714 * 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 Krejci19a96102018-11-15 13:38:09 +01001005static LY_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) {
1138 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1139 } 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) {
1150 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1151 } 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) {
1162 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1163 } 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) {
1175 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1176 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) {
1188 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1189 } 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) {
1200 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1201 } 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) {
1212 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1213 } 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) {
1226 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1227 } 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
1619 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1620 /* we will add line-end anchoring */
1621 ptr[0] = '(';
1622 ++ptr;
1623 }
1624
1625 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1626 if (orig_ptr[0] == '$') {
1627 ptr += sprintf(ptr, "\\$");
1628 } else if (orig_ptr[0] == '^') {
1629 ptr += sprintf(ptr, "\\^");
1630 } else {
1631 ptr[0] = orig_ptr[0];
1632 ++ptr;
1633 }
1634 }
1635
1636 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1637 ptr += sprintf(ptr, ")$");
1638 } else {
1639 ptr[0] = '\0';
1640 ++ptr;
1641 }
1642
1643 /* substitute Unicode Character Blocks with exact Character Ranges */
1644 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1645 start = ptr - perl_regex;
1646
1647 ptr = strchr(ptr, '}');
1648 if (!ptr) {
1649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1650 pattern, perl_regex + start + 2, "unterminated character property");
1651 free(perl_regex);
1652 return LY_EVALID;
1653 }
1654 end = (ptr - perl_regex) + 1;
1655
1656 /* need more space */
1657 if (end - start < URANGE_LEN) {
1658 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1659 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1660 }
1661
1662 /* find our range */
1663 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1664 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1665 break;
1666 }
1667 }
1668 if (!ublock2urange[idx][0]) {
1669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1670 pattern, perl_regex + start + 5, "unknown block name");
1671 free(perl_regex);
1672 return LY_EVALID;
1673 }
1674
1675 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1676 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1677 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1678 ++count;
1679 }
1680 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1681 --count;
1682 }
1683 }
1684 if (count) {
1685 /* skip brackets */
1686 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1687 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1688 } else {
1689 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1690 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1691 }
1692 }
1693
1694 /* must return 0, already checked during parsing */
Radek Krejci54579462019-04-30 12:47:06 +02001695 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1696 &err_code, &err_offset, NULL);
1697 if (!code_local) {
1698 PCRE2_UCHAR err_msg[256] = {0};
1699 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1701 free(perl_regex);
1702 return LY_EVALID;
1703 }
1704 free(perl_regex);
1705
Radek Krejci54579462019-04-30 12:47:06 +02001706 if (code) {
1707 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001708 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001709 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001710 }
1711
1712 return LY_SUCCESS;
1713
1714#undef URANGE_LEN
1715}
1716
Radek Krejcia3045382018-11-22 14:30:31 +01001717/**
1718 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1719 * @param[in] ctx Compile context.
1720 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001721 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1722 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1723 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1724 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1725 */
Radek Krejci19a96102018-11-15 13:38:09 +01001726static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001727lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001728 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1729{
1730 struct lysc_pattern **pattern;
1731 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001732 LY_ERR ret = LY_SUCCESS;
1733
1734 /* first, copy the patterns from the base type */
1735 if (base_patterns) {
1736 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1737 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1738 }
1739
1740 LY_ARRAY_FOR(patterns_p, u) {
1741 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1742 *pattern = calloc(1, sizeof **pattern);
1743 ++(*pattern)->refcount;
1744
Radek Krejci54579462019-04-30 12:47:06 +02001745 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001746 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001747
1748 if (patterns_p[u].arg[0] == 0x15) {
1749 (*pattern)->inverted = 1;
1750 }
Radek Krejci54579462019-04-30 12:47:06 +02001751 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001752 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1753 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001754 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1755 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02001756 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001757 }
1758done:
1759 return ret;
1760}
1761
Radek Krejcia3045382018-11-22 14:30:31 +01001762/**
1763 * @brief map of the possible restrictions combination for the specific built-in type.
1764 */
Radek Krejci19a96102018-11-15 13:38:09 +01001765static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1766 0 /* LY_TYPE_UNKNOWN */,
1767 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001768 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1769 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1770 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1771 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1772 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001773 LYS_SET_BIT /* LY_TYPE_BITS */,
1774 0 /* LY_TYPE_BOOL */,
1775 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1776 0 /* LY_TYPE_EMPTY */,
1777 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1778 LYS_SET_BASE /* LY_TYPE_IDENT */,
1779 LYS_SET_REQINST /* LY_TYPE_INST */,
1780 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001781 LYS_SET_TYPE /* LY_TYPE_UNION */,
1782 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001783 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001784 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001785 LYS_SET_RANGE /* LY_TYPE_INT64 */
1786};
1787
1788/**
1789 * @brief stringification of the YANG built-in data types
1790 */
1791const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1792 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1793 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001794};
1795
Radek Krejcia3045382018-11-22 14:30:31 +01001796/**
1797 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1798 * @param[in] ctx Compile context.
1799 * @param[in] enums_p Array of the parsed enum structures to compile.
1800 * @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 +01001801 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1802 * @param[out] enums Newly created array of the compiled enums information for the current type.
1803 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1804 */
Radek Krejci19a96102018-11-15 13:38:09 +01001805static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001806lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02001807 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01001808{
1809 LY_ERR ret = LY_SUCCESS;
1810 unsigned int u, v, match;
1811 int32_t value = 0;
1812 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001813 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01001814
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001815 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001816 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1817 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1818 return LY_EVALID;
1819 }
1820
1821 LY_ARRAY_FOR(enums_p, u) {
1822 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1823 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001824 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1825 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02001826 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01001827 if (base_enums) {
1828 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1829 LY_ARRAY_FOR(base_enums, v) {
1830 if (!strcmp(e->name, base_enums[v].name)) {
1831 break;
1832 }
1833 }
1834 if (v == LY_ARRAY_SIZE(base_enums)) {
1835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1836 "Invalid %s - derived type adds new item \"%s\".",
1837 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1838 return LY_EVALID;
1839 }
1840 match = v;
1841 }
1842
1843 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02001844 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01001845 if (enums_p[u].flags & LYS_SET_VALUE) {
1846 e->value = (int32_t)enums_p[u].value;
1847 if (!u || e->value >= value) {
1848 value = e->value + 1;
1849 }
1850 /* check collision with other values */
1851 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1852 if (e->value == (*enums)[v].value) {
1853 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1854 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1855 e->value, e->name, (*enums)[v].name);
1856 return LY_EVALID;
1857 }
1858 }
1859 } else if (base_enums) {
1860 /* inherit the assigned value */
1861 e->value = base_enums[match].value;
1862 if (!u || e->value >= value) {
1863 value = e->value + 1;
1864 }
1865 } else {
1866 /* assign value automatically */
1867 if (u && value == INT32_MIN) {
1868 /* counter overflow */
1869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1870 "Invalid enumeration - it is not possible to auto-assign enum value for "
1871 "\"%s\" since the highest value is already 2147483647.", e->name);
1872 return LY_EVALID;
1873 }
1874 e->value = value++;
1875 }
1876 } else { /* LY_TYPE_BITS */
1877 if (enums_p[u].flags & LYS_SET_VALUE) {
1878 e->value = (int32_t)enums_p[u].value;
1879 if (!u || (uint32_t)e->value >= position) {
1880 position = (uint32_t)e->value + 1;
1881 }
1882 /* check collision with other values */
1883 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1884 if (e->value == (*enums)[v].value) {
1885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1886 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1887 (uint32_t)e->value, e->name, (*enums)[v].name);
1888 return LY_EVALID;
1889 }
1890 }
1891 } else if (base_enums) {
1892 /* inherit the assigned value */
1893 e->value = base_enums[match].value;
1894 if (!u || (uint32_t)e->value >= position) {
1895 position = (uint32_t)e->value + 1;
1896 }
1897 } else {
1898 /* assign value automatically */
1899 if (u && position == 0) {
1900 /* counter overflow */
1901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1902 "Invalid bits - it is not possible to auto-assign bit position for "
1903 "\"%s\" since the highest value is already 4294967295.", e->name);
1904 return LY_EVALID;
1905 }
1906 e->value = position++;
1907 }
1908 }
1909
1910 if (base_enums) {
1911 /* the assigned values must not change from the derived type */
1912 if (e->value != base_enums[match].value) {
1913 if (basetype == LY_TYPE_ENUM) {
1914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1915 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1916 e->name, base_enums[match].value, e->value);
1917 } else {
1918 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1919 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1920 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1921 }
1922 return LY_EVALID;
1923 }
1924 }
1925
Radek Krejciec4da802019-05-02 13:02:41 +02001926 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
1927 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001928
1929 if (basetype == LY_TYPE_BITS) {
1930 /* keep bits ordered by position */
1931 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1932 if (v != u) {
1933 memcpy(&storage, e, sizeof *e);
1934 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1935 memcpy(&(*enums)[v], &storage, sizeof storage);
1936 }
1937 }
1938 }
1939
1940done:
1941 return ret;
1942}
1943
Radek Krejcia3045382018-11-22 14:30:31 +01001944#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1945 for ((NODE) = (NODE)->parent; \
1946 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1947 (NODE) = (NODE)->parent); \
1948 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1950 TERM; \
1951 }
1952
1953/**
1954 * @brief Validate the predicate(s) from the leafref path.
1955 * @param[in] ctx Compile context
1956 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1957 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001958 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001959 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001960 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001961 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1962 */
1963static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001964lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001965 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001966{
1967 LY_ERR ret = LY_EVALID;
1968 const struct lys_module *mod;
1969 const struct lysc_node *src_node, *dst_node;
1970 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1971 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001972 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001973 const char *start, *end, *pke_end;
1974 struct ly_set keys = {0};
1975 int i;
1976
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001977 assert(path_context);
1978
Radek Krejcia3045382018-11-22 14:30:31 +01001979 while (**predicate == '[') {
1980 start = (*predicate)++;
1981
1982 while (isspace(**predicate)) {
1983 ++(*predicate);
1984 }
1985 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1986 while (isspace(**predicate)) {
1987 ++(*predicate);
1988 }
1989 if (**predicate != '=') {
1990 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001991 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1992 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001993 goto cleanup;
1994 }
1995 ++(*predicate);
1996 while (isspace(**predicate)) {
1997 ++(*predicate);
1998 }
1999
2000 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2002 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2003 goto cleanup;
2004 }
2005 --pke_end;
2006 while (isspace(*pke_end)) {
2007 --pke_end;
2008 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002009 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002010 /* localize path-key-expr */
2011 pke_start = path_key_expr = *predicate;
2012 /* move after the current predicate */
2013 *predicate = end + 1;
2014
2015 /* source (must be leaf or leaf-list) */
2016 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002017 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002018 if (!mod) {
2019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2020 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002021 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002022 goto cleanup;
2023 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002024 if (!mod->implemented) {
2025 /* make the module implemented */
2026 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2027 }
Radek Krejcia3045382018-11-22 14:30:31 +01002028 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002029 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002030 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002031 src_node = NULL;
2032 if (context_node->keys) {
2033 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2034 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2035 src_node = (const struct lysc_node*)context_node->keys[u];
2036 break;
2037 }
2038 }
2039 }
Radek Krejcia3045382018-11-22 14:30:31 +01002040 if (!src_node) {
2041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002042 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002043 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002044 goto cleanup;
2045 }
Radek Krejcia3045382018-11-22 14:30:31 +01002046
2047 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002048 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002049 i = ly_set_add(&keys, (void*)src_node, 0);
2050 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002051 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002053 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002054 *predicate - start, start, src_node->name);
2055 goto cleanup;
2056 }
2057
2058 /* destination */
2059 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002060 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002061
2062 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2063 if (strncmp(path_key_expr, "current()", 9)) {
2064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2065 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2066 *predicate - start, start);
2067 goto cleanup;
2068 }
2069 path_key_expr += 9;
2070 while (isspace(*path_key_expr)) {
2071 ++path_key_expr;
2072 }
2073
2074 if (*path_key_expr != '/') {
2075 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2076 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2077 *predicate - start, start);
2078 goto cleanup;
2079 }
2080 ++path_key_expr;
2081 while (isspace(*path_key_expr)) {
2082 ++path_key_expr;
2083 }
2084
2085 /* rel-path-keyexpr:
2086 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2087 while (!strncmp(path_key_expr, "..", 2)) {
2088 ++dest_parent_times;
2089 path_key_expr += 2;
2090 while (isspace(*path_key_expr)) {
2091 ++path_key_expr;
2092 }
2093 if (*path_key_expr != '/') {
2094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2095 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2096 *predicate - start, start);
2097 goto cleanup;
2098 }
2099 ++path_key_expr;
2100 while (isspace(*path_key_expr)) {
2101 ++path_key_expr;
2102 }
2103
2104 /* path is supposed to be evaluated in data tree, so we have to skip
2105 * all schema nodes that cannot be instantiated in data tree */
2106 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2107 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2108 *predicate - start, start);
2109 }
2110 if (!dest_parent_times) {
2111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2112 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2113 *predicate - start, start);
2114 goto cleanup;
2115 }
2116 if (path_key_expr == pke_end) {
2117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2118 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2119 *predicate - start, start);
2120 goto cleanup;
2121 }
2122
2123 while(path_key_expr != pke_end) {
2124 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002126 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2127 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002128 goto cleanup;
2129 }
2130
2131 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002132 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002133 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002134 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002135 }
2136 if (!mod) {
2137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002138 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002139 *predicate - start, start, dst_len, dst);
2140 goto cleanup;
2141 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002142 if (!mod->implemented) {
2143 /* make the module implemented */
2144 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2145 }
Radek Krejcia3045382018-11-22 14:30:31 +01002146
2147 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2148 if (!dst_node) {
2149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002150 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002151 *predicate - start, start, path_key_expr - pke_start, pke_start);
2152 goto cleanup;
2153 }
2154 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002155 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 +01002156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002157 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002158 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2159 goto cleanup;
2160 }
2161 }
2162
2163 ret = LY_SUCCESS;
2164cleanup:
2165 ly_set_erase(&keys, NULL);
2166 return ret;
2167}
2168
2169/**
2170 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2171 *
2172 * path-arg = absolute-path / relative-path
2173 * absolute-path = 1*("/" (node-identifier *path-predicate))
2174 * relative-path = 1*(".." "/") descendant-path
2175 *
2176 * @param[in,out] path Path to parse.
2177 * @param[out] prefix Prefix of the token, NULL if there is not any.
2178 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2179 * @param[out] name Name of the token.
2180 * @param[out] nam_len Length of the name.
2181 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2182 * must not be changed between consecutive calls. -1 if the
2183 * path is absolute.
2184 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2185 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2186 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002187LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002188lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2189 int *parent_times, int *has_predicate)
2190{
2191 int par_times = 0;
2192
2193 assert(path && *path);
2194 assert(parent_times);
2195 assert(prefix);
2196 assert(prefix_len);
2197 assert(name);
2198 assert(name_len);
2199 assert(has_predicate);
2200
2201 *prefix = NULL;
2202 *prefix_len = 0;
2203 *name = NULL;
2204 *name_len = 0;
2205 *has_predicate = 0;
2206
2207 if (!*parent_times) {
2208 if (!strncmp(*path, "..", 2)) {
2209 *path += 2;
2210 ++par_times;
2211 while (!strncmp(*path, "/..", 3)) {
2212 *path += 3;
2213 ++par_times;
2214 }
2215 }
2216 if (par_times) {
2217 *parent_times = par_times;
2218 } else {
2219 *parent_times = -1;
2220 }
2221 }
2222
2223 if (**path != '/') {
2224 return LY_EINVAL;
2225 }
2226 /* skip '/' */
2227 ++(*path);
2228
2229 /* node-identifier ([prefix:]name) */
2230 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2231
2232 if ((**path == '/' && (*path)[1]) || !**path) {
2233 /* path continues by another token or this is the last token */
2234 return LY_SUCCESS;
2235 } else if ((*path)[0] != '[') {
2236 /* unexpected character */
2237 return LY_EINVAL;
2238 } else {
2239 /* predicate starting with [ */
2240 *has_predicate = 1;
2241 return LY_SUCCESS;
2242 }
2243}
2244
2245/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002246 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2247 *
2248 * The set of features used for target must be a subset of features used for the leafref.
2249 * This is not a perfect, we should compare the truth tables but it could require too much resources
2250 * and RFC 7950 does not require it explicitely, so we simplify that.
2251 *
2252 * @param[in] refnode The leafref node.
2253 * @param[in] target Tha target node of the leafref.
2254 * @return LY_SUCCESS or LY_EVALID;
2255 */
2256static LY_ERR
2257lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2258{
2259 LY_ERR ret = LY_EVALID;
2260 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002261 unsigned int u, v, count;
2262 struct ly_set features = {0};
2263
2264 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002265 if (iter->iffeatures) {
2266 LY_ARRAY_FOR(iter->iffeatures, u) {
2267 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2268 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002269 }
2270 }
2271 }
2272 }
2273
2274 /* we should have, in features set, a superset of features applicable to the target node.
2275 * So when adding features applicable to the target into the features set, we should not be
2276 * able to actually add any new feature, otherwise it is not a subset of features applicable
2277 * to the leafref itself. */
2278 count = features.count;
2279 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002280 if (iter->iffeatures) {
2281 LY_ARRAY_FOR(iter->iffeatures, u) {
2282 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2283 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002284 /* new feature was added (or LY_EMEM) */
2285 goto cleanup;
2286 }
2287 }
2288 }
2289 }
2290 }
2291 ret = LY_SUCCESS;
2292
2293cleanup:
2294 ly_set_erase(&features, NULL);
2295 return ret;
2296}
2297
2298/**
Radek Krejcia3045382018-11-22 14:30:31 +01002299 * @brief Validate the leafref path.
2300 * @param[in] ctx Compile context
2301 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002302 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002303 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2304 */
2305static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002306lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002307{
2308 const struct lysc_node *node = NULL, *parent = NULL;
2309 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002310 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002311 const char *id, *prefix, *name;
2312 size_t prefix_len, name_len;
2313 int parent_times = 0, has_predicate;
2314 unsigned int iter, u;
2315 LY_ERR ret = LY_SUCCESS;
2316
2317 assert(ctx);
2318 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002319 assert(leafref);
2320
Radek Krejcia3045382018-11-22 14:30:31 +01002321 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002322 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002323 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2324 if (!iter) { /* first iteration */
2325 /* precess ".." in relative paths */
2326 if (parent_times > 0) {
2327 /* move from the context node */
2328 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2329 /* path is supposed to be evaluated in data tree, so we have to skip
2330 * all schema nodes that cannot be instantiated in data tree */
2331 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002332 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002333 }
2334 }
2335 }
2336
2337 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002338 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002339 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002340 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002341 }
2342 if (!mod) {
2343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002344 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2345 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002346 return LY_EVALID;
2347 }
Radek Krejci3d929562019-02-21 11:25:55 +01002348 if (!mod->implemented) {
2349 /* make the module implemented */
2350 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2351 }
Radek Krejcia3045382018-11-22 14:30:31 +01002352
2353 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2354 if (!node) {
2355 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002356 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002357 return LY_EVALID;
2358 }
2359 parent = node;
2360
2361 if (has_predicate) {
2362 /* we have predicate, so the current result must be list */
2363 if (node->nodetype != LYS_LIST) {
2364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2365 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002366 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002367 return LY_EVALID;
2368 }
2369
Radek Krejcibade82a2018-12-05 10:13:48 +01002370 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2371 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002372 }
2373
2374 ++iter;
2375 }
2376 if (ret) {
2377 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002378 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002379 return LY_EVALID;
2380 }
2381
2382 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2383 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2384 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002385 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002386 return LY_EVALID;
2387 }
2388
2389 /* check status */
2390 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2391 return LY_EVALID;
2392 }
2393
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002394 /* check config */
2395 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2396 if (node->flags & LYS_CONFIG_R) {
2397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2398 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2399 leafref->path);
2400 return LY_EVALID;
2401 }
2402 }
2403
Radek Krejci412ddfa2018-11-23 11:44:11 +01002404 /* store the target's type and check for circular chain of leafrefs */
2405 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2406 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2407 if (type == (struct lysc_type*)leafref) {
2408 /* circular chain detected */
2409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2410 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2411 return LY_EVALID;
2412 }
2413 }
2414
Radek Krejci58d171e2018-11-23 13:50:55 +01002415 /* check if leafref and its target are under common if-features */
2416 if (lys_compile_leafref_features_validate(startnode, node)) {
2417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2418 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2419 leafref->path);
2420 return LY_EVALID;
2421 }
2422
Radek Krejcia3045382018-11-22 14:30:31 +01002423 return LY_SUCCESS;
2424}
2425
Radek Krejcicdfecd92018-11-26 11:27:32 +01002426static 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 +02002427 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002428/**
2429 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2430 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002431 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2432 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2433 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2434 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002435 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002436 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002437 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002438 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2439 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2440 * @param[out] type Newly created type structure with the filled information about the type.
2441 * @return LY_ERR value.
2442 */
Radek Krejci19a96102018-11-15 13:38:09 +01002443static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002444lys_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 +02002445 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002446 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002447{
2448 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002449 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002450 struct lysc_type_bin *bin;
2451 struct lysc_type_num *num;
2452 struct lysc_type_str *str;
2453 struct lysc_type_bits *bits;
2454 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002455 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002456 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002457 struct lysc_type_union *un, *un_aux;
2458 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002459
2460 switch (basetype) {
2461 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002462 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002463
2464 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002465 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002466 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2467 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002468 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002469 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002470 }
2471 }
2472
2473 if (tpdfname) {
2474 type_p->compiled = *type;
2475 *type = calloc(1, sizeof(struct lysc_type_bin));
2476 }
2477 break;
2478 case LY_TYPE_BITS:
2479 /* RFC 7950 9.7 - bits */
2480 bits = (struct lysc_type_bits*)(*type);
2481 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002482 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002483 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2484 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002485 }
2486
Radek Krejci555cb5b2018-11-16 14:54:33 +01002487 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002488 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002489 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002490 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002491 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002492 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002493 free(*type);
2494 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002495 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002496 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002497 }
2498
2499 if (tpdfname) {
2500 type_p->compiled = *type;
2501 *type = calloc(1, sizeof(struct lysc_type_bits));
2502 }
2503 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002504 case LY_TYPE_DEC64:
2505 dec = (struct lysc_type_dec*)(*type);
2506
2507 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002508 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002509 if (!type_p->fraction_digits) {
2510 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002511 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002512 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002514 free(*type);
2515 *type = NULL;
2516 }
2517 return LY_EVALID;
2518 }
2519 } else if (type_p->fraction_digits) {
2520 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002521 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2523 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002524 tpdfname);
2525 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2527 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002528 free(*type);
2529 *type = NULL;
2530 }
2531 return LY_EVALID;
2532 }
2533 dec->fraction_digits = type_p->fraction_digits;
2534
2535 /* RFC 7950 9.2.4 - range */
2536 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002537 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2538 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002539 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002540 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002541 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002542 }
2543
2544 if (tpdfname) {
2545 type_p->compiled = *type;
2546 *type = calloc(1, sizeof(struct lysc_type_dec));
2547 }
2548 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002549 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002550 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002551
2552 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002553 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002554 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2555 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002556 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002557 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002558 }
2559 } else if (base && ((struct lysc_type_str*)base)->length) {
2560 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2561 }
2562
2563 /* RFC 7950 9.4.5 - pattern */
2564 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002565 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002566 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002567 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2568 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2569 }
2570
2571 if (tpdfname) {
2572 type_p->compiled = *type;
2573 *type = calloc(1, sizeof(struct lysc_type_str));
2574 }
2575 break;
2576 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002577 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002578
2579 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002580 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002581 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002582 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002583 }
2584
Radek Krejci555cb5b2018-11-16 14:54:33 +01002585 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002586 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002587 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002589 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002591 free(*type);
2592 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002594 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002595 }
2596
2597 if (tpdfname) {
2598 type_p->compiled = *type;
2599 *type = calloc(1, sizeof(struct lysc_type_enum));
2600 }
2601 break;
2602 case LY_TYPE_INT8:
2603 case LY_TYPE_UINT8:
2604 case LY_TYPE_INT16:
2605 case LY_TYPE_UINT16:
2606 case LY_TYPE_INT32:
2607 case LY_TYPE_UINT32:
2608 case LY_TYPE_INT64:
2609 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002610 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002611
2612 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002613 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002614 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2615 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002616 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002617 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002618 }
2619 }
2620
2621 if (tpdfname) {
2622 type_p->compiled = *type;
2623 *type = calloc(1, sizeof(struct lysc_type_num));
2624 }
2625 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002626 case LY_TYPE_IDENT:
2627 idref = (struct lysc_type_identityref*)(*type);
2628
2629 /* RFC 7950 9.10.2 - base */
2630 if (type_p->bases) {
2631 if (base) {
2632 /* only the directly derived identityrefs can contain base specification */
2633 if (tpdfname) {
2634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002635 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002636 tpdfname);
2637 } else {
2638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002639 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002640 free(*type);
2641 *type = NULL;
2642 }
2643 return LY_EVALID;
2644 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002645 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002646 }
2647
2648 if (!base && !type_p->flags) {
2649 /* type derived from identityref built-in type must contain at least one base */
2650 if (tpdfname) {
2651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2652 } else {
2653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2654 free(*type);
2655 *type = NULL;
2656 }
2657 return LY_EVALID;
2658 }
2659
2660 if (tpdfname) {
2661 type_p->compiled = *type;
2662 *type = calloc(1, sizeof(struct lysc_type_identityref));
2663 }
2664 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002665 case LY_TYPE_LEAFREF:
2666 /* RFC 7950 9.9.3 - require-instance */
2667 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002668 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002669 if (tpdfname) {
2670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2671 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2672 } else {
2673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2674 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2675 free(*type);
2676 *type = NULL;
2677 }
2678 return LY_EVALID;
2679 }
Radek Krejcia3045382018-11-22 14:30:31 +01002680 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002681 } else if (base) {
2682 /* inherit */
2683 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002684 } else {
2685 /* default is true */
2686 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2687 }
2688 if (type_p->path) {
2689 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002690 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002691 } else if (base) {
2692 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002693 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002694 } else if (tpdfname) {
2695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2696 return LY_EVALID;
2697 } else {
2698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2699 free(*type);
2700 *type = NULL;
2701 return LY_EVALID;
2702 }
2703 if (tpdfname) {
2704 type_p->compiled = *type;
2705 *type = calloc(1, sizeof(struct lysc_type_leafref));
2706 }
2707 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002708 case LY_TYPE_INST:
2709 /* RFC 7950 9.9.3 - require-instance */
2710 if (type_p->flags & LYS_SET_REQINST) {
2711 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2712 } else {
2713 /* default is true */
2714 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2715 }
2716
2717 if (tpdfname) {
2718 type_p->compiled = *type;
2719 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2720 }
2721 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002722 case LY_TYPE_UNION:
2723 un = (struct lysc_type_union*)(*type);
2724
2725 /* RFC 7950 7.4 - type */
2726 if (type_p->types) {
2727 if (base) {
2728 /* only the directly derived union can contain types specification */
2729 if (tpdfname) {
2730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2731 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2732 tpdfname);
2733 } else {
2734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2735 "Invalid type substatement for the type not directly derived from union built-in type.");
2736 free(*type);
2737 *type = NULL;
2738 }
2739 return LY_EVALID;
2740 }
2741 /* compile the type */
2742 additional = 0;
2743 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2744 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002745 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 +01002746 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2747 /* add space for additional types from the union subtype */
2748 un_aux = (struct lysc_type_union *)un->types[u + additional];
2749 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)));
2750 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2751 un->types = (void*)((uint32_t*)(p) + 1);
2752
2753 /* copy subtypes of the subtype union */
2754 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2755 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2756 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2757 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2758 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2759 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2760 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2761 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2762 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2763 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2764 /* TODO extensions */
2765
2766 } else {
2767 un->types[u + additional] = un_aux->types[v];
2768 ++un_aux->types[v]->refcount;
2769 }
2770 ++additional;
2771 LY_ARRAY_INCREMENT(un->types);
2772 }
2773 /* compensate u increment in main loop */
2774 --additional;
2775
2776 /* free the replaced union subtype */
2777 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2778 } else {
2779 LY_ARRAY_INCREMENT(un->types);
2780 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002781 }
2782 }
2783
2784 if (!base && !type_p->flags) {
2785 /* type derived from union built-in type must contain at least one type */
2786 if (tpdfname) {
2787 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2788 } else {
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2790 free(*type);
2791 *type = NULL;
2792 }
2793 return LY_EVALID;
2794 }
2795
2796 if (tpdfname) {
2797 type_p->compiled = *type;
2798 *type = calloc(1, sizeof(struct lysc_type_union));
2799 }
2800 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002801 case LY_TYPE_BOOL:
2802 case LY_TYPE_EMPTY:
2803 case LY_TYPE_UNKNOWN: /* just to complete switch */
2804 break;
2805 }
2806 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2807done:
2808 return ret;
2809}
2810
Radek Krejcia3045382018-11-22 14:30:31 +01002811/**
2812 * @brief Compile information about the leaf/leaf-list's type.
2813 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002814 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2815 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2816 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2817 * @param[in] context_name Name of the context node or referencing typedef for logging.
2818 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002819 * @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 +01002820 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002821 * @return LY_ERR value.
2822 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002823static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002824lys_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 +02002825 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002826{
2827 LY_ERR ret = LY_SUCCESS;
2828 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002829 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002830 struct type_context {
2831 const struct lysp_tpdf *tpdf;
2832 struct lysp_node *node;
2833 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002834 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002835 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002836 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002837 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002838 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002839
2840 (*type) = NULL;
2841
2842 tctx = calloc(1, sizeof *tctx);
2843 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002844 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002845 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2846 ret == LY_SUCCESS;
2847 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2848 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2849 if (basetype) {
2850 break;
2851 }
2852
2853 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002854 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2855 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002856 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2857
Radek Krejcicdfecd92018-11-26 11:27:32 +01002858 if (units && !*units) {
2859 /* inherit units */
2860 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2861 }
Radek Krejci01342af2019-01-03 15:18:08 +01002862 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002863 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002864 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002865 }
Radek Krejci01342af2019-01-03 15:18:08 +01002866 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002867 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2868 break;
2869 }
2870
Radek Krejci19a96102018-11-15 13:38:09 +01002871 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002872 /* it is not necessary to continue, the rest of the chain was already compiled,
2873 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002874 basetype = tctx->tpdf->type.compiled->basetype;
2875 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002876 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002877 dummyloops = 1;
2878 goto preparenext;
2879 } else {
2880 tctx = NULL;
2881 break;
2882 }
Radek Krejci19a96102018-11-15 13:38:09 +01002883 }
2884
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002885 /* circular typedef reference detection */
2886 for (u = 0; u < tpdf_chain.count; u++) {
2887 /* local part */
2888 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2889 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2891 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2892 free(tctx);
2893 ret = LY_EVALID;
2894 goto cleanup;
2895 }
2896 }
2897 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2898 /* global part for unions corner case */
2899 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2900 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2902 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2903 free(tctx);
2904 ret = LY_EVALID;
2905 goto cleanup;
2906 }
2907 }
2908
Radek Krejci19a96102018-11-15 13:38:09 +01002909 /* store information for the following processing */
2910 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2911
Radek Krejcicdfecd92018-11-26 11:27:32 +01002912preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002913 /* prepare next loop */
2914 tctx_prev = tctx;
2915 tctx = calloc(1, sizeof *tctx);
2916 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2917 }
2918 free(tctx);
2919
2920 /* allocate type according to the basetype */
2921 switch (basetype) {
2922 case LY_TYPE_BINARY:
2923 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002924 break;
2925 case LY_TYPE_BITS:
2926 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002927 break;
2928 case LY_TYPE_BOOL:
2929 case LY_TYPE_EMPTY:
2930 *type = calloc(1, sizeof(struct lysc_type));
2931 break;
2932 case LY_TYPE_DEC64:
2933 *type = calloc(1, sizeof(struct lysc_type_dec));
2934 break;
2935 case LY_TYPE_ENUM:
2936 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002937 break;
2938 case LY_TYPE_IDENT:
2939 *type = calloc(1, sizeof(struct lysc_type_identityref));
2940 break;
2941 case LY_TYPE_INST:
2942 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2943 break;
2944 case LY_TYPE_LEAFREF:
2945 *type = calloc(1, sizeof(struct lysc_type_leafref));
2946 break;
2947 case LY_TYPE_STRING:
2948 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002949 break;
2950 case LY_TYPE_UNION:
2951 *type = calloc(1, sizeof(struct lysc_type_union));
2952 break;
2953 case LY_TYPE_INT8:
2954 case LY_TYPE_UINT8:
2955 case LY_TYPE_INT16:
2956 case LY_TYPE_UINT16:
2957 case LY_TYPE_INT32:
2958 case LY_TYPE_UINT32:
2959 case LY_TYPE_INT64:
2960 case LY_TYPE_UINT64:
2961 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002962 break;
2963 case LY_TYPE_UNKNOWN:
2964 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2965 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2966 ret = LY_EVALID;
2967 goto cleanup;
2968 }
2969 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002970 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002971 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2972 ly_data_type2str[basetype]);
2973 free(*type);
2974 (*type) = NULL;
2975 ret = LY_EVALID;
2976 goto cleanup;
2977 }
2978
2979 /* get restrictions from the referred typedefs */
2980 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2981 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002982
2983 /* remember the typedef context for circular check */
2984 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2985
Radek Krejci43699232018-11-23 14:59:46 +01002986 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002987 base = tctx->tpdf->type.compiled;
2988 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002989 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002990 /* no change, just use the type information from the base */
2991 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2992 ++base->refcount;
2993 continue;
2994 }
2995
2996 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002997 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2999 tctx->tpdf->name, ly_data_type2str[basetype]);
3000 ret = LY_EVALID;
3001 goto cleanup;
3002 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3004 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3005 tctx->tpdf->name, tctx->tpdf->dflt);
3006 ret = LY_EVALID;
3007 goto cleanup;
3008 }
3009
Radek Krejci19a96102018-11-15 13:38:09 +01003010 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003011 /* TODO user type plugins */
3012 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003013 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003014 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 +01003015 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003016 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003017 LY_CHECK_GOTO(ret, cleanup);
3018 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003019 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003020 /* remove the processed typedef contexts from the stack for circular check */
3021 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003022
Radek Krejcic5c27e52018-11-15 14:38:11 +01003023 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003024 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003025 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003026 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003027 /* TODO user type plugins */
3028 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003029 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003030 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 +01003031 LY_CHECK_GOTO(ret, cleanup);
3032 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003033 /* no specific restriction in leaf's type definition, copy from the base */
3034 free(*type);
3035 (*type) = base;
3036 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003037 }
Radek Krejci01342af2019-01-03 15:18:08 +01003038 if (!(*type)->dflt) {
3039 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3040 }
Radek Krejci19a96102018-11-15 13:38:09 +01003041
Radek Krejciec4da802019-05-02 13:02:41 +02003042 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003043
3044cleanup:
3045 ly_set_erase(&tpdf_chain, free);
3046 return ret;
3047}
3048
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003049/**
3050 * @brief Compile status information of the given node.
3051 *
3052 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3053 * has the status correctly set during the compilation.
3054 *
3055 * @param[in] ctx Compile context
3056 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3057 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3058 * the compatibility with the parent's status value.
3059 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3060 * @return LY_ERR value.
3061 */
3062static LY_ERR
3063lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3064{
3065 /* status - it is not inherited by specification, but it does not make sense to have
3066 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3067 if (!((*node_flags) & LYS_STATUS_MASK)) {
3068 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3069 if ((parent_flags & 0x3) != 0x3) {
3070 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3071 * combination of bits (0x3) which marks the uses_status value */
3072 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3073 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3074 }
3075 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3076 } else {
3077 (*node_flags) |= LYS_STATUS_CURR;
3078 }
3079 } else if (parent_flags & LYS_STATUS_MASK) {
3080 /* check status compatibility with the parent */
3081 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3082 if ((*node_flags) & LYS_STATUS_CURR) {
3083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3084 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3085 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3086 } else { /* LYS_STATUS_DEPRC */
3087 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3088 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3089 }
3090 return LY_EVALID;
3091 }
3092 }
3093 return LY_SUCCESS;
3094}
3095
Radek Krejci8cce8532019-03-05 11:27:45 +01003096/**
3097 * @brief Check uniqness of the node/action/notification name.
3098 *
3099 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3100 * structures, but they share the namespace so we need to check their name collisions.
3101 *
3102 * @param[in] ctx Compile context.
3103 * @param[in] children List (linked list) of data nodes to go through.
3104 * @param[in] actions List (sized array) of actions or RPCs to go through.
3105 * @param[in] notifs List (sized array) of Notifications to go through.
3106 * @param[in] name Name of the item to find in the given lists.
3107 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3108 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3109 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3110 */
3111static LY_ERR
3112lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3113 const struct lysc_action *actions, const struct lysc_notif *notifs,
3114 const char *name, void *exclude)
3115{
3116 const struct lysc_node *iter;
3117 unsigned int u;
3118
3119 LY_LIST_FOR(children, iter) {
3120 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3121 goto error;
3122 }
3123 }
3124 LY_ARRAY_FOR(actions, u) {
3125 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3126 goto error;
3127 }
3128 }
3129 LY_ARRAY_FOR(notifs, u) {
3130 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3131 goto error;
3132 }
3133 }
3134 return LY_SUCCESS;
3135error:
3136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3137 return LY_EEXIST;
3138}
3139
Radek Krejciec4da802019-05-02 13:02:41 +02003140static 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 +01003141
Radek Krejcia3045382018-11-22 14:30:31 +01003142/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003143 * @brief Compile parsed RPC/action schema node information.
3144 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003145 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003146 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003147 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3148 * @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).
3149 * Zero means no uses, non-zero value with no status bit set mean the default status.
3150 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3151 */
3152static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003153lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003154 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3155{
3156 LY_ERR ret = LY_SUCCESS;
3157 struct lysp_node *child_p;
3158 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003159 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003160
Radek Krejci8cce8532019-03-05 11:27:45 +01003161 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3162 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3163 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3164 action_p->name, action)) {
3165 return LY_EVALID;
3166 }
3167
Radek Krejciec4da802019-05-02 13:02:41 +02003168 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003169 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003170 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003171 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003172 return LY_EVALID;
3173 }
3174
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003175 action->nodetype = LYS_ACTION;
3176 action->module = ctx->mod;
3177 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003178 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003179 action->sp = action_p;
3180 }
3181 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3182
3183 /* status - it is not inherited by specification, but it does not make sense to have
3184 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3185 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3186
3187 DUP_STRING(ctx->ctx, action_p->name, action->name);
3188 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3189 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003190 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3191 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003192
3193 /* input */
Radek Krejciec4da802019-05-02 13:02:41 +02003194 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3195 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3196 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003197 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003198 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003199 }
Radek Krejciec4da802019-05-02 13:02:41 +02003200 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003201
3202 /* output */
Radek Krejciec4da802019-05-02 13:02:41 +02003203 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3204 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3205 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003206 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003207 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003208 }
3209
3210cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003211 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003212 return ret;
3213}
3214
3215/**
Radek Krejci43981a32019-04-12 09:44:11 +02003216 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003217 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003218 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003219 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3220 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3221 * @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 +02003222 * Zero means no uses, non-zero value with no status bit set mean the default status.
3223 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3224 */
3225static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003226lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003227 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3228{
3229 LY_ERR ret = LY_SUCCESS;
3230 struct lysp_node *child_p;
3231 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003232 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003233
3234 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3235 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3236 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3237 notif_p->name, notif)) {
3238 return LY_EVALID;
3239 }
3240
Radek Krejciec4da802019-05-02 13:02:41 +02003241 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3243 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003244 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003245 return LY_EVALID;
3246 }
3247
3248 notif->nodetype = LYS_NOTIF;
3249 notif->module = ctx->mod;
3250 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003251 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003252 notif->sp = notif_p;
3253 }
3254 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3255
3256 /* status - it is not inherited by specification, but it does not make sense to have
3257 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3258 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3259
3260 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3261 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3262 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003263 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3264 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3265 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003266
Radek Krejciec4da802019-05-02 13:02:41 +02003267 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003268 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003269 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003270 }
3271
3272cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003273 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003274 return ret;
3275}
3276
3277/**
Radek Krejcia3045382018-11-22 14:30:31 +01003278 * @brief Compile parsed container node information.
3279 * @param[in] ctx Compile context
3280 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003281 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3282 * is enriched with the container-specific information.
3283 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3284 */
Radek Krejci19a96102018-11-15 13:38:09 +01003285static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003286lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003287{
3288 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3289 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3290 struct lysp_node *child_p;
3291 unsigned int u;
3292 LY_ERR ret = LY_SUCCESS;
3293
Radek Krejcife909632019-02-12 15:34:42 +01003294 if (cont_p->presence) {
3295 cont->flags |= LYS_PRESENCE;
3296 }
3297
Radek Krejci19a96102018-11-15 13:38:09 +01003298 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003299 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003300 }
3301
Radek Krejciec4da802019-05-02 13:02:41 +02003302 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3303 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3304 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003305
3306done:
3307 return ret;
3308}
3309
Radek Krejci33f72892019-02-21 10:36:58 +01003310/*
3311 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3312 * @param[in] ctx Compile context.
3313 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3314 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003315 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3316 * @return LY_ERR value.
3317 */
3318static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003319lys_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 +01003320{
3321 unsigned int u, v;
3322 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3323
Radek Krejciec4da802019-05-02 13:02:41 +02003324 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 +01003325 leaf->units ? NULL : &leaf->units));
3326 if (leaf->nodetype == LYS_LEAFLIST) {
3327 if (llist->type->dflt && !llist->dflts && !llist->min) {
3328 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3329 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3330 LY_ARRAY_INCREMENT(llist->dflts);
3331 }
3332 } else {
3333 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3334 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3335 }
3336 }
3337 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3338 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3339 ly_set_add(&ctx->unres, leaf, 0);
3340 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3341 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3342 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3343 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3344 ly_set_add(&ctx->unres, leaf, 0);
3345 }
3346 }
3347 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3348 if (leaf->flags & LYS_SET_DFLT) {
3349 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3350 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3351 return LY_EVALID;
3352 }
3353 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3355 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3356 return LY_EVALID;
3357 }
3358 }
3359
3360 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3361 /* configuration data values must be unique - so check the default values */
3362 LY_ARRAY_FOR(llist->dflts, u) {
3363 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3364 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3366 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3367 return LY_EVALID;
3368 }
3369 }
3370 }
3371 }
3372
3373 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3374
3375 return LY_SUCCESS;
3376}
3377
Radek Krejcia3045382018-11-22 14:30:31 +01003378/**
3379 * @brief Compile parsed leaf node information.
3380 * @param[in] ctx Compile context
3381 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003382 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3383 * is enriched with the leaf-specific information.
3384 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3385 */
Radek Krejci19a96102018-11-15 13:38:09 +01003386static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003387lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003388{
3389 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3390 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3391 unsigned int u;
3392 LY_ERR ret = LY_SUCCESS;
3393
Radek Krejciec4da802019-05-02 13:02:41 +02003394 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003395 if (leaf_p->units) {
3396 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3397 leaf->flags |= LYS_SET_UNITS;
3398 }
3399 if (leaf_p->dflt) {
3400 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003401 leaf->flags |= LYS_SET_DFLT;
3402 }
Radek Krejci43699232018-11-23 14:59:46 +01003403
Radek Krejciec4da802019-05-02 13:02:41 +02003404 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003405
Radek Krejci19a96102018-11-15 13:38:09 +01003406done:
3407 return ret;
3408}
3409
Radek Krejcia3045382018-11-22 14:30:31 +01003410/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003411 * @brief Compile parsed leaf-list node information.
3412 * @param[in] ctx Compile context
3413 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003414 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3415 * is enriched with the leaf-list-specific information.
3416 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3417 */
3418static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003419lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003420{
3421 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3422 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003423 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003424 LY_ERR ret = LY_SUCCESS;
3425
Radek Krejciec4da802019-05-02 13:02:41 +02003426 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003427 if (llist_p->units) {
3428 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3429 llist->flags |= LYS_SET_UNITS;
3430 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003431
3432 if (llist_p->dflts) {
3433 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3434 LY_ARRAY_FOR(llist_p->dflts, u) {
3435 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3436 LY_ARRAY_INCREMENT(llist->dflts);
3437 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003438 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003439 }
3440
3441 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003442 if (llist->min) {
3443 llist->flags |= LYS_MAND_TRUE;
3444 }
Radek Krejcib7408632018-11-28 17:12:11 +01003445 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003446
Radek Krejciec4da802019-05-02 13:02:41 +02003447 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003448
3449done:
3450 return ret;
3451}
3452
3453/**
Radek Krejci7af64242019-02-18 13:07:53 +01003454 * @brief Compile information about list's uniques.
3455 * @param[in] ctx Compile context.
3456 * @param[in] context_module Module where the prefixes are going to be resolved.
3457 * @param[in] uniques Sized array list of unique statements.
3458 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3459 * @return LY_ERR value.
3460 */
3461static LY_ERR
3462lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3463{
3464 LY_ERR ret = LY_SUCCESS;
3465 struct lysc_node_leaf **key, ***unique;
3466 const char *keystr, *delim;
3467 size_t len;
3468 unsigned int v;
3469 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003470 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003471
3472 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3473 config = -1;
3474 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3475 keystr = uniques[v];
3476 while (keystr) {
3477 delim = strpbrk(keystr, " \t\n");
3478 if (delim) {
3479 len = delim - keystr;
3480 while (isspace(*delim)) {
3481 ++delim;
3482 }
3483 } else {
3484 len = strlen(keystr);
3485 }
3486
3487 /* unique node must be present */
3488 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003489 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3490 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003491 if (ret != LY_SUCCESS) {
3492 if (ret == LY_EDENIED) {
3493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003494 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003495 len, keystr, lys_nodetype2str((*key)->nodetype));
3496 }
3497 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003498 } else if (flags) {
3499 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3500 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3501 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3502 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003503 }
3504
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003505
Radek Krejci7af64242019-02-18 13:07:53 +01003506 /* all referenced leafs must be of the same config type */
3507 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3509 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3510 return LY_EVALID;
3511 } else if ((*key)->flags & LYS_CONFIG_W) {
3512 config = 1;
3513 } else { /* LYS_CONFIG_R */
3514 config = 0;
3515 }
3516
3517 /* check status */
3518 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3519 (*key)->flags, (*key)->module, (*key)->name));
3520
3521 /* mark leaf as unique */
3522 (*key)->flags |= LYS_UNIQUE;
3523
3524 /* next unique value in line */
3525 keystr = delim;
3526 }
3527 /* next unique definition */
3528 }
3529
3530 return LY_SUCCESS;
3531}
3532
3533/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003534 * @brief Compile parsed list node information.
3535 * @param[in] ctx Compile context
3536 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003537 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3538 * is enriched with the list-specific information.
3539 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3540 */
3541static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003542lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003543{
3544 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3545 struct lysc_node_list *list = (struct lysc_node_list*)node;
3546 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003547 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003548 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003549 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003550 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003551 LY_ERR ret = LY_SUCCESS;
3552
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003553 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003554 if (list->min) {
3555 list->flags |= LYS_MAND_TRUE;
3556 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003557 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3558
3559 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003560 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003561 }
3562
Radek Krejciec4da802019-05-02 13:02:41 +02003563 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003564
3565 /* keys */
3566 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3568 return LY_EVALID;
3569 }
3570
3571 /* find all the keys (must be direct children) */
3572 keystr = list_p->key;
3573 while (keystr) {
3574 delim = strpbrk(keystr, " \t\n");
3575 if (delim) {
3576 len = delim - keystr;
3577 while (isspace(*delim)) {
3578 ++delim;
3579 }
3580 } else {
3581 len = strlen(keystr);
3582 }
3583
3584 /* key node must be present */
3585 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3586 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3587 if (!(*key)) {
3588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3589 "The list's key \"%.*s\" not found.", len, keystr);
3590 return LY_EVALID;
3591 }
3592 /* keys must be unique */
3593 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3594 if (*key == list->keys[u]) {
3595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3596 "Duplicated key identifier \"%.*s\".", len, keystr);
3597 return LY_EVALID;
3598 }
3599 }
3600 /* key must have the same config flag as the list itself */
3601 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3603 return LY_EVALID;
3604 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003605 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003606 /* YANG 1.0 denies key to be of empty type */
3607 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3609 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3610 return LY_EVALID;
3611 }
3612 } else {
3613 /* when and if-feature are illegal on list keys */
3614 if ((*key)->when) {
3615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3616 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3617 return LY_EVALID;
3618 }
3619 if ((*key)->iffeatures) {
3620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3621 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3622 return LY_EVALID;
3623 }
3624 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003625
3626 /* check status */
3627 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3628 (*key)->flags, (*key)->module, (*key)->name));
3629
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003630 /* ignore default values of the key */
3631 if ((*key)->dflt) {
3632 lydict_remove(ctx->ctx, (*key)->dflt);
3633 (*key)->dflt = NULL;
3634 }
3635 /* mark leaf as key */
3636 (*key)->flags |= LYS_KEY;
3637
3638 /* next key value */
3639 keystr = delim;
3640 }
3641
3642 /* uniques */
3643 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003644 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003645 }
3646
Radek Krejciec4da802019-05-02 13:02:41 +02003647 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3648 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003649
3650done:
3651 return ret;
3652}
3653
Radek Krejcib56c7502019-02-13 14:19:54 +01003654/**
3655 * @brief Do some checks and set the default choice's case.
3656 *
3657 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3658 *
3659 * @param[in] ctx Compile context.
3660 * @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,
3661 * not the reference to the imported module.
3662 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3663 * @return LY_ERR value.
3664 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003665static LY_ERR
3666lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3667{
3668 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3669 const char *prefix = NULL, *name;
3670 size_t prefix_len = 0;
3671
3672 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3673 name = strchr(dflt, ':');
3674 if (name) {
3675 prefix = dflt;
3676 prefix_len = name - prefix;
3677 ++name;
3678 } else {
3679 name = dflt;
3680 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003681 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003682 /* prefixed default case make sense only for the prefix of the schema itself */
3683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3684 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3685 prefix_len, prefix);
3686 return LY_EVALID;
3687 }
3688 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3689 if (!ch->dflt) {
3690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3691 "Default case \"%s\" not found.", dflt);
3692 return LY_EVALID;
3693 }
3694 /* no mandatory nodes directly under the default case */
3695 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003696 if (iter->parent != (struct lysc_node*)ch->dflt) {
3697 break;
3698 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003699 if (iter->flags & LYS_MAND_TRUE) {
3700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3701 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3702 return LY_EVALID;
3703 }
3704 }
Radek Krejci01342af2019-01-03 15:18:08 +01003705 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003706 return LY_SUCCESS;
3707}
3708
Radek Krejciccd20f12019-02-15 14:12:27 +01003709static LY_ERR
3710lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3711{
3712 struct lys_module *mod;
3713 const char *prefix = NULL, *name;
3714 size_t prefix_len = 0;
3715 struct lysc_node_case *cs;
3716 struct lysc_node *node;
3717
3718 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3719 name = strchr(dflt, ':');
3720 if (name) {
3721 prefix = dflt;
3722 prefix_len = name - prefix;
3723 ++name;
3724 } else {
3725 name = dflt;
3726 }
3727 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3728 if (prefix) {
3729 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3731 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3732 "The prefix does not match any imported module of the deviation module.",
3733 devnodeid, dflt);
3734 return LY_EVALID;
3735 }
3736 } else {
3737 mod = ctx->mod;
3738 }
3739 /* get the default case */
3740 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3741 if (!cs) {
3742 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3743 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3744 devnodeid, dflt);
3745 return LY_EVALID;
3746 }
3747
3748 /* check that there is no mandatory node */
3749 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003750 if (node->parent != (struct lysc_node*)cs) {
3751 break;
3752 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003753 if (node->flags & LYS_MAND_TRUE) {
3754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3755 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3756 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3757 return LY_EVALID;
3758 }
3759 }
3760
3761 /* set the default case in choice */
3762 ch->dflt = cs;
3763 cs->flags |= LYS_SET_DFLT;
3764
3765 return LY_SUCCESS;
3766}
3767
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003768/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003769 * @brief Compile parsed choice node information.
3770 * @param[in] ctx Compile context
3771 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01003772 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003773 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003774 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3775 */
3776static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003777lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003778{
3779 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3780 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3781 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01003782 LY_ERR ret = LY_SUCCESS;
3783
Radek Krejci056d0a82018-12-06 16:57:25 +01003784 LY_LIST_FOR(ch_p->child, child_p) {
3785 if (child_p->nodetype == LYS_CASE) {
3786 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003787 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003788 }
3789 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003790 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003791 }
3792 }
3793
3794 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003795 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003796 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003797 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003798
Radek Krejci9800fb82018-12-13 14:26:23 +01003799 return ret;
3800}
3801
3802/**
3803 * @brief Compile parsed anydata or anyxml node information.
3804 * @param[in] ctx Compile context
3805 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01003806 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3807 * is enriched with the any-specific information.
3808 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3809 */
3810static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003811lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01003812{
3813 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3814 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3815 unsigned int u;
3816 LY_ERR ret = LY_SUCCESS;
3817
Radek Krejciec4da802019-05-02 13:02:41 +02003818 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01003819
3820 if (any->flags & LYS_CONFIG_W) {
3821 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3822 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3823 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003824done:
3825 return ret;
3826}
3827
Radek Krejcib56c7502019-02-13 14:19:54 +01003828/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003829 * @brief Connect the node into the siblings list and check its name uniqueness.
3830 *
3831 * @param[in] ctx Compile context
3832 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3833 * the choice itself is expected instead of a specific case node.
3834 * @param[in] node Schema node to connect into the list.
3835 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3836 */
3837static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003838lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01003839{
3840 struct lysc_node **children;
3841
3842 if (node->nodetype == LYS_CASE) {
3843 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3844 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02003845 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003846 }
3847 if (children) {
3848 if (!(*children)) {
3849 /* first child */
3850 *children = node;
3851 } else if (*children != node) {
3852 /* by the condition in previous branch we cover the choice/case children
3853 * - the children list is shared by the choice and the the first case, in addition
3854 * the first child of each case must be referenced from the case node. So the node is
3855 * actually always already inserted in case it is the first children - so here such
3856 * a situation actually corresponds to the first branch */
3857 /* insert at the end of the parent's children list */
3858 (*children)->prev->next = node;
3859 node->prev = (*children)->prev;
3860 (*children)->prev = node;
3861
3862 /* check the name uniqueness */
3863 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3864 lysc_node_notifs(parent), node->name, node)) {
3865 return LY_EEXIST;
3866 }
3867 }
3868 }
3869 return LY_SUCCESS;
3870}
3871
Radek Krejci95710c92019-02-11 15:49:55 +01003872/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003873 * @brief Get the XPath context node for the given schema node.
3874 * @param[in] start The schema node where the XPath expression appears.
3875 * @return The context node to evaluate XPath expression in given schema node.
3876 * @return NULL in case the context node is the root node.
3877 */
3878static struct lysc_node *
3879lysc_xpath_context(struct lysc_node *start)
3880{
3881 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3882 start = start->parent);
3883 return start;
3884}
3885
3886/**
3887 * @brief Prepare the case structure in choice node for the new data node.
3888 *
3889 * 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
3890 * created in the choice when the first child was processed.
3891 *
3892 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003893 * @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,
3894 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003895 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3896 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3897 * @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,
3898 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003899 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003900static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02003901lys_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 +01003902{
3903 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02003904 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01003905 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003906 unsigned int u;
3907 LY_ERR ret;
3908
Radek Krejci95710c92019-02-11 15:49:55 +01003909#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003910 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003911 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3913 return NULL; \
3914 } \
3915 }
3916
Radek Krejci95710c92019-02-11 15:49:55 +01003917 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3918 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003919
3920 /* we have to add an implicit case node into the parent choice */
3921 cs = calloc(1, sizeof(struct lysc_node_case));
3922 DUP_STRING(ctx->ctx, child->name, cs->name);
3923 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003924 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003925 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3926 /* the case is already present since the child is not its first children */
3927 return (struct lysc_node_case*)ch->cases->prev;
3928 }
Radek Krejci95710c92019-02-11 15:49:55 +01003929 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003930
3931 /* explicit parent case is not present (this is its first child) */
3932 cs = calloc(1, sizeof(struct lysc_node_case));
3933 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3934 cs->flags = LYS_STATUS_MASK & node_p->flags;
3935 cs->sp = node_p;
3936
Radek Krejcib1b59152019-01-07 13:21:56 +01003937 /* 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 +02003938 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01003939
3940 if (node_p->when) {
3941 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02003942 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01003943 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003944 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003945 }
Radek Krejciec4da802019-05-02 13:02:41 +02003946 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003947 } else {
3948 LOGINT(ctx->ctx);
3949 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003950 }
3951 cs->module = ctx->mod;
3952 cs->prev = (struct lysc_node*)cs;
3953 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02003954 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01003955 cs->parent = (struct lysc_node*)ch;
3956 cs->child = child;
3957
3958 return cs;
3959error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02003960 if (cs) {
3961 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
3962 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003963 return NULL;
3964
3965#undef UNIQUE_CHECK
3966}
3967
Radek Krejcib56c7502019-02-13 14:19:54 +01003968/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003969 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003970 *
3971 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003972 * @param[in] node Target node where the config is supposed to be changed.
3973 * @param[in] config_flag Node's config flag to be applied to the @p node.
3974 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003975 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3976 * 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 +01003977 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3978 * @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 +01003979 * @return LY_ERR value.
3980 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003981static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003982lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3983 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003984{
3985 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003986 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003987
3988 if (config == (node->flags & LYS_CONFIG_MASK)) {
3989 /* nothing to do */
3990 return LY_SUCCESS;
3991 }
3992
3993 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003994 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003995 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003997 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3998 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003999 return LY_EVALID;
4000 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004001 node->flags |= LYS_SET_CONFIG;
4002 } else {
4003 if (node->flags & LYS_SET_CONFIG) {
4004 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4005 /* setting config flags, but have node with explicit config true */
4006 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4007 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
4008 refine_flag ? "refine" : "deviation", nodeid);
4009 return LY_EVALID;
4010 }
4011 /* do not change config on nodes where the config is explicitely set, this does not apply to
4012 * nodes, which are being changed explicitly (targets of refine or deviation) */
4013 return LY_SUCCESS;
4014 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004015 }
4016 node->flags &= ~LYS_CONFIG_MASK;
4017 node->flags |= config;
4018
4019 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004020 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004021 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004022 }
4023
Radek Krejci76b3e962018-12-14 17:01:25 +01004024 return LY_SUCCESS;
4025}
4026
Radek Krejcib56c7502019-02-13 14:19:54 +01004027/**
4028 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4029 *
4030 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4031 * the flag to such parents from a mandatory children.
4032 *
4033 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4034 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4035 * (mandatory children was removed).
4036 */
Radek Krejcife909632019-02-12 15:34:42 +01004037void
4038lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4039{
4040 struct lysc_node *iter;
4041
4042 if (add) { /* set flag */
4043 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4044 parent = parent->parent) {
4045 parent->flags |= LYS_MAND_TRUE;
4046 }
4047 } else { /* unset flag */
4048 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004049 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004050 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004051 /* there is another mandatory node */
4052 return;
4053 }
4054 }
4055 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4056 parent->flags &= ~LYS_MAND_TRUE;
4057 }
4058 }
4059}
4060
Radek Krejci056d0a82018-12-06 16:57:25 +01004061/**
Radek Krejci3641f562019-02-13 15:38:40 +01004062 * @brief Internal sorting process for the lys_compile_augment_sort().
4063 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4064 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4065 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4066 */
4067static void
4068lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4069{
4070 unsigned int v;
4071 size_t len;
4072
4073 len = strlen(aug_p->nodeid);
4074 LY_ARRAY_FOR(result, v) {
4075 if (strlen(result[v]->nodeid) <= len) {
4076 continue;
4077 }
4078 if (v < LY_ARRAY_SIZE(result)) {
4079 /* move the rest of array */
4080 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4081 break;
4082 }
4083 }
4084 result[v] = aug_p;
4085 LY_ARRAY_INCREMENT(result);
4086}
4087
4088/**
4089 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4090 *
4091 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4092 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4093 *
4094 * @param[in] ctx Compile context.
4095 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4096 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4097 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4098 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4099 * @return LY_ERR value.
4100 */
4101LY_ERR
4102lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4103{
4104 struct lysp_augment **result = NULL;
4105 unsigned int u, v;
4106 size_t count = 0;
4107
4108 assert(augments);
4109
4110 /* get count of the augments in module and all its submodules */
4111 if (aug_p) {
4112 count += LY_ARRAY_SIZE(aug_p);
4113 }
4114 LY_ARRAY_FOR(inc_p, u) {
4115 if (inc_p[u].submodule->augments) {
4116 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4117 }
4118 }
4119
4120 if (!count) {
4121 *augments = NULL;
4122 return LY_SUCCESS;
4123 }
4124 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4125
4126 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4127 * together, so there can be even /z/y betwwen them. */
4128 LY_ARRAY_FOR(aug_p, u) {
4129 lys_compile_augment_sort_(&aug_p[u], result);
4130 }
4131 LY_ARRAY_FOR(inc_p, u) {
4132 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4133 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4134 }
4135 }
4136
4137 *augments = result;
4138 return LY_SUCCESS;
4139}
4140
4141/**
4142 * @brief Compile the parsed augment connecting it into its target.
4143 *
4144 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4145 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4146 * are already implemented and compiled.
4147 *
4148 * @param[in] ctx Compile context.
4149 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004150 * @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
4151 * children in case of the augmenting uses data.
4152 * @return LY_SUCCESS on success.
4153 * @return LY_EVALID on failure.
4154 */
4155LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004156lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004157{
4158 LY_ERR ret = LY_SUCCESS;
4159 struct lysp_node *node_p, *case_node_p;
4160 struct lysc_node *target; /* target target of the augment */
4161 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004162 struct lysc_when **when, *when_shared;
4163 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004164 uint16_t flags = 0;
4165 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004166 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004167
Radek Krejci7af64242019-02-18 13:07:53 +01004168 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004169 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004170 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004171 if (ret != LY_SUCCESS) {
4172 if (ret == LY_EDENIED) {
4173 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4174 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4175 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4176 }
4177 return LY_EVALID;
4178 }
4179
4180 /* check for mandatory nodes
4181 * - new cases augmenting some choice can have mandatory nodes
4182 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4183 */
Radek Krejci733988a2019-02-15 15:12:44 +01004184 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004185 allow_mandatory = 1;
4186 }
4187
4188 when_shared = NULL;
4189 LY_LIST_FOR(aug_p->child, node_p) {
4190 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4191 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4192 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004193 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4194 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004195 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4196 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4197 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4198 return LY_EVALID;
4199 }
4200
4201 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004202 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004203 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004204 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004205 } else {
4206 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004207 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004208 }
4209 }
Radek Krejciec4da802019-05-02 13:02:41 +02004210 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004211
Radek Krejcife13da42019-02-15 14:51:01 +01004212 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4213 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004214 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004215 /* 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 +01004216 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 +01004217 } else if (target->nodetype == LYS_CHOICE) {
4218 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4219 node = ((struct lysc_node_choice*)target)->cases->prev;
4220 } else {
4221 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004222 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004223 }
4224
Radek Krejci733988a2019-02-15 15:12:44 +01004225 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004226 node->flags &= ~LYS_MAND_TRUE;
4227 lys_compile_mandatory_parents(target, 0);
4228 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4229 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4230 aug_p->nodeid, node->name);
4231 return LY_EVALID;
4232 }
4233
4234 /* pass augment's when to all the children */
4235 if (aug_p->when) {
4236 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4237 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004238 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004239 LY_CHECK_GOTO(ret, error);
4240 (*when)->context = lysc_xpath_context(target);
4241 when_shared = *when;
4242 } else {
4243 ++when_shared->refcount;
4244 (*when) = when_shared;
4245 }
4246 }
4247 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004248
Radek Krejciec4da802019-05-02 13:02:41 +02004249 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004250 switch (target->nodetype) {
4251 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004252 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004253 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004254 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004255 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004256 break;
4257 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004258 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004259 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004260 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004261 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004262 break;
4263 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004264 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004265 if (aug_p->actions) {
4266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4267 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4268 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4269 return LY_EVALID;
4270 }
4271 if (aug_p->notifs) {
4272 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4273 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4274 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4275 return LY_EVALID;
4276 }
4277 }
Radek Krejci3641f562019-02-13 15:38:40 +01004278
4279error:
Radek Krejciec4da802019-05-02 13:02:41 +02004280 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004281 return ret;
4282}
4283
4284/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004285 * @brief Apply refined or deviated mandatory flag to the target node.
4286 *
4287 * @param[in] ctx Compile context.
4288 * @param[in] node Target node where the mandatory property is supposed to be changed.
4289 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4290 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4291 * @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 +01004292 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4293 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4294 * 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 +01004295 * @return LY_ERR value.
4296 */
4297static LY_ERR
4298lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4299{
4300 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4301 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4302 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4303 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4304 return LY_EVALID;
4305 }
4306
4307 if (mandatory_flag & LYS_MAND_TRUE) {
4308 /* check if node has default value */
4309 if (node->nodetype & LYS_LEAF) {
4310 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004311 if (refine_flag) {
4312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4313 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4314 return LY_EVALID;
4315 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004316 } else {
4317 /* remove the default value taken from the leaf's type */
4318 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4319 ((struct lysc_node_leaf*)node)->dflt = NULL;
4320 }
4321 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004322 if (refine_flag) {
4323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4324 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4325 return LY_EVALID;
4326 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004327 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004328 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004329 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004330 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004331 return LY_EVALID;
4332 }
4333
4334 node->flags &= ~LYS_MAND_FALSE;
4335 node->flags |= LYS_MAND_TRUE;
4336 lys_compile_mandatory_parents(node->parent, 1);
4337 } else {
4338 /* make mandatory false */
4339 node->flags &= ~LYS_MAND_TRUE;
4340 node->flags |= LYS_MAND_FALSE;
4341 lys_compile_mandatory_parents(node->parent, 0);
4342 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4343 /* get the type's default value if any */
4344 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4345 }
4346 }
4347 return LY_SUCCESS;
4348}
4349
4350/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004351 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4352 * If present, also apply uses's modificators.
4353 *
4354 * @param[in] ctx Compile context
4355 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004356 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4357 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4358 * the compile context.
4359 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4360 */
4361static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004362lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004363{
4364 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004365 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004366 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004367 struct lysc_node_container context_node_fake =
4368 {.nodetype = LYS_CONTAINER,
4369 .module = ctx->mod,
4370 .flags = parent ? parent->flags : 0,
4371 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004372 .prev = (struct lysc_node*)&context_node_fake,
4373 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004374 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004375 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004376 int found;
4377 const char *id, *name, *prefix;
4378 size_t prefix_len, name_len;
4379 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004380 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004381 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004382 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004383 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004384 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004385 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004386 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004387 unsigned int actions_index, notifs_index;
4388 struct lysc_notif **notifs = NULL;
4389 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004390
4391 /* search for the grouping definition */
4392 found = 0;
4393 id = uses_p->name;
Radek Krejci15e99fc2019-04-08 14:29:39 +02004394 LY_CHECK_RET(lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004395 if (prefix) {
4396 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4397 if (!mod) {
4398 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4399 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4400 return LY_EVALID;
4401 }
4402 } else {
4403 mod = ctx->mod_def;
4404 }
4405 if (mod == ctx->mod_def) {
4406 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004407 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004408 LY_ARRAY_FOR(grp, u) {
4409 if (!strcmp(grp[u].name, name)) {
4410 grp = &grp[u];
4411 found = 1;
4412 break;
4413 }
4414 }
4415 }
4416 }
4417 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004418 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004419 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004420 if (grp) {
4421 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4422 if (!strcmp(grp[u].name, name)) {
4423 grp = &grp[u];
4424 found = 1;
4425 }
4426 }
4427 }
4428 if (!found && mod->parsed->includes) {
4429 /* ... and all the submodules */
4430 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4431 grp = mod->parsed->includes[u].submodule->groupings;
4432 if (grp) {
4433 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4434 if (!strcmp(grp[v].name, name)) {
4435 grp = &grp[v];
4436 found = 1;
4437 }
4438 }
4439 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004440 }
4441 }
4442 }
4443 if (!found) {
4444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4445 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4446 return LY_EVALID;
4447 }
4448
4449 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4450 grp_stack_count = ctx->groupings.count;
4451 ly_set_add(&ctx->groupings, (void*)grp, 0);
4452 if (grp_stack_count == ctx->groupings.count) {
4453 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4454 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4455 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4456 return LY_EVALID;
4457 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004458 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4459 /* remember that the grouping is instantiated to avoid its standalone validation */
4460 grp->flags |= LYS_USED_GRP;
4461 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004462
4463 /* switch context's mod_def */
4464 mod_old = ctx->mod_def;
4465 ctx->mod_def = mod;
4466
4467 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004468 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 +01004469
Radek Krejcifc11bd72019-04-11 16:00:05 +02004470 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004471 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004472 /* 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 +02004473 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 +01004474
4475 /* some preparation for applying refines */
4476 if (grp->data == node_p) {
4477 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004478 if (parent) {
4479 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4480 } else if (ctx->mod->compiled->data) {
4481 child = ctx->mod->compiled->data;
4482 } else {
4483 child = NULL;
4484 }
4485 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004486 }
4487 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004488 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004489 LY_LIST_FOR(context_node_fake.child, child) {
4490 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004491
Radek Krejcifc11bd72019-04-11 16:00:05 +02004492 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004493 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004494 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004495 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004496 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004497 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004498 when_shared = *when;
4499 } else {
4500 ++when_shared->refcount;
4501 (*when) = when_shared;
4502 }
4503 }
Radek Krejci01342af2019-01-03 15:18:08 +01004504 }
4505 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004506 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004507 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004508 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004509 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 +01004510 }
4511
Radek Krejcifc11bd72019-04-11 16:00:05 +02004512 /* compile actions */
4513 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4514 if (actions) {
4515 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004516 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004517 if (*actions && (uses_p->augments || uses_p->refines)) {
4518 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4519 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4520 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4521 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4522 }
4523 }
4524
4525 /* compile notifications */
4526 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4527 if (notifs) {
4528 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004529 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004530 if (*notifs && (uses_p->augments || uses_p->refines)) {
4531 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4532 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4533 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4534 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4535 }
4536 }
4537
4538
Radek Krejci3641f562019-02-13 15:38:40 +01004539 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004540 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004541 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004542 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004543 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004544
Radek Krejcif0089082019-01-07 16:42:01 +01004545 /* reload previous context's mod_def */
4546 ctx->mod_def = mod_old;
4547
Radek Krejci76b3e962018-12-14 17:01:25 +01004548 /* apply refine */
4549 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004550 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 +01004551 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004552 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004553 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004554
4555 /* default value */
4556 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004557 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4559 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4560 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004561 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004562 }
4563 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4564 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4565 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4566 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004567 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004568 }
4569 if (node->nodetype == LYS_LEAF) {
4570 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4571 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004572 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004573 /* TODO check the default value according to type */
4574 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004575 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4577 "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 +02004578 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004579 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004580 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4581 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4582 }
4583 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004584 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004585 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 +01004586 LY_ARRAY_FOR(rfn->dflts, u) {
4587 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4588 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4589 }
4590 /* TODO check the default values according to type */
4591 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004592 if (((struct lysc_node_choice*)node)->dflt) {
4593 /* unset LYS_SET_DFLT from the current default case */
4594 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4595 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004596 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 +01004597 }
4598 }
4599
Radek Krejci12fb9142019-01-08 09:45:30 +01004600 /* description */
4601 if (rfn->dsc) {
4602 FREE_STRING(ctx->ctx, node->dsc);
4603 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4604 }
4605
4606 /* reference */
4607 if (rfn->ref) {
4608 FREE_STRING(ctx->ctx, node->ref);
4609 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4610 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004611
4612 /* config */
4613 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004614 if (!flags) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004615 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004616 } else {
4617 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4618 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4619 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004620 }
4621
4622 /* mandatory */
4623 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004624 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004625 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004626
4627 /* presence */
4628 if (rfn->presence) {
4629 if (node->nodetype != LYS_CONTAINER) {
4630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4631 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4632 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004633 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004634 }
4635 node->flags |= LYS_PRESENCE;
4636 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004637
4638 /* must */
4639 if (rfn->musts) {
4640 switch (node->nodetype) {
4641 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004642 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 +01004643 break;
4644 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004645 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 +01004646 break;
4647 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004648 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 +01004649 break;
4650 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004651 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 +01004652 break;
4653 case LYS_ANYXML:
4654 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004655 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 +01004656 break;
4657 default:
4658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4659 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4660 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004661 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004662 }
4663 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004664
4665 /* min/max-elements */
4666 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4667 switch (node->nodetype) {
4668 case LYS_LEAFLIST:
4669 if (rfn->flags & LYS_SET_MAX) {
4670 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4671 }
4672 if (rfn->flags & LYS_SET_MIN) {
4673 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004674 if (rfn->min) {
4675 node->flags |= LYS_MAND_TRUE;
4676 lys_compile_mandatory_parents(node->parent, 1);
4677 } else {
4678 node->flags &= ~LYS_MAND_TRUE;
4679 lys_compile_mandatory_parents(node->parent, 0);
4680 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004681 }
4682 break;
4683 case LYS_LIST:
4684 if (rfn->flags & LYS_SET_MAX) {
4685 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4686 }
4687 if (rfn->flags & LYS_SET_MIN) {
4688 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004689 if (rfn->min) {
4690 node->flags |= LYS_MAND_TRUE;
4691 lys_compile_mandatory_parents(node->parent, 1);
4692 } else {
4693 node->flags &= ~LYS_MAND_TRUE;
4694 lys_compile_mandatory_parents(node->parent, 0);
4695 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004696 }
4697 break;
4698 default:
4699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4700 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4701 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004702 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004703 }
4704 }
Radek Krejcif0089082019-01-07 16:42:01 +01004705
4706 /* if-feature */
4707 if (rfn->iffeatures) {
4708 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02004709 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004710 }
Radek Krejci01342af2019-01-03 15:18:08 +01004711 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004712
Radek Krejcif2271f12019-01-07 16:42:23 +01004713 /* do some additional checks of the changed nodes when all the refines are applied */
4714 for (u = 0; u < refined.count; ++u) {
4715 node = (struct lysc_node*)refined.objs[u];
4716 rfn = &uses_p->refines[u];
4717
4718 /* check possible conflict with default value (default added, mandatory left true) */
4719 if ((node->flags & LYS_MAND_TRUE) &&
4720 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4721 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4723 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004724 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004725 }
4726
4727 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4728 if (node->nodetype == LYS_LIST) {
4729 min = ((struct lysc_node_list*)node)->min;
4730 max = ((struct lysc_node_list*)node)->max;
4731 } else {
4732 min = ((struct lysc_node_leaflist*)node)->min;
4733 max = ((struct lysc_node_leaflist*)node)->max;
4734 }
4735 if (min > max) {
4736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4737 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4738 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004739 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004740 }
4741 }
4742 }
4743
Radek Krejcie86bf772018-12-14 11:39:53 +01004744 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004745
4746cleanup:
4747 /* fix connection of the children nodes from fake context node back into the parent */
4748 if (context_node_fake.child) {
4749 context_node_fake.child->prev = child;
4750 }
4751 LY_LIST_FOR(context_node_fake.child, child) {
4752 child->parent = parent;
4753 }
4754
4755 if (uses_p->augments || uses_p->refines) {
4756 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004757 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004758 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4759 LY_ARRAY_FREE(context_node_fake.actions);
4760 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004761 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004762 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4763 LY_ARRAY_FREE(context_node_fake.notifs);
4764 }
4765 }
4766
Radek Krejcie86bf772018-12-14 11:39:53 +01004767 /* reload previous context's mod_def */
4768 ctx->mod_def = mod_old;
4769 /* remove the grouping from the stack for circular groupings dependency check */
4770 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4771 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004772 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004773 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004774
4775 return ret;
4776}
4777
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004778/**
4779 * @brief Validate groupings that were defined but not directly used in the schema itself.
4780 *
4781 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
4782 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
4783 */
4784static LY_ERR
4785lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
4786{
4787 LY_ERR ret;
4788 struct lysp_node_uses fake_uses = {
4789 .parent = node_p,
4790 .nodetype = LYS_USES,
4791 .flags = 0, .next = NULL,
4792 .name = grp->name,
4793 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4794 .refines = NULL, .augments = NULL
4795 };
4796 struct lysc_node_container fake_container = {
4797 .nodetype = LYS_CONTAINER,
4798 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
4799 .module = ctx->mod,
4800 .sp = NULL, .parent = NULL, .next = NULL,
4801 .prev = (struct lysc_node*)&fake_container,
4802 .name = "fake",
4803 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
4804 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4805 };
4806
4807 if (grp->parent) {
4808 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4809 }
4810 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
4811
4812 /* cleanup */
4813 lysc_node_container_free(ctx->ctx, &fake_container);
4814
4815 return ret;
4816}
Radek Krejcife909632019-02-12 15:34:42 +01004817
Radek Krejcie86bf772018-12-14 11:39:53 +01004818/**
Radek Krejcia3045382018-11-22 14:30:31 +01004819 * @brief Compile parsed schema node information.
4820 * @param[in] ctx Compile context
4821 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01004822 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4823 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4824 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004825 * @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).
4826 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004827 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4828 */
Radek Krejci19a96102018-11-15 13:38:09 +01004829static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004830lys_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 +01004831{
4832 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004833 struct lysc_node *node;
4834 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004835 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004836 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004837 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01004838
4839 switch (node_p->nodetype) {
4840 case LYS_CONTAINER:
4841 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4842 node_compile_spec = lys_compile_node_container;
4843 break;
4844 case LYS_LEAF:
4845 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4846 node_compile_spec = lys_compile_node_leaf;
4847 break;
4848 case LYS_LIST:
4849 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004850 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004851 break;
4852 case LYS_LEAFLIST:
4853 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004854 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004855 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004856 case LYS_CHOICE:
4857 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004858 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004859 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004860 case LYS_ANYXML:
4861 case LYS_ANYDATA:
4862 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004863 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004864 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004865 case LYS_USES:
Radek Krejciec4da802019-05-02 13:02:41 +02004866 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004867 default:
4868 LOGINT(ctx->ctx);
4869 return LY_EINT;
4870 }
4871 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4872 node->nodetype = node_p->nodetype;
4873 node->module = ctx->mod;
4874 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004875 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004876
4877 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02004878 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004879 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004880 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02004881 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4882 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004883 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004884 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004885 node->flags |= LYS_CONFIG_R;
4886 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004887 /* config not explicitely set, inherit it from parent */
4888 if (parent) {
4889 node->flags |= parent->flags & LYS_CONFIG_MASK;
4890 } else {
4891 /* default is config true */
4892 node->flags |= LYS_CONFIG_W;
4893 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004894 } else {
4895 /* config set explicitely */
4896 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004897 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004898 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4899 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4900 "Configuration node cannot be child of any state data node.");
4901 goto error;
4902 }
Radek Krejci19a96102018-11-15 13:38:09 +01004903
Radek Krejcia6d57732018-11-29 13:40:37 +01004904 /* *list ordering */
4905 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4906 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004907 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02004908 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
4909 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01004910 node->flags &= ~LYS_ORDBY_MASK;
4911 node->flags |= LYS_ORDBY_SYSTEM;
4912 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4913 /* default ordering is system */
4914 node->flags |= LYS_ORDBY_SYSTEM;
4915 }
4916 }
4917
Radek Krejci19a96102018-11-15 13:38:09 +01004918 /* status - it is not inherited by specification, but it does not make sense to have
4919 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004920 if (!parent || parent->nodetype != LYS_CHOICE) {
4921 /* in case of choice/case's children, postpone the check to the moment we know if
4922 * 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 +01004923 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 +01004924 }
4925
Radek Krejciec4da802019-05-02 13:02:41 +02004926 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004927 node->sp = node_p;
4928 }
4929 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004930 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4931 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004932 if (node_p->when) {
4933 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004934 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004935 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004936 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004937 }
Radek Krejciec4da802019-05-02 13:02:41 +02004938 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
4939 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004940
4941 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02004942 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01004943
Radek Krejcife909632019-02-12 15:34:42 +01004944 /* inherit LYS_MAND_TRUE in parent containers */
4945 if (node->flags & LYS_MAND_TRUE) {
4946 lys_compile_mandatory_parents(parent, 1);
4947 }
4948
Radek Krejci19a96102018-11-15 13:38:09 +01004949 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004950 if (parent) {
4951 if (parent->nodetype == LYS_CHOICE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004952 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01004953 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004954 if (uses_status) {
4955
4956 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004957 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004958 * it directly gets the same status flags as the choice;
4959 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004960 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004961 node->parent = (struct lysc_node*)cs;
4962 } else { /* other than choice */
4963 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004964 }
Radek Krejciec4da802019-05-02 13:02:41 +02004965 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 +01004966 } else {
4967 /* top-level element */
4968 if (!ctx->mod->compiled->data) {
4969 ctx->mod->compiled->data = node;
4970 } else {
4971 /* insert at the end of the module's top-level nodes list */
4972 ctx->mod->compiled->data->prev->next = node;
4973 node->prev = ctx->mod->compiled->data->prev;
4974 ctx->mod->compiled->data->prev = node;
4975 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004976 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4977 ctx->mod->compiled->notifs, node->name, node)) {
4978 return LY_EVALID;
4979 }
Radek Krejci19a96102018-11-15 13:38:09 +01004980 }
4981
4982 return LY_SUCCESS;
4983
4984error:
4985 lysc_node_free(ctx->ctx, node);
4986 return ret;
4987}
4988
Radek Krejciccd20f12019-02-15 14:12:27 +01004989static void
4990lysc_disconnect(struct lysc_node *node)
4991{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004992 struct lysc_node *parent, *child, *prev = NULL, *next;
4993 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004994 int remove_cs = 0;
4995
4996 parent = node->parent;
4997
4998 /* parent's first child */
4999 if (!parent) {
5000 return;
5001 }
5002 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005003 cs = (struct lysc_node_case*)node;
5004 } else if (parent->nodetype == LYS_CASE) {
5005 /* disconnecting some node in a case */
5006 cs = (struct lysc_node_case*)parent;
5007 parent = cs->parent;
5008 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5009 if (child == node) {
5010 if (cs->child == child) {
5011 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5012 /* case with a single child -> remove also the case */
5013 child->parent = NULL;
5014 remove_cs = 1;
5015 } else {
5016 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005017 }
5018 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005019 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005020 }
5021 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005022 if (!remove_cs) {
5023 cs = NULL;
5024 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005025 } else if (lysc_node_children(parent, node->flags) == node) {
5026 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005027 }
5028
5029 if (cs) {
5030 if (remove_cs) {
5031 /* cs has only one child which is being also removed */
5032 lysc_disconnect((struct lysc_node*)cs);
5033 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5034 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005035 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5036 /* default case removed */
5037 ((struct lysc_node_choice*)parent)->dflt = NULL;
5038 }
5039 if (((struct lysc_node_choice*)parent)->cases == cs) {
5040 /* first case removed */
5041 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5042 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005043 if (cs->child) {
5044 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5045 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5046 prev = cs->child->prev;
5047 } /* else all the children are under a single case */
5048 LY_LIST_FOR_SAFE(cs->child, next, child) {
5049 if (child->parent != (struct lysc_node*)cs) {
5050 break;
5051 }
5052 lysc_node_free(node->module->ctx, child);
5053 }
5054 if (prev) {
5055 if (prev->next) {
5056 prev->next = child;
5057 }
5058 if (child) {
5059 child->prev = prev;
5060 } else {
5061 /* link from the first child under the cases */
5062 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5063 }
5064 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005065 }
5066 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005067 }
5068
5069 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005070 if (node->prev->next) {
5071 node->prev->next = node->next;
5072 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005073 if (node->next) {
5074 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005075 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005076 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005077 if (child) {
5078 child->prev = node->prev;
5079 }
5080 } else if (((struct lysc_node_choice*)parent)->cases) {
5081 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005082 }
5083}
5084
5085LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005086lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005087{
5088 LY_ERR ret = LY_EVALID;
5089 struct ly_set devs_p = {0};
5090 struct ly_set targets = {0};
5091 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005092 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005093 struct lysc_action *rpcs;
5094 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005095 struct lysp_deviation *dev;
5096 struct lysp_deviate *d, **dp_new;
5097 struct lysp_deviate_add *d_add;
5098 struct lysp_deviate_del *d_del;
5099 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005100 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005101 struct lysc_deviation {
5102 const char *nodeid;
5103 struct lysc_node *target; /* target node of the deviation */
5104 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005105 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005106 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5107 } **devs = NULL;
5108 int i;
5109 size_t prefix_len, name_len;
5110 const char *prefix, *name, *nodeid;
5111 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005112 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005113 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005114
5115 /* get all deviations from the module and all its submodules ... */
5116 LY_ARRAY_FOR(mod_p->deviations, u) {
5117 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5118 }
5119 LY_ARRAY_FOR(mod_p->includes, v) {
5120 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5121 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5122 }
5123 }
5124 if (!devs_p.count) {
5125 /* nothing to do */
5126 return LY_SUCCESS;
5127 }
5128
5129 /* ... and group them by the target node */
5130 devs = calloc(devs_p.count, sizeof *devs);
5131 for (u = 0; u < devs_p.count; ++u) {
5132 dev = devs_p.objs[u];
5133
5134 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005135 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5136 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005137 if (target->nodetype == LYS_ACTION) {
5138 /* move the target pointer to input/output to make them different from the action and
5139 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5140 * back to the RPC/action node due to a better compatibility and decision code in this function.
5141 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5142 if (flags & LYSC_OPT_RPC_INPUT) {
5143 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5144 flags |= LYSC_OPT_INTERNAL;
5145 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5146 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5147 flags |= LYSC_OPT_INTERNAL;
5148 }
5149 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005150 /* insert into the set of targets with duplicity detection */
5151 i = ly_set_add(&targets, target, 0);
5152 if (!devs[i]) {
5153 /* new record */
5154 devs[i] = calloc(1, sizeof **devs);
5155 devs[i]->target = target;
5156 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005157 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005158 }
5159 /* add deviates into the deviation's list of deviates */
5160 for (d = dev->deviates; d; d = d->next) {
5161 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5162 *dp_new = d;
5163 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5164 devs[i]->not_supported = 1;
5165 }
5166 }
5167 }
5168
5169 /* MACROS for deviates checking */
5170#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5171 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
5172 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
5173 goto cleanup; \
5174 }
5175
5176#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5177 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
5178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
5179 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
5180 goto cleanup; \
5181 }
5182
5183#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005184 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005185 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5186 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
5187 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5188 goto cleanup; \
5189 }
5190
Radek Krejci551b12c2019-02-19 16:11:21 +01005191#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5192 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5194 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
5195 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5196 goto cleanup; \
5197 }
5198
Radek Krejciccd20f12019-02-15 14:12:27 +01005199#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5200 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5202 goto cleanup; \
5203 }
5204
Radek Krejci551b12c2019-02-19 16:11:21 +01005205#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5206 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5208 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5209 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5210 goto cleanup; \
5211 }
5212
Radek Krejciccd20f12019-02-15 14:12:27 +01005213#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5214 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5215 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5216 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5217 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5218 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5219 goto cleanup; \
5220 } \
5221 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5222 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5223
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005224#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5225 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5226 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5227 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5228 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 +01005229 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005230 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005231 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5232 "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 +01005233 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005234 goto cleanup; \
5235 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005236 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5237 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5238 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5239 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5240 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005241 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005242 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5243 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5244 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005245 }
5246
5247 /* apply deviations */
5248 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005249 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5250 /* fix the target pointer in case of RPC's/action's input/output */
5251 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5252 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5253 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5254 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5255 }
5256 }
5257
Radek Krejciccd20f12019-02-15 14:12:27 +01005258 /* not-supported */
5259 if (devs[u]->not_supported) {
5260 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5261 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5262 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5263 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005264
5265#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5266 if (devs[u]->target->parent) { \
5267 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5268 } else { \
5269 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5270 } \
5271 LY_ARRAY_FOR(ARRAY, x) { \
5272 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5273 } \
5274 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5275 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5276 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5277 LY_ARRAY_DECREMENT(ARRAY); \
5278 }
5279
Radek Krejcif538ce52019-03-05 10:46:14 +01005280 if (devs[u]->target->nodetype == LYS_ACTION) {
5281 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5282 /* remove RPC's/action's input */
5283 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5284 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005285 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5286 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005287 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5288 /* remove RPC's/action's output */
5289 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5290 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005291 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5292 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005293 } else {
5294 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005295 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005296 }
5297 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005298 /* remove Notification */
5299 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005300 } else {
5301 /* remove the target node */
5302 lysc_disconnect(devs[u]->target);
5303 lysc_node_free(ctx->ctx, devs[u]->target);
5304 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005305
5306 continue;
5307 }
5308
5309 /* list of deviates (not-supported is not present in the list) */
5310 LY_ARRAY_FOR(devs[u]->deviates, v) {
5311 d = devs[u]->deviates[v];
5312
5313 switch (d->mod) {
5314 case LYS_DEV_ADD:
5315 d_add = (struct lysp_deviate_add*)d;
5316 /* [units-stmt] */
5317 if (d_add->units) {
5318 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5319 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5320
5321 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5322 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5323 }
5324
5325 /* *must-stmt */
5326 if (d_add->musts) {
5327 switch (devs[u]->target->nodetype) {
5328 case LYS_CONTAINER:
5329 case LYS_LIST:
5330 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005331 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005332 break;
5333 case LYS_LEAF:
5334 case LYS_LEAFLIST:
5335 case LYS_ANYDATA:
5336 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005337 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005338 break;
5339 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005340 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005341 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005342 break;
5343 case LYS_ACTION:
5344 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5345 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005346 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005347 break;
5348 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5349 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005350 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005351 break;
5352 }
5353 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005354 default:
5355 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005356 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005357 goto cleanup;
5358 }
5359 }
5360
5361 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005362 if (d_add->uniques) {
5363 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5364 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5365 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005366
5367 /* *default-stmt */
5368 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005369 switch (devs[u]->target->nodetype) {
5370 case LYS_LEAF:
5371 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5372 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5373 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5374 /* first, remove the default value taken from the type */
5375 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5376 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5377 }
5378 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005379 /* mark the new default values as leaf's own */
5380 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005381 break;
5382 case LYS_LEAFLIST:
5383 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5384 /* first, remove the default value taken from the type */
5385 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5386 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5387 }
5388 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5389 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5390 }
5391 /* add new default value(s) */
5392 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5393 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5394 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5395 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5396 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5397 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5398 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005399 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005400 devs[u]->target->flags |= LYS_SET_DFLT;
5401 break;
5402 case LYS_CHOICE:
5403 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5404 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5405 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5406 * to allow making the default case even the augmented case from the deviating module */
5407 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5408 (struct lysc_node_choice*)devs[u]->target)) {
5409 goto cleanup;
5410 }
5411 break;
5412 default:
5413 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005414 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005415 goto cleanup;
5416 }
5417 }
5418
5419 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005420 if (d_add->flags & LYS_CONFIG_MASK) {
5421 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5423 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5424 goto cleanup;
5425 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005426 if (devs[u]->flags) {
5427 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5428 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5429 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005430 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5432 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5433 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5434 goto cleanup;
5435 }
5436 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5437 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005438
5439 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005440 if (d_add->flags & LYS_MAND_MASK) {
5441 if (devs[u]->target->flags & LYS_MAND_MASK) {
5442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5443 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5444 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5445 goto cleanup;
5446 }
5447 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5448 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005449
5450 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005451 if (d_add->flags & LYS_SET_MIN) {
5452 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5453 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5454 /* change value */
5455 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5456 } else if (devs[u]->target->nodetype == LYS_LIST) {
5457 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5458 /* change value */
5459 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5460 } else {
5461 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5462 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5463 goto cleanup;
5464 }
5465 if (d_add->min) {
5466 devs[u]->target->flags |= LYS_MAND_TRUE;
5467 }
5468 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005469
5470 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005471 if (d_add->flags & LYS_SET_MAX) {
5472 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5473 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5474 /* change value */
5475 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5476 } else if (devs[u]->target->nodetype == LYS_LIST) {
5477 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5478 /* change value */
5479 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5480 } else {
5481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5482 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5483 goto cleanup;
5484 }
5485 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005486
5487 break;
5488 case LYS_DEV_DELETE:
5489 d_del = (struct lysp_deviate_del*)d;
5490
5491 /* [units-stmt] */
5492 if (d_del->units) {
5493 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5494 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5495 }
5496
5497 /* *must-stmt */
5498 if (d_del->musts) {
5499 switch (devs[u]->target->nodetype) {
5500 case LYS_CONTAINER:
5501 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005502 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005503 break;
5504 case LYS_LEAF:
5505 case LYS_LEAFLIST:
5506 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005507 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005508 break;
5509 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005510 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005511 break;
5512 case LYS_ACTION:
5513 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5514 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5515 break;
5516 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5517 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5518 break;
5519 }
5520 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005521 default:
5522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005523 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005524 goto cleanup;
5525 }
5526 }
5527
5528 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005529 if (d_del->uniques) {
5530 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5531 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5532 LY_ARRAY_FOR(d_del->uniques, x) {
5533 LY_ARRAY_FOR(list->uniques, z) {
5534 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5535 nodeid = strpbrk(name, " \t\n");
5536 if (nodeid) {
5537 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5538 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5539 break;
5540 }
5541 while (isspace(*nodeid)) {
5542 ++nodeid;
5543 }
5544 } else {
5545 if (strcmp(name, list->uniques[z][y]->name)) {
5546 break;
5547 }
5548 }
5549 }
5550 if (!name) {
5551 /* complete match - remove the unique */
5552 LY_ARRAY_DECREMENT(list->uniques);
5553 LY_ARRAY_FREE(list->uniques[z]);
5554 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5555 --z;
5556 break;
5557 }
5558 }
5559 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5560 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5561 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5562 devs[u]->nodeid, d_del->uniques[x]);
5563 goto cleanup;
5564 }
5565 }
5566 if (!LY_ARRAY_SIZE(list->uniques)) {
5567 LY_ARRAY_FREE(list->uniques);
5568 list->uniques = NULL;
5569 }
5570 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005571
5572 /* *default-stmt */
5573 if (d_del->dflts) {
5574 switch (devs[u]->target->nodetype) {
5575 case LYS_LEAF:
5576 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5577 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5578 dflt, "deleting", "default", d_del->dflts[0]);
5579
5580 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005581 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005582 break;
5583 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005584 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005585 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5586 devs[u]->target->flags &= ~LYS_SET_DFLT;
5587 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005588 break;
5589 case LYS_CHOICE:
5590 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5591 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5592 nodeid = d_del->dflts[0];
5593 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5594 if (prefix) {
5595 /* use module prefixes from the deviation module to match the module of the default case */
5596 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5597 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5598 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5599 "The prefix does not match any imported module of the deviation module.",
5600 devs[u]->nodeid, d_del->dflts[0]);
5601 goto cleanup;
5602 }
5603 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5604 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5605 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5606 "The prefix does not match the default case's module.",
5607 devs[u]->nodeid, d_del->dflts[0]);
5608 goto cleanup;
5609 }
5610 }
5611 /* else {
5612 * strictly, the default prefix would point to the deviation module, but the value should actually
5613 * match the default string in the original module (usually unprefixed), so in this case we do not check
5614 * the module of the default case, just matching its name */
5615 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5617 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5618 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5619 goto cleanup;
5620 }
5621 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5622 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5623 break;
5624 default:
5625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005626 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005627 goto cleanup;
5628 }
5629 }
5630
5631 break;
5632 case LYS_DEV_REPLACE:
5633 d_rpl = (struct lysp_deviate_rpl*)d;
5634
5635 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005636 if (d_rpl->type) {
5637 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5638 /* type is mandatory, so checking for its presence is not necessary */
5639 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejciec4da802019-05-02 13:02:41 +02005640 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 +01005641 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005642
5643 /* [units-stmt] */
5644 if (d_rpl->units) {
5645 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5646 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5647 units, "replacing", "units", d_rpl->units);
5648
5649 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5650 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5651 }
5652
5653 /* [default-stmt] */
5654 if (d_rpl->dflt) {
5655 switch (devs[u]->target->nodetype) {
5656 case LYS_LEAF:
5657 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5658 dflt, "replacing", "default", d_rpl->dflt);
5659
5660 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5661 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5662 break;
5663 case LYS_CHOICE:
5664 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5665 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5666 (struct lysc_node_choice*)devs[u]->target)) {
5667 goto cleanup;
5668 }
5669 break;
5670 default:
5671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005672 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005673 goto cleanup;
5674 }
5675 }
5676
5677 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005678 if (d_rpl->flags & LYS_CONFIG_MASK) {
5679 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5681 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5682 goto cleanup;
5683 }
5684 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5686 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5687 goto cleanup;
5688 }
5689 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5690 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005691
5692 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005693 if (d_rpl->flags & LYS_MAND_MASK) {
5694 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5696 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5697 goto cleanup;
5698 }
5699 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5700 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005701
5702 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005703 if (d_rpl->flags & LYS_SET_MIN) {
5704 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5705 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5706 /* change value */
5707 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5708 } else if (devs[u]->target->nodetype == LYS_LIST) {
5709 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5710 /* change value */
5711 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5712 } else {
5713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5714 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5715 goto cleanup;
5716 }
5717 if (d_rpl->min) {
5718 devs[u]->target->flags |= LYS_MAND_TRUE;
5719 }
5720 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005721
5722 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005723 if (d_rpl->flags & LYS_SET_MAX) {
5724 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5725 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5726 /* change value */
5727 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5728 } else if (devs[u]->target->nodetype == LYS_LIST) {
5729 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5730 /* change value */
5731 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5732 } else {
5733 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5734 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5735 goto cleanup;
5736 }
5737 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005738
5739 break;
5740 default:
5741 LOGINT(ctx->ctx);
5742 goto cleanup;
5743 }
5744 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005745
Radek Krejci33f72892019-02-21 10:36:58 +01005746 /* final check when all deviations of a single target node are applied */
5747
Radek Krejci551b12c2019-02-19 16:11:21 +01005748 /* check min-max compatibility */
5749 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5750 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5751 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5752 } else if (devs[u]->target->nodetype == LYS_LIST) {
5753 min = ((struct lysc_node_list*)devs[u]->target)->min;
5754 max = ((struct lysc_node_list*)devs[u]->target)->max;
5755 } else {
5756 min = max = 0;
5757 }
5758 if (min > max) {
5759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5760 "after deviation (%s): min value %u is bigger than max value %u.",
5761 devs[u]->nodeid, min, max);
5762 goto cleanup;
5763 }
5764
5765 /* check mandatory - default compatibility */
5766 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5767 && (devs[u]->target->flags & LYS_SET_DFLT)
5768 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5770 "Invalid deviation (%s) combining default value and mandatory %s.",
5771 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5772 goto cleanup;
5773 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5774 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5775 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5777 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5778 goto cleanup;
5779 }
5780 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5781 /* mandatory node under a default case */
5782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5783 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5784 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5785 goto cleanup;
5786 }
Radek Krejci33f72892019-02-21 10:36:58 +01005787
5788 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005789 }
5790
5791 ret = LY_SUCCESS;
5792
5793cleanup:
5794 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5795 LY_ARRAY_FREE(devs[u]->deviates);
5796 free(devs[u]);
5797 }
5798 free(devs);
5799 ly_set_erase(&targets, NULL);
5800 ly_set_erase(&devs_p, NULL);
5801
5802 return ret;
5803}
5804
Radek Krejcib56c7502019-02-13 14:19:54 +01005805/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005806 * @brief Compile the given YANG submodule into the main module.
5807 * @param[in] ctx Compile context
5808 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01005809 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5810 */
5811LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005812lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01005813{
5814 unsigned int u;
5815 LY_ERR ret = LY_SUCCESS;
5816 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005817 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005818 struct lysc_module *mainmod = ctx->mod->compiled;
5819
Radek Krejci0af46292019-01-11 16:02:31 +01005820 if (!mainmod->mod->off_features) {
5821 /* features are compiled directly into the compiled module structure,
5822 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5823 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci693262f2019-04-29 15:23:20 +02005824 ret = lys_feature_precompile(ctx->ctx, ctx->mod, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01005825 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5826 LY_CHECK_GOTO(ret, error);
5827 }
5828
Radek Krejciec4da802019-05-02 13:02:41 +02005829 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005830
Radek Krejci8cce8532019-03-05 11:27:45 +01005831 /* TODO data nodes */
5832
Radek Krejciec4da802019-05-02 13:02:41 +02005833 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
5834 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01005835
Radek Krejcid05cbd92018-12-05 14:26:40 +01005836error:
5837 return ret;
5838}
5839
Radek Krejci19a96102018-11-15 13:38:09 +01005840LY_ERR
5841lys_compile(struct lys_module *mod, int options)
5842{
5843 struct lysc_ctx ctx = {0};
5844 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005845 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005846 struct lysp_module *sp;
5847 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005848 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005849 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01005850 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005851 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005852 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005853
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005854 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005855
5856 if (!mod->implemented) {
5857 /* just imported modules are not compiled */
5858 return LY_SUCCESS;
5859 }
5860
Radek Krejci19a96102018-11-15 13:38:09 +01005861 sp = mod->parsed;
5862
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005863 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005864 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005865 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02005866 ctx.options = options;
Radek Krejci19a96102018-11-15 13:38:09 +01005867
5868 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005869 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5870 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005871
Radek Krejciec4da802019-05-02 13:02:41 +02005872 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005873 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005874 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005875 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5876 }
Radek Krejci0af46292019-01-11 16:02:31 +01005877 if (mod->off_features) {
5878 /* there is already precompiled array of features */
5879 mod_c->features = mod->off_features;
5880 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005881 } else {
5882 /* features are compiled directly into the compiled module structure,
5883 * 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 +02005884 ret = lys_feature_precompile(ctx.ctx, ctx.mod, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005885 LY_CHECK_GOTO(ret, error);
5886 }
5887 /* finish feature compilation, not only for the main module, but also for the submodules.
5888 * Due to possible forward references, it must be done when all the features (including submodules)
5889 * are present. */
5890 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005891 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005892 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5893 }
5894 LY_ARRAY_FOR(sp->includes, v) {
5895 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005896 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005897 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5898 }
5899 }
5900
Radek Krejciec4da802019-05-02 13:02:41 +02005901 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005902 if (sp->identities) {
5903 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5904 }
5905
Radek Krejci95710c92019-02-11 15:49:55 +01005906 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005907 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02005908 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005909 LY_CHECK_GOTO(ret, error);
5910 }
Radek Krejci95710c92019-02-11 15:49:55 +01005911
Radek Krejciec4da802019-05-02 13:02:41 +02005912 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
5913 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01005914
Radek Krejci95710c92019-02-11 15:49:55 +01005915 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005916 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005917 LY_CHECK_GOTO(ret, error);
5918 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005919 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005920 LY_CHECK_GOTO(ret, error);
5921 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005922
5923 /* deviations */
Radek Krejciec4da802019-05-02 13:02:41 +02005924 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01005925 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005926
Radek Krejciec4da802019-05-02 13:02:41 +02005927 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005928
Radek Krejcia3045382018-11-22 14:30:31 +01005929 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005930 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5931 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5932 * 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 +01005933 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005934 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005935 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5936 if (type->basetype == LY_TYPE_LEAFREF) {
5937 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005938 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 +01005939 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005940 } else if (type->basetype == LY_TYPE_UNION) {
5941 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5942 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5943 /* validate the path */
5944 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5945 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5946 LY_CHECK_GOTO(ret, error);
5947 }
5948 }
Radek Krejcia3045382018-11-22 14:30:31 +01005949 }
5950 }
5951 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005952 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005953 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005954 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5955 if (type->basetype == LY_TYPE_LEAFREF) {
5956 /* store pointer to the real type */
5957 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5958 typeiter->basetype == LY_TYPE_LEAFREF;
5959 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5960 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005961 } else if (type->basetype == LY_TYPE_UNION) {
5962 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5963 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5964 /* store pointer to the real type */
5965 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5966 typeiter->basetype == LY_TYPE_LEAFREF;
5967 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5968 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5969 }
5970 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005971 }
5972 }
5973 }
Radek Krejciec4da802019-05-02 13:02:41 +02005974
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005975 /* validate non-instantiated groupings from the parsed schema,
5976 * without it we would accept even the schemas with invalid grouping specification */
5977 ctx.options |= LYSC_OPT_GROUPING;
5978 LY_ARRAY_FOR(sp->groupings, u) {
5979 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
5980 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
5981 }
5982 }
5983 LY_LIST_FOR(sp->data, node_p) {
5984 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
5985 LY_ARRAY_FOR(grps, u) {
5986 if (!(grps[u].flags & LYS_USED_GRP)) {
5987 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
5988 }
5989 }
5990 }
5991
Radek Krejcia3045382018-11-22 14:30:31 +01005992 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005993 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02005994 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005995 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005996
Radek Krejciec4da802019-05-02 13:02:41 +02005997 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01005998 lysp_module_free(mod->parsed);
5999 ((struct lys_module*)mod)->parsed = NULL;
6000 }
6001
Radek Krejciec4da802019-05-02 13:02:41 +02006002 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006003 /* remove flag of the modules implemented by dependency */
6004 for (u = 0; u < ctx.ctx->list.count; ++u) {
6005 m = ctx.ctx->list.objs[u];
6006 if (m->implemented == 2) {
6007 m->implemented = 1;
6008 }
6009 }
6010 }
6011
Radek Krejci19a96102018-11-15 13:38:09 +01006012 ((struct lys_module*)mod)->compiled = mod_c;
6013 return LY_SUCCESS;
6014
6015error:
Radek Krejci95710c92019-02-11 15:49:55 +01006016 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01006017 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006018 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006019 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006020 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006021 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006022 mod->compiled = NULL;
6023
6024 /* revert compilation of modules implemented by dependency */
6025 for (u = 0; u < ctx.ctx->list.count; ++u) {
6026 m = ctx.ctx->list.objs[u];
6027 if (m->implemented == 2) {
6028 /* revert features list to the precompiled state */
6029 lys_feature_precompile_revert(&ctx, m);
6030 /* mark module as imported-only / not-implemented */
6031 m->implemented = 0;
6032 /* free the compiled version of the module */
6033 lysc_module_free(m->compiled, NULL);
6034 m->compiled = NULL;
6035 }
6036 }
6037
Radek Krejci19a96102018-11-15 13:38:09 +01006038 return ret;
6039}