blob: 40c14192e999e7c7c93e481b3c22d021e04fdfac [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 Krejci19a96102018-11-15 13:38:09 +0100643 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
644 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
645 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
646 ident->flags = ident_p->flags;
647
648done:
649 return ret;
650}
651
Radek Krejcib56c7502019-02-13 14:19:54 +0100652/**
653 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
654 *
655 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
656 *
657 * @param[in] ctx Compile context for logging.
658 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
659 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
660 * @return LY_SUCCESS if everything is ok.
661 * @return LY_EVALID if the identity is derived from itself.
662 */
Radek Krejci38222632019-02-12 16:55:05 +0100663static LY_ERR
664lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
665{
666 LY_ERR ret = LY_EVALID;
667 unsigned int u, v;
668 struct ly_set recursion = {0};
669 struct lysc_ident *drv;
670
671 if (!derived) {
672 return LY_SUCCESS;
673 }
674
675 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
676 if (ident == derived[u]) {
677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
678 "Identity \"%s\" is indirectly derived from itself.", ident->name);
679 goto cleanup;
680 }
681 ly_set_add(&recursion, derived[u], 0);
682 }
683
684 for (v = 0; v < recursion.count; ++v) {
685 drv = recursion.objs[v];
686 if (!drv->derived) {
687 continue;
688 }
689 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
690 if (ident == drv->derived[u]) {
691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
692 "Identity \"%s\" is indirectly derived from itself.", ident->name);
693 goto cleanup;
694 }
695 ly_set_add(&recursion, drv->derived[u], 0);
696 }
697 }
698 ret = LY_SUCCESS;
699
700cleanup:
701 ly_set_erase(&recursion, NULL);
702 return ret;
703}
704
Radek Krejcia3045382018-11-22 14:30:31 +0100705/**
706 * @brief Find and process the referenced base identities from another identity or identityref
707 *
708 * For bases in identity se backlinks to them from the base identities. For identityref, store
709 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
710 * to distinguish these two use cases.
711 *
712 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
713 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
714 * @param[in] ident Referencing identity to work with.
715 * @param[in] bases Array of bases of identityref to fill in.
716 * @return LY_ERR value.
717 */
Radek Krejci19a96102018-11-15 13:38:09 +0100718static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100719lys_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 +0100720{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100721 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100722 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100723 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100724 struct lysc_ident **idref;
725
726 assert(ident || bases);
727
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100728 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
730 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
731 return LY_EVALID;
732 }
733
734 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
735 s = strchr(bases_p[u], ':');
736 if (s) {
737 /* prefixed identity */
738 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100739 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100740 } else {
741 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100742 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100743 }
744 if (!mod) {
745 if (ident) {
746 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
747 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
748 } else {
749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
750 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
751 }
752 return LY_EVALID;
753 }
754 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100755 if (mod->compiled && mod->compiled->identities) {
756 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
757 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100758 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100759 if (ident == &mod->compiled->identities[v]) {
760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
761 "Identity \"%s\" is derived from itself.", ident->name);
762 return LY_EVALID;
763 }
764 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100765 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100766 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100767 *idref = ident;
768 } else {
769 /* we have match! store the found identity */
770 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100771 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100772 }
773 break;
774 }
775 }
776 }
777 if (!idref || !(*idref)) {
778 if (ident) {
779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
780 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
781 } else {
782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
783 "Unable to find base (%s) of identityref.", bases_p[u]);
784 }
785 return LY_EVALID;
786 }
787 }
788 return LY_SUCCESS;
789}
790
Radek Krejcia3045382018-11-22 14:30:31 +0100791/**
792 * @brief For the given array of identities, set the backlinks from all their base identities.
793 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
794 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
795 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
796 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
797 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100798static LY_ERR
799lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
800{
801 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100802
803 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
804 if (!idents_p[i].bases) {
805 continue;
806 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100807 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100808 }
809 return LY_SUCCESS;
810}
811
Radek Krejci0af46292019-01-11 16:02:31 +0100812LY_ERR
813lys_feature_precompile(struct ly_ctx *ctx, struct lysp_feature *features_p, struct lysc_feature **features)
814{
815 unsigned int offset = 0, u;
816 struct lysc_ctx context = {0};
817
818 assert(ctx);
819 context.ctx = ctx;
820
821 if (!features_p) {
822 return LY_SUCCESS;
823 }
824 if (*features) {
825 offset = LY_ARRAY_SIZE(*features);
826 }
827
828 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
829 LY_ARRAY_FOR(features_p, u) {
830 LY_ARRAY_INCREMENT(*features);
831 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
832 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
833 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
834 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
835 (*features)[offset + u].flags = features_p[u].flags;
836 }
837
838 return LY_SUCCESS;
839}
840
Radek Krejcia3045382018-11-22 14:30:31 +0100841/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100842 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100843 *
844 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
845 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100846 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100847 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
848 * being currently processed).
849 * @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 +0100850 * @return LY_SUCCESS if everything is ok.
851 * @return LY_EVALID if the feature references indirectly itself.
852 */
853static LY_ERR
854lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
855{
856 LY_ERR ret = LY_EVALID;
857 unsigned int u, v;
858 struct ly_set recursion = {0};
859 struct lysc_feature *drv;
860
861 if (!depfeatures) {
862 return LY_SUCCESS;
863 }
864
865 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
866 if (feature == depfeatures[u]) {
867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
868 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
869 goto cleanup;
870 }
871 ly_set_add(&recursion, depfeatures[u], 0);
872 }
873
874 for (v = 0; v < recursion.count; ++v) {
875 drv = recursion.objs[v];
876 if (!drv->depfeatures) {
877 continue;
878 }
879 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
880 if (feature == drv->depfeatures[u]) {
881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
882 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
883 goto cleanup;
884 }
885 ly_set_add(&recursion, drv->depfeatures[u], 0);
886 }
887 }
888 ret = LY_SUCCESS;
889
890cleanup:
891 ly_set_erase(&recursion, NULL);
892 return ret;
893}
894
895/**
Radek Krejci0af46292019-01-11 16:02:31 +0100896 * @brief Create pre-compiled features array.
897 *
898 * See lys_feature_precompile() for more details.
899 *
Radek Krejcia3045382018-11-22 14:30:31 +0100900 * @param[in] ctx Compile context.
901 * @param[in] feature_p Parsed feature definition to compile.
902 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci0af46292019-01-11 16:02:31 +0100903 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100904 * @return LY_ERR value.
905 */
Radek Krejci19a96102018-11-15 13:38:09 +0100906static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100907lys_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 +0100908{
Radek Krejci0af46292019-01-11 16:02:31 +0100909 unsigned int u, v, x;
910 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100911 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100912
Radek Krejci0af46292019-01-11 16:02:31 +0100913 /* find the preprecompiled feature */
914 LY_ARRAY_FOR(features, x) {
915 if (strcmp(features[x].name, feature_p->name)) {
916 continue;
917 }
918 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100919
Radek Krejci0af46292019-01-11 16:02:31 +0100920 /* finish compilation started in lys_feature_precompile() */
921 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
922 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
923 if (feature->iffeatures) {
924 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
925 if (feature->iffeatures[u].features) {
926 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100927 /* check for circular dependency - direct reference first,... */
928 if (feature == feature->iffeatures[u].features[v]) {
929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
930 "Feature \"%s\" is referenced from itself.", feature->name);
931 return LY_EVALID;
932 }
933 /* ... and indirect circular reference */
934 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
935
Radek Krejci0af46292019-01-11 16:02:31 +0100936 /* add itself into the dependants list */
937 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
938 *df = feature;
939 }
Radek Krejci19a96102018-11-15 13:38:09 +0100940 }
Radek Krejci19a96102018-11-15 13:38:09 +0100941 }
942 }
Radek Krejci0af46292019-01-11 16:02:31 +0100943 done:
944 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100945 }
Radek Krejci0af46292019-01-11 16:02:31 +0100946
947 LOGINT(ctx->ctx);
948 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100949}
950
Radek Krejcib56c7502019-02-13 14:19:54 +0100951/**
952 * @brief Revert compiled list of features back to the precompiled state.
953 *
954 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
955 * The features are supposed to be stored again as off_features in ::lys_module structure.
956 *
957 * @param[in] ctx Compilation context.
958 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
959 * and supposed to hold the off_features list.
960 */
Radek Krejci95710c92019-02-11 15:49:55 +0100961static void
962lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
963{
964 unsigned int u, v;
965
966 /* keep the off_features list until the complete lys_module is freed */
967 mod->off_features = mod->compiled->features;
968 mod->compiled->features = NULL;
969
970 /* in the off_features list, remove all the parts (from finished compiling process)
971 * which may points into the data being freed here */
972 LY_ARRAY_FOR(mod->off_features, u) {
973 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
974 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
975 }
976 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
977 mod->off_features[u].iffeatures = NULL;
978
979 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
980 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
981 }
982 LY_ARRAY_FREE(mod->off_features[u].exts);
983 mod->off_features[u].exts = NULL;
984 }
985}
986
Radek Krejcia3045382018-11-22 14:30:31 +0100987/**
988 * @brief Validate and normalize numeric value from a range definition.
989 * @param[in] ctx Compile context.
990 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
991 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
992 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
993 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
994 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
995 * @param[in] value String value of the range boundary.
996 * @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.
997 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
998 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
999 */
Radek Krejci19a96102018-11-15 13:38:09 +01001000static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001001range_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 +01001002{
Radek Krejci6cba4292018-11-15 17:33:29 +01001003 size_t fraction = 0, size;
1004
Radek Krejci19a96102018-11-15 13:38:09 +01001005 *len = 0;
1006
1007 assert(value);
1008 /* parse value */
1009 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1010 return LY_EVALID;
1011 }
1012
1013 if ((value[*len] == '-') || (value[*len] == '+')) {
1014 ++(*len);
1015 }
1016
1017 while (isdigit(value[*len])) {
1018 ++(*len);
1019 }
1020
1021 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001022 if (basetype == LY_TYPE_DEC64) {
1023 goto decimal;
1024 } else {
1025 *valcopy = strndup(value, *len);
1026 return LY_SUCCESS;
1027 }
Radek Krejci19a96102018-11-15 13:38:09 +01001028 }
1029 fraction = *len;
1030
1031 ++(*len);
1032 while (isdigit(value[*len])) {
1033 ++(*len);
1034 }
1035
Radek Krejci6cba4292018-11-15 17:33:29 +01001036 if (basetype == LY_TYPE_DEC64) {
1037decimal:
1038 assert(frdigits);
1039 if (*len - 1 - fraction > frdigits) {
1040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1041 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1042 *len, value, frdigits);
1043 return LY_EINVAL;
1044 }
1045 if (fraction) {
1046 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1047 } else {
1048 size = (*len) + frdigits + 1;
1049 }
1050 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001051 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1052
Radek Krejci6cba4292018-11-15 17:33:29 +01001053 (*valcopy)[size - 1] = '\0';
1054 if (fraction) {
1055 memcpy(&(*valcopy)[0], &value[0], fraction);
1056 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1057 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1058 } else {
1059 memcpy(&(*valcopy)[0], &value[0], *len);
1060 memset(&(*valcopy)[*len], '0', frdigits);
1061 }
Radek Krejci19a96102018-11-15 13:38:09 +01001062 }
1063 return LY_SUCCESS;
1064}
1065
Radek Krejcia3045382018-11-22 14:30:31 +01001066/**
1067 * @brief Check that values in range are in ascendant order.
1068 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001069 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1070 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001071 * @param[in] value Current value to check.
1072 * @param[in] prev_value The last seen value.
1073 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1074 */
Radek Krejci19a96102018-11-15 13:38:09 +01001075static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001076range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001077{
1078 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001079 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001080 return LY_EEXIST;
1081 }
1082 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001083 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001084 return LY_EEXIST;
1085 }
1086 }
1087 return LY_SUCCESS;
1088}
1089
Radek Krejcia3045382018-11-22 14:30:31 +01001090/**
1091 * @brief Set min/max value of the range part.
1092 * @param[in] ctx Compile context.
1093 * @param[in] part Range part structure to fill.
1094 * @param[in] max Flag to distinguish if storing min or max value.
1095 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1096 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1097 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1098 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1099 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001100 * @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 +01001101 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1102 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1103 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1104 * frdigits value), LY_EMEM.
1105 */
Radek Krejci19a96102018-11-15 13:38:09 +01001106static LY_ERR
1107range_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 +01001108 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001109{
1110 LY_ERR ret = LY_SUCCESS;
1111 char *valcopy = NULL;
1112 size_t len;
1113
1114 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001115 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001116 LY_CHECK_GOTO(ret, finalize);
1117 }
1118 if (!valcopy && base_range) {
1119 if (max) {
1120 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1121 } else {
1122 part->min_64 = base_range->parts[0].min_64;
1123 }
1124 if (!first) {
1125 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1126 }
1127 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001128 }
1129
1130 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001131 case LY_TYPE_INT8: /* range */
1132 if (valcopy) {
1133 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1134 } else if (max) {
1135 part->max_64 = INT64_C(127);
1136 } else {
1137 part->min_64 = INT64_C(-128);
1138 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001139 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001140 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001141 }
1142 break;
1143 case LY_TYPE_INT16: /* range */
1144 if (valcopy) {
1145 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1146 } else if (max) {
1147 part->max_64 = INT64_C(32767);
1148 } else {
1149 part->min_64 = INT64_C(-32768);
1150 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001151 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001152 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001153 }
1154 break;
1155 case LY_TYPE_INT32: /* range */
1156 if (valcopy) {
1157 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1158 } else if (max) {
1159 part->max_64 = INT64_C(2147483647);
1160 } else {
1161 part->min_64 = INT64_C(-2147483648);
1162 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001163 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001164 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001165 }
1166 break;
1167 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001168 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001169 if (valcopy) {
1170 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1171 max ? &part->max_64 : &part->min_64);
1172 } else if (max) {
1173 part->max_64 = INT64_C(9223372036854775807);
1174 } else {
1175 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1176 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001177 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001178 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001179 }
1180 break;
1181 case LY_TYPE_UINT8: /* range */
1182 if (valcopy) {
1183 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1184 } else if (max) {
1185 part->max_u64 = UINT64_C(255);
1186 } else {
1187 part->min_u64 = UINT64_C(0);
1188 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001189 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001190 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001191 }
1192 break;
1193 case LY_TYPE_UINT16: /* range */
1194 if (valcopy) {
1195 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1196 } else if (max) {
1197 part->max_u64 = UINT64_C(65535);
1198 } else {
1199 part->min_u64 = UINT64_C(0);
1200 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001201 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001202 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001203 }
1204 break;
1205 case LY_TYPE_UINT32: /* range */
1206 if (valcopy) {
1207 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1208 } else if (max) {
1209 part->max_u64 = UINT64_C(4294967295);
1210 } else {
1211 part->min_u64 = UINT64_C(0);
1212 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001213 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001214 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001215 }
1216 break;
1217 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001218 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001219 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001220 if (valcopy) {
1221 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1222 } else if (max) {
1223 part->max_u64 = UINT64_C(18446744073709551615);
1224 } else {
1225 part->min_u64 = UINT64_C(0);
1226 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001227 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001228 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001229 }
1230 break;
1231 default:
1232 LOGINT(ctx->ctx);
1233 ret = LY_EINT;
1234 }
1235
Radek Krejci5969f272018-11-23 10:03:58 +01001236finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001237 if (ret == LY_EDENIED) {
1238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1239 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1240 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1241 } else if (ret == LY_EVALID) {
1242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1243 "Invalid %s restriction - invalid value \"%s\".",
1244 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1245 } else if (ret == LY_EEXIST) {
1246 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1247 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001248 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001249 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001250 } else if (!ret && value) {
1251 *value = *value + len;
1252 }
1253 free(valcopy);
1254 return ret;
1255}
1256
Radek Krejcia3045382018-11-22 14:30:31 +01001257/**
1258 * @brief Compile the parsed range restriction.
1259 * @param[in] ctx Compile context.
1260 * @param[in] range_p Parsed range structure to compile.
1261 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1262 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1263 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1264 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1265 * range restriction must be more restrictive than the base_range.
1266 * @param[in,out] range Pointer to the created current range structure.
1267 * @return LY_ERR value.
1268 */
Radek Krejci19a96102018-11-15 13:38:09 +01001269static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001270lys_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 +01001271 struct lysc_range *base_range, struct lysc_range **range)
1272{
1273 LY_ERR ret = LY_EVALID;
1274 const char *expr;
1275 struct lysc_range_part *parts = NULL, *part;
1276 int range_expected = 0, uns;
1277 unsigned int parts_done = 0, u, v;
1278
1279 assert(range);
1280 assert(range_p);
1281
1282 expr = range_p->arg;
1283 while(1) {
1284 if (isspace(*expr)) {
1285 ++expr;
1286 } else if (*expr == '\0') {
1287 if (range_expected) {
1288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1289 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1290 length_restr ? "length" : "range", range_p->arg);
1291 goto cleanup;
1292 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1294 "Invalid %s restriction - unexpected end of the expression (%s).",
1295 length_restr ? "length" : "range", range_p->arg);
1296 goto cleanup;
1297 }
1298 parts_done++;
1299 break;
1300 } else if (!strncmp(expr, "min", 3)) {
1301 if (parts) {
1302 /* min cannot be used elsewhere than in the first part */
1303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1304 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1305 expr - range_p->arg, range_p->arg);
1306 goto cleanup;
1307 }
1308 expr += 3;
1309
1310 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001311 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 +01001312 part->max_64 = part->min_64;
1313 } else if (*expr == '|') {
1314 if (!parts || range_expected) {
1315 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1316 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1317 goto cleanup;
1318 }
1319 expr++;
1320 parts_done++;
1321 /* process next part of the expression */
1322 } else if (!strncmp(expr, "..", 2)) {
1323 expr += 2;
1324 while (isspace(*expr)) {
1325 expr++;
1326 }
1327 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1328 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1329 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1330 goto cleanup;
1331 }
1332 /* continue expecting the upper boundary */
1333 range_expected = 1;
1334 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1335 /* number */
1336 if (range_expected) {
1337 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001338 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 +01001339 range_expected = 0;
1340 } else {
1341 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1342 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 +01001343 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001344 part->max_64 = part->min_64;
1345 }
1346
1347 /* continue with possible another expression part */
1348 } else if (!strncmp(expr, "max", 3)) {
1349 expr += 3;
1350 while (isspace(*expr)) {
1351 expr++;
1352 }
1353 if (*expr != '\0') {
1354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1355 length_restr ? "length" : "range", expr);
1356 goto cleanup;
1357 }
1358 if (range_expected) {
1359 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001360 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 +01001361 range_expected = 0;
1362 } else {
1363 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1364 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 +01001365 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001366 part->min_64 = part->max_64;
1367 }
1368 } else {
1369 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1370 length_restr ? "length" : "range", expr);
1371 goto cleanup;
1372 }
1373 }
1374
1375 /* check with the previous range/length restriction */
1376 if (base_range) {
1377 switch (basetype) {
1378 case LY_TYPE_BINARY:
1379 case LY_TYPE_UINT8:
1380 case LY_TYPE_UINT16:
1381 case LY_TYPE_UINT32:
1382 case LY_TYPE_UINT64:
1383 case LY_TYPE_STRING:
1384 uns = 1;
1385 break;
1386 case LY_TYPE_DEC64:
1387 case LY_TYPE_INT8:
1388 case LY_TYPE_INT16:
1389 case LY_TYPE_INT32:
1390 case LY_TYPE_INT64:
1391 uns = 0;
1392 break;
1393 default:
1394 LOGINT(ctx->ctx);
1395 ret = LY_EINT;
1396 goto cleanup;
1397 }
1398 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1399 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1400 goto baseerror;
1401 }
1402 /* current lower bound is not lower than the base */
1403 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1404 /* base has single value */
1405 if (base_range->parts[v].min_64 == parts[u].min_64) {
1406 /* both lower bounds are the same */
1407 if (parts[u].min_64 != parts[u].max_64) {
1408 /* current continues with a range */
1409 goto baseerror;
1410 } else {
1411 /* equal single values, move both forward */
1412 ++v;
1413 continue;
1414 }
1415 } else {
1416 /* base is single value lower than current range, so the
1417 * value from base range is removed in the current,
1418 * move only base and repeat checking */
1419 ++v;
1420 --u;
1421 continue;
1422 }
1423 } else {
1424 /* base is the range */
1425 if (parts[u].min_64 == parts[u].max_64) {
1426 /* current is a single value */
1427 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1428 /* current is behind the base range, so base range is omitted,
1429 * move the base and keep the current for further check */
1430 ++v;
1431 --u;
1432 } /* else it is within the base range, so move the current, but keep the base */
1433 continue;
1434 } else {
1435 /* both are ranges - check the higher bound, the lower was already checked */
1436 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1437 /* higher bound is higher than the current higher bound */
1438 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1439 /* but the current lower bound is also higher, so the base range is omitted,
1440 * continue with the same current, but move the base */
1441 --u;
1442 ++v;
1443 continue;
1444 }
1445 /* current range starts within the base range but end behind it */
1446 goto baseerror;
1447 } else {
1448 /* current range is smaller than the base,
1449 * move current, but stay with the base */
1450 continue;
1451 }
1452 }
1453 }
1454 }
1455 if (u != parts_done) {
1456baseerror:
1457 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1458 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1459 length_restr ? "length" : "range", range_p->arg);
1460 goto cleanup;
1461 }
1462 }
1463
1464 if (!(*range)) {
1465 *range = calloc(1, sizeof **range);
1466 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1467 }
1468
Radek Krejcic8b31002019-01-08 10:24:45 +01001469 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001470 if (range_p->eapptag) {
1471 lydict_remove(ctx->ctx, (*range)->eapptag);
1472 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1473 }
1474 if (range_p->emsg) {
1475 lydict_remove(ctx->ctx, (*range)->emsg);
1476 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1477 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001478 if (range_p->dsc) {
1479 lydict_remove(ctx->ctx, (*range)->dsc);
1480 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1481 }
1482 if (range_p->ref) {
1483 lydict_remove(ctx->ctx, (*range)->ref);
1484 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1485 }
Radek Krejci19a96102018-11-15 13:38:09 +01001486 /* extensions are taken only from the last range by the caller */
1487
1488 (*range)->parts = parts;
1489 parts = NULL;
1490 ret = LY_SUCCESS;
1491cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001492 LY_ARRAY_FREE(parts);
1493
1494 return ret;
1495}
1496
1497/**
1498 * @brief Checks pattern syntax.
1499 *
Radek Krejcia3045382018-11-22 14:30:31 +01001500 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001501 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001502 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1503 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001504 */
1505static LY_ERR
1506lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1507{
1508 int idx, idx2, start, end, err_offset, count;
1509 char *perl_regex, *ptr;
1510 const char *err_msg, *orig_ptr;
1511 pcre *precomp;
1512#define URANGE_LEN 19
1513 char *ublock2urange[][2] = {
1514 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1515 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1516 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1517 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1518 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1519 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1520 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1521 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1522 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1523 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1524 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1525 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1526 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1527 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1528 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1529 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1530 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1531 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1532 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1533 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1534 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1535 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1536 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1537 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1538 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1539 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1540 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1541 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1542 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1543 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1544 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1545 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1546 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1547 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1548 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1549 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1550 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1551 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1552 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1553 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1554 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1555 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1556 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1557 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1558 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1559 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1560 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1561 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1562 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1563 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1564 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1565 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1566 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1567 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1568 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1569 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1570 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1571 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1572 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1573 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1574 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1575 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1576 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1577 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1578 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1579 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1580 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1581 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1582 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1583 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1584 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1585 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1586 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1587 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1588 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1589 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1590 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1591 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1592 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1593 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1594 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1595 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1596 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1597 {NULL, NULL}
1598 };
1599
1600 /* adjust the expression to a Perl equivalent
1601 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1602
1603 /* we need to replace all "$" with "\$", count them now */
1604 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1605
1606 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1607 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1608 perl_regex[0] = '\0';
1609
1610 ptr = perl_regex;
1611
1612 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1613 /* we will add line-end anchoring */
1614 ptr[0] = '(';
1615 ++ptr;
1616 }
1617
1618 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1619 if (orig_ptr[0] == '$') {
1620 ptr += sprintf(ptr, "\\$");
1621 } else if (orig_ptr[0] == '^') {
1622 ptr += sprintf(ptr, "\\^");
1623 } else {
1624 ptr[0] = orig_ptr[0];
1625 ++ptr;
1626 }
1627 }
1628
1629 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1630 ptr += sprintf(ptr, ")$");
1631 } else {
1632 ptr[0] = '\0';
1633 ++ptr;
1634 }
1635
1636 /* substitute Unicode Character Blocks with exact Character Ranges */
1637 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1638 start = ptr - perl_regex;
1639
1640 ptr = strchr(ptr, '}');
1641 if (!ptr) {
1642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1643 pattern, perl_regex + start + 2, "unterminated character property");
1644 free(perl_regex);
1645 return LY_EVALID;
1646 }
1647 end = (ptr - perl_regex) + 1;
1648
1649 /* need more space */
1650 if (end - start < URANGE_LEN) {
1651 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1652 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1653 }
1654
1655 /* find our range */
1656 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1657 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1658 break;
1659 }
1660 }
1661 if (!ublock2urange[idx][0]) {
1662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1663 pattern, perl_regex + start + 5, "unknown block name");
1664 free(perl_regex);
1665 return LY_EVALID;
1666 }
1667
1668 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1669 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1670 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1671 ++count;
1672 }
1673 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1674 --count;
1675 }
1676 }
1677 if (count) {
1678 /* skip brackets */
1679 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1680 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1681 } else {
1682 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1683 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1684 }
1685 }
1686
1687 /* must return 0, already checked during parsing */
1688 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1689 &err_msg, &err_offset, NULL);
1690 if (!precomp) {
1691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1692 free(perl_regex);
1693 return LY_EVALID;
1694 }
1695 free(perl_regex);
1696
1697 if (pcre_precomp) {
1698 *pcre_precomp = precomp;
1699 } else {
1700 free(precomp);
1701 }
1702
1703 return LY_SUCCESS;
1704
1705#undef URANGE_LEN
1706}
1707
Radek Krejcia3045382018-11-22 14:30:31 +01001708/**
1709 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1710 * @param[in] ctx Compile context.
1711 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1712 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1713 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1714 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1715 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1716 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1717 */
Radek Krejci19a96102018-11-15 13:38:09 +01001718static LY_ERR
1719lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1720 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1721{
1722 struct lysc_pattern **pattern;
1723 unsigned int u, v;
1724 const char *err_msg;
1725 LY_ERR ret = LY_SUCCESS;
1726
1727 /* first, copy the patterns from the base type */
1728 if (base_patterns) {
1729 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1730 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1731 }
1732
1733 LY_ARRAY_FOR(patterns_p, u) {
1734 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1735 *pattern = calloc(1, sizeof **pattern);
1736 ++(*pattern)->refcount;
1737
1738 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1739 LY_CHECK_RET(ret);
1740 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1741 if (err_msg) {
1742 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1743 }
1744
1745 if (patterns_p[u].arg[0] == 0x15) {
1746 (*pattern)->inverted = 1;
1747 }
1748 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1749 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001750 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1751 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001752 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1753 options, v, lys_compile_ext, ret, done);
1754 }
1755done:
1756 return ret;
1757}
1758
Radek Krejcia3045382018-11-22 14:30:31 +01001759/**
1760 * @brief map of the possible restrictions combination for the specific built-in type.
1761 */
Radek Krejci19a96102018-11-15 13:38:09 +01001762static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1763 0 /* LY_TYPE_UNKNOWN */,
1764 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001765 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1766 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1767 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1768 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1769 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001770 LYS_SET_BIT /* LY_TYPE_BITS */,
1771 0 /* LY_TYPE_BOOL */,
1772 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1773 0 /* LY_TYPE_EMPTY */,
1774 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1775 LYS_SET_BASE /* LY_TYPE_IDENT */,
1776 LYS_SET_REQINST /* LY_TYPE_INST */,
1777 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001778 LYS_SET_TYPE /* LY_TYPE_UNION */,
1779 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001780 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001781 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001782 LYS_SET_RANGE /* LY_TYPE_INT64 */
1783};
1784
1785/**
1786 * @brief stringification of the YANG built-in data types
1787 */
1788const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1789 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1790 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001791};
1792
Radek Krejcia3045382018-11-22 14:30:31 +01001793/**
1794 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1795 * @param[in] ctx Compile context.
1796 * @param[in] enums_p Array of the parsed enum structures to compile.
1797 * @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.
1798 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1799 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1800 * @param[out] enums Newly created array of the compiled enums information for the current type.
1801 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1802 */
Radek Krejci19a96102018-11-15 13:38:09 +01001803static LY_ERR
1804lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1805 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1806{
1807 LY_ERR ret = LY_SUCCESS;
1808 unsigned int u, v, match;
1809 int32_t value = 0;
1810 uint32_t position = 0;
1811 struct lysc_type_enum_item *e, storage;
1812
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001813 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1815 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1816 return LY_EVALID;
1817 }
1818
1819 LY_ARRAY_FOR(enums_p, u) {
1820 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1821 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001822 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1823 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001824 if (base_enums) {
1825 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1826 LY_ARRAY_FOR(base_enums, v) {
1827 if (!strcmp(e->name, base_enums[v].name)) {
1828 break;
1829 }
1830 }
1831 if (v == LY_ARRAY_SIZE(base_enums)) {
1832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1833 "Invalid %s - derived type adds new item \"%s\".",
1834 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1835 return LY_EVALID;
1836 }
1837 match = v;
1838 }
1839
1840 if (basetype == LY_TYPE_ENUM) {
1841 if (enums_p[u].flags & LYS_SET_VALUE) {
1842 e->value = (int32_t)enums_p[u].value;
1843 if (!u || e->value >= value) {
1844 value = e->value + 1;
1845 }
1846 /* check collision with other values */
1847 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1848 if (e->value == (*enums)[v].value) {
1849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1850 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1851 e->value, e->name, (*enums)[v].name);
1852 return LY_EVALID;
1853 }
1854 }
1855 } else if (base_enums) {
1856 /* inherit the assigned value */
1857 e->value = base_enums[match].value;
1858 if (!u || e->value >= value) {
1859 value = e->value + 1;
1860 }
1861 } else {
1862 /* assign value automatically */
1863 if (u && value == INT32_MIN) {
1864 /* counter overflow */
1865 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1866 "Invalid enumeration - it is not possible to auto-assign enum value for "
1867 "\"%s\" since the highest value is already 2147483647.", e->name);
1868 return LY_EVALID;
1869 }
1870 e->value = value++;
1871 }
1872 } else { /* LY_TYPE_BITS */
1873 if (enums_p[u].flags & LYS_SET_VALUE) {
1874 e->value = (int32_t)enums_p[u].value;
1875 if (!u || (uint32_t)e->value >= position) {
1876 position = (uint32_t)e->value + 1;
1877 }
1878 /* check collision with other values */
1879 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1880 if (e->value == (*enums)[v].value) {
1881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1882 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1883 (uint32_t)e->value, e->name, (*enums)[v].name);
1884 return LY_EVALID;
1885 }
1886 }
1887 } else if (base_enums) {
1888 /* inherit the assigned value */
1889 e->value = base_enums[match].value;
1890 if (!u || (uint32_t)e->value >= position) {
1891 position = (uint32_t)e->value + 1;
1892 }
1893 } else {
1894 /* assign value automatically */
1895 if (u && position == 0) {
1896 /* counter overflow */
1897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1898 "Invalid bits - it is not possible to auto-assign bit position for "
1899 "\"%s\" since the highest value is already 4294967295.", e->name);
1900 return LY_EVALID;
1901 }
1902 e->value = position++;
1903 }
1904 }
1905
1906 if (base_enums) {
1907 /* the assigned values must not change from the derived type */
1908 if (e->value != base_enums[match].value) {
1909 if (basetype == LY_TYPE_ENUM) {
1910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1911 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1912 e->name, base_enums[match].value, e->value);
1913 } else {
1914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1915 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1916 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1917 }
1918 return LY_EVALID;
1919 }
1920 }
1921
1922 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1923 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1924
1925 if (basetype == LY_TYPE_BITS) {
1926 /* keep bits ordered by position */
1927 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1928 if (v != u) {
1929 memcpy(&storage, e, sizeof *e);
1930 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1931 memcpy(&(*enums)[v], &storage, sizeof storage);
1932 }
1933 }
1934 }
1935
1936done:
1937 return ret;
1938}
1939
Radek Krejcia3045382018-11-22 14:30:31 +01001940#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1941 for ((NODE) = (NODE)->parent; \
1942 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1943 (NODE) = (NODE)->parent); \
1944 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1945 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1946 TERM; \
1947 }
1948
1949/**
1950 * @brief Validate the predicate(s) from the leafref path.
1951 * @param[in] ctx Compile context
1952 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1953 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001954 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001955 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001956 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001957 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1958 */
1959static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001960lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001961 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001962{
1963 LY_ERR ret = LY_EVALID;
1964 const struct lys_module *mod;
1965 const struct lysc_node *src_node, *dst_node;
1966 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1967 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001968 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001969 const char *start, *end, *pke_end;
1970 struct ly_set keys = {0};
1971 int i;
1972
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001973 assert(path_context);
1974
Radek Krejcia3045382018-11-22 14:30:31 +01001975 while (**predicate == '[') {
1976 start = (*predicate)++;
1977
1978 while (isspace(**predicate)) {
1979 ++(*predicate);
1980 }
1981 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1982 while (isspace(**predicate)) {
1983 ++(*predicate);
1984 }
1985 if (**predicate != '=') {
1986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001987 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1988 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001989 goto cleanup;
1990 }
1991 ++(*predicate);
1992 while (isspace(**predicate)) {
1993 ++(*predicate);
1994 }
1995
1996 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1998 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1999 goto cleanup;
2000 }
2001 --pke_end;
2002 while (isspace(*pke_end)) {
2003 --pke_end;
2004 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002005 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002006 /* localize path-key-expr */
2007 pke_start = path_key_expr = *predicate;
2008 /* move after the current predicate */
2009 *predicate = end + 1;
2010
2011 /* source (must be leaf or leaf-list) */
2012 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002013 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002014 if (!mod) {
2015 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2016 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002017 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002018 goto cleanup;
2019 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002020 if (!mod->implemented) {
2021 /* make the module implemented */
2022 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2023 }
Radek Krejcia3045382018-11-22 14:30:31 +01002024 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002025 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002026 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002027 src_node = NULL;
2028 if (context_node->keys) {
2029 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2030 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2031 src_node = (const struct lysc_node*)context_node->keys[u];
2032 break;
2033 }
2034 }
2035 }
Radek Krejcia3045382018-11-22 14:30:31 +01002036 if (!src_node) {
2037 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002038 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002039 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002040 goto cleanup;
2041 }
Radek Krejcia3045382018-11-22 14:30:31 +01002042
2043 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002044 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002045 i = ly_set_add(&keys, (void*)src_node, 0);
2046 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002047 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002049 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002050 *predicate - start, start, src_node->name);
2051 goto cleanup;
2052 }
2053
2054 /* destination */
2055 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002056 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002057
2058 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2059 if (strncmp(path_key_expr, "current()", 9)) {
2060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2061 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2062 *predicate - start, start);
2063 goto cleanup;
2064 }
2065 path_key_expr += 9;
2066 while (isspace(*path_key_expr)) {
2067 ++path_key_expr;
2068 }
2069
2070 if (*path_key_expr != '/') {
2071 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2072 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2073 *predicate - start, start);
2074 goto cleanup;
2075 }
2076 ++path_key_expr;
2077 while (isspace(*path_key_expr)) {
2078 ++path_key_expr;
2079 }
2080
2081 /* rel-path-keyexpr:
2082 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2083 while (!strncmp(path_key_expr, "..", 2)) {
2084 ++dest_parent_times;
2085 path_key_expr += 2;
2086 while (isspace(*path_key_expr)) {
2087 ++path_key_expr;
2088 }
2089 if (*path_key_expr != '/') {
2090 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2091 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2092 *predicate - start, start);
2093 goto cleanup;
2094 }
2095 ++path_key_expr;
2096 while (isspace(*path_key_expr)) {
2097 ++path_key_expr;
2098 }
2099
2100 /* path is supposed to be evaluated in data tree, so we have to skip
2101 * all schema nodes that cannot be instantiated in data tree */
2102 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2103 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2104 *predicate - start, start);
2105 }
2106 if (!dest_parent_times) {
2107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2108 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2109 *predicate - start, start);
2110 goto cleanup;
2111 }
2112 if (path_key_expr == pke_end) {
2113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2114 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2115 *predicate - start, start);
2116 goto cleanup;
2117 }
2118
2119 while(path_key_expr != pke_end) {
2120 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2121 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002122 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2123 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002124 goto cleanup;
2125 }
2126
2127 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002128 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002129 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002130 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002131 }
2132 if (!mod) {
2133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002134 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002135 *predicate - start, start, dst_len, dst);
2136 goto cleanup;
2137 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002138 if (!mod->implemented) {
2139 /* make the module implemented */
2140 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2141 }
Radek Krejcia3045382018-11-22 14:30:31 +01002142
2143 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2144 if (!dst_node) {
2145 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002146 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002147 *predicate - start, start, path_key_expr - pke_start, pke_start);
2148 goto cleanup;
2149 }
2150 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002151 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 +01002152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002153 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002154 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2155 goto cleanup;
2156 }
2157 }
2158
2159 ret = LY_SUCCESS;
2160cleanup:
2161 ly_set_erase(&keys, NULL);
2162 return ret;
2163}
2164
2165/**
2166 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2167 *
2168 * path-arg = absolute-path / relative-path
2169 * absolute-path = 1*("/" (node-identifier *path-predicate))
2170 * relative-path = 1*(".." "/") descendant-path
2171 *
2172 * @param[in,out] path Path to parse.
2173 * @param[out] prefix Prefix of the token, NULL if there is not any.
2174 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2175 * @param[out] name Name of the token.
2176 * @param[out] nam_len Length of the name.
2177 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2178 * must not be changed between consecutive calls. -1 if the
2179 * path is absolute.
2180 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2181 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2182 */
2183static LY_ERR
2184lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2185 int *parent_times, int *has_predicate)
2186{
2187 int par_times = 0;
2188
2189 assert(path && *path);
2190 assert(parent_times);
2191 assert(prefix);
2192 assert(prefix_len);
2193 assert(name);
2194 assert(name_len);
2195 assert(has_predicate);
2196
2197 *prefix = NULL;
2198 *prefix_len = 0;
2199 *name = NULL;
2200 *name_len = 0;
2201 *has_predicate = 0;
2202
2203 if (!*parent_times) {
2204 if (!strncmp(*path, "..", 2)) {
2205 *path += 2;
2206 ++par_times;
2207 while (!strncmp(*path, "/..", 3)) {
2208 *path += 3;
2209 ++par_times;
2210 }
2211 }
2212 if (par_times) {
2213 *parent_times = par_times;
2214 } else {
2215 *parent_times = -1;
2216 }
2217 }
2218
2219 if (**path != '/') {
2220 return LY_EINVAL;
2221 }
2222 /* skip '/' */
2223 ++(*path);
2224
2225 /* node-identifier ([prefix:]name) */
2226 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2227
2228 if ((**path == '/' && (*path)[1]) || !**path) {
2229 /* path continues by another token or this is the last token */
2230 return LY_SUCCESS;
2231 } else if ((*path)[0] != '[') {
2232 /* unexpected character */
2233 return LY_EINVAL;
2234 } else {
2235 /* predicate starting with [ */
2236 *has_predicate = 1;
2237 return LY_SUCCESS;
2238 }
2239}
2240
2241/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002242 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2243 *
2244 * The set of features used for target must be a subset of features used for the leafref.
2245 * This is not a perfect, we should compare the truth tables but it could require too much resources
2246 * and RFC 7950 does not require it explicitely, so we simplify that.
2247 *
2248 * @param[in] refnode The leafref node.
2249 * @param[in] target Tha target node of the leafref.
2250 * @return LY_SUCCESS or LY_EVALID;
2251 */
2252static LY_ERR
2253lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2254{
2255 LY_ERR ret = LY_EVALID;
2256 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002257 unsigned int u, v, count;
2258 struct ly_set features = {0};
2259
2260 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002261 if (iter->iffeatures) {
2262 LY_ARRAY_FOR(iter->iffeatures, u) {
2263 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2264 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002265 }
2266 }
2267 }
2268 }
2269
2270 /* we should have, in features set, a superset of features applicable to the target node.
2271 * So when adding features applicable to the target into the features set, we should not be
2272 * able to actually add any new feature, otherwise it is not a subset of features applicable
2273 * to the leafref itself. */
2274 count = features.count;
2275 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002276 if (iter->iffeatures) {
2277 LY_ARRAY_FOR(iter->iffeatures, u) {
2278 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2279 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002280 /* new feature was added (or LY_EMEM) */
2281 goto cleanup;
2282 }
2283 }
2284 }
2285 }
2286 }
2287 ret = LY_SUCCESS;
2288
2289cleanup:
2290 ly_set_erase(&features, NULL);
2291 return ret;
2292}
2293
2294/**
Radek Krejcia3045382018-11-22 14:30:31 +01002295 * @brief Validate the leafref path.
2296 * @param[in] ctx Compile context
2297 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002298 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002299 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2300 */
2301static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002302lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002303{
2304 const struct lysc_node *node = NULL, *parent = NULL;
2305 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002306 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002307 const char *id, *prefix, *name;
2308 size_t prefix_len, name_len;
2309 int parent_times = 0, has_predicate;
2310 unsigned int iter, u;
2311 LY_ERR ret = LY_SUCCESS;
2312
2313 assert(ctx);
2314 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002315 assert(leafref);
2316
Radek Krejcia3045382018-11-22 14:30:31 +01002317 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002318 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002319 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2320 if (!iter) { /* first iteration */
2321 /* precess ".." in relative paths */
2322 if (parent_times > 0) {
2323 /* move from the context node */
2324 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2325 /* path is supposed to be evaluated in data tree, so we have to skip
2326 * all schema nodes that cannot be instantiated in data tree */
2327 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002328 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002329 }
2330 }
2331 }
2332
2333 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002334 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002335 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002336 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002337 }
2338 if (!mod) {
2339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002340 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2341 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002342 return LY_EVALID;
2343 }
Radek Krejci3d929562019-02-21 11:25:55 +01002344 if (!mod->implemented) {
2345 /* make the module implemented */
2346 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2347 }
Radek Krejcia3045382018-11-22 14:30:31 +01002348
2349 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2350 if (!node) {
2351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002352 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002353 return LY_EVALID;
2354 }
2355 parent = node;
2356
2357 if (has_predicate) {
2358 /* we have predicate, so the current result must be list */
2359 if (node->nodetype != LYS_LIST) {
2360 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2361 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002362 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002363 return LY_EVALID;
2364 }
2365
Radek Krejcibade82a2018-12-05 10:13:48 +01002366 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2367 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002368 }
2369
2370 ++iter;
2371 }
2372 if (ret) {
2373 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002374 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002375 return LY_EVALID;
2376 }
2377
2378 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2380 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002381 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002382 return LY_EVALID;
2383 }
2384
2385 /* check status */
2386 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2387 return LY_EVALID;
2388 }
2389
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002390 /* check config */
2391 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2392 if (node->flags & LYS_CONFIG_R) {
2393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2394 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2395 leafref->path);
2396 return LY_EVALID;
2397 }
2398 }
2399
Radek Krejci412ddfa2018-11-23 11:44:11 +01002400 /* store the target's type and check for circular chain of leafrefs */
2401 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2402 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2403 if (type == (struct lysc_type*)leafref) {
2404 /* circular chain detected */
2405 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2406 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2407 return LY_EVALID;
2408 }
2409 }
2410
Radek Krejci58d171e2018-11-23 13:50:55 +01002411 /* check if leafref and its target are under common if-features */
2412 if (lys_compile_leafref_features_validate(startnode, node)) {
2413 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2414 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2415 leafref->path);
2416 return LY_EVALID;
2417 }
2418
Radek Krejcia3045382018-11-22 14:30:31 +01002419 return LY_SUCCESS;
2420}
2421
Radek Krejcicdfecd92018-11-26 11:27:32 +01002422static 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 +01002423 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002424/**
2425 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2426 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002427 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2428 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2429 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2430 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002431 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002432 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002433 * @param[in] basetype Base YANG built-in type of the type to compile.
2434 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2435 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2436 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2437 * @param[out] type Newly created type structure with the filled information about the type.
2438 * @return LY_ERR value.
2439 */
Radek Krejci19a96102018-11-15 13:38:09 +01002440static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002441lys_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,
2442 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2443 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002444{
2445 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002446 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002447 struct lysc_type_bin *bin;
2448 struct lysc_type_num *num;
2449 struct lysc_type_str *str;
2450 struct lysc_type_bits *bits;
2451 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002452 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002453 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002454 struct lysc_type_union *un, *un_aux;
2455 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002456
2457 switch (basetype) {
2458 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002459 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002460
2461 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002462 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002463 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002464 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2465 LY_CHECK_RET(ret);
2466 if (!tpdfname) {
2467 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2468 options, u, lys_compile_ext, ret, done);
2469 }
2470 }
2471
2472 if (tpdfname) {
2473 type_p->compiled = *type;
2474 *type = calloc(1, sizeof(struct lysc_type_bin));
2475 }
2476 break;
2477 case LY_TYPE_BITS:
2478 /* RFC 7950 9.7 - bits */
2479 bits = (struct lysc_type_bits*)(*type);
2480 if (type_p->bits) {
2481 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2482 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2483 (struct lysc_type_enum_item**)&bits->bits);
2484 LY_CHECK_RET(ret);
2485 }
2486
Radek Krejci555cb5b2018-11-16 14:54:33 +01002487 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002488 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002489 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002490 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002491 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002492 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002493 free(*type);
2494 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002495 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002496 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002497 }
2498
2499 if (tpdfname) {
2500 type_p->compiled = *type;
2501 *type = calloc(1, sizeof(struct lysc_type_bits));
2502 }
2503 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002504 case LY_TYPE_DEC64:
2505 dec = (struct lysc_type_dec*)(*type);
2506
2507 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002508 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002509 if (!type_p->fraction_digits) {
2510 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002511 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002512 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002514 free(*type);
2515 *type = NULL;
2516 }
2517 return LY_EVALID;
2518 }
2519 } else if (type_p->fraction_digits) {
2520 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002521 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2523 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002524 tpdfname);
2525 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2527 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002528 free(*type);
2529 *type = NULL;
2530 }
2531 return LY_EVALID;
2532 }
2533 dec->fraction_digits = type_p->fraction_digits;
2534
2535 /* RFC 7950 9.2.4 - range */
2536 if (type_p->range) {
2537 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2538 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2539 LY_CHECK_RET(ret);
2540 if (!tpdfname) {
2541 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2542 options, u, lys_compile_ext, ret, done);
2543 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002544 }
2545
2546 if (tpdfname) {
2547 type_p->compiled = *type;
2548 *type = calloc(1, sizeof(struct lysc_type_dec));
2549 }
2550 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002551 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002552 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002553
2554 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002555 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002556 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002557 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2558 LY_CHECK_RET(ret);
2559 if (!tpdfname) {
2560 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2561 options, u, lys_compile_ext, ret, done);
2562 }
2563 } else if (base && ((struct lysc_type_str*)base)->length) {
2564 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2565 }
2566
2567 /* RFC 7950 9.4.5 - pattern */
2568 if (type_p->patterns) {
2569 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2570 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2571 LY_CHECK_RET(ret);
2572 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2573 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2574 }
2575
2576 if (tpdfname) {
2577 type_p->compiled = *type;
2578 *type = calloc(1, sizeof(struct lysc_type_str));
2579 }
2580 break;
2581 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002582 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002583
2584 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002585 if (type_p->enums) {
2586 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2587 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2588 LY_CHECK_RET(ret);
2589 }
2590
Radek Krejci555cb5b2018-11-16 14:54:33 +01002591 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002592 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002593 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002595 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002597 free(*type);
2598 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002599 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002600 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002601 }
2602
2603 if (tpdfname) {
2604 type_p->compiled = *type;
2605 *type = calloc(1, sizeof(struct lysc_type_enum));
2606 }
2607 break;
2608 case LY_TYPE_INT8:
2609 case LY_TYPE_UINT8:
2610 case LY_TYPE_INT16:
2611 case LY_TYPE_UINT16:
2612 case LY_TYPE_INT32:
2613 case LY_TYPE_UINT32:
2614 case LY_TYPE_INT64:
2615 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002616 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002617
2618 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002619 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002620 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002621 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2622 LY_CHECK_RET(ret);
2623 if (!tpdfname) {
2624 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2625 options, u, lys_compile_ext, ret, done);
2626 }
2627 }
2628
2629 if (tpdfname) {
2630 type_p->compiled = *type;
2631 *type = calloc(1, sizeof(struct lysc_type_num));
2632 }
2633 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002634 case LY_TYPE_IDENT:
2635 idref = (struct lysc_type_identityref*)(*type);
2636
2637 /* RFC 7950 9.10.2 - base */
2638 if (type_p->bases) {
2639 if (base) {
2640 /* only the directly derived identityrefs can contain base specification */
2641 if (tpdfname) {
2642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002643 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002644 tpdfname);
2645 } else {
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 not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002648 free(*type);
2649 *type = NULL;
2650 }
2651 return LY_EVALID;
2652 }
2653 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2654 LY_CHECK_RET(ret);
2655 }
2656
2657 if (!base && !type_p->flags) {
2658 /* type derived from identityref built-in type must contain at least one base */
2659 if (tpdfname) {
2660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2661 } else {
2662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2663 free(*type);
2664 *type = NULL;
2665 }
2666 return LY_EVALID;
2667 }
2668
2669 if (tpdfname) {
2670 type_p->compiled = *type;
2671 *type = calloc(1, sizeof(struct lysc_type_identityref));
2672 }
2673 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002674 case LY_TYPE_LEAFREF:
2675 /* RFC 7950 9.9.3 - require-instance */
2676 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002677 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002678 if (tpdfname) {
2679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2680 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2681 } else {
2682 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2683 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2684 free(*type);
2685 *type = NULL;
2686 }
2687 return LY_EVALID;
2688 }
Radek Krejcia3045382018-11-22 14:30:31 +01002689 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002690 } else if (base) {
2691 /* inherit */
2692 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002693 } else {
2694 /* default is true */
2695 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2696 }
2697 if (type_p->path) {
2698 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002699 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002700 } else if (base) {
2701 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002702 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002703 } else if (tpdfname) {
2704 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2705 return LY_EVALID;
2706 } else {
2707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2708 free(*type);
2709 *type = NULL;
2710 return LY_EVALID;
2711 }
2712 if (tpdfname) {
2713 type_p->compiled = *type;
2714 *type = calloc(1, sizeof(struct lysc_type_leafref));
2715 }
2716 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002717 case LY_TYPE_INST:
2718 /* RFC 7950 9.9.3 - require-instance */
2719 if (type_p->flags & LYS_SET_REQINST) {
2720 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2721 } else {
2722 /* default is true */
2723 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2724 }
2725
2726 if (tpdfname) {
2727 type_p->compiled = *type;
2728 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2729 }
2730 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002731 case LY_TYPE_UNION:
2732 un = (struct lysc_type_union*)(*type);
2733
2734 /* RFC 7950 7.4 - type */
2735 if (type_p->types) {
2736 if (base) {
2737 /* only the directly derived union can contain types specification */
2738 if (tpdfname) {
2739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2740 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2741 tpdfname);
2742 } else {
2743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2744 "Invalid type substatement for the type not directly derived from union built-in type.");
2745 free(*type);
2746 *type = NULL;
2747 }
2748 return LY_EVALID;
2749 }
2750 /* compile the type */
2751 additional = 0;
2752 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2753 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002754 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 +01002755 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2756 /* add space for additional types from the union subtype */
2757 un_aux = (struct lysc_type_union *)un->types[u + additional];
2758 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)));
2759 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2760 un->types = (void*)((uint32_t*)(p) + 1);
2761
2762 /* copy subtypes of the subtype union */
2763 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2764 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2765 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2766 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2767 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2768 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2769 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2770 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2771 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2772 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2773 /* TODO extensions */
2774
2775 } else {
2776 un->types[u + additional] = un_aux->types[v];
2777 ++un_aux->types[v]->refcount;
2778 }
2779 ++additional;
2780 LY_ARRAY_INCREMENT(un->types);
2781 }
2782 /* compensate u increment in main loop */
2783 --additional;
2784
2785 /* free the replaced union subtype */
2786 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2787 } else {
2788 LY_ARRAY_INCREMENT(un->types);
2789 }
2790 LY_CHECK_RET(ret);
2791 }
2792 }
2793
2794 if (!base && !type_p->flags) {
2795 /* type derived from union built-in type must contain at least one type */
2796 if (tpdfname) {
2797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2798 } else {
2799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2800 free(*type);
2801 *type = NULL;
2802 }
2803 return LY_EVALID;
2804 }
2805
2806 if (tpdfname) {
2807 type_p->compiled = *type;
2808 *type = calloc(1, sizeof(struct lysc_type_union));
2809 }
2810 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002811 case LY_TYPE_BOOL:
2812 case LY_TYPE_EMPTY:
2813 case LY_TYPE_UNKNOWN: /* just to complete switch */
2814 break;
2815 }
2816 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2817done:
2818 return ret;
2819}
2820
Radek Krejcia3045382018-11-22 14:30:31 +01002821/**
2822 * @brief Compile information about the leaf/leaf-list's type.
2823 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002824 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2825 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2826 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2827 * @param[in] context_name Name of the context node or referencing typedef for logging.
2828 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002829 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2830 * @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 +01002831 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002832 * @return LY_ERR value.
2833 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002834static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002835lys_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 +01002836 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002837{
2838 LY_ERR ret = LY_SUCCESS;
2839 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002840 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002841 struct type_context {
2842 const struct lysp_tpdf *tpdf;
2843 struct lysp_node *node;
2844 struct lysp_module *mod;
2845 } *tctx, *tctx_prev = NULL;
2846 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002847 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002848 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002849 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002850
2851 (*type) = NULL;
2852
2853 tctx = calloc(1, sizeof *tctx);
2854 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002855 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002856 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2857 ret == LY_SUCCESS;
2858 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2859 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2860 if (basetype) {
2861 break;
2862 }
2863
2864 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002865 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2866 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002867 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2868
Radek Krejcicdfecd92018-11-26 11:27:32 +01002869 if (units && !*units) {
2870 /* inherit units */
2871 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2872 }
Radek Krejci01342af2019-01-03 15:18:08 +01002873 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002874 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002875 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002876 }
Radek Krejci01342af2019-01-03 15:18:08 +01002877 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002878 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2879 break;
2880 }
2881
Radek Krejci19a96102018-11-15 13:38:09 +01002882 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002883 /* it is not necessary to continue, the rest of the chain was already compiled,
2884 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002885 basetype = tctx->tpdf->type.compiled->basetype;
2886 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002887 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002888 dummyloops = 1;
2889 goto preparenext;
2890 } else {
2891 tctx = NULL;
2892 break;
2893 }
Radek Krejci19a96102018-11-15 13:38:09 +01002894 }
2895
2896 /* store information for the following processing */
2897 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2898
Radek Krejcicdfecd92018-11-26 11:27:32 +01002899preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002900 /* prepare next loop */
2901 tctx_prev = tctx;
2902 tctx = calloc(1, sizeof *tctx);
2903 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2904 }
2905 free(tctx);
2906
2907 /* allocate type according to the basetype */
2908 switch (basetype) {
2909 case LY_TYPE_BINARY:
2910 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002911 break;
2912 case LY_TYPE_BITS:
2913 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002914 break;
2915 case LY_TYPE_BOOL:
2916 case LY_TYPE_EMPTY:
2917 *type = calloc(1, sizeof(struct lysc_type));
2918 break;
2919 case LY_TYPE_DEC64:
2920 *type = calloc(1, sizeof(struct lysc_type_dec));
2921 break;
2922 case LY_TYPE_ENUM:
2923 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002924 break;
2925 case LY_TYPE_IDENT:
2926 *type = calloc(1, sizeof(struct lysc_type_identityref));
2927 break;
2928 case LY_TYPE_INST:
2929 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2930 break;
2931 case LY_TYPE_LEAFREF:
2932 *type = calloc(1, sizeof(struct lysc_type_leafref));
2933 break;
2934 case LY_TYPE_STRING:
2935 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002936 break;
2937 case LY_TYPE_UNION:
2938 *type = calloc(1, sizeof(struct lysc_type_union));
2939 break;
2940 case LY_TYPE_INT8:
2941 case LY_TYPE_UINT8:
2942 case LY_TYPE_INT16:
2943 case LY_TYPE_UINT16:
2944 case LY_TYPE_INT32:
2945 case LY_TYPE_UINT32:
2946 case LY_TYPE_INT64:
2947 case LY_TYPE_UINT64:
2948 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002949 break;
2950 case LY_TYPE_UNKNOWN:
2951 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2952 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2953 ret = LY_EVALID;
2954 goto cleanup;
2955 }
2956 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002957 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002958 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2959 ly_data_type2str[basetype]);
2960 free(*type);
2961 (*type) = NULL;
2962 ret = LY_EVALID;
2963 goto cleanup;
2964 }
2965
2966 /* get restrictions from the referred typedefs */
2967 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2968 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002969 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002970 base = tctx->tpdf->type.compiled;
2971 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002972 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002973 /* no change, just use the type information from the base */
2974 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2975 ++base->refcount;
2976 continue;
2977 }
2978
2979 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002980 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2982 tctx->tpdf->name, ly_data_type2str[basetype]);
2983 ret = LY_EVALID;
2984 goto cleanup;
2985 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2987 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2988 tctx->tpdf->name, tctx->tpdf->dflt);
2989 ret = LY_EVALID;
2990 goto cleanup;
2991 }
2992
Radek Krejci19a96102018-11-15 13:38:09 +01002993 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002994 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002995 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 +01002996 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2997 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002998 LY_CHECK_GOTO(ret, cleanup);
2999 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003000 }
3001
Radek Krejcic5c27e52018-11-15 14:38:11 +01003002 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003003 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003004 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003005 (*type)->basetype = basetype;
3006 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01003007 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 +01003008 LY_CHECK_GOTO(ret, cleanup);
3009 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003010 /* no specific restriction in leaf's type definition, copy from the base */
3011 free(*type);
3012 (*type) = base;
3013 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003014 }
Radek Krejci01342af2019-01-03 15:18:08 +01003015 if (!(*type)->dflt) {
3016 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3017 }
Radek Krejci19a96102018-11-15 13:38:09 +01003018
3019 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
3020
3021cleanup:
3022 ly_set_erase(&tpdf_chain, free);
3023 return ret;
3024}
3025
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003026/**
3027 * @brief Compile status information of the given node.
3028 *
3029 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3030 * has the status correctly set during the compilation.
3031 *
3032 * @param[in] ctx Compile context
3033 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3034 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3035 * the compatibility with the parent's status value.
3036 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3037 * @return LY_ERR value.
3038 */
3039static LY_ERR
3040lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3041{
3042 /* status - it is not inherited by specification, but it does not make sense to have
3043 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3044 if (!((*node_flags) & LYS_STATUS_MASK)) {
3045 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3046 if ((parent_flags & 0x3) != 0x3) {
3047 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3048 * combination of bits (0x3) which marks the uses_status value */
3049 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3050 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3051 }
3052 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3053 } else {
3054 (*node_flags) |= LYS_STATUS_CURR;
3055 }
3056 } else if (parent_flags & LYS_STATUS_MASK) {
3057 /* check status compatibility with the parent */
3058 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3059 if ((*node_flags) & LYS_STATUS_CURR) {
3060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3061 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3062 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3063 } else { /* LYS_STATUS_DEPRC */
3064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3065 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3066 }
3067 return LY_EVALID;
3068 }
3069 }
3070 return LY_SUCCESS;
3071}
3072
Radek Krejci8cce8532019-03-05 11:27:45 +01003073/**
3074 * @brief Check uniqness of the node/action/notification name.
3075 *
3076 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3077 * structures, but they share the namespace so we need to check their name collisions.
3078 *
3079 * @param[in] ctx Compile context.
3080 * @param[in] children List (linked list) of data nodes to go through.
3081 * @param[in] actions List (sized array) of actions or RPCs to go through.
3082 * @param[in] notifs List (sized array) of Notifications to go through.
3083 * @param[in] name Name of the item to find in the given lists.
3084 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3085 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3086 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3087 */
3088static LY_ERR
3089lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3090 const struct lysc_action *actions, const struct lysc_notif *notifs,
3091 const char *name, void *exclude)
3092{
3093 const struct lysc_node *iter;
3094 unsigned int u;
3095
3096 LY_LIST_FOR(children, iter) {
3097 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3098 goto error;
3099 }
3100 }
3101 LY_ARRAY_FOR(actions, u) {
3102 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3103 goto error;
3104 }
3105 }
3106 LY_ARRAY_FOR(notifs, u) {
3107 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3108 goto error;
3109 }
3110 }
3111 return LY_SUCCESS;
3112error:
3113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3114 return LY_EEXIST;
3115}
3116
Radek Krejcib1b59152019-01-07 13:21:56 +01003117static 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 +01003118
Radek Krejcia3045382018-11-22 14:30:31 +01003119/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003120 * @brief Compile parsed RPC/action schema node information.
3121 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003122 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003123 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003124 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003125 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3126 * @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).
3127 * Zero means no uses, non-zero value with no status bit set mean the default status.
3128 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3129 */
3130static LY_ERR
3131lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, int options,
3132 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3133{
3134 LY_ERR ret = LY_SUCCESS;
3135 struct lysp_node *child_p;
3136 unsigned int u;
3137
Radek Krejci8cce8532019-03-05 11:27:45 +01003138 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3139 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3140 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3141 action_p->name, action)) {
3142 return LY_EVALID;
3143 }
3144
Radek Krejcifc11bd72019-04-11 16:00:05 +02003145 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003147 "Action \"%s\" is placed inside %s.", action_p->name,
3148 options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003149 return LY_EVALID;
3150 }
3151
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003152 action->nodetype = LYS_ACTION;
3153 action->module = ctx->mod;
3154 action->parent = parent;
3155 if (!(options & LYSC_OPT_FREE_SP)) {
3156 action->sp = action_p;
3157 }
3158 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3159
3160 /* status - it is not inherited by specification, but it does not make sense to have
3161 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3162 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3163
3164 DUP_STRING(ctx->ctx, action_p->name, action->name);
3165 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3166 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
3167 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3168 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, options, u, lys_compile_ext, ret, cleanup);
3169
3170 /* input */
3171 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 +02003172 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 +01003173 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003174 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 +01003175 }
3176
3177 /* output */
3178 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 +02003179 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 +01003180 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003181 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 +01003182 }
3183
3184cleanup:
3185 return ret;
3186}
3187
3188/**
Radek Krejci43981a32019-04-12 09:44:11 +02003189 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003190 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003191 * @param[in] notif_p Parsed Notification schema node.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003192 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003193 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3194 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3195 * @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 +02003196 * Zero means no uses, non-zero value with no status bit set mean the default status.
3197 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3198 */
3199static LY_ERR
3200lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, int options,
3201 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3202{
3203 LY_ERR ret = LY_SUCCESS;
3204 struct lysp_node *child_p;
3205 unsigned int u;
3206
3207 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3208 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3209 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3210 notif_p->name, notif)) {
3211 return LY_EVALID;
3212 }
3213
3214 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
3215 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3216 "Notification \"%s\" is placed inside %s.", notif_p->name,
3217 options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
3218 return LY_EVALID;
3219 }
3220
3221 notif->nodetype = LYS_NOTIF;
3222 notif->module = ctx->mod;
3223 notif->parent = parent;
3224 if (!(options & LYSC_OPT_FREE_SP)) {
3225 notif->sp = notif_p;
3226 }
3227 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3228
3229 /* status - it is not inherited by specification, but it does not make sense to have
3230 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3231 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3232
3233 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3234 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3235 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
3236 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3237 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, options, u, lys_compile_ext, ret, cleanup);
3238 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, options, u, lys_compile_must, ret, cleanup);
3239
3240 LY_LIST_FOR(notif_p->data, child_p) {
3241 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_NOTIFICATION, (struct lysc_node*)notif, uses_status));
3242 }
3243
3244cleanup:
3245 return ret;
3246}
3247
3248/**
Radek Krejcia3045382018-11-22 14:30:31 +01003249 * @brief Compile parsed container node information.
3250 * @param[in] ctx Compile context
3251 * @param[in] node_p Parsed container node.
3252 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3253 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3254 * is enriched with the container-specific information.
3255 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3256 */
Radek Krejci19a96102018-11-15 13:38:09 +01003257static LY_ERR
3258lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3259{
3260 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3261 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3262 struct lysp_node *child_p;
3263 unsigned int u;
3264 LY_ERR ret = LY_SUCCESS;
3265
Radek Krejcife909632019-02-12 15:34:42 +01003266 if (cont_p->presence) {
3267 cont->flags |= LYS_PRESENCE;
3268 }
3269
Radek Krejci19a96102018-11-15 13:38:09 +01003270 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003271 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003272 }
3273
3274 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003275 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 +02003276 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 +01003277
3278done:
3279 return ret;
3280}
3281
Radek Krejci33f72892019-02-21 10:36:58 +01003282/*
3283 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3284 * @param[in] ctx Compile context.
3285 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3286 * @param[in] type_p Parsed type to compile.
3287 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3288 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3289 * @return LY_ERR value.
3290 */
3291static LY_ERR
3292lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, int options, struct lysc_node_leaf *leaf)
3293{
3294 unsigned int u, v;
3295 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3296
3297 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type,
3298 leaf->units ? NULL : &leaf->units));
3299 if (leaf->nodetype == LYS_LEAFLIST) {
3300 if (llist->type->dflt && !llist->dflts && !llist->min) {
3301 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3302 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3303 LY_ARRAY_INCREMENT(llist->dflts);
3304 }
3305 } else {
3306 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3307 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3308 }
3309 }
3310 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3311 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3312 ly_set_add(&ctx->unres, leaf, 0);
3313 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3314 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3315 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3316 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3317 ly_set_add(&ctx->unres, leaf, 0);
3318 }
3319 }
3320 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3321 if (leaf->flags & LYS_SET_DFLT) {
3322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3323 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3324 return LY_EVALID;
3325 }
3326 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3327 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3328 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3329 return LY_EVALID;
3330 }
3331 }
3332
3333 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3334 /* configuration data values must be unique - so check the default values */
3335 LY_ARRAY_FOR(llist->dflts, u) {
3336 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3337 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3338 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3339 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3340 return LY_EVALID;
3341 }
3342 }
3343 }
3344 }
3345
3346 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3347
3348 return LY_SUCCESS;
3349}
3350
Radek Krejcia3045382018-11-22 14:30:31 +01003351/**
3352 * @brief Compile parsed leaf node information.
3353 * @param[in] ctx Compile context
3354 * @param[in] node_p Parsed leaf node.
3355 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3356 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3357 * is enriched with the leaf-specific information.
3358 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3359 */
Radek Krejci19a96102018-11-15 13:38:09 +01003360static LY_ERR
3361lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3362{
3363 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3364 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3365 unsigned int u;
3366 LY_ERR ret = LY_SUCCESS;
3367
Radek Krejci19a96102018-11-15 13:38:09 +01003368 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003369 if (leaf_p->units) {
3370 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3371 leaf->flags |= LYS_SET_UNITS;
3372 }
3373 if (leaf_p->dflt) {
3374 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003375 leaf->flags |= LYS_SET_DFLT;
3376 }
Radek Krejci43699232018-11-23 14:59:46 +01003377
Radek Krejci33f72892019-02-21 10:36:58 +01003378 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, options, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003379
Radek Krejci19a96102018-11-15 13:38:09 +01003380done:
3381 return ret;
3382}
3383
Radek Krejcia3045382018-11-22 14:30:31 +01003384/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003385 * @brief Compile parsed leaf-list node information.
3386 * @param[in] ctx Compile context
3387 * @param[in] node_p Parsed leaf-list node.
3388 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3389 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3390 * is enriched with the leaf-list-specific information.
3391 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3392 */
3393static LY_ERR
3394lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3395{
3396 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3397 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003398 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003399 LY_ERR ret = LY_SUCCESS;
3400
Radek Krejci0e5d8382018-11-28 16:37:53 +01003401 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003402 if (llist_p->units) {
3403 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3404 llist->flags |= LYS_SET_UNITS;
3405 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003406
3407 if (llist_p->dflts) {
3408 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3409 LY_ARRAY_FOR(llist_p->dflts, u) {
3410 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3411 LY_ARRAY_INCREMENT(llist->dflts);
3412 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003413 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003414 }
3415
3416 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003417 if (llist->min) {
3418 llist->flags |= LYS_MAND_TRUE;
3419 }
Radek Krejcib7408632018-11-28 17:12:11 +01003420 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003421
Radek Krejci33f72892019-02-21 10:36:58 +01003422 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, options, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003423
3424done:
3425 return ret;
3426}
3427
3428/**
Radek Krejci7af64242019-02-18 13:07:53 +01003429 * @brief Compile information about list's uniques.
3430 * @param[in] ctx Compile context.
3431 * @param[in] context_module Module where the prefixes are going to be resolved.
3432 * @param[in] uniques Sized array list of unique statements.
3433 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3434 * @return LY_ERR value.
3435 */
3436static LY_ERR
3437lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3438{
3439 LY_ERR ret = LY_SUCCESS;
3440 struct lysc_node_leaf **key, ***unique;
3441 const char *keystr, *delim;
3442 size_t len;
3443 unsigned int v;
3444 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003445 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003446
3447 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3448 config = -1;
3449 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3450 keystr = uniques[v];
3451 while (keystr) {
3452 delim = strpbrk(keystr, " \t\n");
3453 if (delim) {
3454 len = delim - keystr;
3455 while (isspace(*delim)) {
3456 ++delim;
3457 }
3458 } else {
3459 len = strlen(keystr);
3460 }
3461
3462 /* unique node must be present */
3463 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003464 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3465 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003466 if (ret != LY_SUCCESS) {
3467 if (ret == LY_EDENIED) {
3468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003469 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003470 len, keystr, lys_nodetype2str((*key)->nodetype));
3471 }
3472 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003473 } else if (flags) {
3474 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3475 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3476 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3477 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003478 }
3479
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003480
Radek Krejci7af64242019-02-18 13:07:53 +01003481 /* all referenced leafs must be of the same config type */
3482 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3484 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3485 return LY_EVALID;
3486 } else if ((*key)->flags & LYS_CONFIG_W) {
3487 config = 1;
3488 } else { /* LYS_CONFIG_R */
3489 config = 0;
3490 }
3491
3492 /* check status */
3493 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3494 (*key)->flags, (*key)->module, (*key)->name));
3495
3496 /* mark leaf as unique */
3497 (*key)->flags |= LYS_UNIQUE;
3498
3499 /* next unique value in line */
3500 keystr = delim;
3501 }
3502 /* next unique definition */
3503 }
3504
3505 return LY_SUCCESS;
3506}
3507
3508/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003509 * @brief Compile parsed list node information.
3510 * @param[in] ctx Compile context
3511 * @param[in] node_p Parsed list node.
3512 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3513 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3514 * is enriched with the list-specific information.
3515 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3516 */
3517static LY_ERR
3518lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3519{
3520 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3521 struct lysc_node_list *list = (struct lysc_node_list*)node;
3522 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003523 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003524 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003525 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003526 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003527 LY_ERR ret = LY_SUCCESS;
3528
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003529 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003530 if (list->min) {
3531 list->flags |= LYS_MAND_TRUE;
3532 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003533 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3534
3535 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003536 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003537 }
3538
3539 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
3540
3541 /* keys */
3542 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3544 return LY_EVALID;
3545 }
3546
3547 /* find all the keys (must be direct children) */
3548 keystr = list_p->key;
3549 while (keystr) {
3550 delim = strpbrk(keystr, " \t\n");
3551 if (delim) {
3552 len = delim - keystr;
3553 while (isspace(*delim)) {
3554 ++delim;
3555 }
3556 } else {
3557 len = strlen(keystr);
3558 }
3559
3560 /* key node must be present */
3561 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3562 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3563 if (!(*key)) {
3564 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3565 "The list's key \"%.*s\" not found.", len, keystr);
3566 return LY_EVALID;
3567 }
3568 /* keys must be unique */
3569 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3570 if (*key == list->keys[u]) {
3571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3572 "Duplicated key identifier \"%.*s\".", len, keystr);
3573 return LY_EVALID;
3574 }
3575 }
3576 /* key must have the same config flag as the list itself */
3577 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3579 return LY_EVALID;
3580 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003581 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003582 /* YANG 1.0 denies key to be of empty type */
3583 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3584 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3585 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3586 return LY_EVALID;
3587 }
3588 } else {
3589 /* when and if-feature are illegal on list keys */
3590 if ((*key)->when) {
3591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3592 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3593 return LY_EVALID;
3594 }
3595 if ((*key)->iffeatures) {
3596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3597 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3598 return LY_EVALID;
3599 }
3600 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003601
3602 /* check status */
3603 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3604 (*key)->flags, (*key)->module, (*key)->name));
3605
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003606 /* ignore default values of the key */
3607 if ((*key)->dflt) {
3608 lydict_remove(ctx->ctx, (*key)->dflt);
3609 (*key)->dflt = NULL;
3610 }
3611 /* mark leaf as key */
3612 (*key)->flags |= LYS_KEY;
3613
3614 /* next key value */
3615 keystr = delim;
3616 }
3617
3618 /* uniques */
3619 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003620 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003621 }
3622
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003623 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 +02003624 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 +01003625
3626done:
3627 return ret;
3628}
3629
Radek Krejcib56c7502019-02-13 14:19:54 +01003630/**
3631 * @brief Do some checks and set the default choice's case.
3632 *
3633 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3634 *
3635 * @param[in] ctx Compile context.
3636 * @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,
3637 * not the reference to the imported module.
3638 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3639 * @return LY_ERR value.
3640 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003641static LY_ERR
3642lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3643{
3644 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3645 const char *prefix = NULL, *name;
3646 size_t prefix_len = 0;
3647
3648 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3649 name = strchr(dflt, ':');
3650 if (name) {
3651 prefix = dflt;
3652 prefix_len = name - prefix;
3653 ++name;
3654 } else {
3655 name = dflt;
3656 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003657 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003658 /* prefixed default case make sense only for the prefix of the schema itself */
3659 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3660 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3661 prefix_len, prefix);
3662 return LY_EVALID;
3663 }
3664 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3665 if (!ch->dflt) {
3666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3667 "Default case \"%s\" not found.", dflt);
3668 return LY_EVALID;
3669 }
3670 /* no mandatory nodes directly under the default case */
3671 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003672 if (iter->parent != (struct lysc_node*)ch->dflt) {
3673 break;
3674 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003675 if (iter->flags & LYS_MAND_TRUE) {
3676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3677 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3678 return LY_EVALID;
3679 }
3680 }
Radek Krejci01342af2019-01-03 15:18:08 +01003681 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003682 return LY_SUCCESS;
3683}
3684
Radek Krejciccd20f12019-02-15 14:12:27 +01003685static LY_ERR
3686lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3687{
3688 struct lys_module *mod;
3689 const char *prefix = NULL, *name;
3690 size_t prefix_len = 0;
3691 struct lysc_node_case *cs;
3692 struct lysc_node *node;
3693
3694 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3695 name = strchr(dflt, ':');
3696 if (name) {
3697 prefix = dflt;
3698 prefix_len = name - prefix;
3699 ++name;
3700 } else {
3701 name = dflt;
3702 }
3703 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3704 if (prefix) {
3705 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3707 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3708 "The prefix does not match any imported module of the deviation module.",
3709 devnodeid, dflt);
3710 return LY_EVALID;
3711 }
3712 } else {
3713 mod = ctx->mod;
3714 }
3715 /* get the default case */
3716 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3717 if (!cs) {
3718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3719 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3720 devnodeid, dflt);
3721 return LY_EVALID;
3722 }
3723
3724 /* check that there is no mandatory node */
3725 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003726 if (node->parent != (struct lysc_node*)cs) {
3727 break;
3728 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003729 if (node->flags & LYS_MAND_TRUE) {
3730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3731 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3732 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3733 return LY_EVALID;
3734 }
3735 }
3736
3737 /* set the default case in choice */
3738 ch->dflt = cs;
3739 cs->flags |= LYS_SET_DFLT;
3740
3741 return LY_SUCCESS;
3742}
3743
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003744/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003745 * @brief Compile parsed choice node information.
3746 * @param[in] ctx Compile context
3747 * @param[in] node_p Parsed choice node.
3748 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3749 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003750 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003751 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3752 */
3753static LY_ERR
3754lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3755{
3756 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3757 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3758 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003759 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003760 LY_ERR ret = LY_SUCCESS;
3761
Radek Krejci056d0a82018-12-06 16:57:25 +01003762 LY_LIST_FOR(ch_p->child, child_p) {
3763 if (child_p->nodetype == LYS_CASE) {
3764 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003765 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003766 }
3767 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003768 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003769 }
3770 }
3771
3772 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003773 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003774 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003775 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003776
Radek Krejci9800fb82018-12-13 14:26:23 +01003777 return ret;
3778}
3779
3780/**
3781 * @brief Compile parsed anydata or anyxml node information.
3782 * @param[in] ctx Compile context
3783 * @param[in] node_p Parsed anydata or anyxml node.
3784 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3785 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3786 * is enriched with the any-specific information.
3787 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3788 */
3789static LY_ERR
3790lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3791{
3792 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3793 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3794 unsigned int u;
3795 LY_ERR ret = LY_SUCCESS;
3796
3797 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3798
3799 if (any->flags & LYS_CONFIG_W) {
3800 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3801 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3802 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003803done:
3804 return ret;
3805}
3806
Radek Krejcib56c7502019-02-13 14:19:54 +01003807/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003808 * @brief Connect the node into the siblings list and check its name uniqueness.
3809 *
3810 * @param[in] ctx Compile context
3811 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3812 * the choice itself is expected instead of a specific case node.
3813 * @param[in] node Schema node to connect into the list.
Radek Krejci05b774b2019-02-25 13:26:18 +01003814 * @param[in] options Compile options to distinguish input/output when placing node into RPC/action.
Radek Krejci056d0a82018-12-06 16:57:25 +01003815 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3816 */
3817static LY_ERR
Radek Krejci05b774b2019-02-25 13:26:18 +01003818lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node, int options)
Radek Krejci056d0a82018-12-06 16:57:25 +01003819{
3820 struct lysc_node **children;
3821
3822 if (node->nodetype == LYS_CASE) {
3823 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3824 } else {
Radek Krejci05b774b2019-02-25 13:26:18 +01003825 children = lysc_node_children_p(parent, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003826 }
3827 if (children) {
3828 if (!(*children)) {
3829 /* first child */
3830 *children = node;
3831 } else if (*children != node) {
3832 /* by the condition in previous branch we cover the choice/case children
3833 * - the children list is shared by the choice and the the first case, in addition
3834 * the first child of each case must be referenced from the case node. So the node is
3835 * actually always already inserted in case it is the first children - so here such
3836 * a situation actually corresponds to the first branch */
3837 /* insert at the end of the parent's children list */
3838 (*children)->prev->next = node;
3839 node->prev = (*children)->prev;
3840 (*children)->prev = node;
3841
3842 /* check the name uniqueness */
3843 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3844 lysc_node_notifs(parent), node->name, node)) {
3845 return LY_EEXIST;
3846 }
3847 }
3848 }
3849 return LY_SUCCESS;
3850}
3851
Radek Krejci95710c92019-02-11 15:49:55 +01003852/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003853 * @brief Get the XPath context node for the given schema node.
3854 * @param[in] start The schema node where the XPath expression appears.
3855 * @return The context node to evaluate XPath expression in given schema node.
3856 * @return NULL in case the context node is the root node.
3857 */
3858static struct lysc_node *
3859lysc_xpath_context(struct lysc_node *start)
3860{
3861 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3862 start = start->parent);
3863 return start;
3864}
3865
3866/**
3867 * @brief Prepare the case structure in choice node for the new data node.
3868 *
3869 * 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
3870 * created in the choice when the first child was processed.
3871 *
3872 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003873 * @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,
3874 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003875 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3876 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3877 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3878 * @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,
3879 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003880 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003881static struct lysc_node_case*
3882lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3883{
3884 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02003885 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01003886 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003887 unsigned int u;
3888 LY_ERR ret;
3889
Radek Krejci95710c92019-02-11 15:49:55 +01003890#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003891 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003892 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003893 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3894 return NULL; \
3895 } \
3896 }
3897
Radek Krejci95710c92019-02-11 15:49:55 +01003898 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3899 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003900
3901 /* we have to add an implicit case node into the parent choice */
3902 cs = calloc(1, sizeof(struct lysc_node_case));
3903 DUP_STRING(ctx->ctx, child->name, cs->name);
3904 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003905 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003906 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3907 /* the case is already present since the child is not its first children */
3908 return (struct lysc_node_case*)ch->cases->prev;
3909 }
Radek Krejci95710c92019-02-11 15:49:55 +01003910 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003911
3912 /* explicit parent case is not present (this is its first child) */
3913 cs = calloc(1, sizeof(struct lysc_node_case));
3914 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3915 cs->flags = LYS_STATUS_MASK & node_p->flags;
3916 cs->sp = node_p;
3917
Radek Krejcib1b59152019-01-07 13:21:56 +01003918 /* 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 +02003919 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01003920
3921 if (node_p->when) {
3922 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3923 ret = lys_compile_when(ctx, node_p->when, options, when);
3924 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003925 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003926 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003927 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003928 } else {
3929 LOGINT(ctx->ctx);
3930 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003931 }
3932 cs->module = ctx->mod;
3933 cs->prev = (struct lysc_node*)cs;
3934 cs->nodetype = LYS_CASE;
Radek Krejci05b774b2019-02-25 13:26:18 +01003935 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003936 cs->parent = (struct lysc_node*)ch;
3937 cs->child = child;
3938
3939 return cs;
3940error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02003941 if (cs) {
3942 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
3943 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003944 return NULL;
3945
3946#undef UNIQUE_CHECK
3947}
3948
Radek Krejcib56c7502019-02-13 14:19:54 +01003949/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003950 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003951 *
3952 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003953 * @param[in] node Target node where the config is supposed to be changed.
3954 * @param[in] config_flag Node's config flag to be applied to the @p node.
3955 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003956 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3957 * 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 +01003958 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3959 * @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 +01003960 * @return LY_ERR value.
3961 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003962static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003963lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3964 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003965{
3966 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003967 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003968
3969 if (config == (node->flags & LYS_CONFIG_MASK)) {
3970 /* nothing to do */
3971 return LY_SUCCESS;
3972 }
3973
3974 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003975 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003976 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3977 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003978 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3979 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003980 return LY_EVALID;
3981 }
Radek Krejci93dcc392019-02-19 10:43:38 +01003982 node->flags |= LYS_SET_CONFIG;
3983 } else {
3984 if (node->flags & LYS_SET_CONFIG) {
3985 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
3986 /* setting config flags, but have node with explicit config true */
3987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3988 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3989 refine_flag ? "refine" : "deviation", nodeid);
3990 return LY_EVALID;
3991 }
3992 /* do not change config on nodes where the config is explicitely set, this does not apply to
3993 * nodes, which are being changed explicitly (targets of refine or deviation) */
3994 return LY_SUCCESS;
3995 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003996 }
3997 node->flags &= ~LYS_CONFIG_MASK;
3998 node->flags |= config;
3999
4000 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004001 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004002 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004003 }
4004
Radek Krejci76b3e962018-12-14 17:01:25 +01004005 return LY_SUCCESS;
4006}
4007
Radek Krejcib56c7502019-02-13 14:19:54 +01004008/**
4009 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4010 *
4011 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4012 * the flag to such parents from a mandatory children.
4013 *
4014 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4015 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4016 * (mandatory children was removed).
4017 */
Radek Krejcife909632019-02-12 15:34:42 +01004018void
4019lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4020{
4021 struct lysc_node *iter;
4022
4023 if (add) { /* set flag */
4024 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4025 parent = parent->parent) {
4026 parent->flags |= LYS_MAND_TRUE;
4027 }
4028 } else { /* unset flag */
4029 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004030 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004031 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004032 /* there is another mandatory node */
4033 return;
4034 }
4035 }
4036 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4037 parent->flags &= ~LYS_MAND_TRUE;
4038 }
4039 }
4040}
4041
Radek Krejci056d0a82018-12-06 16:57:25 +01004042/**
Radek Krejci3641f562019-02-13 15:38:40 +01004043 * @brief Internal sorting process for the lys_compile_augment_sort().
4044 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4045 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4046 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4047 */
4048static void
4049lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4050{
4051 unsigned int v;
4052 size_t len;
4053
4054 len = strlen(aug_p->nodeid);
4055 LY_ARRAY_FOR(result, v) {
4056 if (strlen(result[v]->nodeid) <= len) {
4057 continue;
4058 }
4059 if (v < LY_ARRAY_SIZE(result)) {
4060 /* move the rest of array */
4061 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4062 break;
4063 }
4064 }
4065 result[v] = aug_p;
4066 LY_ARRAY_INCREMENT(result);
4067}
4068
4069/**
4070 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4071 *
4072 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4073 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4074 *
4075 * @param[in] ctx Compile context.
4076 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4077 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4078 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4079 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4080 * @return LY_ERR value.
4081 */
4082LY_ERR
4083lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4084{
4085 struct lysp_augment **result = NULL;
4086 unsigned int u, v;
4087 size_t count = 0;
4088
4089 assert(augments);
4090
4091 /* get count of the augments in module and all its submodules */
4092 if (aug_p) {
4093 count += LY_ARRAY_SIZE(aug_p);
4094 }
4095 LY_ARRAY_FOR(inc_p, u) {
4096 if (inc_p[u].submodule->augments) {
4097 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4098 }
4099 }
4100
4101 if (!count) {
4102 *augments = NULL;
4103 return LY_SUCCESS;
4104 }
4105 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4106
4107 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4108 * together, so there can be even /z/y betwwen them. */
4109 LY_ARRAY_FOR(aug_p, u) {
4110 lys_compile_augment_sort_(&aug_p[u], result);
4111 }
4112 LY_ARRAY_FOR(inc_p, u) {
4113 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4114 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4115 }
4116 }
4117
4118 *augments = result;
4119 return LY_SUCCESS;
4120}
4121
4122/**
4123 * @brief Compile the parsed augment connecting it into its target.
4124 *
4125 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4126 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4127 * are already implemented and compiled.
4128 *
4129 * @param[in] ctx Compile context.
4130 * @param[in] aug_p Parsed augment to compile.
4131 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4132 * @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
4133 * children in case of the augmenting uses data.
4134 * @return LY_SUCCESS on success.
4135 * @return LY_EVALID on failure.
4136 */
4137LY_ERR
4138lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4139{
4140 LY_ERR ret = LY_SUCCESS;
4141 struct lysp_node *node_p, *case_node_p;
4142 struct lysc_node *target; /* target target of the augment */
4143 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004144 struct lysc_when **when, *when_shared;
4145 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004146 uint16_t flags = 0;
4147 unsigned int u;
Radek Krejci3641f562019-02-13 15:38:40 +01004148
Radek Krejci7af64242019-02-18 13:07:53 +01004149 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004150 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004151 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004152 if (ret != LY_SUCCESS) {
4153 if (ret == LY_EDENIED) {
4154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4155 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4156 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4157 }
4158 return LY_EVALID;
4159 }
4160
4161 /* check for mandatory nodes
4162 * - new cases augmenting some choice can have mandatory nodes
4163 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4164 */
Radek Krejci733988a2019-02-15 15:12:44 +01004165 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004166 allow_mandatory = 1;
4167 }
4168
4169 when_shared = NULL;
4170 LY_LIST_FOR(aug_p->child, node_p) {
4171 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4172 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4173 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004174 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4175 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4177 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4178 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4179 return LY_EVALID;
4180 }
4181
4182 /* compile the children */
4183 if (node_p->nodetype != LYS_CASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004184 LY_CHECK_RET(lys_compile_node(ctx, node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004185 } else {
4186 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004187 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004188 }
4189 }
4190
Radek Krejcife13da42019-02-15 14:51:01 +01004191 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4192 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004193 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004194 /* 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 +01004195 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 +01004196 } else if (target->nodetype == LYS_CHOICE) {
4197 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4198 node = ((struct lysc_node_choice*)target)->cases->prev;
4199 } else {
4200 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004201 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004202 }
4203
Radek Krejci733988a2019-02-15 15:12:44 +01004204 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004205 node->flags &= ~LYS_MAND_TRUE;
4206 lys_compile_mandatory_parents(target, 0);
4207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4208 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4209 aug_p->nodeid, node->name);
4210 return LY_EVALID;
4211 }
4212
4213 /* pass augment's when to all the children */
4214 if (aug_p->when) {
4215 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4216 if (!when_shared) {
4217 ret = lys_compile_when(ctx, aug_p->when, options, when);
4218 LY_CHECK_GOTO(ret, error);
4219 (*when)->context = lysc_xpath_context(target);
4220 when_shared = *when;
4221 } else {
4222 ++when_shared->refcount;
4223 (*when) = when_shared;
4224 }
4225 }
4226 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004227
4228 switch (target->nodetype) {
4229 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004230 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
4231 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004232 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
4233 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004234 break;
4235 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004236 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
4237 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004238 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
4239 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004240 break;
4241 default:
4242 if (aug_p->actions) {
4243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4244 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4245 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4246 return LY_EVALID;
4247 }
4248 if (aug_p->notifs) {
4249 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4250 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4251 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4252 return LY_EVALID;
4253 }
4254 }
Radek Krejci3641f562019-02-13 15:38:40 +01004255
4256error:
4257 return ret;
4258}
4259
4260/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004261 * @brief Apply refined or deviated mandatory flag to the target node.
4262 *
4263 * @param[in] ctx Compile context.
4264 * @param[in] node Target node where the mandatory property is supposed to be changed.
4265 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4266 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4267 * @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 +01004268 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4269 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4270 * 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 +01004271 * @return LY_ERR value.
4272 */
4273static LY_ERR
4274lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4275{
4276 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4278 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4279 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4280 return LY_EVALID;
4281 }
4282
4283 if (mandatory_flag & LYS_MAND_TRUE) {
4284 /* check if node has default value */
4285 if (node->nodetype & LYS_LEAF) {
4286 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004287 if (refine_flag) {
4288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4289 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4290 return LY_EVALID;
4291 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004292 } else {
4293 /* remove the default value taken from the leaf's type */
4294 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4295 ((struct lysc_node_leaf*)node)->dflt = NULL;
4296 }
4297 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004298 if (refine_flag) {
4299 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4300 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4301 return LY_EVALID;
4302 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004303 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004304 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004306 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004307 return LY_EVALID;
4308 }
4309
4310 node->flags &= ~LYS_MAND_FALSE;
4311 node->flags |= LYS_MAND_TRUE;
4312 lys_compile_mandatory_parents(node->parent, 1);
4313 } else {
4314 /* make mandatory false */
4315 node->flags &= ~LYS_MAND_TRUE;
4316 node->flags |= LYS_MAND_FALSE;
4317 lys_compile_mandatory_parents(node->parent, 0);
4318 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4319 /* get the type's default value if any */
4320 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4321 }
4322 }
4323 return LY_SUCCESS;
4324}
4325
4326/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004327 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4328 * If present, also apply uses's modificators.
4329 *
4330 * @param[in] ctx Compile context
4331 * @param[in] uses_p Parsed uses schema node.
4332 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4333 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4334 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4335 * the compile context.
4336 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4337 */
4338static LY_ERR
4339lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
4340{
4341 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004342 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004343 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004344 struct lysc_node_container context_node_fake =
4345 {.nodetype = LYS_CONTAINER,
4346 .module = ctx->mod,
4347 .flags = parent ? parent->flags : 0,
4348 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004349 .prev = (struct lysc_node*)&context_node_fake,
4350 .actions = NULL, .notifs = NULL};
Radek Krejcie86bf772018-12-14 11:39:53 +01004351 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004352 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004353 int found;
4354 const char *id, *name, *prefix;
4355 size_t prefix_len, name_len;
4356 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004357 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004358 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004359 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004360 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004361 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004362 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004363 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004364 unsigned int actions_index, notifs_index;
4365 struct lysc_notif **notifs = NULL;
4366 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004367
4368 /* search for the grouping definition */
4369 found = 0;
4370 id = uses_p->name;
Radek Krejci15e99fc2019-04-08 14:29:39 +02004371 LY_CHECK_RET(lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004372 if (prefix) {
4373 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4374 if (!mod) {
4375 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4376 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4377 return LY_EVALID;
4378 }
4379 } else {
4380 mod = ctx->mod_def;
4381 }
4382 if (mod == ctx->mod_def) {
4383 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
4384 grp = lysp_node_groupings(node_p);
4385 LY_ARRAY_FOR(grp, u) {
4386 if (!strcmp(grp[u].name, name)) {
4387 grp = &grp[u];
4388 found = 1;
4389 break;
4390 }
4391 }
4392 }
4393 }
4394 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004395 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004396 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004397 if (grp) {
4398 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4399 if (!strcmp(grp[u].name, name)) {
4400 grp = &grp[u];
4401 found = 1;
4402 }
4403 }
4404 }
4405 if (!found && mod->parsed->includes) {
4406 /* ... and all the submodules */
4407 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4408 grp = mod->parsed->includes[u].submodule->groupings;
4409 if (grp) {
4410 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4411 if (!strcmp(grp[v].name, name)) {
4412 grp = &grp[v];
4413 found = 1;
4414 }
4415 }
4416 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004417 }
4418 }
4419 }
4420 if (!found) {
4421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4422 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4423 return LY_EVALID;
4424 }
4425
4426 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4427 grp_stack_count = ctx->groupings.count;
4428 ly_set_add(&ctx->groupings, (void*)grp, 0);
4429 if (grp_stack_count == ctx->groupings.count) {
4430 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4432 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4433 return LY_EVALID;
4434 }
4435
4436 /* switch context's mod_def */
4437 mod_old = ctx->mod_def;
4438 ctx->mod_def = mod;
4439
4440 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004441 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 +01004442
Radek Krejcifc11bd72019-04-11 16:00:05 +02004443 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004444 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004445 /* 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 +02004446 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 +01004447 child = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01004448
4449 /* some preparation for applying refines */
4450 if (grp->data == node_p) {
4451 /* remember the first child */
4452 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01004453 }
4454 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004455 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004456 LY_LIST_FOR(context_node_fake.child, child) {
4457 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004458
Radek Krejcifc11bd72019-04-11 16:00:05 +02004459 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004460 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004461 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004462 if (!when_shared) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004463 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004464 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004465 when_shared = *when;
4466 } else {
4467 ++when_shared->refcount;
4468 (*when) = when_shared;
4469 }
4470 }
Radek Krejci01342af2019-01-03 15:18:08 +01004471 }
4472 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004473 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004474 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004475 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004476 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 +01004477 }
4478
Radek Krejcifc11bd72019-04-11 16:00:05 +02004479 /* compile actions */
4480 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4481 if (actions) {
4482 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
4483 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, options, u, lys_compile_action, 0, ret, cleanup);
4484 if (*actions && (uses_p->augments || uses_p->refines)) {
4485 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4486 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4487 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4488 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4489 }
4490 }
4491
4492 /* compile notifications */
4493 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4494 if (notifs) {
4495 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
4496 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, options, u, lys_compile_notif, 0, ret, cleanup);
4497 if (*notifs && (uses_p->augments || uses_p->refines)) {
4498 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4499 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4500 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4501 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4502 }
4503 }
4504
4505
Radek Krejci3641f562019-02-13 15:38:40 +01004506 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004507 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004508 LY_ARRAY_FOR(augments, u) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004509 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004510 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004511
Radek Krejcif0089082019-01-07 16:42:01 +01004512 /* reload previous context's mod_def */
4513 ctx->mod_def = mod_old;
4514
Radek Krejci76b3e962018-12-14 17:01:25 +01004515 /* apply refine */
4516 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004517 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 +01004518 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004519 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004520 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004521
4522 /* default value */
4523 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004524 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004525 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4526 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4527 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004528 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004529 }
4530 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4532 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4533 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004534 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004535 }
4536 if (node->nodetype == LYS_LEAF) {
4537 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4538 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004539 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004540 /* TODO check the default value according to type */
4541 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004542 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4544 "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 +02004545 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004546 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004547 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4548 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4549 }
4550 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004551 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004552 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 +01004553 LY_ARRAY_FOR(rfn->dflts, u) {
4554 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4555 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4556 }
4557 /* TODO check the default values according to type */
4558 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004559 if (((struct lysc_node_choice*)node)->dflt) {
4560 /* unset LYS_SET_DFLT from the current default case */
4561 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4562 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004563 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 +01004564 }
4565 }
4566
Radek Krejci12fb9142019-01-08 09:45:30 +01004567 /* description */
4568 if (rfn->dsc) {
4569 FREE_STRING(ctx->ctx, node->dsc);
4570 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4571 }
4572
4573 /* reference */
4574 if (rfn->ref) {
4575 FREE_STRING(ctx->ctx, node->ref);
4576 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4577 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004578
4579 /* config */
4580 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004581 if (!flags) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004582 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004583 } else {
4584 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4585 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4586 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004587 }
4588
4589 /* mandatory */
4590 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004591 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004592 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004593
4594 /* presence */
4595 if (rfn->presence) {
4596 if (node->nodetype != LYS_CONTAINER) {
4597 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4598 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4599 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004600 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004601 }
4602 node->flags |= LYS_PRESENCE;
4603 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004604
4605 /* must */
4606 if (rfn->musts) {
4607 switch (node->nodetype) {
4608 case LYS_LEAF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004609 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 +01004610 break;
4611 case LYS_LEAFLIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004612 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 +01004613 break;
4614 case LYS_LIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004615 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 +01004616 break;
4617 case LYS_CONTAINER:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004618 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 +01004619 break;
4620 case LYS_ANYXML:
4621 case LYS_ANYDATA:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004622 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 +01004623 break;
4624 default:
4625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4626 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4627 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004628 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004629 }
4630 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004631
4632 /* min/max-elements */
4633 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4634 switch (node->nodetype) {
4635 case LYS_LEAFLIST:
4636 if (rfn->flags & LYS_SET_MAX) {
4637 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4638 }
4639 if (rfn->flags & LYS_SET_MIN) {
4640 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004641 if (rfn->min) {
4642 node->flags |= LYS_MAND_TRUE;
4643 lys_compile_mandatory_parents(node->parent, 1);
4644 } else {
4645 node->flags &= ~LYS_MAND_TRUE;
4646 lys_compile_mandatory_parents(node->parent, 0);
4647 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004648 }
4649 break;
4650 case LYS_LIST:
4651 if (rfn->flags & LYS_SET_MAX) {
4652 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4653 }
4654 if (rfn->flags & LYS_SET_MIN) {
4655 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004656 if (rfn->min) {
4657 node->flags |= LYS_MAND_TRUE;
4658 lys_compile_mandatory_parents(node->parent, 1);
4659 } else {
4660 node->flags &= ~LYS_MAND_TRUE;
4661 lys_compile_mandatory_parents(node->parent, 0);
4662 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004663 }
4664 break;
4665 default:
4666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4667 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4668 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004669 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004670 }
4671 }
Radek Krejcif0089082019-01-07 16:42:01 +01004672
4673 /* if-feature */
4674 if (rfn->iffeatures) {
4675 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004676 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004677 }
Radek Krejci01342af2019-01-03 15:18:08 +01004678 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004679
Radek Krejcif2271f12019-01-07 16:42:23 +01004680 /* do some additional checks of the changed nodes when all the refines are applied */
4681 for (u = 0; u < refined.count; ++u) {
4682 node = (struct lysc_node*)refined.objs[u];
4683 rfn = &uses_p->refines[u];
4684
4685 /* check possible conflict with default value (default added, mandatory left true) */
4686 if ((node->flags & LYS_MAND_TRUE) &&
4687 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4688 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4690 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004691 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004692 }
4693
4694 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4695 if (node->nodetype == LYS_LIST) {
4696 min = ((struct lysc_node_list*)node)->min;
4697 max = ((struct lysc_node_list*)node)->max;
4698 } else {
4699 min = ((struct lysc_node_leaflist*)node)->min;
4700 max = ((struct lysc_node_leaflist*)node)->max;
4701 }
4702 if (min > max) {
4703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4704 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4705 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004706 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004707 }
4708 }
4709 }
4710
Radek Krejcie86bf772018-12-14 11:39:53 +01004711 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004712
4713cleanup:
4714 /* fix connection of the children nodes from fake context node back into the parent */
4715 if (context_node_fake.child) {
4716 context_node_fake.child->prev = child;
4717 }
4718 LY_LIST_FOR(context_node_fake.child, child) {
4719 child->parent = parent;
4720 }
4721
4722 if (uses_p->augments || uses_p->refines) {
4723 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004724 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004725 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4726 LY_ARRAY_FREE(context_node_fake.actions);
4727 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004728 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004729 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4730 LY_ARRAY_FREE(context_node_fake.notifs);
4731 }
4732 }
4733
Radek Krejcie86bf772018-12-14 11:39:53 +01004734 /* reload previous context's mod_def */
4735 ctx->mod_def = mod_old;
4736 /* remove the grouping from the stack for circular groupings dependency check */
4737 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4738 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004739 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004740 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004741
4742 return ret;
4743}
4744
Radek Krejcife909632019-02-12 15:34:42 +01004745
Radek Krejcie86bf772018-12-14 11:39:53 +01004746/**
Radek Krejcia3045382018-11-22 14:30:31 +01004747 * @brief Compile parsed schema node information.
4748 * @param[in] ctx Compile context
4749 * @param[in] node_p Parsed schema node.
4750 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4751 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4752 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4753 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004754 * @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).
4755 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004756 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4757 */
Radek Krejci19a96102018-11-15 13:38:09 +01004758static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01004759lys_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 +01004760{
4761 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004762 struct lysc_node *node;
4763 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004764 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004765 unsigned int u;
4766 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
4767
4768 switch (node_p->nodetype) {
4769 case LYS_CONTAINER:
4770 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4771 node_compile_spec = lys_compile_node_container;
4772 break;
4773 case LYS_LEAF:
4774 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4775 node_compile_spec = lys_compile_node_leaf;
4776 break;
4777 case LYS_LIST:
4778 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004779 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004780 break;
4781 case LYS_LEAFLIST:
4782 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004783 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004784 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004785 case LYS_CHOICE:
4786 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004787 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004788 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004789 case LYS_ANYXML:
4790 case LYS_ANYDATA:
4791 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004792 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004793 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004794 case LYS_USES:
4795 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004796 default:
4797 LOGINT(ctx->ctx);
4798 return LY_EINT;
4799 }
4800 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4801 node->nodetype = node_p->nodetype;
4802 node->module = ctx->mod;
4803 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004804 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004805
4806 /* config */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004807 if (options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
4808 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004809 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004810 node->flags |= (options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4811 } else if (options & LYSC_OPT_NOTIFICATION) {
4812 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004813 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004814 node->flags |= LYS_CONFIG_R;
4815 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004816 /* config not explicitely set, inherit it from parent */
4817 if (parent) {
4818 node->flags |= parent->flags & LYS_CONFIG_MASK;
4819 } else {
4820 /* default is config true */
4821 node->flags |= LYS_CONFIG_W;
4822 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004823 } else {
4824 /* config set explicitely */
4825 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004826 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004827 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4829 "Configuration node cannot be child of any state data node.");
4830 goto error;
4831 }
Radek Krejci19a96102018-11-15 13:38:09 +01004832
Radek Krejcia6d57732018-11-29 13:40:37 +01004833 /* *list ordering */
4834 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4835 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004836 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
4837 (options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
4838 (options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01004839 node->flags &= ~LYS_ORDBY_MASK;
4840 node->flags |= LYS_ORDBY_SYSTEM;
4841 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4842 /* default ordering is system */
4843 node->flags |= LYS_ORDBY_SYSTEM;
4844 }
4845 }
4846
Radek Krejci19a96102018-11-15 13:38:09 +01004847 /* status - it is not inherited by specification, but it does not make sense to have
4848 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004849 if (!parent || parent->nodetype != LYS_CHOICE) {
4850 /* in case of choice/case's children, postpone the check to the moment we know if
4851 * 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 +01004852 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 +01004853 }
4854
4855 if (!(options & LYSC_OPT_FREE_SP)) {
4856 node->sp = node_p;
4857 }
4858 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004859 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4860 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004861 if (node_p->when) {
4862 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4863 ret = lys_compile_when(ctx, node_p->when, options, when);
4864 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004865 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004866 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004867 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004868 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
4869
4870 /* nodetype-specific part */
4871 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
4872
Radek Krejcife909632019-02-12 15:34:42 +01004873 /* inherit LYS_MAND_TRUE in parent containers */
4874 if (node->flags & LYS_MAND_TRUE) {
4875 lys_compile_mandatory_parents(parent, 1);
4876 }
4877
Radek Krejci19a96102018-11-15 13:38:09 +01004878 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004879 if (parent) {
4880 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004881 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
4882 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004883 if (uses_status) {
4884
4885 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004886 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004887 * it directly gets the same status flags as the choice;
4888 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004889 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004890 node->parent = (struct lysc_node*)cs;
4891 } else { /* other than choice */
4892 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004893 }
Radek Krejci05b774b2019-02-25 13:26:18 +01004894 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 +01004895 } else {
4896 /* top-level element */
4897 if (!ctx->mod->compiled->data) {
4898 ctx->mod->compiled->data = node;
4899 } else {
4900 /* insert at the end of the module's top-level nodes list */
4901 ctx->mod->compiled->data->prev->next = node;
4902 node->prev = ctx->mod->compiled->data->prev;
4903 ctx->mod->compiled->data->prev = node;
4904 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004905 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4906 ctx->mod->compiled->notifs, node->name, node)) {
4907 return LY_EVALID;
4908 }
Radek Krejci19a96102018-11-15 13:38:09 +01004909 }
4910
4911 return LY_SUCCESS;
4912
4913error:
4914 lysc_node_free(ctx->ctx, node);
4915 return ret;
4916}
4917
Radek Krejciccd20f12019-02-15 14:12:27 +01004918static void
4919lysc_disconnect(struct lysc_node *node)
4920{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004921 struct lysc_node *parent, *child, *prev = NULL, *next;
4922 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004923 int remove_cs = 0;
4924
4925 parent = node->parent;
4926
4927 /* parent's first child */
4928 if (!parent) {
4929 return;
4930 }
4931 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004932 cs = (struct lysc_node_case*)node;
4933 } else if (parent->nodetype == LYS_CASE) {
4934 /* disconnecting some node in a case */
4935 cs = (struct lysc_node_case*)parent;
4936 parent = cs->parent;
4937 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4938 if (child == node) {
4939 if (cs->child == child) {
4940 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
4941 /* case with a single child -> remove also the case */
4942 child->parent = NULL;
4943 remove_cs = 1;
4944 } else {
4945 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01004946 }
4947 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004948 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01004949 }
4950 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004951 if (!remove_cs) {
4952 cs = NULL;
4953 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004954 } else if (lysc_node_children(parent, node->flags) == node) {
4955 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004956 }
4957
4958 if (cs) {
4959 if (remove_cs) {
4960 /* cs has only one child which is being also removed */
4961 lysc_disconnect((struct lysc_node*)cs);
4962 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
4963 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01004964 if (((struct lysc_node_choice*)parent)->dflt == cs) {
4965 /* default case removed */
4966 ((struct lysc_node_choice*)parent)->dflt = NULL;
4967 }
4968 if (((struct lysc_node_choice*)parent)->cases == cs) {
4969 /* first case removed */
4970 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
4971 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004972 if (cs->child) {
4973 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
4974 if (cs->child->prev->parent != (struct lysc_node*)cs) {
4975 prev = cs->child->prev;
4976 } /* else all the children are under a single case */
4977 LY_LIST_FOR_SAFE(cs->child, next, child) {
4978 if (child->parent != (struct lysc_node*)cs) {
4979 break;
4980 }
4981 lysc_node_free(node->module->ctx, child);
4982 }
4983 if (prev) {
4984 if (prev->next) {
4985 prev->next = child;
4986 }
4987 if (child) {
4988 child->prev = prev;
4989 } else {
4990 /* link from the first child under the cases */
4991 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
4992 }
4993 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004994 }
4995 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004996 }
4997
4998 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01004999 if (node->prev->next) {
5000 node->prev->next = node->next;
5001 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005002 if (node->next) {
5003 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005004 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005005 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005006 if (child) {
5007 child->prev = node->prev;
5008 }
5009 } else if (((struct lysc_node_choice*)parent)->cases) {
5010 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005011 }
5012}
5013
5014LY_ERR
5015lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options)
5016{
5017 LY_ERR ret = LY_EVALID;
5018 struct ly_set devs_p = {0};
5019 struct ly_set targets = {0};
5020 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005021 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005022 struct lysc_action *rpcs;
5023 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005024 struct lysp_deviation *dev;
5025 struct lysp_deviate *d, **dp_new;
5026 struct lysp_deviate_add *d_add;
5027 struct lysp_deviate_del *d_del;
5028 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005029 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005030 struct lysc_deviation {
5031 const char *nodeid;
5032 struct lysc_node *target; /* target node of the deviation */
5033 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005034 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005035 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5036 } **devs = NULL;
5037 int i;
5038 size_t prefix_len, name_len;
5039 const char *prefix, *name, *nodeid;
5040 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005041 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005042 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005043
5044 /* get all deviations from the module and all its submodules ... */
5045 LY_ARRAY_FOR(mod_p->deviations, u) {
5046 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5047 }
5048 LY_ARRAY_FOR(mod_p->includes, v) {
5049 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5050 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5051 }
5052 }
5053 if (!devs_p.count) {
5054 /* nothing to do */
5055 return LY_SUCCESS;
5056 }
5057
5058 /* ... and group them by the target node */
5059 devs = calloc(devs_p.count, sizeof *devs);
5060 for (u = 0; u < devs_p.count; ++u) {
5061 dev = devs_p.objs[u];
5062
5063 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005064 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5065 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005066 if (target->nodetype == LYS_ACTION) {
5067 /* move the target pointer to input/output to make them different from the action and
5068 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5069 * back to the RPC/action node due to a better compatibility and decision code in this function.
5070 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5071 if (flags & LYSC_OPT_RPC_INPUT) {
5072 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5073 flags |= LYSC_OPT_INTERNAL;
5074 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5075 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5076 flags |= LYSC_OPT_INTERNAL;
5077 }
5078 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005079 /* insert into the set of targets with duplicity detection */
5080 i = ly_set_add(&targets, target, 0);
5081 if (!devs[i]) {
5082 /* new record */
5083 devs[i] = calloc(1, sizeof **devs);
5084 devs[i]->target = target;
5085 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005086 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005087 }
5088 /* add deviates into the deviation's list of deviates */
5089 for (d = dev->deviates; d; d = d->next) {
5090 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5091 *dp_new = d;
5092 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5093 devs[i]->not_supported = 1;
5094 }
5095 }
5096 }
5097
5098 /* MACROS for deviates checking */
5099#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5100 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
5101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
5102 goto cleanup; \
5103 }
5104
5105#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5106 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
5107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
5108 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
5109 goto cleanup; \
5110 }
5111
5112#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005113 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5115 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
5116 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5117 goto cleanup; \
5118 }
5119
Radek Krejci551b12c2019-02-19 16:11:21 +01005120#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5121 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5122 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5123 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
5124 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5125 goto cleanup; \
5126 }
5127
Radek Krejciccd20f12019-02-15 14:12:27 +01005128#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5129 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5130 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5131 goto cleanup; \
5132 }
5133
Radek Krejci551b12c2019-02-19 16:11:21 +01005134#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5135 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5137 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5138 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5139 goto cleanup; \
5140 }
5141
Radek Krejciccd20f12019-02-15 14:12:27 +01005142#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5143 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5144 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5145 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5146 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5147 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5148 goto cleanup; \
5149 } \
5150 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5151 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5152
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005153#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5154 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5155 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5156 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5157 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 +01005158 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005159 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005160 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5161 "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 +01005162 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005163 goto cleanup; \
5164 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005165 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5166 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5167 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5168 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5169 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005170 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005171 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5172 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5173 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005174 }
5175
5176 /* apply deviations */
5177 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005178 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5179 /* fix the target pointer in case of RPC's/action's input/output */
5180 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5181 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5182 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5183 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5184 }
5185 }
5186
Radek Krejciccd20f12019-02-15 14:12:27 +01005187 /* not-supported */
5188 if (devs[u]->not_supported) {
5189 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5190 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5191 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5192 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005193
5194#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5195 if (devs[u]->target->parent) { \
5196 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5197 } else { \
5198 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5199 } \
5200 LY_ARRAY_FOR(ARRAY, x) { \
5201 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5202 } \
5203 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5204 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5205 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5206 LY_ARRAY_DECREMENT(ARRAY); \
5207 }
5208
Radek Krejcif538ce52019-03-05 10:46:14 +01005209 if (devs[u]->target->nodetype == LYS_ACTION) {
5210 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5211 /* remove RPC's/action's input */
5212 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5213 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005214 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5215 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005216 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5217 /* remove RPC's/action's output */
5218 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5219 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005220 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5221 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005222 } else {
5223 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005224 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005225 }
5226 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005227 /* remove Notification */
5228 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005229 } else {
5230 /* remove the target node */
5231 lysc_disconnect(devs[u]->target);
5232 lysc_node_free(ctx->ctx, devs[u]->target);
5233 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005234
5235 continue;
5236 }
5237
5238 /* list of deviates (not-supported is not present in the list) */
5239 LY_ARRAY_FOR(devs[u]->deviates, v) {
5240 d = devs[u]->deviates[v];
5241
5242 switch (d->mod) {
5243 case LYS_DEV_ADD:
5244 d_add = (struct lysp_deviate_add*)d;
5245 /* [units-stmt] */
5246 if (d_add->units) {
5247 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5248 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5249
5250 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5251 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5252 }
5253
5254 /* *must-stmt */
5255 if (d_add->musts) {
5256 switch (devs[u]->target->nodetype) {
5257 case LYS_CONTAINER:
5258 case LYS_LIST:
5259 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5260 options, x, lys_compile_must, ret, cleanup);
5261 break;
5262 case LYS_LEAF:
5263 case LYS_LEAFLIST:
5264 case LYS_ANYDATA:
5265 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5266 options, x, lys_compile_must, ret, cleanup);
5267 break;
5268 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005269 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5270 options, x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005271 break;
5272 case LYS_ACTION:
5273 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5274 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5275 options, x, lys_compile_must, ret, cleanup);
5276 break;
5277 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5278 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5279 options, x, lys_compile_must, ret, cleanup);
5280 break;
5281 }
5282 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005283 default:
5284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005285 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005286 goto cleanup;
5287 }
5288 }
5289
5290 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005291 if (d_add->uniques) {
5292 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5293 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5294 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005295
5296 /* *default-stmt */
5297 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005298 switch (devs[u]->target->nodetype) {
5299 case LYS_LEAF:
5300 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5301 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5302 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5303 /* first, remove the default value taken from the type */
5304 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5305 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5306 }
5307 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005308 /* mark the new default values as leaf's own */
5309 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005310 break;
5311 case LYS_LEAFLIST:
5312 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5313 /* first, remove the default value taken from the type */
5314 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5315 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5316 }
5317 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5318 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5319 }
5320 /* add new default value(s) */
5321 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5322 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5323 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5324 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5325 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5326 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5327 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005328 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005329 devs[u]->target->flags |= LYS_SET_DFLT;
5330 break;
5331 case LYS_CHOICE:
5332 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5333 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5334 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5335 * to allow making the default case even the augmented case from the deviating module */
5336 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5337 (struct lysc_node_choice*)devs[u]->target)) {
5338 goto cleanup;
5339 }
5340 break;
5341 default:
5342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005343 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005344 goto cleanup;
5345 }
5346 }
5347
5348 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005349 if (d_add->flags & LYS_CONFIG_MASK) {
5350 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5352 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5353 goto cleanup;
5354 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005355 if (devs[u]->flags) {
5356 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5357 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5358 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005359 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5360 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5361 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5362 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5363 goto cleanup;
5364 }
5365 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5366 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005367
5368 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005369 if (d_add->flags & LYS_MAND_MASK) {
5370 if (devs[u]->target->flags & LYS_MAND_MASK) {
5371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5372 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5373 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5374 goto cleanup;
5375 }
5376 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5377 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005378
5379 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005380 if (d_add->flags & LYS_SET_MIN) {
5381 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5382 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5383 /* change value */
5384 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5385 } else if (devs[u]->target->nodetype == LYS_LIST) {
5386 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5387 /* change value */
5388 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5389 } else {
5390 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5391 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5392 goto cleanup;
5393 }
5394 if (d_add->min) {
5395 devs[u]->target->flags |= LYS_MAND_TRUE;
5396 }
5397 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005398
5399 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005400 if (d_add->flags & LYS_SET_MAX) {
5401 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5402 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5403 /* change value */
5404 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5405 } else if (devs[u]->target->nodetype == LYS_LIST) {
5406 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5407 /* change value */
5408 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5409 } else {
5410 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5411 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5412 goto cleanup;
5413 }
5414 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005415
5416 break;
5417 case LYS_DEV_DELETE:
5418 d_del = (struct lysp_deviate_del*)d;
5419
5420 /* [units-stmt] */
5421 if (d_del->units) {
5422 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5423 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5424 }
5425
5426 /* *must-stmt */
5427 if (d_del->musts) {
5428 switch (devs[u]->target->nodetype) {
5429 case LYS_CONTAINER:
5430 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005431 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005432 break;
5433 case LYS_LEAF:
5434 case LYS_LEAFLIST:
5435 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005436 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005437 break;
5438 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005439 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005440 break;
5441 case LYS_ACTION:
5442 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5443 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5444 break;
5445 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5446 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5447 break;
5448 }
5449 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005450 default:
5451 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005452 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005453 goto cleanup;
5454 }
5455 }
5456
5457 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005458 if (d_del->uniques) {
5459 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5460 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5461 LY_ARRAY_FOR(d_del->uniques, x) {
5462 LY_ARRAY_FOR(list->uniques, z) {
5463 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5464 nodeid = strpbrk(name, " \t\n");
5465 if (nodeid) {
5466 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5467 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5468 break;
5469 }
5470 while (isspace(*nodeid)) {
5471 ++nodeid;
5472 }
5473 } else {
5474 if (strcmp(name, list->uniques[z][y]->name)) {
5475 break;
5476 }
5477 }
5478 }
5479 if (!name) {
5480 /* complete match - remove the unique */
5481 LY_ARRAY_DECREMENT(list->uniques);
5482 LY_ARRAY_FREE(list->uniques[z]);
5483 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5484 --z;
5485 break;
5486 }
5487 }
5488 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5489 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5490 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5491 devs[u]->nodeid, d_del->uniques[x]);
5492 goto cleanup;
5493 }
5494 }
5495 if (!LY_ARRAY_SIZE(list->uniques)) {
5496 LY_ARRAY_FREE(list->uniques);
5497 list->uniques = NULL;
5498 }
5499 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005500
5501 /* *default-stmt */
5502 if (d_del->dflts) {
5503 switch (devs[u]->target->nodetype) {
5504 case LYS_LEAF:
5505 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5506 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5507 dflt, "deleting", "default", d_del->dflts[0]);
5508
5509 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005510 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005511 break;
5512 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005513 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005514 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5515 devs[u]->target->flags &= ~LYS_SET_DFLT;
5516 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005517 break;
5518 case LYS_CHOICE:
5519 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5520 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5521 nodeid = d_del->dflts[0];
5522 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5523 if (prefix) {
5524 /* use module prefixes from the deviation module to match the module of the default case */
5525 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5526 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5527 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5528 "The prefix does not match any imported module of the deviation module.",
5529 devs[u]->nodeid, d_del->dflts[0]);
5530 goto cleanup;
5531 }
5532 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5533 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5534 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5535 "The prefix does not match the default case's module.",
5536 devs[u]->nodeid, d_del->dflts[0]);
5537 goto cleanup;
5538 }
5539 }
5540 /* else {
5541 * strictly, the default prefix would point to the deviation module, but the value should actually
5542 * match the default string in the original module (usually unprefixed), so in this case we do not check
5543 * the module of the default case, just matching its name */
5544 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5545 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5546 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5547 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5548 goto cleanup;
5549 }
5550 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5551 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5552 break;
5553 default:
5554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005555 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005556 goto cleanup;
5557 }
5558 }
5559
5560 break;
5561 case LYS_DEV_REPLACE:
5562 d_rpl = (struct lysp_deviate_rpl*)d;
5563
5564 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005565 if (d_rpl->type) {
5566 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5567 /* type is mandatory, so checking for its presence is not necessary */
5568 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
5569 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup);
5570 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005571
5572 /* [units-stmt] */
5573 if (d_rpl->units) {
5574 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5575 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5576 units, "replacing", "units", d_rpl->units);
5577
5578 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5579 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5580 }
5581
5582 /* [default-stmt] */
5583 if (d_rpl->dflt) {
5584 switch (devs[u]->target->nodetype) {
5585 case LYS_LEAF:
5586 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5587 dflt, "replacing", "default", d_rpl->dflt);
5588
5589 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5590 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5591 break;
5592 case LYS_CHOICE:
5593 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5594 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5595 (struct lysc_node_choice*)devs[u]->target)) {
5596 goto cleanup;
5597 }
5598 break;
5599 default:
5600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005601 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005602 goto cleanup;
5603 }
5604 }
5605
5606 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005607 if (d_rpl->flags & LYS_CONFIG_MASK) {
5608 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5610 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5611 goto cleanup;
5612 }
5613 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5615 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5616 goto cleanup;
5617 }
5618 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5619 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005620
5621 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005622 if (d_rpl->flags & LYS_MAND_MASK) {
5623 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5625 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5626 goto cleanup;
5627 }
5628 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5629 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005630
5631 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005632 if (d_rpl->flags & LYS_SET_MIN) {
5633 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5634 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5635 /* change value */
5636 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5637 } else if (devs[u]->target->nodetype == LYS_LIST) {
5638 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5639 /* change value */
5640 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5641 } else {
5642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5643 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5644 goto cleanup;
5645 }
5646 if (d_rpl->min) {
5647 devs[u]->target->flags |= LYS_MAND_TRUE;
5648 }
5649 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005650
5651 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005652 if (d_rpl->flags & LYS_SET_MAX) {
5653 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5654 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5655 /* change value */
5656 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5657 } else if (devs[u]->target->nodetype == LYS_LIST) {
5658 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5659 /* change value */
5660 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5661 } else {
5662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5663 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5664 goto cleanup;
5665 }
5666 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005667
5668 break;
5669 default:
5670 LOGINT(ctx->ctx);
5671 goto cleanup;
5672 }
5673 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005674
Radek Krejci33f72892019-02-21 10:36:58 +01005675 /* final check when all deviations of a single target node are applied */
5676
Radek Krejci551b12c2019-02-19 16:11:21 +01005677 /* check min-max compatibility */
5678 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5679 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5680 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5681 } else if (devs[u]->target->nodetype == LYS_LIST) {
5682 min = ((struct lysc_node_list*)devs[u]->target)->min;
5683 max = ((struct lysc_node_list*)devs[u]->target)->max;
5684 } else {
5685 min = max = 0;
5686 }
5687 if (min > max) {
5688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5689 "after deviation (%s): min value %u is bigger than max value %u.",
5690 devs[u]->nodeid, min, max);
5691 goto cleanup;
5692 }
5693
5694 /* check mandatory - default compatibility */
5695 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5696 && (devs[u]->target->flags & LYS_SET_DFLT)
5697 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5699 "Invalid deviation (%s) combining default value and mandatory %s.",
5700 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5701 goto cleanup;
5702 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5703 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5704 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5706 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5707 goto cleanup;
5708 }
5709 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5710 /* mandatory node under a default case */
5711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5712 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5713 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5714 goto cleanup;
5715 }
Radek Krejci33f72892019-02-21 10:36:58 +01005716
5717 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005718 }
5719
5720 ret = LY_SUCCESS;
5721
5722cleanup:
5723 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5724 LY_ARRAY_FREE(devs[u]->deviates);
5725 free(devs[u]);
5726 }
5727 free(devs);
5728 ly_set_erase(&targets, NULL);
5729 ly_set_erase(&devs_p, NULL);
5730
5731 return ret;
5732}
5733
Radek Krejcib56c7502019-02-13 14:19:54 +01005734/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005735 * @brief Compile the given YANG submodule into the main module.
5736 * @param[in] ctx Compile context
5737 * @param[in] inc Include structure from the main module defining the submodule.
5738 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
5739 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5740 */
5741LY_ERR
5742lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
5743{
5744 unsigned int u;
5745 LY_ERR ret = LY_SUCCESS;
5746 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005747 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005748 struct lysc_module *mainmod = ctx->mod->compiled;
5749
Radek Krejci0af46292019-01-11 16:02:31 +01005750 if (!mainmod->mod->off_features) {
5751 /* features are compiled directly into the compiled module structure,
5752 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5753 * The features compilation is finished in the main module (lys_compile()). */
5754 ret = lys_feature_precompile(ctx->ctx, submod->features,
5755 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5756 LY_CHECK_GOTO(ret, error);
5757 }
5758
Radek Krejcid05cbd92018-12-05 14:26:40 +01005759 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
5760
Radek Krejci8cce8532019-03-05 11:27:45 +01005761 /* TODO data nodes */
5762
5763 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005764 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, options, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01005765
Radek Krejcid05cbd92018-12-05 14:26:40 +01005766error:
5767 return ret;
5768}
5769
Radek Krejci19a96102018-11-15 13:38:09 +01005770LY_ERR
5771lys_compile(struct lys_module *mod, int options)
5772{
5773 struct lysc_ctx ctx = {0};
5774 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005775 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005776 struct lysp_module *sp;
5777 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005778 struct lysp_augment **augments = NULL;
5779 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005780 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005781 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005782
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005783 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005784
5785 if (!mod->implemented) {
5786 /* just imported modules are not compiled */
5787 return LY_SUCCESS;
5788 }
5789
Radek Krejci19a96102018-11-15 13:38:09 +01005790 sp = mod->parsed;
5791
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005792 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005793 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005794 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005795
5796 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005797 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5798 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005799
Radek Krejci19a96102018-11-15 13:38:09 +01005800 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005801 LY_ARRAY_FOR(sp->includes, u) {
5802 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
5803 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5804 }
Radek Krejci0af46292019-01-11 16:02:31 +01005805 if (mod->off_features) {
5806 /* there is already precompiled array of features */
5807 mod_c->features = mod->off_features;
5808 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005809 } else {
5810 /* features are compiled directly into the compiled module structure,
5811 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
5812 ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features);
5813 LY_CHECK_GOTO(ret, error);
5814 }
5815 /* finish feature compilation, not only for the main module, but also for the submodules.
5816 * Due to possible forward references, it must be done when all the features (including submodules)
5817 * are present. */
5818 LY_ARRAY_FOR(sp->features, u) {
5819 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
5820 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5821 }
5822 LY_ARRAY_FOR(sp->includes, v) {
5823 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
5824 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
5825 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5826 }
5827 }
5828
Radek Krejcid05cbd92018-12-05 14:26:40 +01005829 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005830 if (sp->identities) {
5831 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5832 }
5833
Radek Krejci95710c92019-02-11 15:49:55 +01005834 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005835 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005836 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005837 LY_CHECK_GOTO(ret, error);
5838 }
Radek Krejci95710c92019-02-11 15:49:55 +01005839
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005840 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 +02005841 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 +01005842
Radek Krejci95710c92019-02-11 15:49:55 +01005843 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005844 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005845 LY_CHECK_GOTO(ret, error);
5846 LY_ARRAY_FOR(augments, u) {
5847 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
5848 LY_CHECK_GOTO(ret, error);
5849 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005850
5851 /* deviations */
5852 ret = lys_compile_deviations(&ctx, sp, options);
5853 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005854
5855 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
5856
Radek Krejcia3045382018-11-22 14:30:31 +01005857 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005858 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5859 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5860 * 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 +01005861 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005862 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005863 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5864 if (type->basetype == LY_TYPE_LEAFREF) {
5865 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005866 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 +01005867 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005868 } else if (type->basetype == LY_TYPE_UNION) {
5869 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5870 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5871 /* validate the path */
5872 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5873 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5874 LY_CHECK_GOTO(ret, error);
5875 }
5876 }
Radek Krejcia3045382018-11-22 14:30:31 +01005877 }
5878 }
5879 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005880 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005881 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005882 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5883 if (type->basetype == LY_TYPE_LEAFREF) {
5884 /* store pointer to the real type */
5885 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5886 typeiter->basetype == LY_TYPE_LEAFREF;
5887 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5888 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005889 } else if (type->basetype == LY_TYPE_UNION) {
5890 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5891 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5892 /* store pointer to the real type */
5893 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5894 typeiter->basetype == LY_TYPE_LEAFREF;
5895 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5896 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5897 }
5898 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005899 }
5900 }
5901 }
Radek Krejcia3045382018-11-22 14:30:31 +01005902 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005903 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005904 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005905
Radek Krejci19a96102018-11-15 13:38:09 +01005906 if (options & LYSC_OPT_FREE_SP) {
5907 lysp_module_free(mod->parsed);
5908 ((struct lys_module*)mod)->parsed = NULL;
5909 }
5910
Radek Krejci95710c92019-02-11 15:49:55 +01005911 if (!(options & LYSC_OPT_INTERNAL)) {
5912 /* remove flag of the modules implemented by dependency */
5913 for (u = 0; u < ctx.ctx->list.count; ++u) {
5914 m = ctx.ctx->list.objs[u];
5915 if (m->implemented == 2) {
5916 m->implemented = 1;
5917 }
5918 }
5919 }
5920
Radek Krejci19a96102018-11-15 13:38:09 +01005921 ((struct lys_module*)mod)->compiled = mod_c;
5922 return LY_SUCCESS;
5923
5924error:
Radek Krejci95710c92019-02-11 15:49:55 +01005925 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01005926 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005927 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005928 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01005929 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005930 mod->compiled = NULL;
5931
5932 /* revert compilation of modules implemented by dependency */
5933 for (u = 0; u < ctx.ctx->list.count; ++u) {
5934 m = ctx.ctx->list.objs[u];
5935 if (m->implemented == 2) {
5936 /* revert features list to the precompiled state */
5937 lys_feature_precompile_revert(&ctx, m);
5938 /* mark module as imported-only / not-implemented */
5939 m->implemented = 0;
5940 /* free the compiled version of the module */
5941 lysc_module_free(m->compiled, NULL);
5942 m->compiled = NULL;
5943 }
5944 }
5945
Radek Krejci19a96102018-11-15 13:38:09 +01005946 return ret;
5947}