blob: c2a6a544aa054699e870eb1c994ee8eed5feab9b [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 Krejcic8b31002019-01-08 10:24:45 +0100641 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
642 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
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 Krejcia3045382018-11-22 14:30:31 +01002020 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002021 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002022 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002023 src_node = NULL;
2024 if (context_node->keys) {
2025 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2026 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2027 src_node = (const struct lysc_node*)context_node->keys[u];
2028 break;
2029 }
2030 }
2031 }
Radek Krejcia3045382018-11-22 14:30:31 +01002032 if (!src_node) {
2033 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002034 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002035 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002036 goto cleanup;
2037 }
Radek Krejcia3045382018-11-22 14:30:31 +01002038
2039 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002040 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002041 i = ly_set_add(&keys, (void*)src_node, 0);
2042 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002043 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002045 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002046 *predicate - start, start, src_node->name);
2047 goto cleanup;
2048 }
2049
2050 /* destination */
2051 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002052 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002053
2054 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2055 if (strncmp(path_key_expr, "current()", 9)) {
2056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2057 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2058 *predicate - start, start);
2059 goto cleanup;
2060 }
2061 path_key_expr += 9;
2062 while (isspace(*path_key_expr)) {
2063 ++path_key_expr;
2064 }
2065
2066 if (*path_key_expr != '/') {
2067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2068 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2069 *predicate - start, start);
2070 goto cleanup;
2071 }
2072 ++path_key_expr;
2073 while (isspace(*path_key_expr)) {
2074 ++path_key_expr;
2075 }
2076
2077 /* rel-path-keyexpr:
2078 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2079 while (!strncmp(path_key_expr, "..", 2)) {
2080 ++dest_parent_times;
2081 path_key_expr += 2;
2082 while (isspace(*path_key_expr)) {
2083 ++path_key_expr;
2084 }
2085 if (*path_key_expr != '/') {
2086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2087 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2088 *predicate - start, start);
2089 goto cleanup;
2090 }
2091 ++path_key_expr;
2092 while (isspace(*path_key_expr)) {
2093 ++path_key_expr;
2094 }
2095
2096 /* path is supposed to be evaluated in data tree, so we have to skip
2097 * all schema nodes that cannot be instantiated in data tree */
2098 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2099 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2100 *predicate - start, start);
2101 }
2102 if (!dest_parent_times) {
2103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2104 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2105 *predicate - start, start);
2106 goto cleanup;
2107 }
2108 if (path_key_expr == pke_end) {
2109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2110 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2111 *predicate - start, start);
2112 goto cleanup;
2113 }
2114
2115 while(path_key_expr != pke_end) {
2116 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002118 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2119 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002120 goto cleanup;
2121 }
2122
2123 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002124 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002125 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002126 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002127 }
2128 if (!mod) {
2129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2130 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
2131 *predicate - start, start, dst_len, dst);
2132 goto cleanup;
2133 }
2134
2135 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2136 if (!dst_node) {
2137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2138 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
2139 *predicate - start, start, path_key_expr - pke_start, pke_start);
2140 goto cleanup;
2141 }
2142 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002143 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 +01002144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002145 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002146 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2147 goto cleanup;
2148 }
2149 }
2150
2151 ret = LY_SUCCESS;
2152cleanup:
2153 ly_set_erase(&keys, NULL);
2154 return ret;
2155}
2156
2157/**
2158 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2159 *
2160 * path-arg = absolute-path / relative-path
2161 * absolute-path = 1*("/" (node-identifier *path-predicate))
2162 * relative-path = 1*(".." "/") descendant-path
2163 *
2164 * @param[in,out] path Path to parse.
2165 * @param[out] prefix Prefix of the token, NULL if there is not any.
2166 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2167 * @param[out] name Name of the token.
2168 * @param[out] nam_len Length of the name.
2169 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2170 * must not be changed between consecutive calls. -1 if the
2171 * path is absolute.
2172 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2173 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2174 */
2175static LY_ERR
2176lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2177 int *parent_times, int *has_predicate)
2178{
2179 int par_times = 0;
2180
2181 assert(path && *path);
2182 assert(parent_times);
2183 assert(prefix);
2184 assert(prefix_len);
2185 assert(name);
2186 assert(name_len);
2187 assert(has_predicate);
2188
2189 *prefix = NULL;
2190 *prefix_len = 0;
2191 *name = NULL;
2192 *name_len = 0;
2193 *has_predicate = 0;
2194
2195 if (!*parent_times) {
2196 if (!strncmp(*path, "..", 2)) {
2197 *path += 2;
2198 ++par_times;
2199 while (!strncmp(*path, "/..", 3)) {
2200 *path += 3;
2201 ++par_times;
2202 }
2203 }
2204 if (par_times) {
2205 *parent_times = par_times;
2206 } else {
2207 *parent_times = -1;
2208 }
2209 }
2210
2211 if (**path != '/') {
2212 return LY_EINVAL;
2213 }
2214 /* skip '/' */
2215 ++(*path);
2216
2217 /* node-identifier ([prefix:]name) */
2218 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2219
2220 if ((**path == '/' && (*path)[1]) || !**path) {
2221 /* path continues by another token or this is the last token */
2222 return LY_SUCCESS;
2223 } else if ((*path)[0] != '[') {
2224 /* unexpected character */
2225 return LY_EINVAL;
2226 } else {
2227 /* predicate starting with [ */
2228 *has_predicate = 1;
2229 return LY_SUCCESS;
2230 }
2231}
2232
2233/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002234 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2235 *
2236 * The set of features used for target must be a subset of features used for the leafref.
2237 * This is not a perfect, we should compare the truth tables but it could require too much resources
2238 * and RFC 7950 does not require it explicitely, so we simplify that.
2239 *
2240 * @param[in] refnode The leafref node.
2241 * @param[in] target Tha target node of the leafref.
2242 * @return LY_SUCCESS or LY_EVALID;
2243 */
2244static LY_ERR
2245lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2246{
2247 LY_ERR ret = LY_EVALID;
2248 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002249 unsigned int u, v, count;
2250 struct ly_set features = {0};
2251
2252 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002253 if (iter->iffeatures) {
2254 LY_ARRAY_FOR(iter->iffeatures, u) {
2255 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2256 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002257 }
2258 }
2259 }
2260 }
2261
2262 /* we should have, in features set, a superset of features applicable to the target node.
2263 * So when adding features applicable to the target into the features set, we should not be
2264 * able to actually add any new feature, otherwise it is not a subset of features applicable
2265 * to the leafref itself. */
2266 count = features.count;
2267 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002268 if (iter->iffeatures) {
2269 LY_ARRAY_FOR(iter->iffeatures, u) {
2270 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2271 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002272 /* new feature was added (or LY_EMEM) */
2273 goto cleanup;
2274 }
2275 }
2276 }
2277 }
2278 }
2279 ret = LY_SUCCESS;
2280
2281cleanup:
2282 ly_set_erase(&features, NULL);
2283 return ret;
2284}
2285
2286/**
Radek Krejcia3045382018-11-22 14:30:31 +01002287 * @brief Validate the leafref path.
2288 * @param[in] ctx Compile context
2289 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002290 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002291 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2292 */
2293static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002294lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002295{
2296 const struct lysc_node *node = NULL, *parent = NULL;
2297 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002298 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002299 const char *id, *prefix, *name;
2300 size_t prefix_len, name_len;
2301 int parent_times = 0, has_predicate;
2302 unsigned int iter, u;
2303 LY_ERR ret = LY_SUCCESS;
2304
2305 assert(ctx);
2306 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002307 assert(leafref);
2308
Radek Krejcia3045382018-11-22 14:30:31 +01002309 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002310 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002311 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2312 if (!iter) { /* first iteration */
2313 /* precess ".." in relative paths */
2314 if (parent_times > 0) {
2315 /* move from the context node */
2316 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2317 /* path is supposed to be evaluated in data tree, so we have to skip
2318 * all schema nodes that cannot be instantiated in data tree */
2319 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002320 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002321 }
2322 }
2323 }
2324
2325 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002326 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002327 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002328 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002329 }
2330 if (!mod) {
2331 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002332 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2333 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002334 return LY_EVALID;
2335 }
Radek Krejci3d929562019-02-21 11:25:55 +01002336 if (!mod->implemented) {
2337 /* make the module implemented */
2338 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2339 }
Radek Krejcia3045382018-11-22 14:30:31 +01002340
2341 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2342 if (!node) {
2343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002344 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002345 return LY_EVALID;
2346 }
2347 parent = node;
2348
2349 if (has_predicate) {
2350 /* we have predicate, so the current result must be list */
2351 if (node->nodetype != LYS_LIST) {
2352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2353 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002354 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002355 return LY_EVALID;
2356 }
2357
Radek Krejcibade82a2018-12-05 10:13:48 +01002358 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2359 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002360 }
2361
2362 ++iter;
2363 }
2364 if (ret) {
2365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002366 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002367 return LY_EVALID;
2368 }
2369
2370 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2372 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002373 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002374 return LY_EVALID;
2375 }
2376
2377 /* check status */
2378 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2379 return LY_EVALID;
2380 }
2381
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002382 /* check config */
2383 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2384 if (node->flags & LYS_CONFIG_R) {
2385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2386 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2387 leafref->path);
2388 return LY_EVALID;
2389 }
2390 }
2391
Radek Krejci412ddfa2018-11-23 11:44:11 +01002392 /* store the target's type and check for circular chain of leafrefs */
2393 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2394 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2395 if (type == (struct lysc_type*)leafref) {
2396 /* circular chain detected */
2397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2398 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2399 return LY_EVALID;
2400 }
2401 }
2402
Radek Krejci58d171e2018-11-23 13:50:55 +01002403 /* check if leafref and its target are under common if-features */
2404 if (lys_compile_leafref_features_validate(startnode, node)) {
2405 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2406 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2407 leafref->path);
2408 return LY_EVALID;
2409 }
2410
Radek Krejcia3045382018-11-22 14:30:31 +01002411 return LY_SUCCESS;
2412}
2413
Radek Krejcicdfecd92018-11-26 11:27:32 +01002414static 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 +01002415 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002416/**
2417 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2418 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002419 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2420 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2421 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2422 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002423 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002424 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002425 * @param[in] basetype Base YANG built-in type of the type to compile.
2426 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2427 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2428 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2429 * @param[out] type Newly created type structure with the filled information about the type.
2430 * @return LY_ERR value.
2431 */
Radek Krejci19a96102018-11-15 13:38:09 +01002432static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002433lys_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,
2434 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2435 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002436{
2437 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002438 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002439 struct lysc_type_bin *bin;
2440 struct lysc_type_num *num;
2441 struct lysc_type_str *str;
2442 struct lysc_type_bits *bits;
2443 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002444 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002445 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002446 struct lysc_type_union *un, *un_aux;
2447 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002448
2449 switch (basetype) {
2450 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002451 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002452
2453 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002454 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002455 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002456 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2457 LY_CHECK_RET(ret);
2458 if (!tpdfname) {
2459 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2460 options, u, lys_compile_ext, ret, done);
2461 }
2462 }
2463
2464 if (tpdfname) {
2465 type_p->compiled = *type;
2466 *type = calloc(1, sizeof(struct lysc_type_bin));
2467 }
2468 break;
2469 case LY_TYPE_BITS:
2470 /* RFC 7950 9.7 - bits */
2471 bits = (struct lysc_type_bits*)(*type);
2472 if (type_p->bits) {
2473 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2474 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2475 (struct lysc_type_enum_item**)&bits->bits);
2476 LY_CHECK_RET(ret);
2477 }
2478
Radek Krejci555cb5b2018-11-16 14:54:33 +01002479 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002480 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002481 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002483 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002485 free(*type);
2486 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002487 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002488 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002489 }
2490
2491 if (tpdfname) {
2492 type_p->compiled = *type;
2493 *type = calloc(1, sizeof(struct lysc_type_bits));
2494 }
2495 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002496 case LY_TYPE_DEC64:
2497 dec = (struct lysc_type_dec*)(*type);
2498
2499 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002500 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002501 if (!type_p->fraction_digits) {
2502 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002504 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002506 free(*type);
2507 *type = NULL;
2508 }
2509 return LY_EVALID;
2510 }
2511 } else if (type_p->fraction_digits) {
2512 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002513 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2515 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002516 tpdfname);
2517 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2519 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002520 free(*type);
2521 *type = NULL;
2522 }
2523 return LY_EVALID;
2524 }
2525 dec->fraction_digits = type_p->fraction_digits;
2526
2527 /* RFC 7950 9.2.4 - range */
2528 if (type_p->range) {
2529 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2530 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2531 LY_CHECK_RET(ret);
2532 if (!tpdfname) {
2533 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2534 options, u, lys_compile_ext, ret, done);
2535 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002536 }
2537
2538 if (tpdfname) {
2539 type_p->compiled = *type;
2540 *type = calloc(1, sizeof(struct lysc_type_dec));
2541 }
2542 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002543 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002544 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002545
2546 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002547 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002548 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002549 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2550 LY_CHECK_RET(ret);
2551 if (!tpdfname) {
2552 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2553 options, u, lys_compile_ext, ret, done);
2554 }
2555 } else if (base && ((struct lysc_type_str*)base)->length) {
2556 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2557 }
2558
2559 /* RFC 7950 9.4.5 - pattern */
2560 if (type_p->patterns) {
2561 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2562 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2563 LY_CHECK_RET(ret);
2564 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2565 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2566 }
2567
2568 if (tpdfname) {
2569 type_p->compiled = *type;
2570 *type = calloc(1, sizeof(struct lysc_type_str));
2571 }
2572 break;
2573 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002574 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002575
2576 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002577 if (type_p->enums) {
2578 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2579 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2580 LY_CHECK_RET(ret);
2581 }
2582
Radek Krejci555cb5b2018-11-16 14:54:33 +01002583 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002585 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002587 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002589 free(*type);
2590 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002592 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 }
2594
2595 if (tpdfname) {
2596 type_p->compiled = *type;
2597 *type = calloc(1, sizeof(struct lysc_type_enum));
2598 }
2599 break;
2600 case LY_TYPE_INT8:
2601 case LY_TYPE_UINT8:
2602 case LY_TYPE_INT16:
2603 case LY_TYPE_UINT16:
2604 case LY_TYPE_INT32:
2605 case LY_TYPE_UINT32:
2606 case LY_TYPE_INT64:
2607 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002608 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002609
2610 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002611 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002612 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002613 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2614 LY_CHECK_RET(ret);
2615 if (!tpdfname) {
2616 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2617 options, u, lys_compile_ext, ret, done);
2618 }
2619 }
2620
2621 if (tpdfname) {
2622 type_p->compiled = *type;
2623 *type = calloc(1, sizeof(struct lysc_type_num));
2624 }
2625 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002626 case LY_TYPE_IDENT:
2627 idref = (struct lysc_type_identityref*)(*type);
2628
2629 /* RFC 7950 9.10.2 - base */
2630 if (type_p->bases) {
2631 if (base) {
2632 /* only the directly derived identityrefs can contain base specification */
2633 if (tpdfname) {
2634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002635 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002636 tpdfname);
2637 } else {
2638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002639 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002640 free(*type);
2641 *type = NULL;
2642 }
2643 return LY_EVALID;
2644 }
2645 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2646 LY_CHECK_RET(ret);
2647 }
2648
2649 if (!base && !type_p->flags) {
2650 /* type derived from identityref built-in type must contain at least one base */
2651 if (tpdfname) {
2652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2653 } else {
2654 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2655 free(*type);
2656 *type = NULL;
2657 }
2658 return LY_EVALID;
2659 }
2660
2661 if (tpdfname) {
2662 type_p->compiled = *type;
2663 *type = calloc(1, sizeof(struct lysc_type_identityref));
2664 }
2665 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002666 case LY_TYPE_LEAFREF:
2667 /* RFC 7950 9.9.3 - require-instance */
2668 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002669 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002670 if (tpdfname) {
2671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2672 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2673 } else {
2674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2675 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2676 free(*type);
2677 *type = NULL;
2678 }
2679 return LY_EVALID;
2680 }
Radek Krejcia3045382018-11-22 14:30:31 +01002681 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002682 } else if (base) {
2683 /* inherit */
2684 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002685 } else {
2686 /* default is true */
2687 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2688 }
2689 if (type_p->path) {
2690 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002691 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002692 } else if (base) {
2693 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002694 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002695 } else if (tpdfname) {
2696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2697 return LY_EVALID;
2698 } else {
2699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2700 free(*type);
2701 *type = NULL;
2702 return LY_EVALID;
2703 }
2704 if (tpdfname) {
2705 type_p->compiled = *type;
2706 *type = calloc(1, sizeof(struct lysc_type_leafref));
2707 }
2708 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002709 case LY_TYPE_INST:
2710 /* RFC 7950 9.9.3 - require-instance */
2711 if (type_p->flags & LYS_SET_REQINST) {
2712 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2713 } else {
2714 /* default is true */
2715 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2716 }
2717
2718 if (tpdfname) {
2719 type_p->compiled = *type;
2720 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2721 }
2722 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002723 case LY_TYPE_UNION:
2724 un = (struct lysc_type_union*)(*type);
2725
2726 /* RFC 7950 7.4 - type */
2727 if (type_p->types) {
2728 if (base) {
2729 /* only the directly derived union can contain types specification */
2730 if (tpdfname) {
2731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2732 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2733 tpdfname);
2734 } else {
2735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2736 "Invalid type substatement for the type not directly derived from union built-in type.");
2737 free(*type);
2738 *type = NULL;
2739 }
2740 return LY_EVALID;
2741 }
2742 /* compile the type */
2743 additional = 0;
2744 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2745 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002746 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 +01002747 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2748 /* add space for additional types from the union subtype */
2749 un_aux = (struct lysc_type_union *)un->types[u + additional];
2750 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)));
2751 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2752 un->types = (void*)((uint32_t*)(p) + 1);
2753
2754 /* copy subtypes of the subtype union */
2755 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2756 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2757 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2758 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2759 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2760 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2761 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2762 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2763 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2764 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2765 /* TODO extensions */
2766
2767 } else {
2768 un->types[u + additional] = un_aux->types[v];
2769 ++un_aux->types[v]->refcount;
2770 }
2771 ++additional;
2772 LY_ARRAY_INCREMENT(un->types);
2773 }
2774 /* compensate u increment in main loop */
2775 --additional;
2776
2777 /* free the replaced union subtype */
2778 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2779 } else {
2780 LY_ARRAY_INCREMENT(un->types);
2781 }
2782 LY_CHECK_RET(ret);
2783 }
2784 }
2785
2786 if (!base && !type_p->flags) {
2787 /* type derived from union built-in type must contain at least one type */
2788 if (tpdfname) {
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2790 } else {
2791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2792 free(*type);
2793 *type = NULL;
2794 }
2795 return LY_EVALID;
2796 }
2797
2798 if (tpdfname) {
2799 type_p->compiled = *type;
2800 *type = calloc(1, sizeof(struct lysc_type_union));
2801 }
2802 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002803 case LY_TYPE_BOOL:
2804 case LY_TYPE_EMPTY:
2805 case LY_TYPE_UNKNOWN: /* just to complete switch */
2806 break;
2807 }
2808 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2809done:
2810 return ret;
2811}
2812
Radek Krejcia3045382018-11-22 14:30:31 +01002813/**
2814 * @brief Compile information about the leaf/leaf-list's type.
2815 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002816 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2817 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2818 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2819 * @param[in] context_name Name of the context node or referencing typedef for logging.
2820 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002821 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2822 * @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 +01002823 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002824 * @return LY_ERR value.
2825 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002826static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002827lys_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 +01002828 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002829{
2830 LY_ERR ret = LY_SUCCESS;
2831 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002832 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002833 struct type_context {
2834 const struct lysp_tpdf *tpdf;
2835 struct lysp_node *node;
2836 struct lysp_module *mod;
2837 } *tctx, *tctx_prev = NULL;
2838 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002839 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002840 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002841 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002842
2843 (*type) = NULL;
2844
2845 tctx = calloc(1, sizeof *tctx);
2846 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002847 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002848 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2849 ret == LY_SUCCESS;
2850 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2851 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2852 if (basetype) {
2853 break;
2854 }
2855
2856 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002857 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2858 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002859 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2860
Radek Krejcicdfecd92018-11-26 11:27:32 +01002861 if (units && !*units) {
2862 /* inherit units */
2863 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2864 }
Radek Krejci01342af2019-01-03 15:18:08 +01002865 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002866 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002867 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002868 }
Radek Krejci01342af2019-01-03 15:18:08 +01002869 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002870 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2871 break;
2872 }
2873
Radek Krejci19a96102018-11-15 13:38:09 +01002874 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002875 /* it is not necessary to continue, the rest of the chain was already compiled,
2876 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002877 basetype = tctx->tpdf->type.compiled->basetype;
2878 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002879 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002880 dummyloops = 1;
2881 goto preparenext;
2882 } else {
2883 tctx = NULL;
2884 break;
2885 }
Radek Krejci19a96102018-11-15 13:38:09 +01002886 }
2887
2888 /* store information for the following processing */
2889 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2890
Radek Krejcicdfecd92018-11-26 11:27:32 +01002891preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002892 /* prepare next loop */
2893 tctx_prev = tctx;
2894 tctx = calloc(1, sizeof *tctx);
2895 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2896 }
2897 free(tctx);
2898
2899 /* allocate type according to the basetype */
2900 switch (basetype) {
2901 case LY_TYPE_BINARY:
2902 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002903 break;
2904 case LY_TYPE_BITS:
2905 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002906 break;
2907 case LY_TYPE_BOOL:
2908 case LY_TYPE_EMPTY:
2909 *type = calloc(1, sizeof(struct lysc_type));
2910 break;
2911 case LY_TYPE_DEC64:
2912 *type = calloc(1, sizeof(struct lysc_type_dec));
2913 break;
2914 case LY_TYPE_ENUM:
2915 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002916 break;
2917 case LY_TYPE_IDENT:
2918 *type = calloc(1, sizeof(struct lysc_type_identityref));
2919 break;
2920 case LY_TYPE_INST:
2921 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2922 break;
2923 case LY_TYPE_LEAFREF:
2924 *type = calloc(1, sizeof(struct lysc_type_leafref));
2925 break;
2926 case LY_TYPE_STRING:
2927 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002928 break;
2929 case LY_TYPE_UNION:
2930 *type = calloc(1, sizeof(struct lysc_type_union));
2931 break;
2932 case LY_TYPE_INT8:
2933 case LY_TYPE_UINT8:
2934 case LY_TYPE_INT16:
2935 case LY_TYPE_UINT16:
2936 case LY_TYPE_INT32:
2937 case LY_TYPE_UINT32:
2938 case LY_TYPE_INT64:
2939 case LY_TYPE_UINT64:
2940 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002941 break;
2942 case LY_TYPE_UNKNOWN:
2943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2944 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2945 ret = LY_EVALID;
2946 goto cleanup;
2947 }
2948 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002949 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2951 ly_data_type2str[basetype]);
2952 free(*type);
2953 (*type) = NULL;
2954 ret = LY_EVALID;
2955 goto cleanup;
2956 }
2957
2958 /* get restrictions from the referred typedefs */
2959 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2960 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002961 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002962 base = tctx->tpdf->type.compiled;
2963 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002964 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002965 /* no change, just use the type information from the base */
2966 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2967 ++base->refcount;
2968 continue;
2969 }
2970
2971 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002972 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2974 tctx->tpdf->name, ly_data_type2str[basetype]);
2975 ret = LY_EVALID;
2976 goto cleanup;
2977 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2979 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2980 tctx->tpdf->name, tctx->tpdf->dflt);
2981 ret = LY_EVALID;
2982 goto cleanup;
2983 }
2984
Radek Krejci19a96102018-11-15 13:38:09 +01002985 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002986 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002987 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 +01002988 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2989 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002990 LY_CHECK_GOTO(ret, cleanup);
2991 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002992 }
2993
Radek Krejcic5c27e52018-11-15 14:38:11 +01002994 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002995 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002996 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002997 (*type)->basetype = basetype;
2998 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01002999 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 +01003000 LY_CHECK_GOTO(ret, cleanup);
3001 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003002 /* no specific restriction in leaf's type definition, copy from the base */
3003 free(*type);
3004 (*type) = base;
3005 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003006 }
Radek Krejci01342af2019-01-03 15:18:08 +01003007 if (!(*type)->dflt) {
3008 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3009 }
Radek Krejci19a96102018-11-15 13:38:09 +01003010
3011 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
3012
3013cleanup:
3014 ly_set_erase(&tpdf_chain, free);
3015 return ret;
3016}
3017
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003018/**
3019 * @brief Compile status information of the given node.
3020 *
3021 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3022 * has the status correctly set during the compilation.
3023 *
3024 * @param[in] ctx Compile context
3025 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3026 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3027 * the compatibility with the parent's status value.
3028 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3029 * @return LY_ERR value.
3030 */
3031static LY_ERR
3032lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3033{
3034 /* status - it is not inherited by specification, but it does not make sense to have
3035 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3036 if (!((*node_flags) & LYS_STATUS_MASK)) {
3037 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3038 if ((parent_flags & 0x3) != 0x3) {
3039 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3040 * combination of bits (0x3) which marks the uses_status value */
3041 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3042 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3043 }
3044 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3045 } else {
3046 (*node_flags) |= LYS_STATUS_CURR;
3047 }
3048 } else if (parent_flags & LYS_STATUS_MASK) {
3049 /* check status compatibility with the parent */
3050 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3051 if ((*node_flags) & LYS_STATUS_CURR) {
3052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3053 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3054 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3055 } else { /* LYS_STATUS_DEPRC */
3056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3057 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3058 }
3059 return LY_EVALID;
3060 }
3061 }
3062 return LY_SUCCESS;
3063}
3064
Radek Krejcib1b59152019-01-07 13:21:56 +01003065static 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 +01003066
Radek Krejcia3045382018-11-22 14:30:31 +01003067/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003068 * @brief Compile parsed RPC/action schema node information.
3069 * @param[in] ctx Compile context
3070 * @param[in] node_p Parsed RPC/action schema node.
3071 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3072 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3073 * @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).
3074 * Zero means no uses, non-zero value with no status bit set mean the default status.
3075 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3076 */
3077static LY_ERR
3078lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, int options,
3079 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3080{
3081 LY_ERR ret = LY_SUCCESS;
3082 struct lysp_node *child_p;
3083 unsigned int u;
3084
3085 action->nodetype = LYS_ACTION;
3086 action->module = ctx->mod;
3087 action->parent = parent;
3088 if (!(options & LYSC_OPT_FREE_SP)) {
3089 action->sp = action_p;
3090 }
3091 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3092
3093 /* status - it is not inherited by specification, but it does not make sense to have
3094 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3095 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3096
3097 DUP_STRING(ctx->ctx, action_p->name, action->name);
3098 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3099 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
3100 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3101 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, options, u, lys_compile_ext, ret, cleanup);
3102
3103 /* input */
3104 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, options, u, lys_compile_must, ret, cleanup);
3105 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input.exts, options, u, lys_compile_ext, ret, cleanup);
3106 LY_LIST_FOR(action_p->input.data, child_p) {
3107 LY_CHECK_RET(lys_compile_node(ctx, child_p, options & LYSC_OPT_RPC_INPUT, (struct lysc_node*)action, uses_status));
3108 }
3109
3110 /* output */
3111 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, options, u, lys_compile_must, ret, cleanup);
3112 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output.exts, options, u, lys_compile_ext, ret, cleanup);
3113 LY_LIST_FOR(action_p->output.data, child_p) {
3114 LY_CHECK_RET(lys_compile_node(ctx, child_p, options & LYSC_OPT_RPC_OUTPUT, (struct lysc_node*)action, uses_status));
3115 }
3116
3117cleanup:
3118 return ret;
3119}
3120
3121/**
Radek Krejcia3045382018-11-22 14:30:31 +01003122 * @brief Compile parsed container node information.
3123 * @param[in] ctx Compile context
3124 * @param[in] node_p Parsed container node.
3125 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3126 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3127 * is enriched with the container-specific information.
3128 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3129 */
Radek Krejci19a96102018-11-15 13:38:09 +01003130static LY_ERR
3131lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3132{
3133 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3134 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3135 struct lysp_node *child_p;
3136 unsigned int u;
3137 LY_ERR ret = LY_SUCCESS;
3138
Radek Krejcife909632019-02-12 15:34:42 +01003139 if (cont_p->presence) {
3140 cont->flags |= LYS_PRESENCE;
3141 }
3142
Radek Krejci19a96102018-11-15 13:38:09 +01003143 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003144 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003145 }
3146
3147 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003148 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, options, u, lys_compile_action, 0, ret, done);
3149 // TODO 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 +01003150
3151done:
3152 return ret;
3153}
3154
Radek Krejci33f72892019-02-21 10:36:58 +01003155/*
3156 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3157 * @param[in] ctx Compile context.
3158 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3159 * @param[in] type_p Parsed type to compile.
3160 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3161 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3162 * @return LY_ERR value.
3163 */
3164static LY_ERR
3165lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, int options, struct lysc_node_leaf *leaf)
3166{
3167 unsigned int u, v;
3168 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3169
3170 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type,
3171 leaf->units ? NULL : &leaf->units));
3172 if (leaf->nodetype == LYS_LEAFLIST) {
3173 if (llist->type->dflt && !llist->dflts && !llist->min) {
3174 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3175 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3176 LY_ARRAY_INCREMENT(llist->dflts);
3177 }
3178 } else {
3179 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3180 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3181 }
3182 }
3183 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3184 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3185 ly_set_add(&ctx->unres, leaf, 0);
3186 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3187 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3188 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3189 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3190 ly_set_add(&ctx->unres, leaf, 0);
3191 }
3192 }
3193 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3194 if (leaf->flags & LYS_SET_DFLT) {
3195 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3196 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3197 return LY_EVALID;
3198 }
3199 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3200 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3201 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3202 return LY_EVALID;
3203 }
3204 }
3205
3206 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3207 /* configuration data values must be unique - so check the default values */
3208 LY_ARRAY_FOR(llist->dflts, u) {
3209 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3210 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3212 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3213 return LY_EVALID;
3214 }
3215 }
3216 }
3217 }
3218
3219 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3220
3221 return LY_SUCCESS;
3222}
3223
Radek Krejcia3045382018-11-22 14:30:31 +01003224/**
3225 * @brief Compile parsed leaf node information.
3226 * @param[in] ctx Compile context
3227 * @param[in] node_p Parsed leaf node.
3228 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3229 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3230 * is enriched with the leaf-specific information.
3231 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3232 */
Radek Krejci19a96102018-11-15 13:38:09 +01003233static LY_ERR
3234lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3235{
3236 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3237 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3238 unsigned int u;
3239 LY_ERR ret = LY_SUCCESS;
3240
Radek Krejci19a96102018-11-15 13:38:09 +01003241 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003242 if (leaf_p->units) {
3243 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3244 leaf->flags |= LYS_SET_UNITS;
3245 }
3246 if (leaf_p->dflt) {
3247 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003248 leaf->flags |= LYS_SET_DFLT;
3249 }
Radek Krejci43699232018-11-23 14:59:46 +01003250
Radek Krejci33f72892019-02-21 10:36:58 +01003251 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, options, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003252
Radek Krejci19a96102018-11-15 13:38:09 +01003253done:
3254 return ret;
3255}
3256
Radek Krejcia3045382018-11-22 14:30:31 +01003257/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003258 * @brief Compile parsed leaf-list node information.
3259 * @param[in] ctx Compile context
3260 * @param[in] node_p Parsed leaf-list node.
3261 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3262 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3263 * is enriched with the leaf-list-specific information.
3264 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3265 */
3266static LY_ERR
3267lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3268{
3269 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3270 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003271 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003272 LY_ERR ret = LY_SUCCESS;
3273
Radek Krejci0e5d8382018-11-28 16:37:53 +01003274 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003275 if (llist_p->units) {
3276 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3277 llist->flags |= LYS_SET_UNITS;
3278 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003279
3280 if (llist_p->dflts) {
3281 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3282 LY_ARRAY_FOR(llist_p->dflts, u) {
3283 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3284 LY_ARRAY_INCREMENT(llist->dflts);
3285 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003286 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003287 }
3288
3289 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003290 if (llist->min) {
3291 llist->flags |= LYS_MAND_TRUE;
3292 }
Radek Krejcib7408632018-11-28 17:12:11 +01003293 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003294
Radek Krejci33f72892019-02-21 10:36:58 +01003295 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, options, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003296
3297done:
3298 return ret;
3299}
3300
3301/**
Radek Krejci7af64242019-02-18 13:07:53 +01003302 * @brief Compile information about list's uniques.
3303 * @param[in] ctx Compile context.
3304 * @param[in] context_module Module where the prefixes are going to be resolved.
3305 * @param[in] uniques Sized array list of unique statements.
3306 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3307 * @return LY_ERR value.
3308 */
3309static LY_ERR
3310lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3311{
3312 LY_ERR ret = LY_SUCCESS;
3313 struct lysc_node_leaf **key, ***unique;
3314 const char *keystr, *delim;
3315 size_t len;
3316 unsigned int v;
3317 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003318 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003319
3320 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3321 config = -1;
3322 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3323 keystr = uniques[v];
3324 while (keystr) {
3325 delim = strpbrk(keystr, " \t\n");
3326 if (delim) {
3327 len = delim - keystr;
3328 while (isspace(*delim)) {
3329 ++delim;
3330 }
3331 } else {
3332 len = strlen(keystr);
3333 }
3334
3335 /* unique node must be present */
3336 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003337 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3338 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003339 if (ret != LY_SUCCESS) {
3340 if (ret == LY_EDENIED) {
3341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003342 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003343 len, keystr, lys_nodetype2str((*key)->nodetype));
3344 }
3345 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003346 } else if (flags) {
3347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3348 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3349 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3350 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003351 }
3352
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003353
Radek Krejci7af64242019-02-18 13:07:53 +01003354 /* all referenced leafs must be of the same config type */
3355 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3357 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3358 return LY_EVALID;
3359 } else if ((*key)->flags & LYS_CONFIG_W) {
3360 config = 1;
3361 } else { /* LYS_CONFIG_R */
3362 config = 0;
3363 }
3364
3365 /* check status */
3366 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3367 (*key)->flags, (*key)->module, (*key)->name));
3368
3369 /* mark leaf as unique */
3370 (*key)->flags |= LYS_UNIQUE;
3371
3372 /* next unique value in line */
3373 keystr = delim;
3374 }
3375 /* next unique definition */
3376 }
3377
3378 return LY_SUCCESS;
3379}
3380
3381/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003382 * @brief Compile parsed list node information.
3383 * @param[in] ctx Compile context
3384 * @param[in] node_p Parsed list node.
3385 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3386 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3387 * is enriched with the list-specific information.
3388 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3389 */
3390static LY_ERR
3391lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3392{
3393 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3394 struct lysc_node_list *list = (struct lysc_node_list*)node;
3395 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003396 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003397 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003398 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003399 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003400 LY_ERR ret = LY_SUCCESS;
3401
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003402 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003403 if (list->min) {
3404 list->flags |= LYS_MAND_TRUE;
3405 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003406 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3407
3408 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003409 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003410 }
3411
3412 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
3413
3414 /* keys */
3415 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3417 return LY_EVALID;
3418 }
3419
3420 /* find all the keys (must be direct children) */
3421 keystr = list_p->key;
3422 while (keystr) {
3423 delim = strpbrk(keystr, " \t\n");
3424 if (delim) {
3425 len = delim - keystr;
3426 while (isspace(*delim)) {
3427 ++delim;
3428 }
3429 } else {
3430 len = strlen(keystr);
3431 }
3432
3433 /* key node must be present */
3434 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3435 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3436 if (!(*key)) {
3437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3438 "The list's key \"%.*s\" not found.", len, keystr);
3439 return LY_EVALID;
3440 }
3441 /* keys must be unique */
3442 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3443 if (*key == list->keys[u]) {
3444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3445 "Duplicated key identifier \"%.*s\".", len, keystr);
3446 return LY_EVALID;
3447 }
3448 }
3449 /* key must have the same config flag as the list itself */
3450 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3451 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3452 return LY_EVALID;
3453 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003454 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003455 /* YANG 1.0 denies key to be of empty type */
3456 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3457 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3458 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3459 return LY_EVALID;
3460 }
3461 } else {
3462 /* when and if-feature are illegal on list keys */
3463 if ((*key)->when) {
3464 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3465 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3466 return LY_EVALID;
3467 }
3468 if ((*key)->iffeatures) {
3469 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3470 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3471 return LY_EVALID;
3472 }
3473 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003474
3475 /* check status */
3476 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3477 (*key)->flags, (*key)->module, (*key)->name));
3478
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003479 /* ignore default values of the key */
3480 if ((*key)->dflt) {
3481 lydict_remove(ctx->ctx, (*key)->dflt);
3482 (*key)->dflt = NULL;
3483 }
3484 /* mark leaf as key */
3485 (*key)->flags |= LYS_KEY;
3486
3487 /* next key value */
3488 keystr = delim;
3489 }
3490
3491 /* uniques */
3492 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003493 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003494 }
3495
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003496 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, options, u, lys_compile_action, 0, ret, done);
3497 // TODO 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 +01003498
3499done:
3500 return ret;
3501}
3502
Radek Krejcib56c7502019-02-13 14:19:54 +01003503/**
3504 * @brief Do some checks and set the default choice's case.
3505 *
3506 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3507 *
3508 * @param[in] ctx Compile context.
3509 * @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,
3510 * not the reference to the imported module.
3511 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3512 * @return LY_ERR value.
3513 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003514static LY_ERR
3515lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3516{
3517 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3518 const char *prefix = NULL, *name;
3519 size_t prefix_len = 0;
3520
3521 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3522 name = strchr(dflt, ':');
3523 if (name) {
3524 prefix = dflt;
3525 prefix_len = name - prefix;
3526 ++name;
3527 } else {
3528 name = dflt;
3529 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003530 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003531 /* prefixed default case make sense only for the prefix of the schema itself */
3532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3533 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3534 prefix_len, prefix);
3535 return LY_EVALID;
3536 }
3537 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3538 if (!ch->dflt) {
3539 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3540 "Default case \"%s\" not found.", dflt);
3541 return LY_EVALID;
3542 }
3543 /* no mandatory nodes directly under the default case */
3544 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003545 if (iter->parent != (struct lysc_node*)ch->dflt) {
3546 break;
3547 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003548 if (iter->flags & LYS_MAND_TRUE) {
3549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3550 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3551 return LY_EVALID;
3552 }
3553 }
Radek Krejci01342af2019-01-03 15:18:08 +01003554 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003555 return LY_SUCCESS;
3556}
3557
Radek Krejciccd20f12019-02-15 14:12:27 +01003558static LY_ERR
3559lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3560{
3561 struct lys_module *mod;
3562 const char *prefix = NULL, *name;
3563 size_t prefix_len = 0;
3564 struct lysc_node_case *cs;
3565 struct lysc_node *node;
3566
3567 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3568 name = strchr(dflt, ':');
3569 if (name) {
3570 prefix = dflt;
3571 prefix_len = name - prefix;
3572 ++name;
3573 } else {
3574 name = dflt;
3575 }
3576 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3577 if (prefix) {
3578 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3580 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3581 "The prefix does not match any imported module of the deviation module.",
3582 devnodeid, dflt);
3583 return LY_EVALID;
3584 }
3585 } else {
3586 mod = ctx->mod;
3587 }
3588 /* get the default case */
3589 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3590 if (!cs) {
3591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3592 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3593 devnodeid, dflt);
3594 return LY_EVALID;
3595 }
3596
3597 /* check that there is no mandatory node */
3598 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003599 if (node->parent != (struct lysc_node*)cs) {
3600 break;
3601 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003602 if (node->flags & LYS_MAND_TRUE) {
3603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3604 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3605 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3606 return LY_EVALID;
3607 }
3608 }
3609
3610 /* set the default case in choice */
3611 ch->dflt = cs;
3612 cs->flags |= LYS_SET_DFLT;
3613
3614 return LY_SUCCESS;
3615}
3616
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003617/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003618 * @brief Compile parsed choice node information.
3619 * @param[in] ctx Compile context
3620 * @param[in] node_p Parsed choice node.
3621 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3622 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003623 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003624 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3625 */
3626static LY_ERR
3627lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3628{
3629 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3630 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3631 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003632 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003633 LY_ERR ret = LY_SUCCESS;
3634
Radek Krejci056d0a82018-12-06 16:57:25 +01003635 LY_LIST_FOR(ch_p->child, child_p) {
3636 if (child_p->nodetype == LYS_CASE) {
3637 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003638 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003639 }
3640 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003641 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003642 }
3643 }
3644
3645 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003646 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003647 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003648 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003649
Radek Krejci9800fb82018-12-13 14:26:23 +01003650 return ret;
3651}
3652
3653/**
3654 * @brief Compile parsed anydata or anyxml node information.
3655 * @param[in] ctx Compile context
3656 * @param[in] node_p Parsed anydata or anyxml node.
3657 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3658 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3659 * is enriched with the any-specific information.
3660 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3661 */
3662static LY_ERR
3663lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3664{
3665 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3666 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3667 unsigned int u;
3668 LY_ERR ret = LY_SUCCESS;
3669
3670 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3671
3672 if (any->flags & LYS_CONFIG_W) {
3673 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3674 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3675 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003676done:
3677 return ret;
3678}
3679
Radek Krejcib56c7502019-02-13 14:19:54 +01003680/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003681 * @brief Check uniqness of the node/action/notification name.
3682 *
3683 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3684 * structures, but they share the namespace so we need to check their name collisions.
3685 *
3686 * @param[in] ctx Compile context.
3687 * @param[in] children List (linked list) of data nodes to go through.
3688 * @param[in] actions List (sized array) of actions or RPCs to go through.
3689 * @param[in] notifs List (sized array) of Notifications to go through.
3690 * @param[in] name Name of the item to find in the given lists.
3691 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3692 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3693 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3694 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003695static LY_ERR
3696lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3697 const struct lysc_action *actions, const struct lysc_notif *notifs,
3698 const char *name, void *exclude)
3699{
3700 const struct lysc_node *iter;
3701 unsigned int u;
3702
3703 LY_LIST_FOR(children, iter) {
Radek Krejci95710c92019-02-11 15:49:55 +01003704 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003705 goto error;
3706 }
3707 }
3708 LY_ARRAY_FOR(actions, u) {
Radek Krejci95710c92019-02-11 15:49:55 +01003709 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003710 goto error;
3711 }
3712 }
3713 LY_ARRAY_FOR(notifs, u) {
Radek Krejci95710c92019-02-11 15:49:55 +01003714 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003715 goto error;
3716 }
3717 }
3718 return LY_SUCCESS;
3719error:
3720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition");
3721 return LY_EEXIST;
3722}
3723
3724/**
3725 * @brief Connect the node into the siblings list and check its name uniqueness.
3726 *
3727 * @param[in] ctx Compile context
3728 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3729 * the choice itself is expected instead of a specific case node.
3730 * @param[in] node Schema node to connect into the list.
3731 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3732 */
3733static LY_ERR
3734lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3735{
3736 struct lysc_node **children;
3737
3738 if (node->nodetype == LYS_CASE) {
3739 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3740 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003741 children = lysc_node_children_p(parent, node->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01003742 }
3743 if (children) {
3744 if (!(*children)) {
3745 /* first child */
3746 *children = node;
3747 } else if (*children != node) {
3748 /* by the condition in previous branch we cover the choice/case children
3749 * - the children list is shared by the choice and the the first case, in addition
3750 * the first child of each case must be referenced from the case node. So the node is
3751 * actually always already inserted in case it is the first children - so here such
3752 * a situation actually corresponds to the first branch */
3753 /* insert at the end of the parent's children list */
3754 (*children)->prev->next = node;
3755 node->prev = (*children)->prev;
3756 (*children)->prev = node;
3757
3758 /* check the name uniqueness */
3759 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3760 lysc_node_notifs(parent), node->name, node)) {
3761 return LY_EEXIST;
3762 }
3763 }
3764 }
3765 return LY_SUCCESS;
3766}
3767
Radek Krejci95710c92019-02-11 15:49:55 +01003768/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003769 * @brief Get the XPath context node for the given schema node.
3770 * @param[in] start The schema node where the XPath expression appears.
3771 * @return The context node to evaluate XPath expression in given schema node.
3772 * @return NULL in case the context node is the root node.
3773 */
3774static struct lysc_node *
3775lysc_xpath_context(struct lysc_node *start)
3776{
3777 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3778 start = start->parent);
3779 return start;
3780}
3781
3782/**
3783 * @brief Prepare the case structure in choice node for the new data node.
3784 *
3785 * 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
3786 * created in the choice when the first child was processed.
3787 *
3788 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003789 * @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,
3790 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003791 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3792 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3793 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3794 * @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,
3795 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003796 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003797static struct lysc_node_case*
3798lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3799{
3800 struct lysc_node *iter;
3801 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01003802 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003803 unsigned int u;
3804 LY_ERR ret;
3805
Radek Krejci95710c92019-02-11 15:49:55 +01003806#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003807 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003808 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3810 return NULL; \
3811 } \
3812 }
3813
Radek Krejci95710c92019-02-11 15:49:55 +01003814 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3815 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003816
3817 /* we have to add an implicit case node into the parent choice */
3818 cs = calloc(1, sizeof(struct lysc_node_case));
3819 DUP_STRING(ctx->ctx, child->name, cs->name);
3820 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003821 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003822 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3823 /* the case is already present since the child is not its first children */
3824 return (struct lysc_node_case*)ch->cases->prev;
3825 }
Radek Krejci95710c92019-02-11 15:49:55 +01003826 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003827
3828 /* explicit parent case is not present (this is its first child) */
3829 cs = calloc(1, sizeof(struct lysc_node_case));
3830 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3831 cs->flags = LYS_STATUS_MASK & node_p->flags;
3832 cs->sp = node_p;
3833
Radek Krejcib1b59152019-01-07 13:21:56 +01003834 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003835 LY_CHECK_RET(lys_compile_status(ctx, &cs->flags, ch->flags), NULL);
Radek Krejci00b874b2019-02-12 10:54:50 +01003836
3837 if (node_p->when) {
3838 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3839 ret = lys_compile_when(ctx, node_p->when, options, when);
3840 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003841 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003842 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003843 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003844 } else {
3845 LOGINT(ctx->ctx);
3846 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003847 }
3848 cs->module = ctx->mod;
3849 cs->prev = (struct lysc_node*)cs;
3850 cs->nodetype = LYS_CASE;
3851 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
3852 cs->parent = (struct lysc_node*)ch;
3853 cs->child = child;
3854
3855 return cs;
3856error:
3857 return NULL;
3858
3859#undef UNIQUE_CHECK
3860}
3861
Radek Krejcib56c7502019-02-13 14:19:54 +01003862/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003863 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003864 *
3865 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003866 * @param[in] node Target node where the config is supposed to be changed.
3867 * @param[in] config_flag Node's config flag to be applied to the @p node.
3868 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003869 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3870 * 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 +01003871 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3872 * @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 +01003873 * @return LY_ERR value.
3874 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003875static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003876lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3877 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003878{
3879 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003880 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003881
3882 if (config == (node->flags & LYS_CONFIG_MASK)) {
3883 /* nothing to do */
3884 return LY_SUCCESS;
3885 }
3886
3887 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003888 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003889 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003891 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3892 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003893 return LY_EVALID;
3894 }
Radek Krejci93dcc392019-02-19 10:43:38 +01003895 node->flags |= LYS_SET_CONFIG;
3896 } else {
3897 if (node->flags & LYS_SET_CONFIG) {
3898 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
3899 /* setting config flags, but have node with explicit config true */
3900 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3901 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3902 refine_flag ? "refine" : "deviation", nodeid);
3903 return LY_EVALID;
3904 }
3905 /* do not change config on nodes where the config is explicitely set, this does not apply to
3906 * nodes, which are being changed explicitly (targets of refine or deviation) */
3907 return LY_SUCCESS;
3908 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003909 }
3910 node->flags &= ~LYS_CONFIG_MASK;
3911 node->flags |= config;
3912
3913 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003914 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003915 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01003916 }
3917
Radek Krejci76b3e962018-12-14 17:01:25 +01003918 return LY_SUCCESS;
3919}
3920
Radek Krejcib56c7502019-02-13 14:19:54 +01003921/**
3922 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
3923 *
3924 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
3925 * the flag to such parents from a mandatory children.
3926 *
3927 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
3928 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
3929 * (mandatory children was removed).
3930 */
Radek Krejcife909632019-02-12 15:34:42 +01003931void
3932lys_compile_mandatory_parents(struct lysc_node *parent, int add)
3933{
3934 struct lysc_node *iter;
3935
3936 if (add) { /* set flag */
3937 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3938 parent = parent->parent) {
3939 parent->flags |= LYS_MAND_TRUE;
3940 }
3941 } else { /* unset flag */
3942 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003943 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01003944 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01003945 /* there is another mandatory node */
3946 return;
3947 }
3948 }
3949 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3950 parent->flags &= ~LYS_MAND_TRUE;
3951 }
3952 }
3953}
3954
Radek Krejci056d0a82018-12-06 16:57:25 +01003955/**
Radek Krejci3641f562019-02-13 15:38:40 +01003956 * @brief Internal sorting process for the lys_compile_augment_sort().
3957 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
3958 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
3959 * to be allocated to hold the complete list, its size is just incremented by adding another item.
3960 */
3961static void
3962lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
3963{
3964 unsigned int v;
3965 size_t len;
3966
3967 len = strlen(aug_p->nodeid);
3968 LY_ARRAY_FOR(result, v) {
3969 if (strlen(result[v]->nodeid) <= len) {
3970 continue;
3971 }
3972 if (v < LY_ARRAY_SIZE(result)) {
3973 /* move the rest of array */
3974 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
3975 break;
3976 }
3977 }
3978 result[v] = aug_p;
3979 LY_ARRAY_INCREMENT(result);
3980}
3981
3982/**
3983 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
3984 *
3985 * The sorting is based only on the length of the augment's path since it guarantee the correct order
3986 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
3987 *
3988 * @param[in] ctx Compile context.
3989 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
3990 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
3991 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
3992 * @param[out] augments Resulting sorted sized array of pointers to the augments.
3993 * @return LY_ERR value.
3994 */
3995LY_ERR
3996lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
3997{
3998 struct lysp_augment **result = NULL;
3999 unsigned int u, v;
4000 size_t count = 0;
4001
4002 assert(augments);
4003
4004 /* get count of the augments in module and all its submodules */
4005 if (aug_p) {
4006 count += LY_ARRAY_SIZE(aug_p);
4007 }
4008 LY_ARRAY_FOR(inc_p, u) {
4009 if (inc_p[u].submodule->augments) {
4010 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4011 }
4012 }
4013
4014 if (!count) {
4015 *augments = NULL;
4016 return LY_SUCCESS;
4017 }
4018 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4019
4020 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4021 * together, so there can be even /z/y betwwen them. */
4022 LY_ARRAY_FOR(aug_p, u) {
4023 lys_compile_augment_sort_(&aug_p[u], result);
4024 }
4025 LY_ARRAY_FOR(inc_p, u) {
4026 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4027 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4028 }
4029 }
4030
4031 *augments = result;
4032 return LY_SUCCESS;
4033}
4034
4035/**
4036 * @brief Compile the parsed augment connecting it into its target.
4037 *
4038 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4039 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4040 * are already implemented and compiled.
4041 *
4042 * @param[in] ctx Compile context.
4043 * @param[in] aug_p Parsed augment to compile.
4044 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4045 * @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
4046 * children in case of the augmenting uses data.
4047 * @return LY_SUCCESS on success.
4048 * @return LY_EVALID on failure.
4049 */
4050LY_ERR
4051lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4052{
4053 LY_ERR ret = LY_SUCCESS;
4054 struct lysp_node *node_p, *case_node_p;
4055 struct lysc_node *target; /* target target of the augment */
4056 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004057 struct lysc_when **when, *when_shared;
4058 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004059 uint16_t flags = 0;
4060 unsigned int u;
Radek Krejci3641f562019-02-13 15:38:40 +01004061
Radek Krejci7af64242019-02-18 13:07:53 +01004062 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004063 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004064 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004065 if (ret != LY_SUCCESS) {
4066 if (ret == LY_EDENIED) {
4067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4068 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4069 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4070 }
4071 return LY_EVALID;
4072 }
4073
4074 /* check for mandatory nodes
4075 * - new cases augmenting some choice can have mandatory nodes
4076 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4077 */
Radek Krejci733988a2019-02-15 15:12:44 +01004078 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004079 allow_mandatory = 1;
4080 }
4081
4082 when_shared = NULL;
4083 LY_LIST_FOR(aug_p->child, node_p) {
4084 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4085 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4086 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004087 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4088 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4090 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4091 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4092 return LY_EVALID;
4093 }
4094
4095 /* compile the children */
4096 if (node_p->nodetype != LYS_CASE) {
4097 LY_CHECK_RET(lys_compile_node(ctx, node_p, options, target, 0));
4098 } else {
4099 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
4100 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options, target, 0));
4101 }
4102 }
4103
Radek Krejcife13da42019-02-15 14:51:01 +01004104 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4105 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004106 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004107 /* 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 +01004108 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 +01004109 } else if (target->nodetype == LYS_CHOICE) {
4110 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4111 node = ((struct lysc_node_choice*)target)->cases->prev;
4112 } else {
4113 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004114 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004115 }
4116
Radek Krejci733988a2019-02-15 15:12:44 +01004117 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004118 node->flags &= ~LYS_MAND_TRUE;
4119 lys_compile_mandatory_parents(target, 0);
4120 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4121 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4122 aug_p->nodeid, node->name);
4123 return LY_EVALID;
4124 }
4125
4126 /* pass augment's when to all the children */
4127 if (aug_p->when) {
4128 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4129 if (!when_shared) {
4130 ret = lys_compile_when(ctx, aug_p->when, options, when);
4131 LY_CHECK_GOTO(ret, error);
4132 (*when)->context = lysc_xpath_context(target);
4133 when_shared = *when;
4134 } else {
4135 ++when_shared->refcount;
4136 (*when) = when_shared;
4137 }
4138 }
4139 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004140
4141 switch (target->nodetype) {
4142 case LYS_CONTAINER:
4143 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)node)->actions, target,
4144 options, u, lys_compile_action, 0, ret, error);
4145 /* TODO notifications */
4146 break;
4147 case LYS_LIST:
4148 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)node)->actions, target,
4149 options, u, lys_compile_action, 0, ret, error);
4150 /* TODO notifications */
4151 break;
4152 default:
4153 if (aug_p->actions) {
4154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4155 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4156 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4157 return LY_EVALID;
4158 }
4159 if (aug_p->notifs) {
4160 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4161 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4162 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4163 return LY_EVALID;
4164 }
4165 }
Radek Krejci3641f562019-02-13 15:38:40 +01004166
4167error:
4168 return ret;
4169}
4170
4171/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004172 * @brief Apply refined or deviated mandatory flag to the target node.
4173 *
4174 * @param[in] ctx Compile context.
4175 * @param[in] node Target node where the mandatory property is supposed to be changed.
4176 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4177 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4178 * @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 +01004179 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4180 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4181 * 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 +01004182 * @return LY_ERR value.
4183 */
4184static LY_ERR
4185lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4186{
4187 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4188 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4189 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4190 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4191 return LY_EVALID;
4192 }
4193
4194 if (mandatory_flag & LYS_MAND_TRUE) {
4195 /* check if node has default value */
4196 if (node->nodetype & LYS_LEAF) {
4197 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004198 if (refine_flag) {
4199 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4200 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4201 return LY_EVALID;
4202 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004203 } else {
4204 /* remove the default value taken from the leaf's type */
4205 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4206 ((struct lysc_node_leaf*)node)->dflt = NULL;
4207 }
4208 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004209 if (refine_flag) {
4210 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4211 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4212 return LY_EVALID;
4213 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004214 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004215 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004216 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004217 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004218 return LY_EVALID;
4219 }
4220
4221 node->flags &= ~LYS_MAND_FALSE;
4222 node->flags |= LYS_MAND_TRUE;
4223 lys_compile_mandatory_parents(node->parent, 1);
4224 } else {
4225 /* make mandatory false */
4226 node->flags &= ~LYS_MAND_TRUE;
4227 node->flags |= LYS_MAND_FALSE;
4228 lys_compile_mandatory_parents(node->parent, 0);
4229 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4230 /* get the type's default value if any */
4231 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4232 }
4233 }
4234 return LY_SUCCESS;
4235}
4236
4237/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004238 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4239 * If present, also apply uses's modificators.
4240 *
4241 * @param[in] ctx Compile context
4242 * @param[in] uses_p Parsed uses schema node.
4243 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4244 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4245 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4246 * the compile context.
4247 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4248 */
4249static LY_ERR
4250lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
4251{
4252 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004253 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004254 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004255 struct lysc_node_container context_node_fake =
4256 {.nodetype = LYS_CONTAINER,
4257 .module = ctx->mod,
4258 .flags = parent ? parent->flags : 0,
4259 .child = NULL, .next = NULL,
4260 .prev = (struct lysc_node*)&context_node_fake};
Radek Krejcie86bf772018-12-14 11:39:53 +01004261 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004262 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004263 int found;
4264 const char *id, *name, *prefix;
4265 size_t prefix_len, name_len;
4266 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004267 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004268 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004269 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004270 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004271 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004272 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004273 struct lysp_augment **augments = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004274
4275 /* search for the grouping definition */
4276 found = 0;
4277 id = uses_p->name;
4278 lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
4279 if (prefix) {
4280 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4281 if (!mod) {
4282 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4283 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4284 return LY_EVALID;
4285 }
4286 } else {
4287 mod = ctx->mod_def;
4288 }
4289 if (mod == ctx->mod_def) {
4290 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
4291 grp = lysp_node_groupings(node_p);
4292 LY_ARRAY_FOR(grp, u) {
4293 if (!strcmp(grp[u].name, name)) {
4294 grp = &grp[u];
4295 found = 1;
4296 break;
4297 }
4298 }
4299 }
4300 }
4301 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004302 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004303 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004304 if (grp) {
4305 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4306 if (!strcmp(grp[u].name, name)) {
4307 grp = &grp[u];
4308 found = 1;
4309 }
4310 }
4311 }
4312 if (!found && mod->parsed->includes) {
4313 /* ... and all the submodules */
4314 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4315 grp = mod->parsed->includes[u].submodule->groupings;
4316 if (grp) {
4317 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4318 if (!strcmp(grp[v].name, name)) {
4319 grp = &grp[v];
4320 found = 1;
4321 }
4322 }
4323 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004324 }
4325 }
4326 }
4327 if (!found) {
4328 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4329 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4330 return LY_EVALID;
4331 }
4332
4333 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4334 grp_stack_count = ctx->groupings.count;
4335 ly_set_add(&ctx->groupings, (void*)grp, 0);
4336 if (grp_stack_count == ctx->groupings.count) {
4337 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4338 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4339 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4340 return LY_EVALID;
4341 }
4342
4343 /* switch context's mod_def */
4344 mod_old = ctx->mod_def;
4345 ctx->mod_def = mod;
4346
4347 /* check status */
4348 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), error);
4349
Radek Krejcie86bf772018-12-14 11:39:53 +01004350 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004351 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
4352 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004353 child = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01004354
4355 /* some preparation for applying refines */
4356 if (grp->data == node_p) {
4357 /* remember the first child */
4358 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01004359 }
4360 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004361 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004362 LY_LIST_FOR(context_node_fake.child, child) {
4363 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004364
4365 /* pass uses's when to all the children */
4366 if (uses_p->when) {
4367 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, error);
4368 if (!when_shared) {
Radek Krejci3641f562019-02-13 15:38:40 +01004369 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004370 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004371 when_shared = *when;
4372 } else {
4373 ++when_shared->refcount;
4374 (*when) = when_shared;
4375 }
4376 }
Radek Krejci01342af2019-01-03 15:18:08 +01004377 }
4378 if (context_node_fake.child) {
4379 child = context_node_fake.child->prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004380 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 +01004381 }
4382
Radek Krejci3641f562019-02-13 15:38:40 +01004383 /* sort and apply augments */
4384 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), error);
4385 LY_ARRAY_FOR(augments, u) {
4386 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), error);
4387 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004388
Radek Krejcif0089082019-01-07 16:42:01 +01004389 /* reload previous context's mod_def */
4390 ctx->mod_def = mod_old;
4391
Radek Krejci76b3e962018-12-14 17:01:25 +01004392 /* apply refine */
4393 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004394 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 +01004395 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejci01342af2019-01-03 15:18:08 +01004396 error);
Radek Krejcif2271f12019-01-07 16:42:23 +01004397 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004398
4399 /* default value */
4400 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004401 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004402 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4403 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4404 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
4405 goto error;
4406 }
4407 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4408 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4409 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4410 rfn->nodeid, lys_nodetype2str(node->nodetype));
4411 goto error;
4412 }
4413 if (node->nodetype == LYS_LEAF) {
4414 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4415 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004416 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004417 /* TODO check the default value according to type */
4418 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004419 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004420 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4421 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
4422 goto error;
4423 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004424 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4425 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4426 }
4427 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004428 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004429 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, error);
4430 LY_ARRAY_FOR(rfn->dflts, u) {
4431 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4432 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4433 }
4434 /* TODO check the default values according to type */
4435 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004436 if (((struct lysc_node_choice*)node)->dflt) {
4437 /* unset LYS_SET_DFLT from the current default case */
4438 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4439 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004440 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), error);
4441 }
4442 }
4443
Radek Krejci12fb9142019-01-08 09:45:30 +01004444 /* description */
4445 if (rfn->dsc) {
4446 FREE_STRING(ctx->ctx, node->dsc);
4447 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4448 }
4449
4450 /* reference */
4451 if (rfn->ref) {
4452 FREE_STRING(ctx->ctx, node->ref);
4453 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4454 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004455
4456 /* config */
4457 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004458 if (!flags) {
4459 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), error);
4460 } else {
4461 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4462 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4463 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004464 }
4465
4466 /* mandatory */
4467 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004468 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), error);
Radek Krejci76b3e962018-12-14 17:01:25 +01004469 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004470
4471 /* presence */
4472 if (rfn->presence) {
4473 if (node->nodetype != LYS_CONTAINER) {
4474 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4475 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4476 rfn->nodeid, lys_nodetype2str(node->nodetype));
4477 goto error;
4478 }
4479 node->flags |= LYS_PRESENCE;
4480 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004481
4482 /* must */
4483 if (rfn->musts) {
4484 switch (node->nodetype) {
4485 case LYS_LEAF:
4486 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, error);
4487 break;
4488 case LYS_LEAFLIST:
4489 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, error);
4490 break;
4491 case LYS_LIST:
4492 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, error);
4493 break;
4494 case LYS_CONTAINER:
4495 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, error);
4496 break;
4497 case LYS_ANYXML:
4498 case LYS_ANYDATA:
4499 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, error);
4500 break;
4501 default:
4502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4503 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4504 rfn->nodeid, lys_nodetype2str(node->nodetype));
4505 goto error;
4506 }
4507 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004508
4509 /* min/max-elements */
4510 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4511 switch (node->nodetype) {
4512 case LYS_LEAFLIST:
4513 if (rfn->flags & LYS_SET_MAX) {
4514 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4515 }
4516 if (rfn->flags & LYS_SET_MIN) {
4517 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004518 if (rfn->min) {
4519 node->flags |= LYS_MAND_TRUE;
4520 lys_compile_mandatory_parents(node->parent, 1);
4521 } else {
4522 node->flags &= ~LYS_MAND_TRUE;
4523 lys_compile_mandatory_parents(node->parent, 0);
4524 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004525 }
4526 break;
4527 case LYS_LIST:
4528 if (rfn->flags & LYS_SET_MAX) {
4529 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4530 }
4531 if (rfn->flags & LYS_SET_MIN) {
4532 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004533 if (rfn->min) {
4534 node->flags |= LYS_MAND_TRUE;
4535 lys_compile_mandatory_parents(node->parent, 1);
4536 } else {
4537 node->flags &= ~LYS_MAND_TRUE;
4538 lys_compile_mandatory_parents(node->parent, 0);
4539 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004540 }
4541 break;
4542 default:
4543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4544 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4545 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
4546 goto error;
4547 }
4548 }
Radek Krejcif0089082019-01-07 16:42:01 +01004549
4550 /* if-feature */
4551 if (rfn->iffeatures) {
4552 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
4553 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
4554 }
Radek Krejci01342af2019-01-03 15:18:08 +01004555 }
4556 /* fix connection of the children nodes from fake context node back into the parent */
4557 if (context_node_fake.child) {
4558 context_node_fake.child->prev = child;
4559 }
4560 LY_LIST_FOR(context_node_fake.child, child) {
4561 child->parent = parent;
Radek Krejcie86bf772018-12-14 11:39:53 +01004562 }
4563
Radek Krejcif2271f12019-01-07 16:42:23 +01004564 /* do some additional checks of the changed nodes when all the refines are applied */
4565 for (u = 0; u < refined.count; ++u) {
4566 node = (struct lysc_node*)refined.objs[u];
4567 rfn = &uses_p->refines[u];
4568
4569 /* check possible conflict with default value (default added, mandatory left true) */
4570 if ((node->flags & LYS_MAND_TRUE) &&
4571 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4572 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4574 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
4575 goto error;
4576 }
4577
4578 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4579 if (node->nodetype == LYS_LIST) {
4580 min = ((struct lysc_node_list*)node)->min;
4581 max = ((struct lysc_node_list*)node)->max;
4582 } else {
4583 min = ((struct lysc_node_leaflist*)node)->min;
4584 max = ((struct lysc_node_leaflist*)node)->max;
4585 }
4586 if (min > max) {
4587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4588 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4589 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
4590 goto error;
4591 }
4592 }
4593 }
4594
Radek Krejcie86bf772018-12-14 11:39:53 +01004595 ret = LY_SUCCESS;
4596error:
4597 /* reload previous context's mod_def */
4598 ctx->mod_def = mod_old;
4599 /* remove the grouping from the stack for circular groupings dependency check */
4600 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4601 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004602 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004603 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004604
4605 return ret;
4606}
4607
Radek Krejcife909632019-02-12 15:34:42 +01004608
Radek Krejcie86bf772018-12-14 11:39:53 +01004609/**
Radek Krejcia3045382018-11-22 14:30:31 +01004610 * @brief Compile parsed schema node information.
4611 * @param[in] ctx Compile context
4612 * @param[in] node_p Parsed schema node.
4613 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4614 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4615 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4616 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004617 * @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).
4618 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004619 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4620 */
Radek Krejci19a96102018-11-15 13:38:09 +01004621static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01004622lys_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 +01004623{
4624 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004625 struct lysc_node *node;
4626 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004627 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004628 unsigned int u;
4629 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
4630
4631 switch (node_p->nodetype) {
4632 case LYS_CONTAINER:
4633 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4634 node_compile_spec = lys_compile_node_container;
4635 break;
4636 case LYS_LEAF:
4637 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4638 node_compile_spec = lys_compile_node_leaf;
4639 break;
4640 case LYS_LIST:
4641 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004642 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004643 break;
4644 case LYS_LEAFLIST:
4645 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004646 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004647 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004648 case LYS_CHOICE:
4649 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004650 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004651 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004652 case LYS_ANYXML:
4653 case LYS_ANYDATA:
4654 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004655 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004656 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004657 case LYS_USES:
4658 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004659 default:
4660 LOGINT(ctx->ctx);
4661 return LY_EINT;
4662 }
4663 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4664 node->nodetype = node_p->nodetype;
4665 node->module = ctx->mod;
4666 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004667 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004668
4669 /* config */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004670 if (options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
4671 /* ignore config statements inside RPC/action data */
4672 node->flags &= LYS_CONFIG_MASK;
4673 node->flags |= (options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4674 } else if (options & LYSC_OPT_NOTIFICATION) {
4675 /* ignore config statements inside Notification data */
4676 node->flags &= LYS_CONFIG_MASK;
4677 node->flags |= LYS_CONFIG_R;
4678 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004679 /* config not explicitely set, inherit it from parent */
4680 if (parent) {
4681 node->flags |= parent->flags & LYS_CONFIG_MASK;
4682 } else {
4683 /* default is config true */
4684 node->flags |= LYS_CONFIG_W;
4685 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004686 } else {
4687 /* config set explicitely */
4688 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004689 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004690 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4692 "Configuration node cannot be child of any state data node.");
4693 goto error;
4694 }
Radek Krejci19a96102018-11-15 13:38:09 +01004695
Radek Krejcia6d57732018-11-29 13:40:37 +01004696 /* *list ordering */
4697 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4698 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
4699 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
4700 "RPC/action output parameters or notification content (%s).", ctx->path);
4701 node->flags &= ~LYS_ORDBY_MASK;
4702 node->flags |= LYS_ORDBY_SYSTEM;
4703 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4704 /* default ordering is system */
4705 node->flags |= LYS_ORDBY_SYSTEM;
4706 }
4707 }
4708
Radek Krejci19a96102018-11-15 13:38:09 +01004709 /* status - it is not inherited by specification, but it does not make sense to have
4710 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004711 if (!parent || parent->nodetype != LYS_CHOICE) {
4712 /* in case of choice/case's children, postpone the check to the moment we know if
4713 * 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 +01004714 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 +01004715 }
4716
4717 if (!(options & LYSC_OPT_FREE_SP)) {
4718 node->sp = node_p;
4719 }
4720 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004721 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4722 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004723 if (node_p->when) {
4724 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4725 ret = lys_compile_when(ctx, node_p->when, options, when);
4726 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004727 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004728 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004729 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004730 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
4731
4732 /* nodetype-specific part */
4733 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
4734
Radek Krejcife909632019-02-12 15:34:42 +01004735 /* inherit LYS_MAND_TRUE in parent containers */
4736 if (node->flags & LYS_MAND_TRUE) {
4737 lys_compile_mandatory_parents(parent, 1);
4738 }
4739
Radek Krejci19a96102018-11-15 13:38:09 +01004740 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004741 if (parent) {
4742 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004743 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
4744 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004745 if (uses_status) {
4746
4747 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004748 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004749 * it directly gets the same status flags as the choice;
4750 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004751 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004752 node->parent = (struct lysc_node*)cs;
4753 } else { /* other than choice */
4754 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004755 }
Radek Krejci95710c92019-02-11 15:49:55 +01004756 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci19a96102018-11-15 13:38:09 +01004757 } else {
4758 /* top-level element */
4759 if (!ctx->mod->compiled->data) {
4760 ctx->mod->compiled->data = node;
4761 } else {
4762 /* insert at the end of the module's top-level nodes list */
4763 ctx->mod->compiled->data->prev->next = node;
4764 node->prev = ctx->mod->compiled->data->prev;
4765 ctx->mod->compiled->data->prev = node;
4766 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004767 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4768 ctx->mod->compiled->notifs, node->name, node)) {
4769 return LY_EVALID;
4770 }
Radek Krejci19a96102018-11-15 13:38:09 +01004771 }
4772
4773 return LY_SUCCESS;
4774
4775error:
4776 lysc_node_free(ctx->ctx, node);
4777 return ret;
4778}
4779
Radek Krejciccd20f12019-02-15 14:12:27 +01004780static void
4781lysc_disconnect(struct lysc_node *node)
4782{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004783 struct lysc_node *parent, *child, *prev = NULL, *next;
4784 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004785 int remove_cs = 0;
4786
4787 parent = node->parent;
4788
4789 /* parent's first child */
4790 if (!parent) {
4791 return;
4792 }
4793 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004794 cs = (struct lysc_node_case*)node;
4795 } else if (parent->nodetype == LYS_CASE) {
4796 /* disconnecting some node in a case */
4797 cs = (struct lysc_node_case*)parent;
4798 parent = cs->parent;
4799 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4800 if (child == node) {
4801 if (cs->child == child) {
4802 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
4803 /* case with a single child -> remove also the case */
4804 child->parent = NULL;
4805 remove_cs = 1;
4806 } else {
4807 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01004808 }
4809 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004810 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01004811 }
4812 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004813 if (!remove_cs) {
4814 cs = NULL;
4815 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004816 } else if (lysc_node_children(parent, node->flags) == node) {
4817 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004818 }
4819
4820 if (cs) {
4821 if (remove_cs) {
4822 /* cs has only one child which is being also removed */
4823 lysc_disconnect((struct lysc_node*)cs);
4824 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
4825 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01004826 if (((struct lysc_node_choice*)parent)->dflt == cs) {
4827 /* default case removed */
4828 ((struct lysc_node_choice*)parent)->dflt = NULL;
4829 }
4830 if (((struct lysc_node_choice*)parent)->cases == cs) {
4831 /* first case removed */
4832 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
4833 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004834 if (cs->child) {
4835 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
4836 if (cs->child->prev->parent != (struct lysc_node*)cs) {
4837 prev = cs->child->prev;
4838 } /* else all the children are under a single case */
4839 LY_LIST_FOR_SAFE(cs->child, next, child) {
4840 if (child->parent != (struct lysc_node*)cs) {
4841 break;
4842 }
4843 lysc_node_free(node->module->ctx, child);
4844 }
4845 if (prev) {
4846 if (prev->next) {
4847 prev->next = child;
4848 }
4849 if (child) {
4850 child->prev = prev;
4851 } else {
4852 /* link from the first child under the cases */
4853 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
4854 }
4855 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004856 }
4857 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004858 }
4859
4860 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01004861 if (node->prev->next) {
4862 node->prev->next = node->next;
4863 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004864 if (node->next) {
4865 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004866 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004867 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01004868 if (child) {
4869 child->prev = node->prev;
4870 }
4871 } else if (((struct lysc_node_choice*)parent)->cases) {
4872 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01004873 }
4874}
4875
4876LY_ERR
4877lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options)
4878{
4879 LY_ERR ret = LY_EVALID;
4880 struct ly_set devs_p = {0};
4881 struct ly_set targets = {0};
4882 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01004883 struct lysc_node_list *list;
Radek Krejciccd20f12019-02-15 14:12:27 +01004884 struct lysp_deviation *dev;
4885 struct lysp_deviate *d, **dp_new;
4886 struct lysp_deviate_add *d_add;
4887 struct lysp_deviate_del *d_del;
4888 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01004889 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01004890 struct lysc_deviation {
4891 const char *nodeid;
4892 struct lysc_node *target; /* target node of the deviation */
4893 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004894 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01004895 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
4896 } **devs = NULL;
4897 int i;
4898 size_t prefix_len, name_len;
4899 const char *prefix, *name, *nodeid;
4900 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01004901 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004902 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01004903
4904 /* get all deviations from the module and all its submodules ... */
4905 LY_ARRAY_FOR(mod_p->deviations, u) {
4906 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
4907 }
4908 LY_ARRAY_FOR(mod_p->includes, v) {
4909 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
4910 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
4911 }
4912 }
4913 if (!devs_p.count) {
4914 /* nothing to do */
4915 return LY_SUCCESS;
4916 }
4917
4918 /* ... and group them by the target node */
4919 devs = calloc(devs_p.count, sizeof *devs);
4920 for (u = 0; u < devs_p.count; ++u) {
4921 dev = devs_p.objs[u];
4922
4923 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004924 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
4925 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01004926
4927 /* insert into the set of targets with duplicity detection */
4928 i = ly_set_add(&targets, target, 0);
4929 if (!devs[i]) {
4930 /* new record */
4931 devs[i] = calloc(1, sizeof **devs);
4932 devs[i]->target = target;
4933 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004934 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01004935 }
4936 /* add deviates into the deviation's list of deviates */
4937 for (d = dev->deviates; d; d = d->next) {
4938 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
4939 *dp_new = d;
4940 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
4941 devs[i]->not_supported = 1;
4942 }
4943 }
4944 }
4945
4946 /* MACROS for deviates checking */
4947#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
4948 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
4949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
4950 goto cleanup; \
4951 }
4952
4953#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
4954 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
4955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
4956 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
4957 goto cleanup; \
4958 }
4959
4960#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01004961 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01004962 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
4963 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
4964 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
4965 goto cleanup; \
4966 }
4967
Radek Krejci551b12c2019-02-19 16:11:21 +01004968#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
4969 if (((TYPE)devs[u]->target)->MEMBER COND) { \
4970 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
4971 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
4972 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
4973 goto cleanup; \
4974 }
4975
Radek Krejciccd20f12019-02-15 14:12:27 +01004976#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
4977 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
4978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
4979 goto cleanup; \
4980 }
4981
Radek Krejci551b12c2019-02-19 16:11:21 +01004982#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
4983 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
4984 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
4985 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
4986 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
4987 goto cleanup; \
4988 }
4989
Radek Krejciccd20f12019-02-15 14:12:27 +01004990#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
4991 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
4992 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
4993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
4994 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
4995 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
4996 goto cleanup; \
4997 } \
4998 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
4999 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5000
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005001#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5002 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5003 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5004 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5005 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 +01005006 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005007 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5009 "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 +01005010 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005011 goto cleanup; \
5012 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005013 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5014 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5015 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5016 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5017 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005018 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005019 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5020 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5021 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005022 }
5023
5024 /* apply deviations */
5025 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5026 /* not-supported */
5027 if (devs[u]->not_supported) {
5028 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5029 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5030 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5031 }
5032 /* remove the target node */
5033 lysc_disconnect(devs[u]->target);
5034 lysc_node_free(ctx->ctx, devs[u]->target);
5035
5036 continue;
5037 }
5038
5039 /* list of deviates (not-supported is not present in the list) */
5040 LY_ARRAY_FOR(devs[u]->deviates, v) {
5041 d = devs[u]->deviates[v];
5042
5043 switch (d->mod) {
5044 case LYS_DEV_ADD:
5045 d_add = (struct lysp_deviate_add*)d;
5046 /* [units-stmt] */
5047 if (d_add->units) {
5048 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5049 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5050
5051 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5052 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5053 }
5054
5055 /* *must-stmt */
5056 if (d_add->musts) {
5057 switch (devs[u]->target->nodetype) {
5058 case LYS_CONTAINER:
5059 case LYS_LIST:
5060 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5061 options, x, lys_compile_must, ret, cleanup);
5062 break;
5063 case LYS_LEAF:
5064 case LYS_LEAFLIST:
5065 case LYS_ANYDATA:
5066 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5067 options, x, lys_compile_must, ret, cleanup);
5068 break;
5069 case LYS_NOTIF:
Radek Krejciccd20f12019-02-15 14:12:27 +01005070 /* TODO */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005071 break;
5072 case LYS_ACTION:
5073 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5074 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5075 options, x, lys_compile_must, ret, cleanup);
5076 break;
5077 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5078 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5079 options, x, lys_compile_must, ret, cleanup);
5080 break;
5081 }
5082 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005083 default:
5084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005085 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005086 goto cleanup;
5087 }
5088 }
5089
5090 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005091 if (d_add->uniques) {
5092 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5093 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5094 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005095
5096 /* *default-stmt */
5097 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005098 switch (devs[u]->target->nodetype) {
5099 case LYS_LEAF:
5100 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5101 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5102 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5103 /* first, remove the default value taken from the type */
5104 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5105 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5106 }
5107 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005108 /* mark the new default values as leaf's own */
5109 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005110 break;
5111 case LYS_LEAFLIST:
5112 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5113 /* first, remove the default value taken from the type */
5114 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5115 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5116 }
5117 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5118 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5119 }
5120 /* add new default value(s) */
5121 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5122 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5123 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5124 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5125 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5126 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5127 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005128 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005129 devs[u]->target->flags |= LYS_SET_DFLT;
5130 break;
5131 case LYS_CHOICE:
5132 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5133 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5134 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5135 * to allow making the default case even the augmented case from the deviating module */
5136 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5137 (struct lysc_node_choice*)devs[u]->target)) {
5138 goto cleanup;
5139 }
5140 break;
5141 default:
5142 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005143 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005144 goto cleanup;
5145 }
5146 }
5147
5148 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005149 if (d_add->flags & LYS_CONFIG_MASK) {
5150 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5152 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5153 goto cleanup;
5154 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005155 if (devs[u]->flags) {
5156 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5157 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5158 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005159 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5160 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5161 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5162 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5163 goto cleanup;
5164 }
5165 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5166 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005167
5168 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005169 if (d_add->flags & LYS_MAND_MASK) {
5170 if (devs[u]->target->flags & LYS_MAND_MASK) {
5171 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5172 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5173 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5174 goto cleanup;
5175 }
5176 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5177 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005178
5179 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005180 if (d_add->flags & LYS_SET_MIN) {
5181 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5182 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5183 /* change value */
5184 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5185 } else if (devs[u]->target->nodetype == LYS_LIST) {
5186 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5187 /* change value */
5188 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5189 } else {
5190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5191 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5192 goto cleanup;
5193 }
5194 if (d_add->min) {
5195 devs[u]->target->flags |= LYS_MAND_TRUE;
5196 }
5197 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005198
5199 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005200 if (d_add->flags & LYS_SET_MAX) {
5201 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5202 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5203 /* change value */
5204 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5205 } else if (devs[u]->target->nodetype == LYS_LIST) {
5206 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5207 /* change value */
5208 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5209 } else {
5210 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5211 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5212 goto cleanup;
5213 }
5214 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005215
5216 break;
5217 case LYS_DEV_DELETE:
5218 d_del = (struct lysp_deviate_del*)d;
5219
5220 /* [units-stmt] */
5221 if (d_del->units) {
5222 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5223 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5224 }
5225
5226 /* *must-stmt */
5227 if (d_del->musts) {
5228 switch (devs[u]->target->nodetype) {
5229 case LYS_CONTAINER:
5230 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005231 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005232 break;
5233 case LYS_LEAF:
5234 case LYS_LEAFLIST:
5235 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005236 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005237 break;
5238 case LYS_NOTIF:
Radek Krejciccd20f12019-02-15 14:12:27 +01005239 /* TODO */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005240 break;
5241 case LYS_ACTION:
5242 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5243 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5244 break;
5245 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5246 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5247 break;
5248 }
5249 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005250 default:
5251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005252 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005253 goto cleanup;
5254 }
5255 }
5256
5257 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005258 if (d_del->uniques) {
5259 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5260 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5261 LY_ARRAY_FOR(d_del->uniques, x) {
5262 LY_ARRAY_FOR(list->uniques, z) {
5263 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5264 nodeid = strpbrk(name, " \t\n");
5265 if (nodeid) {
5266 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5267 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5268 break;
5269 }
5270 while (isspace(*nodeid)) {
5271 ++nodeid;
5272 }
5273 } else {
5274 if (strcmp(name, list->uniques[z][y]->name)) {
5275 break;
5276 }
5277 }
5278 }
5279 if (!name) {
5280 /* complete match - remove the unique */
5281 LY_ARRAY_DECREMENT(list->uniques);
5282 LY_ARRAY_FREE(list->uniques[z]);
5283 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5284 --z;
5285 break;
5286 }
5287 }
5288 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5290 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5291 devs[u]->nodeid, d_del->uniques[x]);
5292 goto cleanup;
5293 }
5294 }
5295 if (!LY_ARRAY_SIZE(list->uniques)) {
5296 LY_ARRAY_FREE(list->uniques);
5297 list->uniques = NULL;
5298 }
5299 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005300
5301 /* *default-stmt */
5302 if (d_del->dflts) {
5303 switch (devs[u]->target->nodetype) {
5304 case LYS_LEAF:
5305 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5306 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5307 dflt, "deleting", "default", d_del->dflts[0]);
5308
5309 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005310 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005311 break;
5312 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005313 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005314 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5315 devs[u]->target->flags &= ~LYS_SET_DFLT;
5316 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005317 break;
5318 case LYS_CHOICE:
5319 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5320 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5321 nodeid = d_del->dflts[0];
5322 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5323 if (prefix) {
5324 /* use module prefixes from the deviation module to match the module of the default case */
5325 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5326 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5327 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5328 "The prefix does not match any imported module of the deviation module.",
5329 devs[u]->nodeid, d_del->dflts[0]);
5330 goto cleanup;
5331 }
5332 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5333 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5334 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5335 "The prefix does not match the default case's module.",
5336 devs[u]->nodeid, d_del->dflts[0]);
5337 goto cleanup;
5338 }
5339 }
5340 /* else {
5341 * strictly, the default prefix would point to the deviation module, but the value should actually
5342 * match the default string in the original module (usually unprefixed), so in this case we do not check
5343 * the module of the default case, just matching its name */
5344 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5345 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5346 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5347 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5348 goto cleanup;
5349 }
5350 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5351 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5352 break;
5353 default:
5354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005355 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005356 goto cleanup;
5357 }
5358 }
5359
5360 break;
5361 case LYS_DEV_REPLACE:
5362 d_rpl = (struct lysp_deviate_rpl*)d;
5363
5364 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005365 if (d_rpl->type) {
5366 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5367 /* type is mandatory, so checking for its presence is not necessary */
5368 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
5369 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup);
5370 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005371
5372 /* [units-stmt] */
5373 if (d_rpl->units) {
5374 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5375 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5376 units, "replacing", "units", d_rpl->units);
5377
5378 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5379 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5380 }
5381
5382 /* [default-stmt] */
5383 if (d_rpl->dflt) {
5384 switch (devs[u]->target->nodetype) {
5385 case LYS_LEAF:
5386 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5387 dflt, "replacing", "default", d_rpl->dflt);
5388
5389 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5390 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5391 break;
5392 case LYS_CHOICE:
5393 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5394 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5395 (struct lysc_node_choice*)devs[u]->target)) {
5396 goto cleanup;
5397 }
5398 break;
5399 default:
5400 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005401 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005402 goto cleanup;
5403 }
5404 }
5405
5406 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005407 if (d_rpl->flags & LYS_CONFIG_MASK) {
5408 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5410 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5411 goto cleanup;
5412 }
5413 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5414 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5415 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5416 goto cleanup;
5417 }
5418 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5419 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005420
5421 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005422 if (d_rpl->flags & LYS_MAND_MASK) {
5423 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5424 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5425 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5426 goto cleanup;
5427 }
5428 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5429 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005430
5431 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005432 if (d_rpl->flags & LYS_SET_MIN) {
5433 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5434 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5435 /* change value */
5436 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5437 } else if (devs[u]->target->nodetype == LYS_LIST) {
5438 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5439 /* change value */
5440 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5441 } else {
5442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5443 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5444 goto cleanup;
5445 }
5446 if (d_rpl->min) {
5447 devs[u]->target->flags |= LYS_MAND_TRUE;
5448 }
5449 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005450
5451 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005452 if (d_rpl->flags & LYS_SET_MAX) {
5453 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5454 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5455 /* change value */
5456 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5457 } else if (devs[u]->target->nodetype == LYS_LIST) {
5458 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5459 /* change value */
5460 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5461 } else {
5462 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5463 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5464 goto cleanup;
5465 }
5466 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005467
5468 break;
5469 default:
5470 LOGINT(ctx->ctx);
5471 goto cleanup;
5472 }
5473 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005474
Radek Krejci33f72892019-02-21 10:36:58 +01005475 /* final check when all deviations of a single target node are applied */
5476
Radek Krejci551b12c2019-02-19 16:11:21 +01005477 /* check min-max compatibility */
5478 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5479 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5480 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5481 } else if (devs[u]->target->nodetype == LYS_LIST) {
5482 min = ((struct lysc_node_list*)devs[u]->target)->min;
5483 max = ((struct lysc_node_list*)devs[u]->target)->max;
5484 } else {
5485 min = max = 0;
5486 }
5487 if (min > max) {
5488 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5489 "after deviation (%s): min value %u is bigger than max value %u.",
5490 devs[u]->nodeid, min, max);
5491 goto cleanup;
5492 }
5493
5494 /* check mandatory - default compatibility */
5495 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5496 && (devs[u]->target->flags & LYS_SET_DFLT)
5497 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5499 "Invalid deviation (%s) combining default value and mandatory %s.",
5500 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5501 goto cleanup;
5502 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5503 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5504 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5506 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5507 goto cleanup;
5508 }
5509 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5510 /* mandatory node under a default case */
5511 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5512 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5513 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5514 goto cleanup;
5515 }
Radek Krejci33f72892019-02-21 10:36:58 +01005516
5517 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005518 }
5519
5520 ret = LY_SUCCESS;
5521
5522cleanup:
5523 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5524 LY_ARRAY_FREE(devs[u]->deviates);
5525 free(devs[u]);
5526 }
5527 free(devs);
5528 ly_set_erase(&targets, NULL);
5529 ly_set_erase(&devs_p, NULL);
5530
5531 return ret;
5532}
5533
Radek Krejcib56c7502019-02-13 14:19:54 +01005534/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005535 * @brief Compile the given YANG submodule into the main module.
5536 * @param[in] ctx Compile context
5537 * @param[in] inc Include structure from the main module defining the submodule.
5538 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
5539 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5540 */
5541LY_ERR
5542lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
5543{
5544 unsigned int u;
5545 LY_ERR ret = LY_SUCCESS;
5546 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005547 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005548 struct lysc_module *mainmod = ctx->mod->compiled;
5549
Radek Krejci0af46292019-01-11 16:02:31 +01005550 if (!mainmod->mod->off_features) {
5551 /* features are compiled directly into the compiled module structure,
5552 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5553 * The features compilation is finished in the main module (lys_compile()). */
5554 ret = lys_feature_precompile(ctx->ctx, submod->features,
5555 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5556 LY_CHECK_GOTO(ret, error);
5557 }
5558
Radek Krejcid05cbd92018-12-05 14:26:40 +01005559 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
5560
5561error:
5562 return ret;
5563}
5564
Radek Krejci19a96102018-11-15 13:38:09 +01005565LY_ERR
5566lys_compile(struct lys_module *mod, int options)
5567{
5568 struct lysc_ctx ctx = {0};
5569 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005570 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005571 struct lysp_module *sp;
5572 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005573 struct lysp_augment **augments = NULL;
5574 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005575 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005576 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005577
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005578 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005579
5580 if (!mod->implemented) {
5581 /* just imported modules are not compiled */
5582 return LY_SUCCESS;
5583 }
5584
Radek Krejci19a96102018-11-15 13:38:09 +01005585 sp = mod->parsed;
5586
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005587 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005588 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005589 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005590
5591 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005592 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5593 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005594
Radek Krejci19a96102018-11-15 13:38:09 +01005595 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005596 LY_ARRAY_FOR(sp->includes, u) {
5597 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
5598 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5599 }
Radek Krejci0af46292019-01-11 16:02:31 +01005600 if (mod->off_features) {
5601 /* there is already precompiled array of features */
5602 mod_c->features = mod->off_features;
5603 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005604 } else {
5605 /* features are compiled directly into the compiled module structure,
5606 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
5607 ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features);
5608 LY_CHECK_GOTO(ret, error);
5609 }
5610 /* finish feature compilation, not only for the main module, but also for the submodules.
5611 * Due to possible forward references, it must be done when all the features (including submodules)
5612 * are present. */
5613 LY_ARRAY_FOR(sp->features, u) {
5614 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
5615 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5616 }
5617 LY_ARRAY_FOR(sp->includes, v) {
5618 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
5619 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
5620 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5621 }
5622 }
5623
Radek Krejcid05cbd92018-12-05 14:26:40 +01005624 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005625 if (sp->identities) {
5626 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5627 }
5628
Radek Krejci95710c92019-02-11 15:49:55 +01005629 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005630 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005631 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005632 LY_CHECK_GOTO(ret, error);
5633 }
Radek Krejci95710c92019-02-11 15:49:55 +01005634
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005635 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
5636 // TODO 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 +01005637
Radek Krejci95710c92019-02-11 15:49:55 +01005638 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005639 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005640 LY_CHECK_GOTO(ret, error);
5641 LY_ARRAY_FOR(augments, u) {
5642 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
5643 LY_CHECK_GOTO(ret, error);
5644 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005645
5646 /* deviations */
5647 ret = lys_compile_deviations(&ctx, sp, options);
5648 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005649
5650 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
5651
Radek Krejcia3045382018-11-22 14:30:31 +01005652 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005653 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5654 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5655 * 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 +01005656 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005657 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005658 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5659 if (type->basetype == LY_TYPE_LEAFREF) {
5660 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005661 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 +01005662 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005663 } else if (type->basetype == LY_TYPE_UNION) {
5664 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5665 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5666 /* validate the path */
5667 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5668 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5669 LY_CHECK_GOTO(ret, error);
5670 }
5671 }
Radek Krejcia3045382018-11-22 14:30:31 +01005672 }
5673 }
5674 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005675 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005676 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005677 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5678 if (type->basetype == LY_TYPE_LEAFREF) {
5679 /* store pointer to the real type */
5680 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5681 typeiter->basetype == LY_TYPE_LEAFREF;
5682 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5683 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005684 } else if (type->basetype == LY_TYPE_UNION) {
5685 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5686 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5687 /* store pointer to the real type */
5688 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5689 typeiter->basetype == LY_TYPE_LEAFREF;
5690 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5691 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5692 }
5693 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005694 }
5695 }
5696 }
Radek Krejcia3045382018-11-22 14:30:31 +01005697 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005698 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005699 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005700
Radek Krejci19a96102018-11-15 13:38:09 +01005701 if (options & LYSC_OPT_FREE_SP) {
5702 lysp_module_free(mod->parsed);
5703 ((struct lys_module*)mod)->parsed = NULL;
5704 }
5705
Radek Krejci95710c92019-02-11 15:49:55 +01005706 if (!(options & LYSC_OPT_INTERNAL)) {
5707 /* remove flag of the modules implemented by dependency */
5708 for (u = 0; u < ctx.ctx->list.count; ++u) {
5709 m = ctx.ctx->list.objs[u];
5710 if (m->implemented == 2) {
5711 m->implemented = 1;
5712 }
5713 }
5714 }
5715
Radek Krejci19a96102018-11-15 13:38:09 +01005716 ((struct lys_module*)mod)->compiled = mod_c;
5717 return LY_SUCCESS;
5718
5719error:
Radek Krejci95710c92019-02-11 15:49:55 +01005720 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01005721 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005722 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005723 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01005724 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005725 mod->compiled = NULL;
5726
5727 /* revert compilation of modules implemented by dependency */
5728 for (u = 0; u < ctx.ctx->list.count; ++u) {
5729 m = ctx.ctx->list.objs[u];
5730 if (m->implemented == 2) {
5731 /* revert features list to the precompiled state */
5732 lys_feature_precompile_revert(&ctx, m);
5733 /* mark module as imported-only / not-implemented */
5734 m->implemented = 0;
5735 /* free the compiled version of the module */
5736 lysc_module_free(m->compiled, NULL);
5737 m->compiled = NULL;
5738 }
5739 }
5740
Radek Krejci19a96102018-11-15 13:38:09 +01005741 return ret;
5742}