blob: e0af0b619695b4227243d46376a63711b2bb3c45 [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
17#include <ctype.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <stdio.h>
Radek Krejci19a96102018-11-15 13:38:09 +010019
20#include "libyang.h"
21#include "context.h"
22#include "tree_schema_internal.h"
23#include "xpath.h"
24
25/**
26 * @brief Duplicate string into dictionary
27 * @param[in] CTX libyang context of the dictionary.
28 * @param[in] ORIG String to duplicate.
29 * @param[out] DUP Where to store the result.
30 */
31#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
32
33#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
34 if (ARRAY_P) { \
35 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010036 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010037 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
38 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010039 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER + __array_offset]); \
40 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
41 } \
42 }
43
Radek Krejci6eeb58f2019-02-22 16:29:37 +010044#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, OPTIONS, ITER, FUNC, USES_STATUS, RET, GOTO) \
45 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
47 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
48 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
50 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
51 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejcid05cbd92018-12-05 14:26:40 +010055#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
56 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
61 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
66#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
67 if (MEMBER_P) { \
68 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
69 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
70 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
71 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 }
73
Radek Krejci00b874b2019-02-12 10:54:50 +010074#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \
75 if (MEMBER_P) { \
76 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
77 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
78 LY_ARRAY_INCREMENT(ARRAY_C); \
79 RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \
80 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
81 }
82
Radek Krejcid05cbd92018-12-05 14:26:40 +010083#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
84 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010085 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
86 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
88 return LY_EVALID; \
89 } \
90 } \
91 }
92
Radek Krejci19a96102018-11-15 13:38:09 +010093static struct lysc_ext_instance *
94lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
95{
96 /* TODO */
97 (void) ctx;
98 (void) orig;
99 return NULL;
100}
101
Radek Krejcib56c7502019-02-13 14:19:54 +0100102/**
103 * @brief Duplicate the compiled pattern structure.
104 *
105 * Instead of duplicating memory, the reference counter in the @p orig is increased.
106 *
107 * @param[in] orig The pattern structure to duplicate.
108 * @return The duplicated structure to use.
109 */
Radek Krejci19a96102018-11-15 13:38:09 +0100110static struct lysc_pattern*
111lysc_pattern_dup(struct lysc_pattern *orig)
112{
113 ++orig->refcount;
114 return orig;
115}
116
Radek Krejcib56c7502019-02-13 14:19:54 +0100117/**
118 * @brief Duplicate the array of compiled patterns.
119 *
120 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
121 *
122 * @param[in] ctx Libyang context for logging.
123 * @param[in] orig The patterns sized array to duplicate.
124 * @return New sized array as a copy of @p orig.
125 * @return NULL in case of memory allocation error.
126 */
Radek Krejci19a96102018-11-15 13:38:09 +0100127static struct lysc_pattern**
128lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
129{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100130 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100131 unsigned int u;
132
Radek Krejcib56c7502019-02-13 14:19:54 +0100133 assert(orig);
134
Radek Krejci19a96102018-11-15 13:38:09 +0100135 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
136 LY_ARRAY_FOR(orig, u) {
137 dup[u] = lysc_pattern_dup(orig[u]);
138 LY_ARRAY_INCREMENT(dup);
139 }
140 return dup;
141}
142
Radek Krejcib56c7502019-02-13 14:19:54 +0100143/**
144 * @brief Duplicate compiled range structure.
145 *
146 * @param[in] ctx Libyang context for logging.
147 * @param[in] orig The range structure to be duplicated.
148 * @return New compiled range structure as a copy of @p orig.
149 * @return NULL in case of memory allocation error.
150 */
Radek Krejci19a96102018-11-15 13:38:09 +0100151struct lysc_range*
152lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
153{
154 struct lysc_range *dup;
155 LY_ERR ret;
156
Radek Krejcib56c7502019-02-13 14:19:54 +0100157 assert(orig);
158
Radek Krejci19a96102018-11-15 13:38:09 +0100159 dup = calloc(1, sizeof *dup);
160 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
161 if (orig->parts) {
162 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
163 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
164 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
165 }
166 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
167 DUP_STRING(ctx, orig->emsg, dup->emsg);
168 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
169
170 return dup;
171cleanup:
172 free(dup);
173 (void) ret; /* set but not used due to the return type */
174 return NULL;
175}
176
Radek Krejcib56c7502019-02-13 14:19:54 +0100177/**
178 * @brief Stack for processing if-feature expressions.
179 */
Radek Krejci19a96102018-11-15 13:38:09 +0100180struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100181 int size; /**< number of items in the stack */
182 int index; /**< first empty item */
183 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100184};
185
Radek Krejcib56c7502019-02-13 14:19:54 +0100186/**
187 * @brief Add @ref ifftokens into the stack.
188 * @param[in] stack The if-feature stack to use.
189 * @param[in] value One of the @ref ifftokens to store in the stack.
190 * @return LY_EMEM in case of memory allocation error
191 * @return LY_ESUCCESS if the value successfully stored.
192 */
Radek Krejci19a96102018-11-15 13:38:09 +0100193static LY_ERR
194iff_stack_push(struct iff_stack *stack, uint8_t value)
195{
196 if (stack->index == stack->size) {
197 stack->size += 4;
198 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
199 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
200 }
201 stack->stack[stack->index++] = value;
202 return LY_SUCCESS;
203}
204
Radek Krejcib56c7502019-02-13 14:19:54 +0100205/**
206 * @brief Get (and remove) the last item form the stack.
207 * @param[in] stack The if-feature stack to use.
208 * @return The value from the top of the stack.
209 */
Radek Krejci19a96102018-11-15 13:38:09 +0100210static uint8_t
211iff_stack_pop(struct iff_stack *stack)
212{
Radek Krejcib56c7502019-02-13 14:19:54 +0100213 assert(stack && stack->index);
214
Radek Krejci19a96102018-11-15 13:38:09 +0100215 stack->index--;
216 return stack->stack[stack->index];
217}
218
Radek Krejcib56c7502019-02-13 14:19:54 +0100219/**
220 * @brief Clean up the stack.
221 * @param[in] stack The if-feature stack to use.
222 */
Radek Krejci19a96102018-11-15 13:38:09 +0100223static void
224iff_stack_clean(struct iff_stack *stack)
225{
226 stack->size = 0;
227 free(stack->stack);
228}
229
Radek Krejcib56c7502019-02-13 14:19:54 +0100230/**
231 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
232 * (libyang format of the if-feature expression).
233 * @param[in,out] list The 2bits array to modify.
234 * @param[in] op The operand (@ref ifftokens) to store.
235 * @param[in] pos Position (0-based) where to store the given @p op.
236 */
Radek Krejci19a96102018-11-15 13:38:09 +0100237static void
238iff_setop(uint8_t *list, uint8_t op, int pos)
239{
240 uint8_t *item;
241 uint8_t mask = 3;
242
243 assert(pos >= 0);
244 assert(op <= 3); /* max 2 bits */
245
246 item = &list[pos / 4];
247 mask = mask << 2 * (pos % 4);
248 *item = (*item) & ~mask;
249 *item = (*item) | (op << 2 * (pos % 4));
250}
251
Radek Krejcib56c7502019-02-13 14:19:54 +0100252#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
253#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100254
Radek Krejci0af46292019-01-11 16:02:31 +0100255/**
256 * @brief Find a feature of the given name and referenced in the given module.
257 *
258 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
259 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
260 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
261 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
262 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
263 * assigned till that time will be still valid.
264 *
265 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
266 * @param[in] name Name of the feature including possible prefix.
267 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
268 * @return Pointer to the feature structure if found, NULL otherwise.
269 */
Radek Krejci19a96102018-11-15 13:38:09 +0100270static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100271lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100272{
273 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100274 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100275
276 for (i = 0; i < len; ++i) {
277 if (name[i] == ':') {
278 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100279 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100280 LY_CHECK_RET(!mod, NULL);
281
282 name = &name[i + 1];
283 len = len - i - 1;
284 }
285 }
286
287 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100288 if (mod->implemented) {
289 /* module is implemented so there is already the compiled schema */
290 flist = mod->compiled->features;
291 } else {
292 flist = mod->off_features;
293 }
294 LY_ARRAY_FOR(flist, i) {
295 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100296 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
297 return f;
298 }
299 }
300
301 return NULL;
302}
303
304static LY_ERR
305lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
306{
307 const char *name;
308 unsigned int u;
309 const struct lys_module *mod;
310 struct lysp_ext *edef = NULL;
311
312 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
313 ext->insubstmt = ext_p->insubstmt;
314 ext->insubstmt_index = ext_p->insubstmt_index;
315
316 /* get module where the extension definition should be placed */
317 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100318 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100319 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
320 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
321 LY_EVALID);
322 LY_CHECK_ERR_RET(!mod->parsed->extensions,
323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
324 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100325 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100326 LY_EVALID);
327 name = &ext_p->name[u + 1];
328 /* find the extension definition there */
329 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
330 if (!strcmp(name, mod->parsed->extensions[u].name)) {
331 edef = &mod->parsed->extensions[u];
332 break;
333 }
334 }
335 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
336 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
337 LY_EVALID);
338 /* TODO plugins */
339
340 return LY_SUCCESS;
341}
342
Radek Krejcib56c7502019-02-13 14:19:54 +0100343/**
344 * @brief Compile information from the if-feature statement
345 * @param[in] ctx Compile context.
346 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
347 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
348 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
349 * @return LY_ERR value.
350 */
Radek Krejci19a96102018-11-15 13:38:09 +0100351static LY_ERR
352lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
353{
354 const char *c = *value;
355 int r, rc = EXIT_FAILURE;
356 int i, j, last_not, checkversion = 0;
357 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
358 uint8_t op;
359 struct iff_stack stack = {0, 0, NULL};
360 struct lysc_feature *f;
361
362 assert(c);
363
364 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
365 for (i = j = last_not = 0; c[i]; i++) {
366 if (c[i] == '(') {
367 j++;
368 checkversion = 1;
369 continue;
370 } else if (c[i] == ')') {
371 j--;
372 continue;
373 } else if (isspace(c[i])) {
374 checkversion = 1;
375 continue;
376 }
377
378 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
379 if (c[i + r] == '\0') {
380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
381 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
382 return LY_EVALID;
383 } else if (!isspace(c[i + r])) {
384 /* feature name starting with the not/and/or */
385 last_not = 0;
386 f_size++;
387 } else if (c[i] == 'n') { /* not operation */
388 if (last_not) {
389 /* double not */
390 expr_size = expr_size - 2;
391 last_not = 0;
392 } else {
393 last_not = 1;
394 }
395 } else { /* and, or */
396 f_exp++;
397 /* not a not operation */
398 last_not = 0;
399 }
400 i += r;
401 } else {
402 f_size++;
403 last_not = 0;
404 }
405 expr_size++;
406
407 while (!isspace(c[i])) {
408 if (!c[i] || c[i] == ')') {
409 i--;
410 break;
411 }
412 i++;
413 }
414 }
415 if (j || f_exp != f_size) {
416 /* not matching count of ( and ) */
417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
418 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
419 return LY_EVALID;
420 }
421
422 if (checkversion || expr_size > 1) {
423 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100424 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
426 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
427 return LY_EVALID;
428 }
429 }
430
431 /* allocate the memory */
432 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
433 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
434 stack.stack = malloc(expr_size * sizeof *stack.stack);
435 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
436
437 stack.size = expr_size;
438 f_size--; expr_size--; /* used as indexes from now */
439
440 for (i--; i >= 0; i--) {
441 if (c[i] == ')') {
442 /* push it on stack */
443 iff_stack_push(&stack, LYS_IFF_RP);
444 continue;
445 } else if (c[i] == '(') {
446 /* pop from the stack into result all operators until ) */
447 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
448 iff_setop(iff->expr, op, expr_size--);
449 }
450 continue;
451 } else if (isspace(c[i])) {
452 continue;
453 }
454
455 /* end of operator or operand -> find beginning and get what is it */
456 j = i + 1;
457 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
458 i--;
459 }
460 i++; /* go back by one step */
461
462 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
463 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
464 /* double not */
465 iff_stack_pop(&stack);
466 } else {
467 /* not has the highest priority, so do not pop from the stack
468 * as in case of AND and OR */
469 iff_stack_push(&stack, LYS_IFF_NOT);
470 }
471 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
472 /* as for OR - pop from the stack all operators with the same or higher
473 * priority and store them to the result, then push the AND to the stack */
474 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
475 op = iff_stack_pop(&stack);
476 iff_setop(iff->expr, op, expr_size--);
477 }
478 iff_stack_push(&stack, LYS_IFF_AND);
479 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
480 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
481 op = iff_stack_pop(&stack);
482 iff_setop(iff->expr, op, expr_size--);
483 }
484 iff_stack_push(&stack, LYS_IFF_OR);
485 } else {
486 /* feature name, length is j - i */
487
488 /* add it to the expression */
489 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
490
491 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100492 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
493 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
494 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
495 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100496 iff->features[f_size] = f;
497 LY_ARRAY_INCREMENT(iff->features);
498 f_size--;
499 }
500 }
501 while (stack.index) {
502 op = iff_stack_pop(&stack);
503 iff_setop(iff->expr, op, expr_size--);
504 }
505
506 if (++expr_size || ++f_size) {
507 /* not all expected operators and operands found */
508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
509 "Invalid value \"%s\" of if-feature - processing error.", *value);
510 rc = LY_EINT;
511 } else {
512 rc = LY_SUCCESS;
513 }
514
515error:
516 /* cleanup */
517 iff_stack_clean(&stack);
518
519 return rc;
520}
521
Radek Krejcib56c7502019-02-13 14:19:54 +0100522/**
523 * @brief Compile information from the when statement
524 * @param[in] ctx Compile context.
525 * @param[in] when_p The parsed when statement structure.
526 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
527 * @param[out] when Pointer where to store pointer to the created compiled when structure.
528 * @return LY_ERR value.
529 */
Radek Krejci19a96102018-11-15 13:38:09 +0100530static LY_ERR
Radek Krejci00b874b2019-02-12 10:54:50 +0100531lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100532{
533 unsigned int u;
534 LY_ERR ret = LY_SUCCESS;
535
Radek Krejci00b874b2019-02-12 10:54:50 +0100536 *when = calloc(1, sizeof **when);
537 (*when)->refcount = 1;
538 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
539 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
540 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
541 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
542 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, options, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100543
544done:
545 return ret;
546}
547
Radek Krejcib56c7502019-02-13 14:19:54 +0100548/**
549 * @brief Compile information from the must statement
550 * @param[in] ctx Compile context.
551 * @param[in] must_p The parsed must statement structure.
552 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
553 * @param[in,out] must Prepared (empty) compiled must structure to fill.
554 * @return LY_ERR value.
555 */
Radek Krejci19a96102018-11-15 13:38:09 +0100556static LY_ERR
557lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
558{
559 unsigned int u;
560 LY_ERR ret = LY_SUCCESS;
561
562 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
563 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
564
565 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
566 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100567 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
568 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100569 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
570
571done:
572 return ret;
573}
574
Radek Krejcib56c7502019-02-13 14:19:54 +0100575/**
576 * @brief Compile information from the import statement
577 * @param[in] ctx Compile context.
578 * @param[in] imp_p The parsed import statement structure.
579 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
580 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
581 * @return LY_ERR value.
582 */
Radek Krejci19a96102018-11-15 13:38:09 +0100583static LY_ERR
584lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
585{
586 unsigned int u;
587 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100588 LY_ERR ret = LY_SUCCESS;
589
590 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
591 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
592 imp->module = imp_p->module;
593
Radek Krejci7f2a5362018-11-28 13:05:37 +0100594 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
595 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100596 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100597 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 /* try to use filepath if present */
599 if (imp->module->filepath) {
600 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
601 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100602 if (mod != imp->module) {
603 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100605 mod = NULL;
606 }
607 }
608 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100609 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100610 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 +0100611 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100612 return LY_ENOTFOUND;
613 }
614 }
Radek Krejci19a96102018-11-15 13:38:09 +0100615 }
616
617done:
618 return ret;
619}
620
Radek Krejcib56c7502019-02-13 14:19:54 +0100621/**
622 * @brief Compile information from the identity statement
623 *
624 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
625 *
626 * @param[in] ctx Compile context.
627 * @param[in] ident_p The parsed identity statement structure.
628 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
629 * @param[in] idents List of so far compiled identities to check the name uniqueness.
630 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
631 * @return LY_ERR value.
632 */
Radek Krejci19a96102018-11-15 13:38:09 +0100633static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100634lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100635{
636 unsigned int u;
637 LY_ERR ret = LY_SUCCESS;
638
Radek Krejcid05cbd92018-12-05 14:26:40 +0100639 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100640 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200641 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
642 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200643 ident->module = ctx->mod;
Radek Krejci19a96102018-11-15 13:38:09 +0100644 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
645 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
646 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
647 ident->flags = ident_p->flags;
648
649done:
650 return ret;
651}
652
Radek Krejcib56c7502019-02-13 14:19:54 +0100653/**
654 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
655 *
656 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
657 *
658 * @param[in] ctx Compile context for logging.
659 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
660 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
661 * @return LY_SUCCESS if everything is ok.
662 * @return LY_EVALID if the identity is derived from itself.
663 */
Radek Krejci38222632019-02-12 16:55:05 +0100664static LY_ERR
665lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
666{
667 LY_ERR ret = LY_EVALID;
668 unsigned int u, v;
669 struct ly_set recursion = {0};
670 struct lysc_ident *drv;
671
672 if (!derived) {
673 return LY_SUCCESS;
674 }
675
676 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
677 if (ident == derived[u]) {
678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
679 "Identity \"%s\" is indirectly derived from itself.", ident->name);
680 goto cleanup;
681 }
682 ly_set_add(&recursion, derived[u], 0);
683 }
684
685 for (v = 0; v < recursion.count; ++v) {
686 drv = recursion.objs[v];
687 if (!drv->derived) {
688 continue;
689 }
690 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
691 if (ident == drv->derived[u]) {
692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
693 "Identity \"%s\" is indirectly derived from itself.", ident->name);
694 goto cleanup;
695 }
696 ly_set_add(&recursion, drv->derived[u], 0);
697 }
698 }
699 ret = LY_SUCCESS;
700
701cleanup:
702 ly_set_erase(&recursion, NULL);
703 return ret;
704}
705
Radek Krejcia3045382018-11-22 14:30:31 +0100706/**
707 * @brief Find and process the referenced base identities from another identity or identityref
708 *
709 * For bases in identity se backlinks to them from the base identities. For identityref, store
710 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
711 * to distinguish these two use cases.
712 *
713 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
714 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
715 * @param[in] ident Referencing identity to work with.
716 * @param[in] bases Array of bases of identityref to fill in.
717 * @return LY_ERR value.
718 */
Radek Krejci19a96102018-11-15 13:38:09 +0100719static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100720lys_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 +0100721{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100722 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100723 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100724 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100725 struct lysc_ident **idref;
726
727 assert(ident || bases);
728
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
731 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
732 return LY_EVALID;
733 }
734
735 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
736 s = strchr(bases_p[u], ':');
737 if (s) {
738 /* prefixed identity */
739 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100740 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100741 } else {
742 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100743 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100744 }
745 if (!mod) {
746 if (ident) {
747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
748 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
749 } else {
750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
751 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
752 }
753 return LY_EVALID;
754 }
755 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100756 if (mod->compiled && mod->compiled->identities) {
757 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
758 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100759 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100760 if (ident == &mod->compiled->identities[v]) {
761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
762 "Identity \"%s\" is derived from itself.", ident->name);
763 return LY_EVALID;
764 }
765 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100766 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100767 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100768 *idref = ident;
769 } else {
770 /* we have match! store the found identity */
771 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100772 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100773 }
774 break;
775 }
776 }
777 }
778 if (!idref || !(*idref)) {
779 if (ident) {
780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
781 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
782 } else {
783 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
784 "Unable to find base (%s) of identityref.", bases_p[u]);
785 }
786 return LY_EVALID;
787 }
788 }
789 return LY_SUCCESS;
790}
791
Radek Krejcia3045382018-11-22 14:30:31 +0100792/**
793 * @brief For the given array of identities, set the backlinks from all their base identities.
794 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
795 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
796 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
797 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
798 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100799static LY_ERR
800lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
801{
802 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100803
804 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
805 if (!idents_p[i].bases) {
806 continue;
807 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100808 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100809 }
810 return LY_SUCCESS;
811}
812
Radek Krejci0af46292019-01-11 16:02:31 +0100813LY_ERR
Radek Krejci693262f2019-04-29 15:23:20 +0200814lys_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 +0100815{
816 unsigned int offset = 0, u;
817 struct lysc_ctx context = {0};
818
819 assert(ctx);
820 context.ctx = ctx;
821
822 if (!features_p) {
823 return LY_SUCCESS;
824 }
825 if (*features) {
826 offset = LY_ARRAY_SIZE(*features);
827 }
828
829 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
830 LY_ARRAY_FOR(features_p, u) {
831 LY_ARRAY_INCREMENT(*features);
832 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
833 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
834 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
835 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
836 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci693262f2019-04-29 15:23:20 +0200837 (*features)[offset + u].module = module;
Radek Krejci0af46292019-01-11 16:02:31 +0100838 }
839
840 return LY_SUCCESS;
841}
842
Radek Krejcia3045382018-11-22 14:30:31 +0100843/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100844 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100845 *
846 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
847 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100848 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100849 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
850 * being currently processed).
851 * @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 +0100852 * @return LY_SUCCESS if everything is ok.
853 * @return LY_EVALID if the feature references indirectly itself.
854 */
855static LY_ERR
856lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
857{
858 LY_ERR ret = LY_EVALID;
859 unsigned int u, v;
860 struct ly_set recursion = {0};
861 struct lysc_feature *drv;
862
863 if (!depfeatures) {
864 return LY_SUCCESS;
865 }
866
867 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
868 if (feature == depfeatures[u]) {
869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
870 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
871 goto cleanup;
872 }
873 ly_set_add(&recursion, depfeatures[u], 0);
874 }
875
876 for (v = 0; v < recursion.count; ++v) {
877 drv = recursion.objs[v];
878 if (!drv->depfeatures) {
879 continue;
880 }
881 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
882 if (feature == drv->depfeatures[u]) {
883 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
884 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
885 goto cleanup;
886 }
887 ly_set_add(&recursion, drv->depfeatures[u], 0);
888 }
889 }
890 ret = LY_SUCCESS;
891
892cleanup:
893 ly_set_erase(&recursion, NULL);
894 return ret;
895}
896
897/**
Radek Krejci0af46292019-01-11 16:02:31 +0100898 * @brief Create pre-compiled features array.
899 *
900 * See lys_feature_precompile() for more details.
901 *
Radek Krejcia3045382018-11-22 14:30:31 +0100902 * @param[in] ctx Compile context.
903 * @param[in] feature_p Parsed feature definition to compile.
904 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci0af46292019-01-11 16:02:31 +0100905 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100906 * @return LY_ERR value.
907 */
Radek Krejci19a96102018-11-15 13:38:09 +0100908static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100909lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +0100910{
Radek Krejci0af46292019-01-11 16:02:31 +0100911 unsigned int u, v, x;
912 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100913 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100914
Radek Krejci0af46292019-01-11 16:02:31 +0100915 /* find the preprecompiled feature */
916 LY_ARRAY_FOR(features, x) {
917 if (strcmp(features[x].name, feature_p->name)) {
918 continue;
919 }
920 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100921
Radek Krejci0af46292019-01-11 16:02:31 +0100922 /* finish compilation started in lys_feature_precompile() */
923 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
924 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
925 if (feature->iffeatures) {
926 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
927 if (feature->iffeatures[u].features) {
928 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100929 /* check for circular dependency - direct reference first,... */
930 if (feature == feature->iffeatures[u].features[v]) {
931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
932 "Feature \"%s\" is referenced from itself.", feature->name);
933 return LY_EVALID;
934 }
935 /* ... and indirect circular reference */
936 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
937
Radek Krejci0af46292019-01-11 16:02:31 +0100938 /* add itself into the dependants list */
939 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
940 *df = feature;
941 }
Radek Krejci19a96102018-11-15 13:38:09 +0100942 }
Radek Krejci19a96102018-11-15 13:38:09 +0100943 }
944 }
Radek Krejci0af46292019-01-11 16:02:31 +0100945 done:
946 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100947 }
Radek Krejci0af46292019-01-11 16:02:31 +0100948
949 LOGINT(ctx->ctx);
950 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100951}
952
Radek Krejcib56c7502019-02-13 14:19:54 +0100953/**
954 * @brief Revert compiled list of features back to the precompiled state.
955 *
956 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
957 * The features are supposed to be stored again as off_features in ::lys_module structure.
958 *
959 * @param[in] ctx Compilation context.
960 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
961 * and supposed to hold the off_features list.
962 */
Radek Krejci95710c92019-02-11 15:49:55 +0100963static void
964lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
965{
966 unsigned int u, v;
967
968 /* keep the off_features list until the complete lys_module is freed */
969 mod->off_features = mod->compiled->features;
970 mod->compiled->features = NULL;
971
972 /* in the off_features list, remove all the parts (from finished compiling process)
973 * which may points into the data being freed here */
974 LY_ARRAY_FOR(mod->off_features, u) {
975 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
976 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
977 }
978 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
979 mod->off_features[u].iffeatures = NULL;
980
981 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
982 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
983 }
984 LY_ARRAY_FREE(mod->off_features[u].exts);
985 mod->off_features[u].exts = NULL;
986 }
987}
988
Radek Krejcia3045382018-11-22 14:30:31 +0100989/**
990 * @brief Validate and normalize numeric value from a range definition.
991 * @param[in] ctx Compile context.
992 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
993 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
994 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
995 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
996 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
997 * @param[in] value String value of the range boundary.
998 * @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.
999 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1000 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1001 */
Radek Krejci19a96102018-11-15 13:38:09 +01001002static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001003range_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 +01001004{
Radek Krejci6cba4292018-11-15 17:33:29 +01001005 size_t fraction = 0, size;
1006
Radek Krejci19a96102018-11-15 13:38:09 +01001007 *len = 0;
1008
1009 assert(value);
1010 /* parse value */
1011 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1012 return LY_EVALID;
1013 }
1014
1015 if ((value[*len] == '-') || (value[*len] == '+')) {
1016 ++(*len);
1017 }
1018
1019 while (isdigit(value[*len])) {
1020 ++(*len);
1021 }
1022
1023 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001024 if (basetype == LY_TYPE_DEC64) {
1025 goto decimal;
1026 } else {
1027 *valcopy = strndup(value, *len);
1028 return LY_SUCCESS;
1029 }
Radek Krejci19a96102018-11-15 13:38:09 +01001030 }
1031 fraction = *len;
1032
1033 ++(*len);
1034 while (isdigit(value[*len])) {
1035 ++(*len);
1036 }
1037
Radek Krejci6cba4292018-11-15 17:33:29 +01001038 if (basetype == LY_TYPE_DEC64) {
1039decimal:
1040 assert(frdigits);
1041 if (*len - 1 - fraction > frdigits) {
1042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1043 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1044 *len, value, frdigits);
1045 return LY_EINVAL;
1046 }
1047 if (fraction) {
1048 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1049 } else {
1050 size = (*len) + frdigits + 1;
1051 }
1052 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001053 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1054
Radek Krejci6cba4292018-11-15 17:33:29 +01001055 (*valcopy)[size - 1] = '\0';
1056 if (fraction) {
1057 memcpy(&(*valcopy)[0], &value[0], fraction);
1058 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1059 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1060 } else {
1061 memcpy(&(*valcopy)[0], &value[0], *len);
1062 memset(&(*valcopy)[*len], '0', frdigits);
1063 }
Radek Krejci19a96102018-11-15 13:38:09 +01001064 }
1065 return LY_SUCCESS;
1066}
1067
Radek Krejcia3045382018-11-22 14:30:31 +01001068/**
1069 * @brief Check that values in range are in ascendant order.
1070 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001071 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1072 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001073 * @param[in] value Current value to check.
1074 * @param[in] prev_value The last seen value.
1075 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1076 */
Radek Krejci19a96102018-11-15 13:38:09 +01001077static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001078range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001079{
1080 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001081 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001082 return LY_EEXIST;
1083 }
1084 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001085 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001086 return LY_EEXIST;
1087 }
1088 }
1089 return LY_SUCCESS;
1090}
1091
Radek Krejcia3045382018-11-22 14:30:31 +01001092/**
1093 * @brief Set min/max value of the range part.
1094 * @param[in] ctx Compile context.
1095 * @param[in] part Range part structure to fill.
1096 * @param[in] max Flag to distinguish if storing min or max value.
1097 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1098 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1099 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1100 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1101 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001102 * @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 +01001103 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1104 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1105 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1106 * frdigits value), LY_EMEM.
1107 */
Radek Krejci19a96102018-11-15 13:38:09 +01001108static LY_ERR
1109range_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 +01001110 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001111{
1112 LY_ERR ret = LY_SUCCESS;
1113 char *valcopy = NULL;
1114 size_t len;
1115
1116 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001117 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001118 LY_CHECK_GOTO(ret, finalize);
1119 }
1120 if (!valcopy && base_range) {
1121 if (max) {
1122 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1123 } else {
1124 part->min_64 = base_range->parts[0].min_64;
1125 }
1126 if (!first) {
1127 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1128 }
1129 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001130 }
1131
1132 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001133 case LY_TYPE_INT8: /* range */
1134 if (valcopy) {
1135 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1136 } else if (max) {
1137 part->max_64 = INT64_C(127);
1138 } else {
1139 part->min_64 = INT64_C(-128);
1140 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001141 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001142 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001143 }
1144 break;
1145 case LY_TYPE_INT16: /* range */
1146 if (valcopy) {
1147 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1148 } else if (max) {
1149 part->max_64 = INT64_C(32767);
1150 } else {
1151 part->min_64 = INT64_C(-32768);
1152 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001153 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001154 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001155 }
1156 break;
1157 case LY_TYPE_INT32: /* range */
1158 if (valcopy) {
1159 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1160 } else if (max) {
1161 part->max_64 = INT64_C(2147483647);
1162 } else {
1163 part->min_64 = INT64_C(-2147483648);
1164 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001165 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001166 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001167 }
1168 break;
1169 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001170 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001171 if (valcopy) {
1172 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1173 max ? &part->max_64 : &part->min_64);
1174 } else if (max) {
1175 part->max_64 = INT64_C(9223372036854775807);
1176 } else {
1177 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1178 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001179 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001180 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001181 }
1182 break;
1183 case LY_TYPE_UINT8: /* range */
1184 if (valcopy) {
1185 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1186 } else if (max) {
1187 part->max_u64 = UINT64_C(255);
1188 } else {
1189 part->min_u64 = UINT64_C(0);
1190 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001191 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001192 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001193 }
1194 break;
1195 case LY_TYPE_UINT16: /* range */
1196 if (valcopy) {
1197 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1198 } else if (max) {
1199 part->max_u64 = UINT64_C(65535);
1200 } else {
1201 part->min_u64 = UINT64_C(0);
1202 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001203 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001204 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001205 }
1206 break;
1207 case LY_TYPE_UINT32: /* range */
1208 if (valcopy) {
1209 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1210 } else if (max) {
1211 part->max_u64 = UINT64_C(4294967295);
1212 } else {
1213 part->min_u64 = UINT64_C(0);
1214 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001215 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001216 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001217 }
1218 break;
1219 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001220 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001221 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001222 if (valcopy) {
1223 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1224 } else if (max) {
1225 part->max_u64 = UINT64_C(18446744073709551615);
1226 } else {
1227 part->min_u64 = UINT64_C(0);
1228 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001229 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001230 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001231 }
1232 break;
1233 default:
1234 LOGINT(ctx->ctx);
1235 ret = LY_EINT;
1236 }
1237
Radek Krejci5969f272018-11-23 10:03:58 +01001238finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001239 if (ret == LY_EDENIED) {
1240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1241 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1242 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1243 } else if (ret == LY_EVALID) {
1244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1245 "Invalid %s restriction - invalid value \"%s\".",
1246 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1247 } else if (ret == LY_EEXIST) {
1248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1249 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001250 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001251 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001252 } else if (!ret && value) {
1253 *value = *value + len;
1254 }
1255 free(valcopy);
1256 return ret;
1257}
1258
Radek Krejcia3045382018-11-22 14:30:31 +01001259/**
1260 * @brief Compile the parsed range restriction.
1261 * @param[in] ctx Compile context.
1262 * @param[in] range_p Parsed range structure to compile.
1263 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1264 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1265 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1266 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1267 * range restriction must be more restrictive than the base_range.
1268 * @param[in,out] range Pointer to the created current range structure.
1269 * @return LY_ERR value.
1270 */
Radek Krejci19a96102018-11-15 13:38:09 +01001271static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001272lys_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 +01001273 struct lysc_range *base_range, struct lysc_range **range)
1274{
1275 LY_ERR ret = LY_EVALID;
1276 const char *expr;
1277 struct lysc_range_part *parts = NULL, *part;
1278 int range_expected = 0, uns;
1279 unsigned int parts_done = 0, u, v;
1280
1281 assert(range);
1282 assert(range_p);
1283
1284 expr = range_p->arg;
1285 while(1) {
1286 if (isspace(*expr)) {
1287 ++expr;
1288 } else if (*expr == '\0') {
1289 if (range_expected) {
1290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1291 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1292 length_restr ? "length" : "range", range_p->arg);
1293 goto cleanup;
1294 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1296 "Invalid %s restriction - unexpected end of the expression (%s).",
1297 length_restr ? "length" : "range", range_p->arg);
1298 goto cleanup;
1299 }
1300 parts_done++;
1301 break;
1302 } else if (!strncmp(expr, "min", 3)) {
1303 if (parts) {
1304 /* min cannot be used elsewhere than in the first part */
1305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1306 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1307 expr - range_p->arg, range_p->arg);
1308 goto cleanup;
1309 }
1310 expr += 3;
1311
1312 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001313 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 +01001314 part->max_64 = part->min_64;
1315 } else if (*expr == '|') {
1316 if (!parts || range_expected) {
1317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1318 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1319 goto cleanup;
1320 }
1321 expr++;
1322 parts_done++;
1323 /* process next part of the expression */
1324 } else if (!strncmp(expr, "..", 2)) {
1325 expr += 2;
1326 while (isspace(*expr)) {
1327 expr++;
1328 }
1329 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1331 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1332 goto cleanup;
1333 }
1334 /* continue expecting the upper boundary */
1335 range_expected = 1;
1336 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1337 /* number */
1338 if (range_expected) {
1339 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001340 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 +01001341 range_expected = 0;
1342 } else {
1343 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1344 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 +01001345 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001346 part->max_64 = part->min_64;
1347 }
1348
1349 /* continue with possible another expression part */
1350 } else if (!strncmp(expr, "max", 3)) {
1351 expr += 3;
1352 while (isspace(*expr)) {
1353 expr++;
1354 }
1355 if (*expr != '\0') {
1356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1357 length_restr ? "length" : "range", expr);
1358 goto cleanup;
1359 }
1360 if (range_expected) {
1361 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001362 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 +01001363 range_expected = 0;
1364 } else {
1365 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1366 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 +01001367 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001368 part->min_64 = part->max_64;
1369 }
1370 } else {
1371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1372 length_restr ? "length" : "range", expr);
1373 goto cleanup;
1374 }
1375 }
1376
1377 /* check with the previous range/length restriction */
1378 if (base_range) {
1379 switch (basetype) {
1380 case LY_TYPE_BINARY:
1381 case LY_TYPE_UINT8:
1382 case LY_TYPE_UINT16:
1383 case LY_TYPE_UINT32:
1384 case LY_TYPE_UINT64:
1385 case LY_TYPE_STRING:
1386 uns = 1;
1387 break;
1388 case LY_TYPE_DEC64:
1389 case LY_TYPE_INT8:
1390 case LY_TYPE_INT16:
1391 case LY_TYPE_INT32:
1392 case LY_TYPE_INT64:
1393 uns = 0;
1394 break;
1395 default:
1396 LOGINT(ctx->ctx);
1397 ret = LY_EINT;
1398 goto cleanup;
1399 }
1400 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1401 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1402 goto baseerror;
1403 }
1404 /* current lower bound is not lower than the base */
1405 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1406 /* base has single value */
1407 if (base_range->parts[v].min_64 == parts[u].min_64) {
1408 /* both lower bounds are the same */
1409 if (parts[u].min_64 != parts[u].max_64) {
1410 /* current continues with a range */
1411 goto baseerror;
1412 } else {
1413 /* equal single values, move both forward */
1414 ++v;
1415 continue;
1416 }
1417 } else {
1418 /* base is single value lower than current range, so the
1419 * value from base range is removed in the current,
1420 * move only base and repeat checking */
1421 ++v;
1422 --u;
1423 continue;
1424 }
1425 } else {
1426 /* base is the range */
1427 if (parts[u].min_64 == parts[u].max_64) {
1428 /* current is a single value */
1429 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1430 /* current is behind the base range, so base range is omitted,
1431 * move the base and keep the current for further check */
1432 ++v;
1433 --u;
1434 } /* else it is within the base range, so move the current, but keep the base */
1435 continue;
1436 } else {
1437 /* both are ranges - check the higher bound, the lower was already checked */
1438 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1439 /* higher bound is higher than the current higher bound */
1440 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1441 /* but the current lower bound is also higher, so the base range is omitted,
1442 * continue with the same current, but move the base */
1443 --u;
1444 ++v;
1445 continue;
1446 }
1447 /* current range starts within the base range but end behind it */
1448 goto baseerror;
1449 } else {
1450 /* current range is smaller than the base,
1451 * move current, but stay with the base */
1452 continue;
1453 }
1454 }
1455 }
1456 }
1457 if (u != parts_done) {
1458baseerror:
1459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1460 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1461 length_restr ? "length" : "range", range_p->arg);
1462 goto cleanup;
1463 }
1464 }
1465
1466 if (!(*range)) {
1467 *range = calloc(1, sizeof **range);
1468 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1469 }
1470
Radek Krejcic8b31002019-01-08 10:24:45 +01001471 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001472 if (range_p->eapptag) {
1473 lydict_remove(ctx->ctx, (*range)->eapptag);
1474 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1475 }
1476 if (range_p->emsg) {
1477 lydict_remove(ctx->ctx, (*range)->emsg);
1478 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1479 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001480 if (range_p->dsc) {
1481 lydict_remove(ctx->ctx, (*range)->dsc);
1482 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1483 }
1484 if (range_p->ref) {
1485 lydict_remove(ctx->ctx, (*range)->ref);
1486 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1487 }
Radek Krejci19a96102018-11-15 13:38:09 +01001488 /* extensions are taken only from the last range by the caller */
1489
1490 (*range)->parts = parts;
1491 parts = NULL;
1492 ret = LY_SUCCESS;
1493cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001494 LY_ARRAY_FREE(parts);
1495
1496 return ret;
1497}
1498
1499/**
1500 * @brief Checks pattern syntax.
1501 *
Radek Krejcia3045382018-11-22 14:30:31 +01001502 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001503 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001504 * @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 +01001505 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001506 */
1507static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001508lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001509{
Radek Krejci54579462019-04-30 12:47:06 +02001510 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001511 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001512 int err_code;
1513 const char *orig_ptr;
1514 PCRE2_SIZE err_offset;
1515 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001516#define URANGE_LEN 19
1517 char *ublock2urange[][2] = {
1518 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1519 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1520 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1521 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1522 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1523 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1524 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1525 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1526 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1527 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1528 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1529 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1530 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1531 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1532 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1533 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1534 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1535 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1536 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1537 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1538 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1539 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1540 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1541 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1542 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1543 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1544 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1545 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1546 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1547 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1548 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1549 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1550 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1551 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1552 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1553 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1554 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1555 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1556 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1557 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1558 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1559 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1560 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1561 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1562 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1563 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1564 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1565 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1566 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1567 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1568 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1569 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1570 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1571 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1572 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1573 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1574 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1575 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1576 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1577 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1578 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1579 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1580 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1581 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1582 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1583 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1584 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1585 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1586 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1587 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1588 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1589 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1590 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1591 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1592 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1593 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1594 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1595 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1596 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1597 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1598 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1599 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1600 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1601 {NULL, NULL}
1602 };
1603
1604 /* adjust the expression to a Perl equivalent
1605 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1606
1607 /* we need to replace all "$" with "\$", count them now */
1608 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1609
1610 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1611 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1612 perl_regex[0] = '\0';
1613
1614 ptr = perl_regex;
1615
1616 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1617 /* we will add line-end anchoring */
1618 ptr[0] = '(';
1619 ++ptr;
1620 }
1621
1622 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1623 if (orig_ptr[0] == '$') {
1624 ptr += sprintf(ptr, "\\$");
1625 } else if (orig_ptr[0] == '^') {
1626 ptr += sprintf(ptr, "\\^");
1627 } else {
1628 ptr[0] = orig_ptr[0];
1629 ++ptr;
1630 }
1631 }
1632
1633 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1634 ptr += sprintf(ptr, ")$");
1635 } else {
1636 ptr[0] = '\0';
1637 ++ptr;
1638 }
1639
1640 /* substitute Unicode Character Blocks with exact Character Ranges */
1641 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1642 start = ptr - perl_regex;
1643
1644 ptr = strchr(ptr, '}');
1645 if (!ptr) {
1646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1647 pattern, perl_regex + start + 2, "unterminated character property");
1648 free(perl_regex);
1649 return LY_EVALID;
1650 }
1651 end = (ptr - perl_regex) + 1;
1652
1653 /* need more space */
1654 if (end - start < URANGE_LEN) {
1655 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1656 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1657 }
1658
1659 /* find our range */
1660 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1661 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1662 break;
1663 }
1664 }
1665 if (!ublock2urange[idx][0]) {
1666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1667 pattern, perl_regex + start + 5, "unknown block name");
1668 free(perl_regex);
1669 return LY_EVALID;
1670 }
1671
1672 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1673 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1674 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1675 ++count;
1676 }
1677 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1678 --count;
1679 }
1680 }
1681 if (count) {
1682 /* skip brackets */
1683 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1684 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1685 } else {
1686 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1687 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1688 }
1689 }
1690
1691 /* must return 0, already checked during parsing */
Radek Krejci54579462019-04-30 12:47:06 +02001692 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1693 &err_code, &err_offset, NULL);
1694 if (!code_local) {
1695 PCRE2_UCHAR err_msg[256] = {0};
1696 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1698 free(perl_regex);
1699 return LY_EVALID;
1700 }
1701 free(perl_regex);
1702
Radek Krejci54579462019-04-30 12:47:06 +02001703 if (code) {
1704 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001705 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001706 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001707 }
1708
1709 return LY_SUCCESS;
1710
1711#undef URANGE_LEN
1712}
1713
Radek Krejcia3045382018-11-22 14:30:31 +01001714/**
1715 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1716 * @param[in] ctx Compile context.
1717 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1718 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1719 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1720 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1721 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1722 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1723 */
Radek Krejci19a96102018-11-15 13:38:09 +01001724static LY_ERR
1725lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1726 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1727{
1728 struct lysc_pattern **pattern;
1729 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001730 LY_ERR ret = LY_SUCCESS;
1731
1732 /* first, copy the patterns from the base type */
1733 if (base_patterns) {
1734 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1735 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1736 }
1737
1738 LY_ARRAY_FOR(patterns_p, u) {
1739 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1740 *pattern = calloc(1, sizeof **pattern);
1741 ++(*pattern)->refcount;
1742
Radek Krejci54579462019-04-30 12:47:06 +02001743 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001744 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001745
1746 if (patterns_p[u].arg[0] == 0x15) {
1747 (*pattern)->inverted = 1;
1748 }
Radek Krejci54579462019-04-30 12:47:06 +02001749 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001750 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1751 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001752 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1753 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001754 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1755 options, v, lys_compile_ext, ret, done);
1756 }
1757done:
1758 return ret;
1759}
1760
Radek Krejcia3045382018-11-22 14:30:31 +01001761/**
1762 * @brief map of the possible restrictions combination for the specific built-in type.
1763 */
Radek Krejci19a96102018-11-15 13:38:09 +01001764static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1765 0 /* LY_TYPE_UNKNOWN */,
1766 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001767 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1768 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1769 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1770 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1771 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001772 LYS_SET_BIT /* LY_TYPE_BITS */,
1773 0 /* LY_TYPE_BOOL */,
1774 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1775 0 /* LY_TYPE_EMPTY */,
1776 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1777 LYS_SET_BASE /* LY_TYPE_IDENT */,
1778 LYS_SET_REQINST /* LY_TYPE_INST */,
1779 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001780 LYS_SET_TYPE /* LY_TYPE_UNION */,
1781 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001782 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001783 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001784 LYS_SET_RANGE /* LY_TYPE_INT64 */
1785};
1786
1787/**
1788 * @brief stringification of the YANG built-in data types
1789 */
1790const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1791 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1792 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001793};
1794
Radek Krejcia3045382018-11-22 14:30:31 +01001795/**
1796 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1797 * @param[in] ctx Compile context.
1798 * @param[in] enums_p Array of the parsed enum structures to compile.
1799 * @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.
1800 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1801 * @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
1806lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
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
1926 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1927 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1928
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 */
2187static LY_ERR
2188lys_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 Krejci01342af2019-01-03 15:18:08 +01002427 struct lysp_type *type_p, int options, 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.
2438 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2439 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2440 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2441 * @param[out] type Newly created type structure with the filled information about the type.
2442 * @return LY_ERR value.
2443 */
Radek Krejci19a96102018-11-15 13:38:09 +01002444static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002445lys_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,
2446 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2447 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002448{
2449 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002450 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002451 struct lysc_type_bin *bin;
2452 struct lysc_type_num *num;
2453 struct lysc_type_str *str;
2454 struct lysc_type_bits *bits;
2455 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002456 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002457 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002458 struct lysc_type_union *un, *un_aux;
2459 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002460
2461 switch (basetype) {
2462 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002463 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002464
2465 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002466 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002467 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002468 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2469 LY_CHECK_RET(ret);
2470 if (!tpdfname) {
2471 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2472 options, u, lys_compile_ext, ret, done);
2473 }
2474 }
2475
2476 if (tpdfname) {
2477 type_p->compiled = *type;
2478 *type = calloc(1, sizeof(struct lysc_type_bin));
2479 }
2480 break;
2481 case LY_TYPE_BITS:
2482 /* RFC 7950 9.7 - bits */
2483 bits = (struct lysc_type_bits*)(*type);
2484 if (type_p->bits) {
2485 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
Radek Krejci693262f2019-04-29 15:23:20 +02002486 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2487 (struct lysc_type_bitenum_item**)&bits->bits);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002488 LY_CHECK_RET(ret);
2489 }
2490
Radek Krejci555cb5b2018-11-16 14:54:33 +01002491 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002492 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002493 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002495 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002496 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002497 free(*type);
2498 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002499 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002500 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002501 }
2502
2503 if (tpdfname) {
2504 type_p->compiled = *type;
2505 *type = calloc(1, sizeof(struct lysc_type_bits));
2506 }
2507 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002508 case LY_TYPE_DEC64:
2509 dec = (struct lysc_type_dec*)(*type);
2510
2511 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002512 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002513 if (!type_p->fraction_digits) {
2514 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002515 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002516 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002518 free(*type);
2519 *type = NULL;
2520 }
2521 return LY_EVALID;
2522 }
2523 } else if (type_p->fraction_digits) {
2524 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002525 if (tpdfname) {
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 \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002528 tpdfname);
2529 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2531 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002532 free(*type);
2533 *type = NULL;
2534 }
2535 return LY_EVALID;
2536 }
2537 dec->fraction_digits = type_p->fraction_digits;
2538
2539 /* RFC 7950 9.2.4 - range */
2540 if (type_p->range) {
2541 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2542 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2543 LY_CHECK_RET(ret);
2544 if (!tpdfname) {
2545 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2546 options, u, lys_compile_ext, ret, done);
2547 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002548 }
2549
2550 if (tpdfname) {
2551 type_p->compiled = *type;
2552 *type = calloc(1, sizeof(struct lysc_type_dec));
2553 }
2554 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002555 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002556 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002557
2558 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002559 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002560 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002561 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2562 LY_CHECK_RET(ret);
2563 if (!tpdfname) {
2564 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2565 options, u, lys_compile_ext, ret, done);
2566 }
2567 } else if (base && ((struct lysc_type_str*)base)->length) {
2568 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2569 }
2570
2571 /* RFC 7950 9.4.5 - pattern */
2572 if (type_p->patterns) {
2573 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2574 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2575 LY_CHECK_RET(ret);
2576 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2577 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2578 }
2579
2580 if (tpdfname) {
2581 type_p->compiled = *type;
2582 *type = calloc(1, sizeof(struct lysc_type_str));
2583 }
2584 break;
2585 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002586 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002587
2588 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002589 if (type_p->enums) {
2590 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2591 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2592 LY_CHECK_RET(ret);
2593 }
2594
Radek Krejci555cb5b2018-11-16 14:54:33 +01002595 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002596 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002597 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002599 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002601 free(*type);
2602 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002603 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002604 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002605 }
2606
2607 if (tpdfname) {
2608 type_p->compiled = *type;
2609 *type = calloc(1, sizeof(struct lysc_type_enum));
2610 }
2611 break;
2612 case LY_TYPE_INT8:
2613 case LY_TYPE_UINT8:
2614 case LY_TYPE_INT16:
2615 case LY_TYPE_UINT16:
2616 case LY_TYPE_INT32:
2617 case LY_TYPE_UINT32:
2618 case LY_TYPE_INT64:
2619 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002620 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002621
2622 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002623 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002624 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002625 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2626 LY_CHECK_RET(ret);
2627 if (!tpdfname) {
2628 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2629 options, u, lys_compile_ext, ret, done);
2630 }
2631 }
2632
2633 if (tpdfname) {
2634 type_p->compiled = *type;
2635 *type = calloc(1, sizeof(struct lysc_type_num));
2636 }
2637 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002638 case LY_TYPE_IDENT:
2639 idref = (struct lysc_type_identityref*)(*type);
2640
2641 /* RFC 7950 9.10.2 - base */
2642 if (type_p->bases) {
2643 if (base) {
2644 /* only the directly derived identityrefs can contain base specification */
2645 if (tpdfname) {
2646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002647 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002648 tpdfname);
2649 } else {
2650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002651 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002652 free(*type);
2653 *type = NULL;
2654 }
2655 return LY_EVALID;
2656 }
2657 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2658 LY_CHECK_RET(ret);
2659 }
2660
2661 if (!base && !type_p->flags) {
2662 /* type derived from identityref built-in type must contain at least one base */
2663 if (tpdfname) {
2664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2665 } else {
2666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2667 free(*type);
2668 *type = NULL;
2669 }
2670 return LY_EVALID;
2671 }
2672
2673 if (tpdfname) {
2674 type_p->compiled = *type;
2675 *type = calloc(1, sizeof(struct lysc_type_identityref));
2676 }
2677 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002678 case LY_TYPE_LEAFREF:
2679 /* RFC 7950 9.9.3 - require-instance */
2680 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002681 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002682 if (tpdfname) {
2683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2684 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2685 } else {
2686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2687 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2688 free(*type);
2689 *type = NULL;
2690 }
2691 return LY_EVALID;
2692 }
Radek Krejcia3045382018-11-22 14:30:31 +01002693 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002694 } else if (base) {
2695 /* inherit */
2696 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002697 } else {
2698 /* default is true */
2699 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2700 }
2701 if (type_p->path) {
2702 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002703 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002704 } else if (base) {
2705 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002706 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002707 } else if (tpdfname) {
2708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2709 return LY_EVALID;
2710 } else {
2711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2712 free(*type);
2713 *type = NULL;
2714 return LY_EVALID;
2715 }
2716 if (tpdfname) {
2717 type_p->compiled = *type;
2718 *type = calloc(1, sizeof(struct lysc_type_leafref));
2719 }
2720 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002721 case LY_TYPE_INST:
2722 /* RFC 7950 9.9.3 - require-instance */
2723 if (type_p->flags & LYS_SET_REQINST) {
2724 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2725 } else {
2726 /* default is true */
2727 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2728 }
2729
2730 if (tpdfname) {
2731 type_p->compiled = *type;
2732 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2733 }
2734 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002735 case LY_TYPE_UNION:
2736 un = (struct lysc_type_union*)(*type);
2737
2738 /* RFC 7950 7.4 - type */
2739 if (type_p->types) {
2740 if (base) {
2741 /* only the directly derived union can contain types specification */
2742 if (tpdfname) {
2743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2744 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2745 tpdfname);
2746 } else {
2747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2748 "Invalid type substatement for the type not directly derived from union built-in type.");
2749 free(*type);
2750 *type = NULL;
2751 }
2752 return LY_EVALID;
2753 }
2754 /* compile the type */
2755 additional = 0;
2756 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2757 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002758 ret = lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], options, &un->types[u + additional], NULL);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002759 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2760 /* add space for additional types from the union subtype */
2761 un_aux = (struct lysc_type_union *)un->types[u + additional];
2762 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)));
2763 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2764 un->types = (void*)((uint32_t*)(p) + 1);
2765
2766 /* copy subtypes of the subtype union */
2767 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2768 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2769 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2770 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2771 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2772 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2773 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2774 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2775 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2776 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2777 /* TODO extensions */
2778
2779 } else {
2780 un->types[u + additional] = un_aux->types[v];
2781 ++un_aux->types[v]->refcount;
2782 }
2783 ++additional;
2784 LY_ARRAY_INCREMENT(un->types);
2785 }
2786 /* compensate u increment in main loop */
2787 --additional;
2788
2789 /* free the replaced union subtype */
2790 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2791 } else {
2792 LY_ARRAY_INCREMENT(un->types);
2793 }
2794 LY_CHECK_RET(ret);
2795 }
2796 }
2797
2798 if (!base && !type_p->flags) {
2799 /* type derived from union built-in type must contain at least one type */
2800 if (tpdfname) {
2801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2802 } else {
2803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2804 free(*type);
2805 *type = NULL;
2806 }
2807 return LY_EVALID;
2808 }
2809
2810 if (tpdfname) {
2811 type_p->compiled = *type;
2812 *type = calloc(1, sizeof(struct lysc_type_union));
2813 }
2814 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002815 case LY_TYPE_BOOL:
2816 case LY_TYPE_EMPTY:
2817 case LY_TYPE_UNKNOWN: /* just to complete switch */
2818 break;
2819 }
2820 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2821done:
2822 return ret;
2823}
2824
Radek Krejcia3045382018-11-22 14:30:31 +01002825/**
2826 * @brief Compile information about the leaf/leaf-list's type.
2827 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002828 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2829 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2830 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2831 * @param[in] context_name Name of the context node or referencing typedef for logging.
2832 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002833 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2834 * @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 +01002835 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002836 * @return LY_ERR value.
2837 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002838static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002839lys_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 Krejci01342af2019-01-03 15:18:08 +01002840 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002841{
2842 LY_ERR ret = LY_SUCCESS;
2843 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002844 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002845 struct type_context {
2846 const struct lysp_tpdf *tpdf;
2847 struct lysp_node *node;
2848 struct lysp_module *mod;
2849 } *tctx, *tctx_prev = NULL;
2850 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002851 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002852 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002853 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002854
2855 (*type) = NULL;
2856
2857 tctx = calloc(1, sizeof *tctx);
2858 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002859 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002860 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2861 ret == LY_SUCCESS;
2862 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2863 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2864 if (basetype) {
2865 break;
2866 }
2867
2868 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002869 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2870 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002871 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2872
Radek Krejcicdfecd92018-11-26 11:27:32 +01002873 if (units && !*units) {
2874 /* inherit units */
2875 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2876 }
Radek Krejci01342af2019-01-03 15:18:08 +01002877 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002878 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002879 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002880 }
Radek Krejci01342af2019-01-03 15:18:08 +01002881 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002882 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2883 break;
2884 }
2885
Radek Krejci19a96102018-11-15 13:38:09 +01002886 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002887 /* it is not necessary to continue, the rest of the chain was already compiled,
2888 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002889 basetype = tctx->tpdf->type.compiled->basetype;
2890 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002891 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002892 dummyloops = 1;
2893 goto preparenext;
2894 } else {
2895 tctx = NULL;
2896 break;
2897 }
Radek Krejci19a96102018-11-15 13:38:09 +01002898 }
2899
2900 /* store information for the following processing */
2901 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2902
Radek Krejcicdfecd92018-11-26 11:27:32 +01002903preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002904 /* prepare next loop */
2905 tctx_prev = tctx;
2906 tctx = calloc(1, sizeof *tctx);
2907 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2908 }
2909 free(tctx);
2910
2911 /* allocate type according to the basetype */
2912 switch (basetype) {
2913 case LY_TYPE_BINARY:
2914 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002915 break;
2916 case LY_TYPE_BITS:
2917 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002918 break;
2919 case LY_TYPE_BOOL:
2920 case LY_TYPE_EMPTY:
2921 *type = calloc(1, sizeof(struct lysc_type));
2922 break;
2923 case LY_TYPE_DEC64:
2924 *type = calloc(1, sizeof(struct lysc_type_dec));
2925 break;
2926 case LY_TYPE_ENUM:
2927 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002928 break;
2929 case LY_TYPE_IDENT:
2930 *type = calloc(1, sizeof(struct lysc_type_identityref));
2931 break;
2932 case LY_TYPE_INST:
2933 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2934 break;
2935 case LY_TYPE_LEAFREF:
2936 *type = calloc(1, sizeof(struct lysc_type_leafref));
2937 break;
2938 case LY_TYPE_STRING:
2939 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002940 break;
2941 case LY_TYPE_UNION:
2942 *type = calloc(1, sizeof(struct lysc_type_union));
2943 break;
2944 case LY_TYPE_INT8:
2945 case LY_TYPE_UINT8:
2946 case LY_TYPE_INT16:
2947 case LY_TYPE_UINT16:
2948 case LY_TYPE_INT32:
2949 case LY_TYPE_UINT32:
2950 case LY_TYPE_INT64:
2951 case LY_TYPE_UINT64:
2952 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002953 break;
2954 case LY_TYPE_UNKNOWN:
2955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2956 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2957 ret = LY_EVALID;
2958 goto cleanup;
2959 }
2960 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002961 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002962 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2963 ly_data_type2str[basetype]);
2964 free(*type);
2965 (*type) = NULL;
2966 ret = LY_EVALID;
2967 goto cleanup;
2968 }
2969
2970 /* get restrictions from the referred typedefs */
2971 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2972 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002973 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002974 base = tctx->tpdf->type.compiled;
2975 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002976 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002977 /* no change, just use the type information from the base */
2978 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2979 ++base->refcount;
2980 continue;
2981 }
2982
2983 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002984 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2985 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2986 tctx->tpdf->name, ly_data_type2str[basetype]);
2987 ret = LY_EVALID;
2988 goto cleanup;
2989 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2990 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2991 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2992 tctx->tpdf->name, tctx->tpdf->dflt);
2993 ret = LY_EVALID;
2994 goto cleanup;
2995 }
2996
Radek Krejci19a96102018-11-15 13:38:09 +01002997 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002998 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002999 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 +01003000 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
3001 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003002 LY_CHECK_GOTO(ret, cleanup);
3003 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003004 }
3005
Radek Krejcic5c27e52018-11-15 14:38:11 +01003006 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003007 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003008 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003009 (*type)->basetype = basetype;
3010 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01003011 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, options, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003012 LY_CHECK_GOTO(ret, cleanup);
3013 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003014 /* no specific restriction in leaf's type definition, copy from the base */
3015 free(*type);
3016 (*type) = base;
3017 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003018 }
Radek Krejci01342af2019-01-03 15:18:08 +01003019 if (!(*type)->dflt) {
3020 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3021 }
Radek Krejci19a96102018-11-15 13:38:09 +01003022
3023 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
3024
3025cleanup:
3026 ly_set_erase(&tpdf_chain, free);
3027 return ret;
3028}
3029
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003030/**
3031 * @brief Compile status information of the given node.
3032 *
3033 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3034 * has the status correctly set during the compilation.
3035 *
3036 * @param[in] ctx Compile context
3037 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3038 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3039 * the compatibility with the parent's status value.
3040 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3041 * @return LY_ERR value.
3042 */
3043static LY_ERR
3044lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3045{
3046 /* status - it is not inherited by specification, but it does not make sense to have
3047 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3048 if (!((*node_flags) & LYS_STATUS_MASK)) {
3049 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3050 if ((parent_flags & 0x3) != 0x3) {
3051 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3052 * combination of bits (0x3) which marks the uses_status value */
3053 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3054 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3055 }
3056 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3057 } else {
3058 (*node_flags) |= LYS_STATUS_CURR;
3059 }
3060 } else if (parent_flags & LYS_STATUS_MASK) {
3061 /* check status compatibility with the parent */
3062 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3063 if ((*node_flags) & LYS_STATUS_CURR) {
3064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3065 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3066 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3067 } else { /* LYS_STATUS_DEPRC */
3068 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3069 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3070 }
3071 return LY_EVALID;
3072 }
3073 }
3074 return LY_SUCCESS;
3075}
3076
Radek Krejci8cce8532019-03-05 11:27:45 +01003077/**
3078 * @brief Check uniqness of the node/action/notification name.
3079 *
3080 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3081 * structures, but they share the namespace so we need to check their name collisions.
3082 *
3083 * @param[in] ctx Compile context.
3084 * @param[in] children List (linked list) of data nodes to go through.
3085 * @param[in] actions List (sized array) of actions or RPCs to go through.
3086 * @param[in] notifs List (sized array) of Notifications to go through.
3087 * @param[in] name Name of the item to find in the given lists.
3088 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3089 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3090 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3091 */
3092static LY_ERR
3093lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3094 const struct lysc_action *actions, const struct lysc_notif *notifs,
3095 const char *name, void *exclude)
3096{
3097 const struct lysc_node *iter;
3098 unsigned int u;
3099
3100 LY_LIST_FOR(children, iter) {
3101 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3102 goto error;
3103 }
3104 }
3105 LY_ARRAY_FOR(actions, u) {
3106 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3107 goto error;
3108 }
3109 }
3110 LY_ARRAY_FOR(notifs, u) {
3111 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3112 goto error;
3113 }
3114 }
3115 return LY_SUCCESS;
3116error:
3117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3118 return LY_EEXIST;
3119}
3120
Radek Krejcib1b59152019-01-07 13:21:56 +01003121static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01003122
Radek Krejcia3045382018-11-22 14:30:31 +01003123/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003124 * @brief Compile parsed RPC/action schema node information.
3125 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003126 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003127 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003128 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003129 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3130 * @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).
3131 * Zero means no uses, non-zero value with no status bit set mean the default status.
3132 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3133 */
3134static LY_ERR
3135lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, int options,
3136 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3137{
3138 LY_ERR ret = LY_SUCCESS;
3139 struct lysp_node *child_p;
3140 unsigned int u;
3141
Radek Krejci8cce8532019-03-05 11:27:45 +01003142 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3143 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3144 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3145 action_p->name, action)) {
3146 return LY_EVALID;
3147 }
3148
Radek Krejcifc11bd72019-04-11 16:00:05 +02003149 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003151 "Action \"%s\" is placed inside %s.", action_p->name,
3152 options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003153 return LY_EVALID;
3154 }
3155
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003156 action->nodetype = LYS_ACTION;
3157 action->module = ctx->mod;
3158 action->parent = parent;
3159 if (!(options & LYSC_OPT_FREE_SP)) {
3160 action->sp = action_p;
3161 }
3162 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3163
3164 /* status - it is not inherited by specification, but it does not make sense to have
3165 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3166 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3167
3168 DUP_STRING(ctx->ctx, action_p->name, action->name);
3169 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3170 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
3171 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3172 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, options, u, lys_compile_ext, ret, cleanup);
3173
3174 /* input */
3175 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003176 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, options, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003177 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003178 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_RPC_INPUT, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003179 }
3180
3181 /* output */
3182 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003183 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, options, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003184 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003185 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_RPC_OUTPUT, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003186 }
3187
3188cleanup:
3189 return ret;
3190}
3191
3192/**
Radek Krejci43981a32019-04-12 09:44:11 +02003193 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003194 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003195 * @param[in] notif_p Parsed Notification schema node.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003196 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003197 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3198 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3199 * @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 +02003200 * Zero means no uses, non-zero value with no status bit set mean the default status.
3201 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3202 */
3203static LY_ERR
3204lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, int options,
3205 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3206{
3207 LY_ERR ret = LY_SUCCESS;
3208 struct lysp_node *child_p;
3209 unsigned int u;
3210
3211 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3212 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3213 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3214 notif_p->name, notif)) {
3215 return LY_EVALID;
3216 }
3217
3218 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
3219 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3220 "Notification \"%s\" is placed inside %s.", notif_p->name,
3221 options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
3222 return LY_EVALID;
3223 }
3224
3225 notif->nodetype = LYS_NOTIF;
3226 notif->module = ctx->mod;
3227 notif->parent = parent;
3228 if (!(options & LYSC_OPT_FREE_SP)) {
3229 notif->sp = notif_p;
3230 }
3231 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3232
3233 /* status - it is not inherited by specification, but it does not make sense to have
3234 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3235 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3236
3237 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3238 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3239 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
3240 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3241 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, options, u, lys_compile_ext, ret, cleanup);
3242 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, options, u, lys_compile_must, ret, cleanup);
3243
3244 LY_LIST_FOR(notif_p->data, child_p) {
3245 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_NOTIFICATION, (struct lysc_node*)notif, uses_status));
3246 }
3247
3248cleanup:
3249 return ret;
3250}
3251
3252/**
Radek Krejcia3045382018-11-22 14:30:31 +01003253 * @brief Compile parsed container node information.
3254 * @param[in] ctx Compile context
3255 * @param[in] node_p Parsed container node.
3256 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3257 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3258 * is enriched with the container-specific information.
3259 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3260 */
Radek Krejci19a96102018-11-15 13:38:09 +01003261static LY_ERR
3262lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3263{
3264 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3265 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3266 struct lysp_node *child_p;
3267 unsigned int u;
3268 LY_ERR ret = LY_SUCCESS;
3269
Radek Krejcife909632019-02-12 15:34:42 +01003270 if (cont_p->presence) {
3271 cont->flags |= LYS_PRESENCE;
3272 }
3273
Radek Krejci19a96102018-11-15 13:38:09 +01003274 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003275 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003276 }
3277
3278 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003279 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, options, u, lys_compile_action, 0, ret, done);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003280 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, options, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003281
3282done:
3283 return ret;
3284}
3285
Radek Krejci33f72892019-02-21 10:36:58 +01003286/*
3287 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3288 * @param[in] ctx Compile context.
3289 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3290 * @param[in] type_p Parsed type to compile.
3291 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3292 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3293 * @return LY_ERR value.
3294 */
3295static LY_ERR
3296lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, int options, struct lysc_node_leaf *leaf)
3297{
3298 unsigned int u, v;
3299 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3300
3301 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type,
3302 leaf->units ? NULL : &leaf->units));
3303 if (leaf->nodetype == LYS_LEAFLIST) {
3304 if (llist->type->dflt && !llist->dflts && !llist->min) {
3305 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3306 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3307 LY_ARRAY_INCREMENT(llist->dflts);
3308 }
3309 } else {
3310 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3311 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3312 }
3313 }
3314 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3315 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3316 ly_set_add(&ctx->unres, leaf, 0);
3317 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3318 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3319 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3320 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3321 ly_set_add(&ctx->unres, leaf, 0);
3322 }
3323 }
3324 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3325 if (leaf->flags & LYS_SET_DFLT) {
3326 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3327 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3328 return LY_EVALID;
3329 }
3330 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3331 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3332 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3333 return LY_EVALID;
3334 }
3335 }
3336
3337 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3338 /* configuration data values must be unique - so check the default values */
3339 LY_ARRAY_FOR(llist->dflts, u) {
3340 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3341 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3343 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3344 return LY_EVALID;
3345 }
3346 }
3347 }
3348 }
3349
3350 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3351
3352 return LY_SUCCESS;
3353}
3354
Radek Krejcia3045382018-11-22 14:30:31 +01003355/**
3356 * @brief Compile parsed leaf node information.
3357 * @param[in] ctx Compile context
3358 * @param[in] node_p Parsed leaf node.
3359 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3360 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3361 * is enriched with the leaf-specific information.
3362 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3363 */
Radek Krejci19a96102018-11-15 13:38:09 +01003364static LY_ERR
3365lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3366{
3367 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3368 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3369 unsigned int u;
3370 LY_ERR ret = LY_SUCCESS;
3371
Radek Krejci19a96102018-11-15 13:38:09 +01003372 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003373 if (leaf_p->units) {
3374 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3375 leaf->flags |= LYS_SET_UNITS;
3376 }
3377 if (leaf_p->dflt) {
3378 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003379 leaf->flags |= LYS_SET_DFLT;
3380 }
Radek Krejci43699232018-11-23 14:59:46 +01003381
Radek Krejci33f72892019-02-21 10:36:58 +01003382 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, options, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003383
Radek Krejci19a96102018-11-15 13:38:09 +01003384done:
3385 return ret;
3386}
3387
Radek Krejcia3045382018-11-22 14:30:31 +01003388/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003389 * @brief Compile parsed leaf-list node information.
3390 * @param[in] ctx Compile context
3391 * @param[in] node_p Parsed leaf-list node.
3392 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3393 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3394 * is enriched with the leaf-list-specific information.
3395 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3396 */
3397static LY_ERR
3398lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3399{
3400 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3401 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003402 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003403 LY_ERR ret = LY_SUCCESS;
3404
Radek Krejci0e5d8382018-11-28 16:37:53 +01003405 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003406 if (llist_p->units) {
3407 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3408 llist->flags |= LYS_SET_UNITS;
3409 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003410
3411 if (llist_p->dflts) {
3412 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3413 LY_ARRAY_FOR(llist_p->dflts, u) {
3414 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3415 LY_ARRAY_INCREMENT(llist->dflts);
3416 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003417 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003418 }
3419
3420 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003421 if (llist->min) {
3422 llist->flags |= LYS_MAND_TRUE;
3423 }
Radek Krejcib7408632018-11-28 17:12:11 +01003424 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003425
Radek Krejci33f72892019-02-21 10:36:58 +01003426 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, options, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003427
3428done:
3429 return ret;
3430}
3431
3432/**
Radek Krejci7af64242019-02-18 13:07:53 +01003433 * @brief Compile information about list's uniques.
3434 * @param[in] ctx Compile context.
3435 * @param[in] context_module Module where the prefixes are going to be resolved.
3436 * @param[in] uniques Sized array list of unique statements.
3437 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3438 * @return LY_ERR value.
3439 */
3440static LY_ERR
3441lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3442{
3443 LY_ERR ret = LY_SUCCESS;
3444 struct lysc_node_leaf **key, ***unique;
3445 const char *keystr, *delim;
3446 size_t len;
3447 unsigned int v;
3448 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003449 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003450
3451 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3452 config = -1;
3453 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3454 keystr = uniques[v];
3455 while (keystr) {
3456 delim = strpbrk(keystr, " \t\n");
3457 if (delim) {
3458 len = delim - keystr;
3459 while (isspace(*delim)) {
3460 ++delim;
3461 }
3462 } else {
3463 len = strlen(keystr);
3464 }
3465
3466 /* unique node must be present */
3467 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003468 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3469 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003470 if (ret != LY_SUCCESS) {
3471 if (ret == LY_EDENIED) {
3472 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003473 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003474 len, keystr, lys_nodetype2str((*key)->nodetype));
3475 }
3476 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003477 } else if (flags) {
3478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3479 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3480 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3481 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003482 }
3483
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003484
Radek Krejci7af64242019-02-18 13:07:53 +01003485 /* all referenced leafs must be of the same config type */
3486 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3487 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3488 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3489 return LY_EVALID;
3490 } else if ((*key)->flags & LYS_CONFIG_W) {
3491 config = 1;
3492 } else { /* LYS_CONFIG_R */
3493 config = 0;
3494 }
3495
3496 /* check status */
3497 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3498 (*key)->flags, (*key)->module, (*key)->name));
3499
3500 /* mark leaf as unique */
3501 (*key)->flags |= LYS_UNIQUE;
3502
3503 /* next unique value in line */
3504 keystr = delim;
3505 }
3506 /* next unique definition */
3507 }
3508
3509 return LY_SUCCESS;
3510}
3511
3512/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003513 * @brief Compile parsed list node information.
3514 * @param[in] ctx Compile context
3515 * @param[in] node_p Parsed list node.
3516 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3517 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3518 * is enriched with the list-specific information.
3519 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3520 */
3521static LY_ERR
3522lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3523{
3524 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3525 struct lysc_node_list *list = (struct lysc_node_list*)node;
3526 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003527 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003528 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003529 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003530 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003531 LY_ERR ret = LY_SUCCESS;
3532
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003533 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003534 if (list->min) {
3535 list->flags |= LYS_MAND_TRUE;
3536 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003537 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3538
3539 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003540 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003541 }
3542
3543 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
3544
3545 /* keys */
3546 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3547 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3548 return LY_EVALID;
3549 }
3550
3551 /* find all the keys (must be direct children) */
3552 keystr = list_p->key;
3553 while (keystr) {
3554 delim = strpbrk(keystr, " \t\n");
3555 if (delim) {
3556 len = delim - keystr;
3557 while (isspace(*delim)) {
3558 ++delim;
3559 }
3560 } else {
3561 len = strlen(keystr);
3562 }
3563
3564 /* key node must be present */
3565 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3566 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3567 if (!(*key)) {
3568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3569 "The list's key \"%.*s\" not found.", len, keystr);
3570 return LY_EVALID;
3571 }
3572 /* keys must be unique */
3573 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3574 if (*key == list->keys[u]) {
3575 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3576 "Duplicated key identifier \"%.*s\".", len, keystr);
3577 return LY_EVALID;
3578 }
3579 }
3580 /* key must have the same config flag as the list itself */
3581 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3582 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3583 return LY_EVALID;
3584 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003585 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003586 /* YANG 1.0 denies key to be of empty type */
3587 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3589 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3590 return LY_EVALID;
3591 }
3592 } else {
3593 /* when and if-feature are illegal on list keys */
3594 if ((*key)->when) {
3595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3596 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3597 return LY_EVALID;
3598 }
3599 if ((*key)->iffeatures) {
3600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3601 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3602 return LY_EVALID;
3603 }
3604 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003605
3606 /* check status */
3607 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3608 (*key)->flags, (*key)->module, (*key)->name));
3609
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003610 /* ignore default values of the key */
3611 if ((*key)->dflt) {
3612 lydict_remove(ctx->ctx, (*key)->dflt);
3613 (*key)->dflt = NULL;
3614 }
3615 /* mark leaf as key */
3616 (*key)->flags |= LYS_KEY;
3617
3618 /* next key value */
3619 keystr = delim;
3620 }
3621
3622 /* uniques */
3623 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003624 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003625 }
3626
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003627 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, options, u, lys_compile_action, 0, ret, done);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003628 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, options, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003629
3630done:
3631 return ret;
3632}
3633
Radek Krejcib56c7502019-02-13 14:19:54 +01003634/**
3635 * @brief Do some checks and set the default choice's case.
3636 *
3637 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3638 *
3639 * @param[in] ctx Compile context.
3640 * @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,
3641 * not the reference to the imported module.
3642 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3643 * @return LY_ERR value.
3644 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003645static LY_ERR
3646lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3647{
3648 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3649 const char *prefix = NULL, *name;
3650 size_t prefix_len = 0;
3651
3652 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3653 name = strchr(dflt, ':');
3654 if (name) {
3655 prefix = dflt;
3656 prefix_len = name - prefix;
3657 ++name;
3658 } else {
3659 name = dflt;
3660 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003661 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003662 /* prefixed default case make sense only for the prefix of the schema itself */
3663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3664 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3665 prefix_len, prefix);
3666 return LY_EVALID;
3667 }
3668 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3669 if (!ch->dflt) {
3670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3671 "Default case \"%s\" not found.", dflt);
3672 return LY_EVALID;
3673 }
3674 /* no mandatory nodes directly under the default case */
3675 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003676 if (iter->parent != (struct lysc_node*)ch->dflt) {
3677 break;
3678 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003679 if (iter->flags & LYS_MAND_TRUE) {
3680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3681 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3682 return LY_EVALID;
3683 }
3684 }
Radek Krejci01342af2019-01-03 15:18:08 +01003685 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003686 return LY_SUCCESS;
3687}
3688
Radek Krejciccd20f12019-02-15 14:12:27 +01003689static LY_ERR
3690lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3691{
3692 struct lys_module *mod;
3693 const char *prefix = NULL, *name;
3694 size_t prefix_len = 0;
3695 struct lysc_node_case *cs;
3696 struct lysc_node *node;
3697
3698 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3699 name = strchr(dflt, ':');
3700 if (name) {
3701 prefix = dflt;
3702 prefix_len = name - prefix;
3703 ++name;
3704 } else {
3705 name = dflt;
3706 }
3707 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3708 if (prefix) {
3709 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3711 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3712 "The prefix does not match any imported module of the deviation module.",
3713 devnodeid, dflt);
3714 return LY_EVALID;
3715 }
3716 } else {
3717 mod = ctx->mod;
3718 }
3719 /* get the default case */
3720 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3721 if (!cs) {
3722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3723 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3724 devnodeid, dflt);
3725 return LY_EVALID;
3726 }
3727
3728 /* check that there is no mandatory node */
3729 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003730 if (node->parent != (struct lysc_node*)cs) {
3731 break;
3732 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003733 if (node->flags & LYS_MAND_TRUE) {
3734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3735 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3736 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3737 return LY_EVALID;
3738 }
3739 }
3740
3741 /* set the default case in choice */
3742 ch->dflt = cs;
3743 cs->flags |= LYS_SET_DFLT;
3744
3745 return LY_SUCCESS;
3746}
3747
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003748/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003749 * @brief Compile parsed choice node information.
3750 * @param[in] ctx Compile context
3751 * @param[in] node_p Parsed choice node.
3752 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3753 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003754 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003755 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3756 */
3757static LY_ERR
3758lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3759{
3760 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3761 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3762 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003763 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003764 LY_ERR ret = LY_SUCCESS;
3765
Radek Krejci056d0a82018-12-06 16:57:25 +01003766 LY_LIST_FOR(ch_p->child, child_p) {
3767 if (child_p->nodetype == LYS_CASE) {
3768 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003769 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003770 }
3771 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003772 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003773 }
3774 }
3775
3776 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003777 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003778 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003779 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003780
Radek Krejci9800fb82018-12-13 14:26:23 +01003781 return ret;
3782}
3783
3784/**
3785 * @brief Compile parsed anydata or anyxml node information.
3786 * @param[in] ctx Compile context
3787 * @param[in] node_p Parsed anydata or anyxml node.
3788 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3789 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3790 * is enriched with the any-specific information.
3791 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3792 */
3793static LY_ERR
3794lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3795{
3796 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3797 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3798 unsigned int u;
3799 LY_ERR ret = LY_SUCCESS;
3800
3801 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3802
3803 if (any->flags & LYS_CONFIG_W) {
3804 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3805 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3806 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003807done:
3808 return ret;
3809}
3810
Radek Krejcib56c7502019-02-13 14:19:54 +01003811/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003812 * @brief Connect the node into the siblings list and check its name uniqueness.
3813 *
3814 * @param[in] ctx Compile context
3815 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3816 * the choice itself is expected instead of a specific case node.
3817 * @param[in] node Schema node to connect into the list.
Radek Krejci05b774b2019-02-25 13:26:18 +01003818 * @param[in] options Compile options to distinguish input/output when placing node into RPC/action.
Radek Krejci056d0a82018-12-06 16:57:25 +01003819 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3820 */
3821static LY_ERR
Radek Krejci05b774b2019-02-25 13:26:18 +01003822lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node, int options)
Radek Krejci056d0a82018-12-06 16:57:25 +01003823{
3824 struct lysc_node **children;
3825
3826 if (node->nodetype == LYS_CASE) {
3827 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3828 } else {
Radek Krejci05b774b2019-02-25 13:26:18 +01003829 children = lysc_node_children_p(parent, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003830 }
3831 if (children) {
3832 if (!(*children)) {
3833 /* first child */
3834 *children = node;
3835 } else if (*children != node) {
3836 /* by the condition in previous branch we cover the choice/case children
3837 * - the children list is shared by the choice and the the first case, in addition
3838 * the first child of each case must be referenced from the case node. So the node is
3839 * actually always already inserted in case it is the first children - so here such
3840 * a situation actually corresponds to the first branch */
3841 /* insert at the end of the parent's children list */
3842 (*children)->prev->next = node;
3843 node->prev = (*children)->prev;
3844 (*children)->prev = node;
3845
3846 /* check the name uniqueness */
3847 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3848 lysc_node_notifs(parent), node->name, node)) {
3849 return LY_EEXIST;
3850 }
3851 }
3852 }
3853 return LY_SUCCESS;
3854}
3855
Radek Krejci95710c92019-02-11 15:49:55 +01003856/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003857 * @brief Get the XPath context node for the given schema node.
3858 * @param[in] start The schema node where the XPath expression appears.
3859 * @return The context node to evaluate XPath expression in given schema node.
3860 * @return NULL in case the context node is the root node.
3861 */
3862static struct lysc_node *
3863lysc_xpath_context(struct lysc_node *start)
3864{
3865 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3866 start = start->parent);
3867 return start;
3868}
3869
3870/**
3871 * @brief Prepare the case structure in choice node for the new data node.
3872 *
3873 * 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
3874 * created in the choice when the first child was processed.
3875 *
3876 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003877 * @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,
3878 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003879 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3880 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3881 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3882 * @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,
3883 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003884 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003885static struct lysc_node_case*
3886lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3887{
3888 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02003889 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01003890 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003891 unsigned int u;
3892 LY_ERR ret;
3893
Radek Krejci95710c92019-02-11 15:49:55 +01003894#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003895 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003896 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3898 return NULL; \
3899 } \
3900 }
3901
Radek Krejci95710c92019-02-11 15:49:55 +01003902 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3903 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003904
3905 /* we have to add an implicit case node into the parent choice */
3906 cs = calloc(1, sizeof(struct lysc_node_case));
3907 DUP_STRING(ctx->ctx, child->name, cs->name);
3908 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003909 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003910 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3911 /* the case is already present since the child is not its first children */
3912 return (struct lysc_node_case*)ch->cases->prev;
3913 }
Radek Krejci95710c92019-02-11 15:49:55 +01003914 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003915
3916 /* explicit parent case is not present (this is its first child) */
3917 cs = calloc(1, sizeof(struct lysc_node_case));
3918 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3919 cs->flags = LYS_STATUS_MASK & node_p->flags;
3920 cs->sp = node_p;
3921
Radek Krejcib1b59152019-01-07 13:21:56 +01003922 /* 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 +02003923 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01003924
3925 if (node_p->when) {
3926 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3927 ret = lys_compile_when(ctx, node_p->when, options, when);
3928 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003929 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003930 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003931 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003932 } else {
3933 LOGINT(ctx->ctx);
3934 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003935 }
3936 cs->module = ctx->mod;
3937 cs->prev = (struct lysc_node*)cs;
3938 cs->nodetype = LYS_CASE;
Radek Krejci05b774b2019-02-25 13:26:18 +01003939 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003940 cs->parent = (struct lysc_node*)ch;
3941 cs->child = child;
3942
3943 return cs;
3944error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02003945 if (cs) {
3946 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
3947 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003948 return NULL;
3949
3950#undef UNIQUE_CHECK
3951}
3952
Radek Krejcib56c7502019-02-13 14:19:54 +01003953/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003954 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003955 *
3956 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003957 * @param[in] node Target node where the config is supposed to be changed.
3958 * @param[in] config_flag Node's config flag to be applied to the @p node.
3959 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003960 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3961 * 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 +01003962 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3963 * @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 +01003964 * @return LY_ERR value.
3965 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003966static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003967lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3968 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003969{
3970 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003971 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003972
3973 if (config == (node->flags & LYS_CONFIG_MASK)) {
3974 /* nothing to do */
3975 return LY_SUCCESS;
3976 }
3977
3978 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003979 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003980 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003982 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3983 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003984 return LY_EVALID;
3985 }
Radek Krejci93dcc392019-02-19 10:43:38 +01003986 node->flags |= LYS_SET_CONFIG;
3987 } else {
3988 if (node->flags & LYS_SET_CONFIG) {
3989 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
3990 /* setting config flags, but have node with explicit config true */
3991 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3992 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3993 refine_flag ? "refine" : "deviation", nodeid);
3994 return LY_EVALID;
3995 }
3996 /* do not change config on nodes where the config is explicitely set, this does not apply to
3997 * nodes, which are being changed explicitly (targets of refine or deviation) */
3998 return LY_SUCCESS;
3999 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004000 }
4001 node->flags &= ~LYS_CONFIG_MASK;
4002 node->flags |= config;
4003
4004 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004005 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004006 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004007 }
4008
Radek Krejci76b3e962018-12-14 17:01:25 +01004009 return LY_SUCCESS;
4010}
4011
Radek Krejcib56c7502019-02-13 14:19:54 +01004012/**
4013 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4014 *
4015 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4016 * the flag to such parents from a mandatory children.
4017 *
4018 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4019 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4020 * (mandatory children was removed).
4021 */
Radek Krejcife909632019-02-12 15:34:42 +01004022void
4023lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4024{
4025 struct lysc_node *iter;
4026
4027 if (add) { /* set flag */
4028 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4029 parent = parent->parent) {
4030 parent->flags |= LYS_MAND_TRUE;
4031 }
4032 } else { /* unset flag */
4033 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004034 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004035 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004036 /* there is another mandatory node */
4037 return;
4038 }
4039 }
4040 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4041 parent->flags &= ~LYS_MAND_TRUE;
4042 }
4043 }
4044}
4045
Radek Krejci056d0a82018-12-06 16:57:25 +01004046/**
Radek Krejci3641f562019-02-13 15:38:40 +01004047 * @brief Internal sorting process for the lys_compile_augment_sort().
4048 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4049 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4050 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4051 */
4052static void
4053lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4054{
4055 unsigned int v;
4056 size_t len;
4057
4058 len = strlen(aug_p->nodeid);
4059 LY_ARRAY_FOR(result, v) {
4060 if (strlen(result[v]->nodeid) <= len) {
4061 continue;
4062 }
4063 if (v < LY_ARRAY_SIZE(result)) {
4064 /* move the rest of array */
4065 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4066 break;
4067 }
4068 }
4069 result[v] = aug_p;
4070 LY_ARRAY_INCREMENT(result);
4071}
4072
4073/**
4074 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4075 *
4076 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4077 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4078 *
4079 * @param[in] ctx Compile context.
4080 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4081 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4082 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4083 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4084 * @return LY_ERR value.
4085 */
4086LY_ERR
4087lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4088{
4089 struct lysp_augment **result = NULL;
4090 unsigned int u, v;
4091 size_t count = 0;
4092
4093 assert(augments);
4094
4095 /* get count of the augments in module and all its submodules */
4096 if (aug_p) {
4097 count += LY_ARRAY_SIZE(aug_p);
4098 }
4099 LY_ARRAY_FOR(inc_p, u) {
4100 if (inc_p[u].submodule->augments) {
4101 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4102 }
4103 }
4104
4105 if (!count) {
4106 *augments = NULL;
4107 return LY_SUCCESS;
4108 }
4109 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4110
4111 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4112 * together, so there can be even /z/y betwwen them. */
4113 LY_ARRAY_FOR(aug_p, u) {
4114 lys_compile_augment_sort_(&aug_p[u], result);
4115 }
4116 LY_ARRAY_FOR(inc_p, u) {
4117 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4118 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4119 }
4120 }
4121
4122 *augments = result;
4123 return LY_SUCCESS;
4124}
4125
4126/**
4127 * @brief Compile the parsed augment connecting it into its target.
4128 *
4129 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4130 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4131 * are already implemented and compiled.
4132 *
4133 * @param[in] ctx Compile context.
4134 * @param[in] aug_p Parsed augment to compile.
4135 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4136 * @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
4137 * children in case of the augmenting uses data.
4138 * @return LY_SUCCESS on success.
4139 * @return LY_EVALID on failure.
4140 */
4141LY_ERR
4142lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4143{
4144 LY_ERR ret = LY_SUCCESS;
4145 struct lysp_node *node_p, *case_node_p;
4146 struct lysc_node *target; /* target target of the augment */
4147 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004148 struct lysc_when **when, *when_shared;
4149 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004150 uint16_t flags = 0;
4151 unsigned int u;
Radek Krejci3641f562019-02-13 15:38:40 +01004152
Radek Krejci7af64242019-02-18 13:07:53 +01004153 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004154 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004155 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004156 if (ret != LY_SUCCESS) {
4157 if (ret == LY_EDENIED) {
4158 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4159 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4160 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4161 }
4162 return LY_EVALID;
4163 }
4164
4165 /* check for mandatory nodes
4166 * - new cases augmenting some choice can have mandatory nodes
4167 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4168 */
Radek Krejci733988a2019-02-15 15:12:44 +01004169 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004170 allow_mandatory = 1;
4171 }
4172
4173 when_shared = NULL;
4174 LY_LIST_FOR(aug_p->child, node_p) {
4175 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4176 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4177 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004178 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4179 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004180 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4181 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4182 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4183 return LY_EVALID;
4184 }
4185
4186 /* compile the children */
4187 if (node_p->nodetype != LYS_CASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004188 LY_CHECK_RET(lys_compile_node(ctx, node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004189 } else {
4190 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004191 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004192 }
4193 }
4194
Radek Krejcife13da42019-02-15 14:51:01 +01004195 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4196 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004197 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004198 /* 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 +01004199 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 +01004200 } else if (target->nodetype == LYS_CHOICE) {
4201 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4202 node = ((struct lysc_node_choice*)target)->cases->prev;
4203 } else {
4204 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004205 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004206 }
4207
Radek Krejci733988a2019-02-15 15:12:44 +01004208 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004209 node->flags &= ~LYS_MAND_TRUE;
4210 lys_compile_mandatory_parents(target, 0);
4211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4212 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4213 aug_p->nodeid, node->name);
4214 return LY_EVALID;
4215 }
4216
4217 /* pass augment's when to all the children */
4218 if (aug_p->when) {
4219 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4220 if (!when_shared) {
4221 ret = lys_compile_when(ctx, aug_p->when, options, when);
4222 LY_CHECK_GOTO(ret, error);
4223 (*when)->context = lysc_xpath_context(target);
4224 when_shared = *when;
4225 } else {
4226 ++when_shared->refcount;
4227 (*when) = when_shared;
4228 }
4229 }
4230 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004231
4232 switch (target->nodetype) {
4233 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004234 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
4235 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004236 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
4237 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004238 break;
4239 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004240 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
4241 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004242 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
4243 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004244 break;
4245 default:
4246 if (aug_p->actions) {
4247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4248 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4249 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4250 return LY_EVALID;
4251 }
4252 if (aug_p->notifs) {
4253 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4254 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4255 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4256 return LY_EVALID;
4257 }
4258 }
Radek Krejci3641f562019-02-13 15:38:40 +01004259
4260error:
4261 return ret;
4262}
4263
4264/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004265 * @brief Apply refined or deviated mandatory flag to the target node.
4266 *
4267 * @param[in] ctx Compile context.
4268 * @param[in] node Target node where the mandatory property is supposed to be changed.
4269 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4270 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4271 * @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 +01004272 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4273 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4274 * 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 +01004275 * @return LY_ERR value.
4276 */
4277static LY_ERR
4278lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4279{
4280 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4282 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4283 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4284 return LY_EVALID;
4285 }
4286
4287 if (mandatory_flag & LYS_MAND_TRUE) {
4288 /* check if node has default value */
4289 if (node->nodetype & LYS_LEAF) {
4290 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004291 if (refine_flag) {
4292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4293 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4294 return LY_EVALID;
4295 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004296 } else {
4297 /* remove the default value taken from the leaf's type */
4298 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4299 ((struct lysc_node_leaf*)node)->dflt = NULL;
4300 }
4301 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004302 if (refine_flag) {
4303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4304 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4305 return LY_EVALID;
4306 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004307 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004308 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004310 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004311 return LY_EVALID;
4312 }
4313
4314 node->flags &= ~LYS_MAND_FALSE;
4315 node->flags |= LYS_MAND_TRUE;
4316 lys_compile_mandatory_parents(node->parent, 1);
4317 } else {
4318 /* make mandatory false */
4319 node->flags &= ~LYS_MAND_TRUE;
4320 node->flags |= LYS_MAND_FALSE;
4321 lys_compile_mandatory_parents(node->parent, 0);
4322 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4323 /* get the type's default value if any */
4324 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4325 }
4326 }
4327 return LY_SUCCESS;
4328}
4329
4330/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004331 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4332 * If present, also apply uses's modificators.
4333 *
4334 * @param[in] ctx Compile context
4335 * @param[in] uses_p Parsed uses schema node.
4336 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4337 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4338 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4339 * the compile context.
4340 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4341 */
4342static LY_ERR
4343lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
4344{
4345 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004346 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004347 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004348 struct lysc_node_container context_node_fake =
4349 {.nodetype = LYS_CONTAINER,
4350 .module = ctx->mod,
4351 .flags = parent ? parent->flags : 0,
4352 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004353 .prev = (struct lysc_node*)&context_node_fake,
4354 .actions = NULL, .notifs = NULL};
Radek Krejcie86bf772018-12-14 11:39:53 +01004355 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004356 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004357 int found;
4358 const char *id, *name, *prefix;
4359 size_t prefix_len, name_len;
4360 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004361 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004362 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004363 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004364 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004365 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004366 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004367 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004368 unsigned int actions_index, notifs_index;
4369 struct lysc_notif **notifs = NULL;
4370 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004371
4372 /* search for the grouping definition */
4373 found = 0;
4374 id = uses_p->name;
Radek Krejci15e99fc2019-04-08 14:29:39 +02004375 LY_CHECK_RET(lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004376 if (prefix) {
4377 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4378 if (!mod) {
4379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4380 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4381 return LY_EVALID;
4382 }
4383 } else {
4384 mod = ctx->mod_def;
4385 }
4386 if (mod == ctx->mod_def) {
4387 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
4388 grp = lysp_node_groupings(node_p);
4389 LY_ARRAY_FOR(grp, u) {
4390 if (!strcmp(grp[u].name, name)) {
4391 grp = &grp[u];
4392 found = 1;
4393 break;
4394 }
4395 }
4396 }
4397 }
4398 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004399 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004400 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004401 if (grp) {
4402 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4403 if (!strcmp(grp[u].name, name)) {
4404 grp = &grp[u];
4405 found = 1;
4406 }
4407 }
4408 }
4409 if (!found && mod->parsed->includes) {
4410 /* ... and all the submodules */
4411 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4412 grp = mod->parsed->includes[u].submodule->groupings;
4413 if (grp) {
4414 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4415 if (!strcmp(grp[v].name, name)) {
4416 grp = &grp[v];
4417 found = 1;
4418 }
4419 }
4420 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004421 }
4422 }
4423 }
4424 if (!found) {
4425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4426 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4427 return LY_EVALID;
4428 }
4429
4430 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4431 grp_stack_count = ctx->groupings.count;
4432 ly_set_add(&ctx->groupings, (void*)grp, 0);
4433 if (grp_stack_count == ctx->groupings.count) {
4434 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4435 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4436 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4437 return LY_EVALID;
4438 }
4439
4440 /* switch context's mod_def */
4441 mod_old = ctx->mod_def;
4442 ctx->mod_def = mod;
4443
4444 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004445 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 +01004446
Radek Krejcifc11bd72019-04-11 16:00:05 +02004447 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004448 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004449 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004450 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004451 child = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01004452
4453 /* some preparation for applying refines */
4454 if (grp->data == node_p) {
4455 /* remember the first child */
4456 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01004457 }
4458 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004459 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004460 LY_LIST_FOR(context_node_fake.child, child) {
4461 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004462
Radek Krejcifc11bd72019-04-11 16:00:05 +02004463 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004464 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004465 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004466 if (!when_shared) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004467 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004468 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004469 when_shared = *when;
4470 } else {
4471 ++when_shared->refcount;
4472 (*when) = when_shared;
4473 }
4474 }
Radek Krejci01342af2019-01-03 15:18:08 +01004475 }
4476 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004477 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004478 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004479 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004480 context_node_fake.child->prev = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01004481 }
4482
Radek Krejcifc11bd72019-04-11 16:00:05 +02004483 /* compile actions */
4484 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4485 if (actions) {
4486 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
4487 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, options, u, lys_compile_action, 0, ret, cleanup);
4488 if (*actions && (uses_p->augments || uses_p->refines)) {
4489 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4490 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4491 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4492 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4493 }
4494 }
4495
4496 /* compile notifications */
4497 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4498 if (notifs) {
4499 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
4500 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, options, u, lys_compile_notif, 0, ret, cleanup);
4501 if (*notifs && (uses_p->augments || uses_p->refines)) {
4502 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4503 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4504 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4505 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4506 }
4507 }
4508
4509
Radek Krejci3641f562019-02-13 15:38:40 +01004510 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004511 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004512 LY_ARRAY_FOR(augments, u) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004513 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004514 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004515
Radek Krejcif0089082019-01-07 16:42:01 +01004516 /* reload previous context's mod_def */
4517 ctx->mod_def = mod_old;
4518
Radek Krejci76b3e962018-12-14 17:01:25 +01004519 /* apply refine */
4520 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004521 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 +01004522 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004523 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004524 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004525
4526 /* default value */
4527 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004528 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4530 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4531 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004532 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004533 }
4534 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4536 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4537 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004538 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004539 }
4540 if (node->nodetype == LYS_LEAF) {
4541 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4542 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004543 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004544 /* TODO check the default value according to type */
4545 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004546 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004547 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4548 "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 +02004549 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004550 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004551 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4552 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4553 }
4554 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004555 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004556 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 +01004557 LY_ARRAY_FOR(rfn->dflts, u) {
4558 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4559 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4560 }
4561 /* TODO check the default values according to type */
4562 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004563 if (((struct lysc_node_choice*)node)->dflt) {
4564 /* unset LYS_SET_DFLT from the current default case */
4565 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4566 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004567 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 +01004568 }
4569 }
4570
Radek Krejci12fb9142019-01-08 09:45:30 +01004571 /* description */
4572 if (rfn->dsc) {
4573 FREE_STRING(ctx->ctx, node->dsc);
4574 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4575 }
4576
4577 /* reference */
4578 if (rfn->ref) {
4579 FREE_STRING(ctx->ctx, node->ref);
4580 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4581 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004582
4583 /* config */
4584 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004585 if (!flags) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004586 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004587 } else {
4588 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4589 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4590 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004591 }
4592
4593 /* mandatory */
4594 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004595 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004596 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004597
4598 /* presence */
4599 if (rfn->presence) {
4600 if (node->nodetype != LYS_CONTAINER) {
4601 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4602 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4603 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004604 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004605 }
4606 node->flags |= LYS_PRESENCE;
4607 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004608
4609 /* must */
4610 if (rfn->musts) {
4611 switch (node->nodetype) {
4612 case LYS_LEAF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004613 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004614 break;
4615 case LYS_LEAFLIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004616 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004617 break;
4618 case LYS_LIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004619 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004620 break;
4621 case LYS_CONTAINER:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004622 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004623 break;
4624 case LYS_ANYXML:
4625 case LYS_ANYDATA:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004626 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004627 break;
4628 default:
4629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4630 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4631 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004632 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004633 }
4634 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004635
4636 /* min/max-elements */
4637 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4638 switch (node->nodetype) {
4639 case LYS_LEAFLIST:
4640 if (rfn->flags & LYS_SET_MAX) {
4641 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4642 }
4643 if (rfn->flags & LYS_SET_MIN) {
4644 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004645 if (rfn->min) {
4646 node->flags |= LYS_MAND_TRUE;
4647 lys_compile_mandatory_parents(node->parent, 1);
4648 } else {
4649 node->flags &= ~LYS_MAND_TRUE;
4650 lys_compile_mandatory_parents(node->parent, 0);
4651 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004652 }
4653 break;
4654 case LYS_LIST:
4655 if (rfn->flags & LYS_SET_MAX) {
4656 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4657 }
4658 if (rfn->flags & LYS_SET_MIN) {
4659 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004660 if (rfn->min) {
4661 node->flags |= LYS_MAND_TRUE;
4662 lys_compile_mandatory_parents(node->parent, 1);
4663 } else {
4664 node->flags &= ~LYS_MAND_TRUE;
4665 lys_compile_mandatory_parents(node->parent, 0);
4666 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004667 }
4668 break;
4669 default:
4670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4671 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4672 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004673 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004674 }
4675 }
Radek Krejcif0089082019-01-07 16:42:01 +01004676
4677 /* if-feature */
4678 if (rfn->iffeatures) {
4679 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004680 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004681 }
Radek Krejci01342af2019-01-03 15:18:08 +01004682 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004683
Radek Krejcif2271f12019-01-07 16:42:23 +01004684 /* do some additional checks of the changed nodes when all the refines are applied */
4685 for (u = 0; u < refined.count; ++u) {
4686 node = (struct lysc_node*)refined.objs[u];
4687 rfn = &uses_p->refines[u];
4688
4689 /* check possible conflict with default value (default added, mandatory left true) */
4690 if ((node->flags & LYS_MAND_TRUE) &&
4691 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4692 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4694 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004695 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004696 }
4697
4698 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4699 if (node->nodetype == LYS_LIST) {
4700 min = ((struct lysc_node_list*)node)->min;
4701 max = ((struct lysc_node_list*)node)->max;
4702 } else {
4703 min = ((struct lysc_node_leaflist*)node)->min;
4704 max = ((struct lysc_node_leaflist*)node)->max;
4705 }
4706 if (min > max) {
4707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4708 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4709 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004710 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004711 }
4712 }
4713 }
4714
Radek Krejcie86bf772018-12-14 11:39:53 +01004715 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004716
4717cleanup:
4718 /* fix connection of the children nodes from fake context node back into the parent */
4719 if (context_node_fake.child) {
4720 context_node_fake.child->prev = child;
4721 }
4722 LY_LIST_FOR(context_node_fake.child, child) {
4723 child->parent = parent;
4724 }
4725
4726 if (uses_p->augments || uses_p->refines) {
4727 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004728 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004729 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4730 LY_ARRAY_FREE(context_node_fake.actions);
4731 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004732 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004733 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4734 LY_ARRAY_FREE(context_node_fake.notifs);
4735 }
4736 }
4737
Radek Krejcie86bf772018-12-14 11:39:53 +01004738 /* reload previous context's mod_def */
4739 ctx->mod_def = mod_old;
4740 /* remove the grouping from the stack for circular groupings dependency check */
4741 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4742 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004743 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004744 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004745
4746 return ret;
4747}
4748
Radek Krejcife909632019-02-12 15:34:42 +01004749
Radek Krejcie86bf772018-12-14 11:39:53 +01004750/**
Radek Krejcia3045382018-11-22 14:30:31 +01004751 * @brief Compile parsed schema node information.
4752 * @param[in] ctx Compile context
4753 * @param[in] node_p Parsed schema node.
4754 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4755 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4756 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4757 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004758 * @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).
4759 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004760 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4761 */
Radek Krejci19a96102018-11-15 13:38:09 +01004762static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01004763lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status)
Radek Krejci19a96102018-11-15 13:38:09 +01004764{
4765 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004766 struct lysc_node *node;
4767 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004768 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004769 unsigned int u;
4770 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
4771
4772 switch (node_p->nodetype) {
4773 case LYS_CONTAINER:
4774 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4775 node_compile_spec = lys_compile_node_container;
4776 break;
4777 case LYS_LEAF:
4778 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4779 node_compile_spec = lys_compile_node_leaf;
4780 break;
4781 case LYS_LIST:
4782 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004783 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004784 break;
4785 case LYS_LEAFLIST:
4786 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004787 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004788 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004789 case LYS_CHOICE:
4790 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004791 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004792 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004793 case LYS_ANYXML:
4794 case LYS_ANYDATA:
4795 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004796 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004797 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004798 case LYS_USES:
4799 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004800 default:
4801 LOGINT(ctx->ctx);
4802 return LY_EINT;
4803 }
4804 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4805 node->nodetype = node_p->nodetype;
4806 node->module = ctx->mod;
4807 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004808 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004809
4810 /* config */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004811 if (options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
4812 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004813 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004814 node->flags |= (options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4815 } else if (options & LYSC_OPT_NOTIFICATION) {
4816 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004817 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004818 node->flags |= LYS_CONFIG_R;
4819 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004820 /* config not explicitely set, inherit it from parent */
4821 if (parent) {
4822 node->flags |= parent->flags & LYS_CONFIG_MASK;
4823 } else {
4824 /* default is config true */
4825 node->flags |= LYS_CONFIG_W;
4826 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004827 } else {
4828 /* config set explicitely */
4829 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004830 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004831 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4833 "Configuration node cannot be child of any state data node.");
4834 goto error;
4835 }
Radek Krejci19a96102018-11-15 13:38:09 +01004836
Radek Krejcia6d57732018-11-29 13:40:37 +01004837 /* *list ordering */
4838 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4839 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004840 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
4841 (options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
4842 (options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01004843 node->flags &= ~LYS_ORDBY_MASK;
4844 node->flags |= LYS_ORDBY_SYSTEM;
4845 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4846 /* default ordering is system */
4847 node->flags |= LYS_ORDBY_SYSTEM;
4848 }
4849 }
4850
Radek Krejci19a96102018-11-15 13:38:09 +01004851 /* status - it is not inherited by specification, but it does not make sense to have
4852 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004853 if (!parent || parent->nodetype != LYS_CHOICE) {
4854 /* in case of choice/case's children, postpone the check to the moment we know if
4855 * 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 +01004856 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 +01004857 }
4858
4859 if (!(options & LYSC_OPT_FREE_SP)) {
4860 node->sp = node_p;
4861 }
4862 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004863 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4864 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004865 if (node_p->when) {
4866 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4867 ret = lys_compile_when(ctx, node_p->when, options, when);
4868 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004869 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004870 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004871 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004872 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
4873
4874 /* nodetype-specific part */
4875 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
4876
Radek Krejcife909632019-02-12 15:34:42 +01004877 /* inherit LYS_MAND_TRUE in parent containers */
4878 if (node->flags & LYS_MAND_TRUE) {
4879 lys_compile_mandatory_parents(parent, 1);
4880 }
4881
Radek Krejci19a96102018-11-15 13:38:09 +01004882 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004883 if (parent) {
4884 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004885 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
4886 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004887 if (uses_status) {
4888
4889 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004890 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004891 * it directly gets the same status flags as the choice;
4892 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004893 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004894 node->parent = (struct lysc_node*)cs;
4895 } else { /* other than choice */
4896 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004897 }
Radek Krejci05b774b2019-02-25 13:26:18 +01004898 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node, options), LY_EVALID);
Radek Krejci19a96102018-11-15 13:38:09 +01004899 } else {
4900 /* top-level element */
4901 if (!ctx->mod->compiled->data) {
4902 ctx->mod->compiled->data = node;
4903 } else {
4904 /* insert at the end of the module's top-level nodes list */
4905 ctx->mod->compiled->data->prev->next = node;
4906 node->prev = ctx->mod->compiled->data->prev;
4907 ctx->mod->compiled->data->prev = node;
4908 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004909 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4910 ctx->mod->compiled->notifs, node->name, node)) {
4911 return LY_EVALID;
4912 }
Radek Krejci19a96102018-11-15 13:38:09 +01004913 }
4914
4915 return LY_SUCCESS;
4916
4917error:
4918 lysc_node_free(ctx->ctx, node);
4919 return ret;
4920}
4921
Radek Krejciccd20f12019-02-15 14:12:27 +01004922static void
4923lysc_disconnect(struct lysc_node *node)
4924{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004925 struct lysc_node *parent, *child, *prev = NULL, *next;
4926 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004927 int remove_cs = 0;
4928
4929 parent = node->parent;
4930
4931 /* parent's first child */
4932 if (!parent) {
4933 return;
4934 }
4935 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004936 cs = (struct lysc_node_case*)node;
4937 } else if (parent->nodetype == LYS_CASE) {
4938 /* disconnecting some node in a case */
4939 cs = (struct lysc_node_case*)parent;
4940 parent = cs->parent;
4941 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4942 if (child == node) {
4943 if (cs->child == child) {
4944 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
4945 /* case with a single child -> remove also the case */
4946 child->parent = NULL;
4947 remove_cs = 1;
4948 } else {
4949 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01004950 }
4951 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004952 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01004953 }
4954 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004955 if (!remove_cs) {
4956 cs = NULL;
4957 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004958 } else if (lysc_node_children(parent, node->flags) == node) {
4959 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004960 }
4961
4962 if (cs) {
4963 if (remove_cs) {
4964 /* cs has only one child which is being also removed */
4965 lysc_disconnect((struct lysc_node*)cs);
4966 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
4967 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01004968 if (((struct lysc_node_choice*)parent)->dflt == cs) {
4969 /* default case removed */
4970 ((struct lysc_node_choice*)parent)->dflt = NULL;
4971 }
4972 if (((struct lysc_node_choice*)parent)->cases == cs) {
4973 /* first case removed */
4974 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
4975 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004976 if (cs->child) {
4977 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
4978 if (cs->child->prev->parent != (struct lysc_node*)cs) {
4979 prev = cs->child->prev;
4980 } /* else all the children are under a single case */
4981 LY_LIST_FOR_SAFE(cs->child, next, child) {
4982 if (child->parent != (struct lysc_node*)cs) {
4983 break;
4984 }
4985 lysc_node_free(node->module->ctx, child);
4986 }
4987 if (prev) {
4988 if (prev->next) {
4989 prev->next = child;
4990 }
4991 if (child) {
4992 child->prev = prev;
4993 } else {
4994 /* link from the first child under the cases */
4995 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
4996 }
4997 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004998 }
4999 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005000 }
5001
5002 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005003 if (node->prev->next) {
5004 node->prev->next = node->next;
5005 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005006 if (node->next) {
5007 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005008 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005009 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005010 if (child) {
5011 child->prev = node->prev;
5012 }
5013 } else if (((struct lysc_node_choice*)parent)->cases) {
5014 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005015 }
5016}
5017
5018LY_ERR
5019lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options)
5020{
5021 LY_ERR ret = LY_EVALID;
5022 struct ly_set devs_p = {0};
5023 struct ly_set targets = {0};
5024 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005025 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005026 struct lysc_action *rpcs;
5027 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005028 struct lysp_deviation *dev;
5029 struct lysp_deviate *d, **dp_new;
5030 struct lysp_deviate_add *d_add;
5031 struct lysp_deviate_del *d_del;
5032 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005033 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005034 struct lysc_deviation {
5035 const char *nodeid;
5036 struct lysc_node *target; /* target node of the deviation */
5037 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005038 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005039 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5040 } **devs = NULL;
5041 int i;
5042 size_t prefix_len, name_len;
5043 const char *prefix, *name, *nodeid;
5044 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005045 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005046 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005047
5048 /* get all deviations from the module and all its submodules ... */
5049 LY_ARRAY_FOR(mod_p->deviations, u) {
5050 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5051 }
5052 LY_ARRAY_FOR(mod_p->includes, v) {
5053 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5054 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5055 }
5056 }
5057 if (!devs_p.count) {
5058 /* nothing to do */
5059 return LY_SUCCESS;
5060 }
5061
5062 /* ... and group them by the target node */
5063 devs = calloc(devs_p.count, sizeof *devs);
5064 for (u = 0; u < devs_p.count; ++u) {
5065 dev = devs_p.objs[u];
5066
5067 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005068 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5069 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005070 if (target->nodetype == LYS_ACTION) {
5071 /* move the target pointer to input/output to make them different from the action and
5072 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5073 * back to the RPC/action node due to a better compatibility and decision code in this function.
5074 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5075 if (flags & LYSC_OPT_RPC_INPUT) {
5076 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5077 flags |= LYSC_OPT_INTERNAL;
5078 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5079 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5080 flags |= LYSC_OPT_INTERNAL;
5081 }
5082 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005083 /* insert into the set of targets with duplicity detection */
5084 i = ly_set_add(&targets, target, 0);
5085 if (!devs[i]) {
5086 /* new record */
5087 devs[i] = calloc(1, sizeof **devs);
5088 devs[i]->target = target;
5089 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005090 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005091 }
5092 /* add deviates into the deviation's list of deviates */
5093 for (d = dev->deviates; d; d = d->next) {
5094 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5095 *dp_new = d;
5096 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5097 devs[i]->not_supported = 1;
5098 }
5099 }
5100 }
5101
5102 /* MACROS for deviates checking */
5103#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5104 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
5105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
5106 goto cleanup; \
5107 }
5108
5109#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5110 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
5111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
5112 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
5113 goto cleanup; \
5114 }
5115
5116#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005117 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5119 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
5120 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5121 goto cleanup; \
5122 }
5123
Radek Krejci551b12c2019-02-19 16:11:21 +01005124#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5125 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5127 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
5128 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5129 goto cleanup; \
5130 }
5131
Radek Krejciccd20f12019-02-15 14:12:27 +01005132#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5133 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5135 goto cleanup; \
5136 }
5137
Radek Krejci551b12c2019-02-19 16:11:21 +01005138#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5139 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5140 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5141 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5142 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5143 goto cleanup; \
5144 }
5145
Radek Krejciccd20f12019-02-15 14:12:27 +01005146#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5147 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5148 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5150 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5151 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5152 goto cleanup; \
5153 } \
5154 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5155 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5156
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005157#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5158 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5159 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5160 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5161 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 +01005162 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005163 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005164 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5165 "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 +01005166 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005167 goto cleanup; \
5168 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005169 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5170 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5171 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5172 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5173 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005174 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005175 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5176 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5177 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005178 }
5179
5180 /* apply deviations */
5181 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005182 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5183 /* fix the target pointer in case of RPC's/action's input/output */
5184 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5185 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5186 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5187 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5188 }
5189 }
5190
Radek Krejciccd20f12019-02-15 14:12:27 +01005191 /* not-supported */
5192 if (devs[u]->not_supported) {
5193 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5194 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5195 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5196 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005197
5198#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5199 if (devs[u]->target->parent) { \
5200 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5201 } else { \
5202 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5203 } \
5204 LY_ARRAY_FOR(ARRAY, x) { \
5205 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5206 } \
5207 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5208 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5209 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5210 LY_ARRAY_DECREMENT(ARRAY); \
5211 }
5212
Radek Krejcif538ce52019-03-05 10:46:14 +01005213 if (devs[u]->target->nodetype == LYS_ACTION) {
5214 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5215 /* remove RPC's/action's input */
5216 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5217 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005218 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5219 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005220 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5221 /* remove RPC's/action's output */
5222 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5223 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005224 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5225 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005226 } else {
5227 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005228 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005229 }
5230 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005231 /* remove Notification */
5232 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005233 } else {
5234 /* remove the target node */
5235 lysc_disconnect(devs[u]->target);
5236 lysc_node_free(ctx->ctx, devs[u]->target);
5237 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005238
5239 continue;
5240 }
5241
5242 /* list of deviates (not-supported is not present in the list) */
5243 LY_ARRAY_FOR(devs[u]->deviates, v) {
5244 d = devs[u]->deviates[v];
5245
5246 switch (d->mod) {
5247 case LYS_DEV_ADD:
5248 d_add = (struct lysp_deviate_add*)d;
5249 /* [units-stmt] */
5250 if (d_add->units) {
5251 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5252 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5253
5254 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5255 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5256 }
5257
5258 /* *must-stmt */
5259 if (d_add->musts) {
5260 switch (devs[u]->target->nodetype) {
5261 case LYS_CONTAINER:
5262 case LYS_LIST:
5263 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5264 options, x, lys_compile_must, ret, cleanup);
5265 break;
5266 case LYS_LEAF:
5267 case LYS_LEAFLIST:
5268 case LYS_ANYDATA:
5269 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5270 options, x, lys_compile_must, ret, cleanup);
5271 break;
5272 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005273 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5274 options, x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005275 break;
5276 case LYS_ACTION:
5277 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5278 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5279 options, x, lys_compile_must, ret, cleanup);
5280 break;
5281 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5282 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5283 options, x, lys_compile_must, ret, cleanup);
5284 break;
5285 }
5286 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005287 default:
5288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005289 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005290 goto cleanup;
5291 }
5292 }
5293
5294 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005295 if (d_add->uniques) {
5296 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5297 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5298 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005299
5300 /* *default-stmt */
5301 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005302 switch (devs[u]->target->nodetype) {
5303 case LYS_LEAF:
5304 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5305 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5306 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5307 /* first, remove the default value taken from the type */
5308 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5309 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5310 }
5311 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005312 /* mark the new default values as leaf's own */
5313 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005314 break;
5315 case LYS_LEAFLIST:
5316 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5317 /* first, remove the default value taken from the type */
5318 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5319 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5320 }
5321 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5322 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5323 }
5324 /* add new default value(s) */
5325 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5326 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5327 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5328 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5329 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5330 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5331 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005332 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005333 devs[u]->target->flags |= LYS_SET_DFLT;
5334 break;
5335 case LYS_CHOICE:
5336 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5337 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5338 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5339 * to allow making the default case even the augmented case from the deviating module */
5340 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5341 (struct lysc_node_choice*)devs[u]->target)) {
5342 goto cleanup;
5343 }
5344 break;
5345 default:
5346 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005347 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005348 goto cleanup;
5349 }
5350 }
5351
5352 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005353 if (d_add->flags & LYS_CONFIG_MASK) {
5354 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5355 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5356 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5357 goto cleanup;
5358 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005359 if (devs[u]->flags) {
5360 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5361 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5362 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005363 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5365 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5366 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5367 goto cleanup;
5368 }
5369 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5370 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005371
5372 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005373 if (d_add->flags & LYS_MAND_MASK) {
5374 if (devs[u]->target->flags & LYS_MAND_MASK) {
5375 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5376 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5377 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5378 goto cleanup;
5379 }
5380 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5381 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005382
5383 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005384 if (d_add->flags & LYS_SET_MIN) {
5385 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5386 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5387 /* change value */
5388 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5389 } else if (devs[u]->target->nodetype == LYS_LIST) {
5390 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5391 /* change value */
5392 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5393 } else {
5394 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5395 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5396 goto cleanup;
5397 }
5398 if (d_add->min) {
5399 devs[u]->target->flags |= LYS_MAND_TRUE;
5400 }
5401 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005402
5403 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005404 if (d_add->flags & LYS_SET_MAX) {
5405 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5406 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5407 /* change value */
5408 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5409 } else if (devs[u]->target->nodetype == LYS_LIST) {
5410 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5411 /* change value */
5412 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5413 } else {
5414 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5415 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5416 goto cleanup;
5417 }
5418 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005419
5420 break;
5421 case LYS_DEV_DELETE:
5422 d_del = (struct lysp_deviate_del*)d;
5423
5424 /* [units-stmt] */
5425 if (d_del->units) {
5426 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5427 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5428 }
5429
5430 /* *must-stmt */
5431 if (d_del->musts) {
5432 switch (devs[u]->target->nodetype) {
5433 case LYS_CONTAINER:
5434 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005435 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005436 break;
5437 case LYS_LEAF:
5438 case LYS_LEAFLIST:
5439 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005440 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005441 break;
5442 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005443 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005444 break;
5445 case LYS_ACTION:
5446 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5447 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5448 break;
5449 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5450 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5451 break;
5452 }
5453 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005454 default:
5455 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005456 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005457 goto cleanup;
5458 }
5459 }
5460
5461 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005462 if (d_del->uniques) {
5463 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5464 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5465 LY_ARRAY_FOR(d_del->uniques, x) {
5466 LY_ARRAY_FOR(list->uniques, z) {
5467 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5468 nodeid = strpbrk(name, " \t\n");
5469 if (nodeid) {
5470 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5471 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5472 break;
5473 }
5474 while (isspace(*nodeid)) {
5475 ++nodeid;
5476 }
5477 } else {
5478 if (strcmp(name, list->uniques[z][y]->name)) {
5479 break;
5480 }
5481 }
5482 }
5483 if (!name) {
5484 /* complete match - remove the unique */
5485 LY_ARRAY_DECREMENT(list->uniques);
5486 LY_ARRAY_FREE(list->uniques[z]);
5487 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5488 --z;
5489 break;
5490 }
5491 }
5492 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5494 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5495 devs[u]->nodeid, d_del->uniques[x]);
5496 goto cleanup;
5497 }
5498 }
5499 if (!LY_ARRAY_SIZE(list->uniques)) {
5500 LY_ARRAY_FREE(list->uniques);
5501 list->uniques = NULL;
5502 }
5503 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005504
5505 /* *default-stmt */
5506 if (d_del->dflts) {
5507 switch (devs[u]->target->nodetype) {
5508 case LYS_LEAF:
5509 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5510 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5511 dflt, "deleting", "default", d_del->dflts[0]);
5512
5513 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005514 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005515 break;
5516 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005517 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005518 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5519 devs[u]->target->flags &= ~LYS_SET_DFLT;
5520 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005521 break;
5522 case LYS_CHOICE:
5523 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5524 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5525 nodeid = d_del->dflts[0];
5526 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5527 if (prefix) {
5528 /* use module prefixes from the deviation module to match the module of the default case */
5529 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5530 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5531 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5532 "The prefix does not match any imported module of the deviation module.",
5533 devs[u]->nodeid, d_del->dflts[0]);
5534 goto cleanup;
5535 }
5536 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5537 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5538 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5539 "The prefix does not match the default case's module.",
5540 devs[u]->nodeid, d_del->dflts[0]);
5541 goto cleanup;
5542 }
5543 }
5544 /* else {
5545 * strictly, the default prefix would point to the deviation module, but the value should actually
5546 * match the default string in the original module (usually unprefixed), so in this case we do not check
5547 * the module of the default case, just matching its name */
5548 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5550 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5551 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5552 goto cleanup;
5553 }
5554 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5555 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5556 break;
5557 default:
5558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005559 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005560 goto cleanup;
5561 }
5562 }
5563
5564 break;
5565 case LYS_DEV_REPLACE:
5566 d_rpl = (struct lysp_deviate_rpl*)d;
5567
5568 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005569 if (d_rpl->type) {
5570 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5571 /* type is mandatory, so checking for its presence is not necessary */
5572 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
5573 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup);
5574 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005575
5576 /* [units-stmt] */
5577 if (d_rpl->units) {
5578 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5579 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5580 units, "replacing", "units", d_rpl->units);
5581
5582 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5583 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5584 }
5585
5586 /* [default-stmt] */
5587 if (d_rpl->dflt) {
5588 switch (devs[u]->target->nodetype) {
5589 case LYS_LEAF:
5590 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5591 dflt, "replacing", "default", d_rpl->dflt);
5592
5593 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5594 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5595 break;
5596 case LYS_CHOICE:
5597 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5598 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5599 (struct lysc_node_choice*)devs[u]->target)) {
5600 goto cleanup;
5601 }
5602 break;
5603 default:
5604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005605 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005606 goto cleanup;
5607 }
5608 }
5609
5610 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005611 if (d_rpl->flags & LYS_CONFIG_MASK) {
5612 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5613 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5614 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5615 goto cleanup;
5616 }
5617 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5619 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5620 goto cleanup;
5621 }
5622 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5623 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005624
5625 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005626 if (d_rpl->flags & LYS_MAND_MASK) {
5627 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5628 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5629 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5630 goto cleanup;
5631 }
5632 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5633 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005634
5635 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005636 if (d_rpl->flags & LYS_SET_MIN) {
5637 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5638 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5639 /* change value */
5640 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5641 } else if (devs[u]->target->nodetype == LYS_LIST) {
5642 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5643 /* change value */
5644 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5645 } else {
5646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5647 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5648 goto cleanup;
5649 }
5650 if (d_rpl->min) {
5651 devs[u]->target->flags |= LYS_MAND_TRUE;
5652 }
5653 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005654
5655 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005656 if (d_rpl->flags & LYS_SET_MAX) {
5657 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5658 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5659 /* change value */
5660 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5661 } else if (devs[u]->target->nodetype == LYS_LIST) {
5662 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5663 /* change value */
5664 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5665 } else {
5666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5667 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5668 goto cleanup;
5669 }
5670 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005671
5672 break;
5673 default:
5674 LOGINT(ctx->ctx);
5675 goto cleanup;
5676 }
5677 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005678
Radek Krejci33f72892019-02-21 10:36:58 +01005679 /* final check when all deviations of a single target node are applied */
5680
Radek Krejci551b12c2019-02-19 16:11:21 +01005681 /* check min-max compatibility */
5682 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5683 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5684 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5685 } else if (devs[u]->target->nodetype == LYS_LIST) {
5686 min = ((struct lysc_node_list*)devs[u]->target)->min;
5687 max = ((struct lysc_node_list*)devs[u]->target)->max;
5688 } else {
5689 min = max = 0;
5690 }
5691 if (min > max) {
5692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5693 "after deviation (%s): min value %u is bigger than max value %u.",
5694 devs[u]->nodeid, min, max);
5695 goto cleanup;
5696 }
5697
5698 /* check mandatory - default compatibility */
5699 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5700 && (devs[u]->target->flags & LYS_SET_DFLT)
5701 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5702 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5703 "Invalid deviation (%s) combining default value and mandatory %s.",
5704 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5705 goto cleanup;
5706 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5707 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5708 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5710 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5711 goto cleanup;
5712 }
5713 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5714 /* mandatory node under a default case */
5715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5716 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5717 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5718 goto cleanup;
5719 }
Radek Krejci33f72892019-02-21 10:36:58 +01005720
5721 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005722 }
5723
5724 ret = LY_SUCCESS;
5725
5726cleanup:
5727 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5728 LY_ARRAY_FREE(devs[u]->deviates);
5729 free(devs[u]);
5730 }
5731 free(devs);
5732 ly_set_erase(&targets, NULL);
5733 ly_set_erase(&devs_p, NULL);
5734
5735 return ret;
5736}
5737
Radek Krejcib56c7502019-02-13 14:19:54 +01005738/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005739 * @brief Compile the given YANG submodule into the main module.
5740 * @param[in] ctx Compile context
5741 * @param[in] inc Include structure from the main module defining the submodule.
5742 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
5743 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5744 */
5745LY_ERR
5746lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
5747{
5748 unsigned int u;
5749 LY_ERR ret = LY_SUCCESS;
5750 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005751 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005752 struct lysc_module *mainmod = ctx->mod->compiled;
5753
Radek Krejci0af46292019-01-11 16:02:31 +01005754 if (!mainmod->mod->off_features) {
5755 /* features are compiled directly into the compiled module structure,
5756 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5757 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci693262f2019-04-29 15:23:20 +02005758 ret = lys_feature_precompile(ctx->ctx, ctx->mod, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01005759 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5760 LY_CHECK_GOTO(ret, error);
5761 }
5762
Radek Krejcid05cbd92018-12-05 14:26:40 +01005763 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
5764
Radek Krejci8cce8532019-03-05 11:27:45 +01005765 /* TODO data nodes */
5766
5767 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005768 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, options, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01005769
Radek Krejcid05cbd92018-12-05 14:26:40 +01005770error:
5771 return ret;
5772}
5773
Radek Krejci19a96102018-11-15 13:38:09 +01005774LY_ERR
5775lys_compile(struct lys_module *mod, int options)
5776{
5777 struct lysc_ctx ctx = {0};
5778 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005779 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005780 struct lysp_module *sp;
5781 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005782 struct lysp_augment **augments = NULL;
5783 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005784 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005785 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005786
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005787 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005788
5789 if (!mod->implemented) {
5790 /* just imported modules are not compiled */
5791 return LY_SUCCESS;
5792 }
5793
Radek Krejci19a96102018-11-15 13:38:09 +01005794 sp = mod->parsed;
5795
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005796 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005797 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005798 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005799
5800 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005801 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5802 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005803
Radek Krejci19a96102018-11-15 13:38:09 +01005804 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005805 LY_ARRAY_FOR(sp->includes, u) {
5806 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
5807 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5808 }
Radek Krejci0af46292019-01-11 16:02:31 +01005809 if (mod->off_features) {
5810 /* there is already precompiled array of features */
5811 mod_c->features = mod->off_features;
5812 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005813 } else {
5814 /* features are compiled directly into the compiled module structure,
5815 * 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 +02005816 ret = lys_feature_precompile(ctx.ctx, ctx.mod, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005817 LY_CHECK_GOTO(ret, error);
5818 }
5819 /* finish feature compilation, not only for the main module, but also for the submodules.
5820 * Due to possible forward references, it must be done when all the features (including submodules)
5821 * are present. */
5822 LY_ARRAY_FOR(sp->features, u) {
5823 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
5824 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5825 }
5826 LY_ARRAY_FOR(sp->includes, v) {
5827 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
5828 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
5829 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5830 }
5831 }
5832
Radek Krejcid05cbd92018-12-05 14:26:40 +01005833 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005834 if (sp->identities) {
5835 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5836 }
5837
Radek Krejci95710c92019-02-11 15:49:55 +01005838 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005839 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005840 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005841 LY_CHECK_GOTO(ret, error);
5842 }
Radek Krejci95710c92019-02-11 15:49:55 +01005843
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005844 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005845 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, options, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01005846
Radek Krejci95710c92019-02-11 15:49:55 +01005847 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005848 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005849 LY_CHECK_GOTO(ret, error);
5850 LY_ARRAY_FOR(augments, u) {
5851 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
5852 LY_CHECK_GOTO(ret, error);
5853 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005854
5855 /* deviations */
5856 ret = lys_compile_deviations(&ctx, sp, options);
5857 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005858
5859 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
5860
Radek Krejcia3045382018-11-22 14:30:31 +01005861 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005862 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5863 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5864 * 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 +01005865 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005866 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005867 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5868 if (type->basetype == LY_TYPE_LEAFREF) {
5869 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005870 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 +01005871 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005872 } else if (type->basetype == LY_TYPE_UNION) {
5873 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5874 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5875 /* validate the path */
5876 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5877 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5878 LY_CHECK_GOTO(ret, error);
5879 }
5880 }
Radek Krejcia3045382018-11-22 14:30:31 +01005881 }
5882 }
5883 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005884 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005885 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005886 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5887 if (type->basetype == LY_TYPE_LEAFREF) {
5888 /* store pointer to the real type */
5889 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5890 typeiter->basetype == LY_TYPE_LEAFREF;
5891 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5892 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005893 } else if (type->basetype == LY_TYPE_UNION) {
5894 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5895 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5896 /* store pointer to the real type */
5897 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5898 typeiter->basetype == LY_TYPE_LEAFREF;
5899 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5900 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5901 }
5902 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005903 }
5904 }
5905 }
Radek Krejcia3045382018-11-22 14:30:31 +01005906 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005907 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005908 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005909
Radek Krejci19a96102018-11-15 13:38:09 +01005910 if (options & LYSC_OPT_FREE_SP) {
5911 lysp_module_free(mod->parsed);
5912 ((struct lys_module*)mod)->parsed = NULL;
5913 }
5914
Radek Krejci95710c92019-02-11 15:49:55 +01005915 if (!(options & LYSC_OPT_INTERNAL)) {
5916 /* remove flag of the modules implemented by dependency */
5917 for (u = 0; u < ctx.ctx->list.count; ++u) {
5918 m = ctx.ctx->list.objs[u];
5919 if (m->implemented == 2) {
5920 m->implemented = 1;
5921 }
5922 }
5923 }
5924
Radek Krejci19a96102018-11-15 13:38:09 +01005925 ((struct lys_module*)mod)->compiled = mod_c;
5926 return LY_SUCCESS;
5927
5928error:
Radek Krejci95710c92019-02-11 15:49:55 +01005929 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01005930 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005931 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005932 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01005933 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005934 mod->compiled = NULL;
5935
5936 /* revert compilation of modules implemented by dependency */
5937 for (u = 0; u < ctx.ctx->list.count; ++u) {
5938 m = ctx.ctx->list.objs[u];
5939 if (m->implemented == 2) {
5940 /* revert features list to the precompiled state */
5941 lys_feature_precompile_revert(&ctx, m);
5942 /* mark module as imported-only / not-implemented */
5943 m->implemented = 0;
5944 /* free the compiled version of the module */
5945 lysc_module_free(m->compiled, NULL);
5946 m->compiled = NULL;
5947 }
5948 }
5949
Radek Krejci19a96102018-11-15 13:38:09 +01005950 return ret;
5951}