blob: a9bf7de15acab31dbbb1533921b66c3fe66e4d1a [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <ctype.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <stdio.h>
Radek Krejci19a96102018-11-15 13:38:09 +010019
20#include "libyang.h"
21#include "context.h"
22#include "tree_schema_internal.h"
23#include "xpath.h"
24
25/**
26 * @brief Duplicate string into dictionary
27 * @param[in] CTX libyang context of the dictionary.
28 * @param[in] ORIG String to duplicate.
29 * @param[out] DUP Where to store the result.
30 */
31#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
32
33#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
34 if (ARRAY_P) { \
35 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010036 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010037 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
38 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010039 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER + __array_offset]); \
40 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
41 } \
42 }
43
Radek Krejci6eeb58f2019-02-22 16:29:37 +010044#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, OPTIONS, ITER, FUNC, USES_STATUS, RET, GOTO) \
45 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
47 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
48 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
50 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
51 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejcid05cbd92018-12-05 14:26:40 +010055#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
56 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
61 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
66#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
67 if (MEMBER_P) { \
68 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
69 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
70 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
71 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 }
73
Radek Krejci00b874b2019-02-12 10:54:50 +010074#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \
75 if (MEMBER_P) { \
76 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
77 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
78 LY_ARRAY_INCREMENT(ARRAY_C); \
79 RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \
80 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
81 }
82
Radek Krejcid05cbd92018-12-05 14:26:40 +010083#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
84 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010085 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
86 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
88 return LY_EVALID; \
89 } \
90 } \
91 }
92
Radek Krejci19a96102018-11-15 13:38:09 +010093static struct lysc_ext_instance *
94lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
95{
96 /* TODO */
97 (void) ctx;
98 (void) orig;
99 return NULL;
100}
101
Radek Krejcib56c7502019-02-13 14:19:54 +0100102/**
103 * @brief Duplicate the compiled pattern structure.
104 *
105 * Instead of duplicating memory, the reference counter in the @p orig is increased.
106 *
107 * @param[in] orig The pattern structure to duplicate.
108 * @return The duplicated structure to use.
109 */
Radek Krejci19a96102018-11-15 13:38:09 +0100110static struct lysc_pattern*
111lysc_pattern_dup(struct lysc_pattern *orig)
112{
113 ++orig->refcount;
114 return orig;
115}
116
Radek Krejcib56c7502019-02-13 14:19:54 +0100117/**
118 * @brief Duplicate the array of compiled patterns.
119 *
120 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
121 *
122 * @param[in] ctx Libyang context for logging.
123 * @param[in] orig The patterns sized array to duplicate.
124 * @return New sized array as a copy of @p orig.
125 * @return NULL in case of memory allocation error.
126 */
Radek Krejci19a96102018-11-15 13:38:09 +0100127static struct lysc_pattern**
128lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
129{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100130 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100131 unsigned int u;
132
Radek Krejcib56c7502019-02-13 14:19:54 +0100133 assert(orig);
134
Radek Krejci19a96102018-11-15 13:38:09 +0100135 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
136 LY_ARRAY_FOR(orig, u) {
137 dup[u] = lysc_pattern_dup(orig[u]);
138 LY_ARRAY_INCREMENT(dup);
139 }
140 return dup;
141}
142
Radek Krejcib56c7502019-02-13 14:19:54 +0100143/**
144 * @brief Duplicate compiled range structure.
145 *
146 * @param[in] ctx Libyang context for logging.
147 * @param[in] orig The range structure to be duplicated.
148 * @return New compiled range structure as a copy of @p orig.
149 * @return NULL in case of memory allocation error.
150 */
Radek Krejci19a96102018-11-15 13:38:09 +0100151struct lysc_range*
152lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
153{
154 struct lysc_range *dup;
155 LY_ERR ret;
156
Radek Krejcib56c7502019-02-13 14:19:54 +0100157 assert(orig);
158
Radek Krejci19a96102018-11-15 13:38:09 +0100159 dup = calloc(1, sizeof *dup);
160 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
161 if (orig->parts) {
162 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
163 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
164 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
165 }
166 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
167 DUP_STRING(ctx, orig->emsg, dup->emsg);
168 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
169
170 return dup;
171cleanup:
172 free(dup);
173 (void) ret; /* set but not used due to the return type */
174 return NULL;
175}
176
Radek Krejcib56c7502019-02-13 14:19:54 +0100177/**
178 * @brief Stack for processing if-feature expressions.
179 */
Radek Krejci19a96102018-11-15 13:38:09 +0100180struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100181 int size; /**< number of items in the stack */
182 int index; /**< first empty item */
183 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100184};
185
Radek Krejcib56c7502019-02-13 14:19:54 +0100186/**
187 * @brief Add @ref ifftokens into the stack.
188 * @param[in] stack The if-feature stack to use.
189 * @param[in] value One of the @ref ifftokens to store in the stack.
190 * @return LY_EMEM in case of memory allocation error
191 * @return LY_ESUCCESS if the value successfully stored.
192 */
Radek Krejci19a96102018-11-15 13:38:09 +0100193static LY_ERR
194iff_stack_push(struct iff_stack *stack, uint8_t value)
195{
196 if (stack->index == stack->size) {
197 stack->size += 4;
198 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
199 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
200 }
201 stack->stack[stack->index++] = value;
202 return LY_SUCCESS;
203}
204
Radek Krejcib56c7502019-02-13 14:19:54 +0100205/**
206 * @brief Get (and remove) the last item form the stack.
207 * @param[in] stack The if-feature stack to use.
208 * @return The value from the top of the stack.
209 */
Radek Krejci19a96102018-11-15 13:38:09 +0100210static uint8_t
211iff_stack_pop(struct iff_stack *stack)
212{
Radek Krejcib56c7502019-02-13 14:19:54 +0100213 assert(stack && stack->index);
214
Radek Krejci19a96102018-11-15 13:38:09 +0100215 stack->index--;
216 return stack->stack[stack->index];
217}
218
Radek Krejcib56c7502019-02-13 14:19:54 +0100219/**
220 * @brief Clean up the stack.
221 * @param[in] stack The if-feature stack to use.
222 */
Radek Krejci19a96102018-11-15 13:38:09 +0100223static void
224iff_stack_clean(struct iff_stack *stack)
225{
226 stack->size = 0;
227 free(stack->stack);
228}
229
Radek Krejcib56c7502019-02-13 14:19:54 +0100230/**
231 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
232 * (libyang format of the if-feature expression).
233 * @param[in,out] list The 2bits array to modify.
234 * @param[in] op The operand (@ref ifftokens) to store.
235 * @param[in] pos Position (0-based) where to store the given @p op.
236 */
Radek Krejci19a96102018-11-15 13:38:09 +0100237static void
238iff_setop(uint8_t *list, uint8_t op, int pos)
239{
240 uint8_t *item;
241 uint8_t mask = 3;
242
243 assert(pos >= 0);
244 assert(op <= 3); /* max 2 bits */
245
246 item = &list[pos / 4];
247 mask = mask << 2 * (pos % 4);
248 *item = (*item) & ~mask;
249 *item = (*item) | (op << 2 * (pos % 4));
250}
251
Radek Krejcib56c7502019-02-13 14:19:54 +0100252#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
253#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100254
Radek Krejci0af46292019-01-11 16:02:31 +0100255/**
256 * @brief Find a feature of the given name and referenced in the given module.
257 *
258 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
259 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
260 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
261 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
262 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
263 * assigned till that time will be still valid.
264 *
265 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
266 * @param[in] name Name of the feature including possible prefix.
267 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
268 * @return Pointer to the feature structure if found, NULL otherwise.
269 */
Radek Krejci19a96102018-11-15 13:38:09 +0100270static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100271lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100272{
273 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100274 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100275
276 for (i = 0; i < len; ++i) {
277 if (name[i] == ':') {
278 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100279 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100280 LY_CHECK_RET(!mod, NULL);
281
282 name = &name[i + 1];
283 len = len - i - 1;
284 }
285 }
286
287 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100288 if (mod->implemented) {
289 /* module is implemented so there is already the compiled schema */
290 flist = mod->compiled->features;
291 } else {
292 flist = mod->off_features;
293 }
294 LY_ARRAY_FOR(flist, i) {
295 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100296 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
297 return f;
298 }
299 }
300
301 return NULL;
302}
303
304static LY_ERR
305lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
306{
307 const char *name;
308 unsigned int u;
309 const struct lys_module *mod;
310 struct lysp_ext *edef = NULL;
311
312 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
313 ext->insubstmt = ext_p->insubstmt;
314 ext->insubstmt_index = ext_p->insubstmt_index;
315
316 /* get module where the extension definition should be placed */
317 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100318 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100319 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
320 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
321 LY_EVALID);
322 LY_CHECK_ERR_RET(!mod->parsed->extensions,
323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
324 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100325 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100326 LY_EVALID);
327 name = &ext_p->name[u + 1];
328 /* find the extension definition there */
329 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
330 if (!strcmp(name, mod->parsed->extensions[u].name)) {
331 edef = &mod->parsed->extensions[u];
332 break;
333 }
334 }
335 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
336 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
337 LY_EVALID);
338 /* TODO plugins */
339
340 return LY_SUCCESS;
341}
342
Radek Krejcib56c7502019-02-13 14:19:54 +0100343/**
344 * @brief Compile information from the if-feature statement
345 * @param[in] ctx Compile context.
346 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
347 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
348 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
349 * @return LY_ERR value.
350 */
Radek Krejci19a96102018-11-15 13:38:09 +0100351static LY_ERR
352lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
353{
354 const char *c = *value;
355 int r, rc = EXIT_FAILURE;
356 int i, j, last_not, checkversion = 0;
357 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
358 uint8_t op;
359 struct iff_stack stack = {0, 0, NULL};
360 struct lysc_feature *f;
361
362 assert(c);
363
364 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
365 for (i = j = last_not = 0; c[i]; i++) {
366 if (c[i] == '(') {
367 j++;
368 checkversion = 1;
369 continue;
370 } else if (c[i] == ')') {
371 j--;
372 continue;
373 } else if (isspace(c[i])) {
374 checkversion = 1;
375 continue;
376 }
377
378 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
379 if (c[i + r] == '\0') {
380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
381 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
382 return LY_EVALID;
383 } else if (!isspace(c[i + r])) {
384 /* feature name starting with the not/and/or */
385 last_not = 0;
386 f_size++;
387 } else if (c[i] == 'n') { /* not operation */
388 if (last_not) {
389 /* double not */
390 expr_size = expr_size - 2;
391 last_not = 0;
392 } else {
393 last_not = 1;
394 }
395 } else { /* and, or */
396 f_exp++;
397 /* not a not operation */
398 last_not = 0;
399 }
400 i += r;
401 } else {
402 f_size++;
403 last_not = 0;
404 }
405 expr_size++;
406
407 while (!isspace(c[i])) {
408 if (!c[i] || c[i] == ')') {
409 i--;
410 break;
411 }
412 i++;
413 }
414 }
415 if (j || f_exp != f_size) {
416 /* not matching count of ( and ) */
417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
418 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
419 return LY_EVALID;
420 }
421
422 if (checkversion || expr_size > 1) {
423 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100424 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
426 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
427 return LY_EVALID;
428 }
429 }
430
431 /* allocate the memory */
432 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
433 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
434 stack.stack = malloc(expr_size * sizeof *stack.stack);
435 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
436
437 stack.size = expr_size;
438 f_size--; expr_size--; /* used as indexes from now */
439
440 for (i--; i >= 0; i--) {
441 if (c[i] == ')') {
442 /* push it on stack */
443 iff_stack_push(&stack, LYS_IFF_RP);
444 continue;
445 } else if (c[i] == '(') {
446 /* pop from the stack into result all operators until ) */
447 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
448 iff_setop(iff->expr, op, expr_size--);
449 }
450 continue;
451 } else if (isspace(c[i])) {
452 continue;
453 }
454
455 /* end of operator or operand -> find beginning and get what is it */
456 j = i + 1;
457 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
458 i--;
459 }
460 i++; /* go back by one step */
461
462 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
463 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
464 /* double not */
465 iff_stack_pop(&stack);
466 } else {
467 /* not has the highest priority, so do not pop from the stack
468 * as in case of AND and OR */
469 iff_stack_push(&stack, LYS_IFF_NOT);
470 }
471 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
472 /* as for OR - pop from the stack all operators with the same or higher
473 * priority and store them to the result, then push the AND to the stack */
474 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
475 op = iff_stack_pop(&stack);
476 iff_setop(iff->expr, op, expr_size--);
477 }
478 iff_stack_push(&stack, LYS_IFF_AND);
479 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
480 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
481 op = iff_stack_pop(&stack);
482 iff_setop(iff->expr, op, expr_size--);
483 }
484 iff_stack_push(&stack, LYS_IFF_OR);
485 } else {
486 /* feature name, length is j - i */
487
488 /* add it to the expression */
489 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
490
491 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100492 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
493 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
494 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
495 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100496 iff->features[f_size] = f;
497 LY_ARRAY_INCREMENT(iff->features);
498 f_size--;
499 }
500 }
501 while (stack.index) {
502 op = iff_stack_pop(&stack);
503 iff_setop(iff->expr, op, expr_size--);
504 }
505
506 if (++expr_size || ++f_size) {
507 /* not all expected operators and operands found */
508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
509 "Invalid value \"%s\" of if-feature - processing error.", *value);
510 rc = LY_EINT;
511 } else {
512 rc = LY_SUCCESS;
513 }
514
515error:
516 /* cleanup */
517 iff_stack_clean(&stack);
518
519 return rc;
520}
521
Radek Krejcib56c7502019-02-13 14:19:54 +0100522/**
523 * @brief Compile information from the when statement
524 * @param[in] ctx Compile context.
525 * @param[in] when_p The parsed when statement structure.
526 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
527 * @param[out] when Pointer where to store pointer to the created compiled when structure.
528 * @return LY_ERR value.
529 */
Radek Krejci19a96102018-11-15 13:38:09 +0100530static LY_ERR
Radek Krejci00b874b2019-02-12 10:54:50 +0100531lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100532{
533 unsigned int u;
534 LY_ERR ret = LY_SUCCESS;
535
Radek Krejci00b874b2019-02-12 10:54:50 +0100536 *when = calloc(1, sizeof **when);
537 (*when)->refcount = 1;
538 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
539 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
540 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
541 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
542 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, options, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100543
544done:
545 return ret;
546}
547
Radek Krejcib56c7502019-02-13 14:19:54 +0100548/**
549 * @brief Compile information from the must statement
550 * @param[in] ctx Compile context.
551 * @param[in] must_p The parsed must statement structure.
552 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
553 * @param[in,out] must Prepared (empty) compiled must structure to fill.
554 * @return LY_ERR value.
555 */
Radek Krejci19a96102018-11-15 13:38:09 +0100556static LY_ERR
557lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
558{
559 unsigned int u;
560 LY_ERR ret = LY_SUCCESS;
561
562 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
563 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
564
565 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
566 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100567 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
568 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100569 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
570
571done:
572 return ret;
573}
574
Radek Krejcib56c7502019-02-13 14:19:54 +0100575/**
576 * @brief Compile information from the import statement
577 * @param[in] ctx Compile context.
578 * @param[in] imp_p The parsed import statement structure.
579 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
580 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
581 * @return LY_ERR value.
582 */
Radek Krejci19a96102018-11-15 13:38:09 +0100583static LY_ERR
584lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
585{
586 unsigned int u;
587 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100588 LY_ERR ret = LY_SUCCESS;
589
590 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
591 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
592 imp->module = imp_p->module;
593
Radek Krejci7f2a5362018-11-28 13:05:37 +0100594 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
595 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100596 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100597 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 /* try to use filepath if present */
599 if (imp->module->filepath) {
600 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
601 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100602 if (mod != imp->module) {
603 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100605 mod = NULL;
606 }
607 }
608 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100609 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100610 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100611 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100612 return LY_ENOTFOUND;
613 }
614 }
Radek Krejci19a96102018-11-15 13:38:09 +0100615 }
616
617done:
618 return ret;
619}
620
Radek Krejcib56c7502019-02-13 14:19:54 +0100621/**
622 * @brief Compile information from the identity statement
623 *
624 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
625 *
626 * @param[in] ctx Compile context.
627 * @param[in] ident_p The parsed identity statement structure.
628 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
629 * @param[in] idents List of so far compiled identities to check the name uniqueness.
630 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
631 * @return LY_ERR value.
632 */
Radek Krejci19a96102018-11-15 13:38:09 +0100633static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100634lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100635{
636 unsigned int u;
637 LY_ERR ret = LY_SUCCESS;
638
Radek Krejcid05cbd92018-12-05 14:26:40 +0100639 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100640 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200641 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
642 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200643 ident->module = ctx->mod;
Radek Krejci19a96102018-11-15 13:38:09 +0100644 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
645 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
646 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
647 ident->flags = ident_p->flags;
648
649done:
650 return ret;
651}
652
Radek Krejcib56c7502019-02-13 14:19:54 +0100653/**
654 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
655 *
656 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
657 *
658 * @param[in] ctx Compile context for logging.
659 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
660 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
661 * @return LY_SUCCESS if everything is ok.
662 * @return LY_EVALID if the identity is derived from itself.
663 */
Radek Krejci38222632019-02-12 16:55:05 +0100664static LY_ERR
665lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
666{
667 LY_ERR ret = LY_EVALID;
668 unsigned int u, v;
669 struct ly_set recursion = {0};
670 struct lysc_ident *drv;
671
672 if (!derived) {
673 return LY_SUCCESS;
674 }
675
676 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
677 if (ident == derived[u]) {
678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
679 "Identity \"%s\" is indirectly derived from itself.", ident->name);
680 goto cleanup;
681 }
682 ly_set_add(&recursion, derived[u], 0);
683 }
684
685 for (v = 0; v < recursion.count; ++v) {
686 drv = recursion.objs[v];
687 if (!drv->derived) {
688 continue;
689 }
690 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
691 if (ident == drv->derived[u]) {
692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
693 "Identity \"%s\" is indirectly derived from itself.", ident->name);
694 goto cleanup;
695 }
696 ly_set_add(&recursion, drv->derived[u], 0);
697 }
698 }
699 ret = LY_SUCCESS;
700
701cleanup:
702 ly_set_erase(&recursion, NULL);
703 return ret;
704}
705
Radek Krejcia3045382018-11-22 14:30:31 +0100706/**
707 * @brief Find and process the referenced base identities from another identity or identityref
708 *
709 * For bases in identity se backlinks to them from the base identities. For identityref, store
710 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
711 * to distinguish these two use cases.
712 *
713 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
714 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
715 * @param[in] ident Referencing identity to work with.
716 * @param[in] bases Array of bases of identityref to fill in.
717 * @return LY_ERR value.
718 */
Radek Krejci19a96102018-11-15 13:38:09 +0100719static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100720lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +0100721{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100722 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100723 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100724 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100725 struct lysc_ident **idref;
726
727 assert(ident || bases);
728
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
731 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
732 return LY_EVALID;
733 }
734
735 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
736 s = strchr(bases_p[u], ':');
737 if (s) {
738 /* prefixed identity */
739 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100740 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100741 } else {
742 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100743 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100744 }
745 if (!mod) {
746 if (ident) {
747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
748 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
749 } else {
750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
751 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
752 }
753 return LY_EVALID;
754 }
755 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100756 if (mod->compiled && mod->compiled->identities) {
757 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
758 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100759 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100760 if (ident == &mod->compiled->identities[v]) {
761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
762 "Identity \"%s\" is derived from itself.", ident->name);
763 return LY_EVALID;
764 }
765 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100766 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100767 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100768 *idref = ident;
769 } else {
770 /* we have match! store the found identity */
771 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100772 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100773 }
774 break;
775 }
776 }
777 }
778 if (!idref || !(*idref)) {
779 if (ident) {
780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
781 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
782 } else {
783 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
784 "Unable to find base (%s) of identityref.", bases_p[u]);
785 }
786 return LY_EVALID;
787 }
788 }
789 return LY_SUCCESS;
790}
791
Radek Krejcia3045382018-11-22 14:30:31 +0100792/**
793 * @brief For the given array of identities, set the backlinks from all their base identities.
794 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
795 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
796 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
797 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
798 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100799static LY_ERR
800lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
801{
802 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100803
804 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
805 if (!idents_p[i].bases) {
806 continue;
807 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100808 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100809 }
810 return LY_SUCCESS;
811}
812
Radek Krejci0af46292019-01-11 16:02:31 +0100813LY_ERR
Radek Krejci693262f2019-04-29 15:23:20 +0200814lys_feature_precompile(struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +0100815{
816 unsigned int offset = 0, u;
817 struct lysc_ctx context = {0};
818
819 assert(ctx);
820 context.ctx = ctx;
821
822 if (!features_p) {
823 return LY_SUCCESS;
824 }
825 if (*features) {
826 offset = LY_ARRAY_SIZE(*features);
827 }
828
829 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
830 LY_ARRAY_FOR(features_p, u) {
831 LY_ARRAY_INCREMENT(*features);
832 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
833 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
834 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
835 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
836 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci693262f2019-04-29 15:23:20 +0200837 (*features)[offset + u].module = module;
Radek Krejci0af46292019-01-11 16:02:31 +0100838 }
839
840 return LY_SUCCESS;
841}
842
Radek Krejcia3045382018-11-22 14:30:31 +0100843/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100844 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100845 *
846 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
847 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100848 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100849 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
850 * being currently processed).
851 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +0100852 * @return LY_SUCCESS if everything is ok.
853 * @return LY_EVALID if the feature references indirectly itself.
854 */
855static LY_ERR
856lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
857{
858 LY_ERR ret = LY_EVALID;
859 unsigned int u, v;
860 struct ly_set recursion = {0};
861 struct lysc_feature *drv;
862
863 if (!depfeatures) {
864 return LY_SUCCESS;
865 }
866
867 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
868 if (feature == depfeatures[u]) {
869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
870 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
871 goto cleanup;
872 }
873 ly_set_add(&recursion, depfeatures[u], 0);
874 }
875
876 for (v = 0; v < recursion.count; ++v) {
877 drv = recursion.objs[v];
878 if (!drv->depfeatures) {
879 continue;
880 }
881 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
882 if (feature == drv->depfeatures[u]) {
883 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
884 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
885 goto cleanup;
886 }
887 ly_set_add(&recursion, drv->depfeatures[u], 0);
888 }
889 }
890 ret = LY_SUCCESS;
891
892cleanup:
893 ly_set_erase(&recursion, NULL);
894 return ret;
895}
896
897/**
Radek Krejci0af46292019-01-11 16:02:31 +0100898 * @brief Create pre-compiled features array.
899 *
900 * See lys_feature_precompile() for more details.
901 *
Radek Krejcia3045382018-11-22 14:30:31 +0100902 * @param[in] ctx Compile context.
903 * @param[in] feature_p Parsed feature definition to compile.
904 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci0af46292019-01-11 16:02:31 +0100905 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100906 * @return LY_ERR value.
907 */
Radek Krejci19a96102018-11-15 13:38:09 +0100908static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100909lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +0100910{
Radek Krejci0af46292019-01-11 16:02:31 +0100911 unsigned int u, v, x;
912 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100913 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100914
Radek Krejci0af46292019-01-11 16:02:31 +0100915 /* find the preprecompiled feature */
916 LY_ARRAY_FOR(features, x) {
917 if (strcmp(features[x].name, feature_p->name)) {
918 continue;
919 }
920 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100921
Radek Krejci0af46292019-01-11 16:02:31 +0100922 /* finish compilation started in lys_feature_precompile() */
923 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
924 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
925 if (feature->iffeatures) {
926 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
927 if (feature->iffeatures[u].features) {
928 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100929 /* check for circular dependency - direct reference first,... */
930 if (feature == feature->iffeatures[u].features[v]) {
931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
932 "Feature \"%s\" is referenced from itself.", feature->name);
933 return LY_EVALID;
934 }
935 /* ... and indirect circular reference */
936 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
937
Radek Krejci0af46292019-01-11 16:02:31 +0100938 /* add itself into the dependants list */
939 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
940 *df = feature;
941 }
Radek Krejci19a96102018-11-15 13:38:09 +0100942 }
Radek Krejci19a96102018-11-15 13:38:09 +0100943 }
944 }
Radek Krejci0af46292019-01-11 16:02:31 +0100945 done:
946 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100947 }
Radek Krejci0af46292019-01-11 16:02:31 +0100948
949 LOGINT(ctx->ctx);
950 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100951}
952
Radek Krejcib56c7502019-02-13 14:19:54 +0100953/**
954 * @brief Revert compiled list of features back to the precompiled state.
955 *
956 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
957 * The features are supposed to be stored again as off_features in ::lys_module structure.
958 *
959 * @param[in] ctx Compilation context.
960 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
961 * and supposed to hold the off_features list.
962 */
Radek Krejci95710c92019-02-11 15:49:55 +0100963static void
964lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
965{
966 unsigned int u, v;
967
968 /* keep the off_features list until the complete lys_module is freed */
969 mod->off_features = mod->compiled->features;
970 mod->compiled->features = NULL;
971
972 /* in the off_features list, remove all the parts (from finished compiling process)
973 * which may points into the data being freed here */
974 LY_ARRAY_FOR(mod->off_features, u) {
975 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
976 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
977 }
978 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
979 mod->off_features[u].iffeatures = NULL;
980
981 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
982 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
983 }
984 LY_ARRAY_FREE(mod->off_features[u].exts);
985 mod->off_features[u].exts = NULL;
986 }
987}
988
Radek Krejcia3045382018-11-22 14:30:31 +0100989/**
990 * @brief Validate and normalize numeric value from a range definition.
991 * @param[in] ctx Compile context.
992 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
993 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
994 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
995 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
996 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
997 * @param[in] value String value of the range boundary.
998 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
999 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1000 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1001 */
Radek Krejci19a96102018-11-15 13:38:09 +01001002static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001003range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001004{
Radek Krejci6cba4292018-11-15 17:33:29 +01001005 size_t fraction = 0, size;
1006
Radek Krejci19a96102018-11-15 13:38:09 +01001007 *len = 0;
1008
1009 assert(value);
1010 /* parse value */
1011 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1012 return LY_EVALID;
1013 }
1014
1015 if ((value[*len] == '-') || (value[*len] == '+')) {
1016 ++(*len);
1017 }
1018
1019 while (isdigit(value[*len])) {
1020 ++(*len);
1021 }
1022
1023 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001024 if (basetype == LY_TYPE_DEC64) {
1025 goto decimal;
1026 } else {
1027 *valcopy = strndup(value, *len);
1028 return LY_SUCCESS;
1029 }
Radek Krejci19a96102018-11-15 13:38:09 +01001030 }
1031 fraction = *len;
1032
1033 ++(*len);
1034 while (isdigit(value[*len])) {
1035 ++(*len);
1036 }
1037
Radek Krejci6cba4292018-11-15 17:33:29 +01001038 if (basetype == LY_TYPE_DEC64) {
1039decimal:
1040 assert(frdigits);
1041 if (*len - 1 - fraction > frdigits) {
1042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1043 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1044 *len, value, frdigits);
1045 return LY_EINVAL;
1046 }
1047 if (fraction) {
1048 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1049 } else {
1050 size = (*len) + frdigits + 1;
1051 }
1052 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001053 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1054
Radek Krejci6cba4292018-11-15 17:33:29 +01001055 (*valcopy)[size - 1] = '\0';
1056 if (fraction) {
1057 memcpy(&(*valcopy)[0], &value[0], fraction);
1058 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1059 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1060 } else {
1061 memcpy(&(*valcopy)[0], &value[0], *len);
1062 memset(&(*valcopy)[*len], '0', frdigits);
1063 }
Radek Krejci19a96102018-11-15 13:38:09 +01001064 }
1065 return LY_SUCCESS;
1066}
1067
Radek Krejcia3045382018-11-22 14:30:31 +01001068/**
1069 * @brief Check that values in range are in ascendant order.
1070 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001071 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1072 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001073 * @param[in] value Current value to check.
1074 * @param[in] prev_value The last seen value.
1075 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1076 */
Radek Krejci19a96102018-11-15 13:38:09 +01001077static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001078range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001079{
1080 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001081 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001082 return LY_EEXIST;
1083 }
1084 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001085 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001086 return LY_EEXIST;
1087 }
1088 }
1089 return LY_SUCCESS;
1090}
1091
Radek Krejcia3045382018-11-22 14:30:31 +01001092/**
1093 * @brief Set min/max value of the range part.
1094 * @param[in] ctx Compile context.
1095 * @param[in] part Range part structure to fill.
1096 * @param[in] max Flag to distinguish if storing min or max value.
1097 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1098 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1099 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1100 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1101 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001102 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001103 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1104 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1105 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1106 * frdigits value), LY_EMEM.
1107 */
Radek Krejci19a96102018-11-15 13:38:09 +01001108static LY_ERR
1109range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001110 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001111{
1112 LY_ERR ret = LY_SUCCESS;
1113 char *valcopy = NULL;
1114 size_t len;
1115
1116 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001117 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001118 LY_CHECK_GOTO(ret, finalize);
1119 }
1120 if (!valcopy && base_range) {
1121 if (max) {
1122 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1123 } else {
1124 part->min_64 = base_range->parts[0].min_64;
1125 }
1126 if (!first) {
1127 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1128 }
1129 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001130 }
1131
1132 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001133 case LY_TYPE_INT8: /* range */
1134 if (valcopy) {
1135 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1136 } else if (max) {
1137 part->max_64 = INT64_C(127);
1138 } else {
1139 part->min_64 = INT64_C(-128);
1140 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001141 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001142 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001143 }
1144 break;
1145 case LY_TYPE_INT16: /* range */
1146 if (valcopy) {
1147 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1148 } else if (max) {
1149 part->max_64 = INT64_C(32767);
1150 } else {
1151 part->min_64 = INT64_C(-32768);
1152 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001153 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001154 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001155 }
1156 break;
1157 case LY_TYPE_INT32: /* range */
1158 if (valcopy) {
1159 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1160 } else if (max) {
1161 part->max_64 = INT64_C(2147483647);
1162 } else {
1163 part->min_64 = INT64_C(-2147483648);
1164 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001165 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001166 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001167 }
1168 break;
1169 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001170 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001171 if (valcopy) {
1172 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1173 max ? &part->max_64 : &part->min_64);
1174 } else if (max) {
1175 part->max_64 = INT64_C(9223372036854775807);
1176 } else {
1177 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1178 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001179 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001180 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001181 }
1182 break;
1183 case LY_TYPE_UINT8: /* range */
1184 if (valcopy) {
1185 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1186 } else if (max) {
1187 part->max_u64 = UINT64_C(255);
1188 } else {
1189 part->min_u64 = UINT64_C(0);
1190 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001191 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001192 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001193 }
1194 break;
1195 case LY_TYPE_UINT16: /* range */
1196 if (valcopy) {
1197 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1198 } else if (max) {
1199 part->max_u64 = UINT64_C(65535);
1200 } else {
1201 part->min_u64 = UINT64_C(0);
1202 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001203 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001204 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001205 }
1206 break;
1207 case LY_TYPE_UINT32: /* range */
1208 if (valcopy) {
1209 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1210 } else if (max) {
1211 part->max_u64 = UINT64_C(4294967295);
1212 } else {
1213 part->min_u64 = UINT64_C(0);
1214 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001215 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001216 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001217 }
1218 break;
1219 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001220 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001221 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001222 if (valcopy) {
1223 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1224 } else if (max) {
1225 part->max_u64 = UINT64_C(18446744073709551615);
1226 } else {
1227 part->min_u64 = UINT64_C(0);
1228 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001229 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001230 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001231 }
1232 break;
1233 default:
1234 LOGINT(ctx->ctx);
1235 ret = LY_EINT;
1236 }
1237
Radek Krejci5969f272018-11-23 10:03:58 +01001238finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001239 if (ret == LY_EDENIED) {
1240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1241 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1242 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1243 } else if (ret == LY_EVALID) {
1244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1245 "Invalid %s restriction - invalid value \"%s\".",
1246 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1247 } else if (ret == LY_EEXIST) {
1248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1249 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001250 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001251 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001252 } else if (!ret && value) {
1253 *value = *value + len;
1254 }
1255 free(valcopy);
1256 return ret;
1257}
1258
Radek Krejcia3045382018-11-22 14:30:31 +01001259/**
1260 * @brief Compile the parsed range restriction.
1261 * @param[in] ctx Compile context.
1262 * @param[in] range_p Parsed range structure to compile.
1263 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1264 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1265 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1266 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1267 * range restriction must be more restrictive than the base_range.
1268 * @param[in,out] range Pointer to the created current range structure.
1269 * @return LY_ERR value.
1270 */
Radek Krejci19a96102018-11-15 13:38:09 +01001271static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001272lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001273 struct lysc_range *base_range, struct lysc_range **range)
1274{
1275 LY_ERR ret = LY_EVALID;
1276 const char *expr;
1277 struct lysc_range_part *parts = NULL, *part;
1278 int range_expected = 0, uns;
1279 unsigned int parts_done = 0, u, v;
1280
1281 assert(range);
1282 assert(range_p);
1283
1284 expr = range_p->arg;
1285 while(1) {
1286 if (isspace(*expr)) {
1287 ++expr;
1288 } else if (*expr == '\0') {
1289 if (range_expected) {
1290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1291 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1292 length_restr ? "length" : "range", range_p->arg);
1293 goto cleanup;
1294 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1296 "Invalid %s restriction - unexpected end of the expression (%s).",
1297 length_restr ? "length" : "range", range_p->arg);
1298 goto cleanup;
1299 }
1300 parts_done++;
1301 break;
1302 } else if (!strncmp(expr, "min", 3)) {
1303 if (parts) {
1304 /* min cannot be used elsewhere than in the first part */
1305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1306 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1307 expr - range_p->arg, range_p->arg);
1308 goto cleanup;
1309 }
1310 expr += 3;
1311
1312 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001313 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001314 part->max_64 = part->min_64;
1315 } else if (*expr == '|') {
1316 if (!parts || range_expected) {
1317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1318 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1319 goto cleanup;
1320 }
1321 expr++;
1322 parts_done++;
1323 /* process next part of the expression */
1324 } else if (!strncmp(expr, "..", 2)) {
1325 expr += 2;
1326 while (isspace(*expr)) {
1327 expr++;
1328 }
1329 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1331 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1332 goto cleanup;
1333 }
1334 /* continue expecting the upper boundary */
1335 range_expected = 1;
1336 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1337 /* number */
1338 if (range_expected) {
1339 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001340 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001341 range_expected = 0;
1342 } else {
1343 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1344 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001345 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001346 part->max_64 = part->min_64;
1347 }
1348
1349 /* continue with possible another expression part */
1350 } else if (!strncmp(expr, "max", 3)) {
1351 expr += 3;
1352 while (isspace(*expr)) {
1353 expr++;
1354 }
1355 if (*expr != '\0') {
1356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1357 length_restr ? "length" : "range", expr);
1358 goto cleanup;
1359 }
1360 if (range_expected) {
1361 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001362 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001363 range_expected = 0;
1364 } else {
1365 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1366 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001367 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001368 part->min_64 = part->max_64;
1369 }
1370 } else {
1371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1372 length_restr ? "length" : "range", expr);
1373 goto cleanup;
1374 }
1375 }
1376
1377 /* check with the previous range/length restriction */
1378 if (base_range) {
1379 switch (basetype) {
1380 case LY_TYPE_BINARY:
1381 case LY_TYPE_UINT8:
1382 case LY_TYPE_UINT16:
1383 case LY_TYPE_UINT32:
1384 case LY_TYPE_UINT64:
1385 case LY_TYPE_STRING:
1386 uns = 1;
1387 break;
1388 case LY_TYPE_DEC64:
1389 case LY_TYPE_INT8:
1390 case LY_TYPE_INT16:
1391 case LY_TYPE_INT32:
1392 case LY_TYPE_INT64:
1393 uns = 0;
1394 break;
1395 default:
1396 LOGINT(ctx->ctx);
1397 ret = LY_EINT;
1398 goto cleanup;
1399 }
1400 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1401 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1402 goto baseerror;
1403 }
1404 /* current lower bound is not lower than the base */
1405 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1406 /* base has single value */
1407 if (base_range->parts[v].min_64 == parts[u].min_64) {
1408 /* both lower bounds are the same */
1409 if (parts[u].min_64 != parts[u].max_64) {
1410 /* current continues with a range */
1411 goto baseerror;
1412 } else {
1413 /* equal single values, move both forward */
1414 ++v;
1415 continue;
1416 }
1417 } else {
1418 /* base is single value lower than current range, so the
1419 * value from base range is removed in the current,
1420 * move only base and repeat checking */
1421 ++v;
1422 --u;
1423 continue;
1424 }
1425 } else {
1426 /* base is the range */
1427 if (parts[u].min_64 == parts[u].max_64) {
1428 /* current is a single value */
1429 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1430 /* current is behind the base range, so base range is omitted,
1431 * move the base and keep the current for further check */
1432 ++v;
1433 --u;
1434 } /* else it is within the base range, so move the current, but keep the base */
1435 continue;
1436 } else {
1437 /* both are ranges - check the higher bound, the lower was already checked */
1438 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1439 /* higher bound is higher than the current higher bound */
1440 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1441 /* but the current lower bound is also higher, so the base range is omitted,
1442 * continue with the same current, but move the base */
1443 --u;
1444 ++v;
1445 continue;
1446 }
1447 /* current range starts within the base range but end behind it */
1448 goto baseerror;
1449 } else {
1450 /* current range is smaller than the base,
1451 * move current, but stay with the base */
1452 continue;
1453 }
1454 }
1455 }
1456 }
1457 if (u != parts_done) {
1458baseerror:
1459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1460 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1461 length_restr ? "length" : "range", range_p->arg);
1462 goto cleanup;
1463 }
1464 }
1465
1466 if (!(*range)) {
1467 *range = calloc(1, sizeof **range);
1468 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1469 }
1470
Radek Krejcic8b31002019-01-08 10:24:45 +01001471 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001472 if (range_p->eapptag) {
1473 lydict_remove(ctx->ctx, (*range)->eapptag);
1474 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1475 }
1476 if (range_p->emsg) {
1477 lydict_remove(ctx->ctx, (*range)->emsg);
1478 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1479 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001480 if (range_p->dsc) {
1481 lydict_remove(ctx->ctx, (*range)->dsc);
1482 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1483 }
1484 if (range_p->ref) {
1485 lydict_remove(ctx->ctx, (*range)->ref);
1486 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1487 }
Radek Krejci19a96102018-11-15 13:38:09 +01001488 /* extensions are taken only from the last range by the caller */
1489
1490 (*range)->parts = parts;
1491 parts = NULL;
1492 ret = LY_SUCCESS;
1493cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001494 LY_ARRAY_FREE(parts);
1495
1496 return ret;
1497}
1498
1499/**
1500 * @brief Checks pattern syntax.
1501 *
Radek Krejcia3045382018-11-22 14:30:31 +01001502 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001503 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001504 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1505 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001506 */
1507static LY_ERR
1508lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1509{
1510 int idx, idx2, start, end, err_offset, count;
1511 char *perl_regex, *ptr;
1512 const char *err_msg, *orig_ptr;
1513 pcre *precomp;
1514#define URANGE_LEN 19
1515 char *ublock2urange[][2] = {
1516 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1517 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1518 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1519 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1520 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1521 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1522 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1523 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1524 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1525 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1526 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1527 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1528 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1529 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1530 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1531 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1532 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1533 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1534 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1535 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1536 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1537 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1538 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1539 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1540 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1541 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1542 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1543 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1544 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1545 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1546 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1547 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1548 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1549 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1550 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1551 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1552 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1553 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1554 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1555 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1556 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1557 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1558 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1559 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1560 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1561 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1562 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1563 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1564 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1565 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1566 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1567 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1568 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1569 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1570 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1571 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1572 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1573 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1574 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1575 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1576 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1577 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1578 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1579 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1580 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1581 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1582 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1583 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1584 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1585 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1586 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1587 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1588 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1589 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1590 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1591 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1592 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1593 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1594 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1595 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1596 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1597 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1598 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1599 {NULL, NULL}
1600 };
1601
1602 /* adjust the expression to a Perl equivalent
1603 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1604
1605 /* we need to replace all "$" with "\$", count them now */
1606 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1607
1608 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1609 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1610 perl_regex[0] = '\0';
1611
1612 ptr = perl_regex;
1613
1614 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1615 /* we will add line-end anchoring */
1616 ptr[0] = '(';
1617 ++ptr;
1618 }
1619
1620 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1621 if (orig_ptr[0] == '$') {
1622 ptr += sprintf(ptr, "\\$");
1623 } else if (orig_ptr[0] == '^') {
1624 ptr += sprintf(ptr, "\\^");
1625 } else {
1626 ptr[0] = orig_ptr[0];
1627 ++ptr;
1628 }
1629 }
1630
1631 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1632 ptr += sprintf(ptr, ")$");
1633 } else {
1634 ptr[0] = '\0';
1635 ++ptr;
1636 }
1637
1638 /* substitute Unicode Character Blocks with exact Character Ranges */
1639 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1640 start = ptr - perl_regex;
1641
1642 ptr = strchr(ptr, '}');
1643 if (!ptr) {
1644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1645 pattern, perl_regex + start + 2, "unterminated character property");
1646 free(perl_regex);
1647 return LY_EVALID;
1648 }
1649 end = (ptr - perl_regex) + 1;
1650
1651 /* need more space */
1652 if (end - start < URANGE_LEN) {
1653 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1654 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1655 }
1656
1657 /* find our range */
1658 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1659 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1660 break;
1661 }
1662 }
1663 if (!ublock2urange[idx][0]) {
1664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1665 pattern, perl_regex + start + 5, "unknown block name");
1666 free(perl_regex);
1667 return LY_EVALID;
1668 }
1669
1670 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1671 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1672 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1673 ++count;
1674 }
1675 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1676 --count;
1677 }
1678 }
1679 if (count) {
1680 /* skip brackets */
1681 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1682 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1683 } else {
1684 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1685 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1686 }
1687 }
1688
1689 /* must return 0, already checked during parsing */
1690 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1691 &err_msg, &err_offset, NULL);
1692 if (!precomp) {
1693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1694 free(perl_regex);
1695 return LY_EVALID;
1696 }
1697 free(perl_regex);
1698
1699 if (pcre_precomp) {
1700 *pcre_precomp = precomp;
1701 } else {
1702 free(precomp);
1703 }
1704
1705 return LY_SUCCESS;
1706
1707#undef URANGE_LEN
1708}
1709
Radek Krejcia3045382018-11-22 14:30:31 +01001710/**
1711 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1712 * @param[in] ctx Compile context.
1713 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1714 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1715 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1716 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1717 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1718 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1719 */
Radek Krejci19a96102018-11-15 13:38:09 +01001720static LY_ERR
1721lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1722 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1723{
1724 struct lysc_pattern **pattern;
1725 unsigned int u, v;
1726 const char *err_msg;
1727 LY_ERR ret = LY_SUCCESS;
1728
1729 /* first, copy the patterns from the base type */
1730 if (base_patterns) {
1731 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1732 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1733 }
1734
1735 LY_ARRAY_FOR(patterns_p, u) {
1736 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1737 *pattern = calloc(1, sizeof **pattern);
1738 ++(*pattern)->refcount;
1739
1740 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1741 LY_CHECK_RET(ret);
1742 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1743 if (err_msg) {
1744 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1745 }
1746
1747 if (patterns_p[u].arg[0] == 0x15) {
1748 (*pattern)->inverted = 1;
1749 }
Radek Krejci693262f2019-04-29 15:23:20 +02001750 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->orig);
Radek Krejci19a96102018-11-15 13:38:09 +01001751 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1752 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001753 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1754 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001755 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1756 options, v, lys_compile_ext, ret, done);
1757 }
1758done:
1759 return ret;
1760}
1761
Radek Krejcia3045382018-11-22 14:30:31 +01001762/**
1763 * @brief map of the possible restrictions combination for the specific built-in type.
1764 */
Radek Krejci19a96102018-11-15 13:38:09 +01001765static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1766 0 /* LY_TYPE_UNKNOWN */,
1767 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001768 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1769 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1770 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1771 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1772 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001773 LYS_SET_BIT /* LY_TYPE_BITS */,
1774 0 /* LY_TYPE_BOOL */,
1775 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1776 0 /* LY_TYPE_EMPTY */,
1777 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1778 LYS_SET_BASE /* LY_TYPE_IDENT */,
1779 LYS_SET_REQINST /* LY_TYPE_INST */,
1780 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001781 LYS_SET_TYPE /* LY_TYPE_UNION */,
1782 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001783 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001784 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001785 LYS_SET_RANGE /* LY_TYPE_INT64 */
1786};
1787
1788/**
1789 * @brief stringification of the YANG built-in data types
1790 */
1791const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1792 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1793 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001794};
1795
Radek Krejcia3045382018-11-22 14:30:31 +01001796/**
1797 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1798 * @param[in] ctx Compile context.
1799 * @param[in] enums_p Array of the parsed enum structures to compile.
1800 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1801 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1802 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1803 * @param[out] enums Newly created array of the compiled enums information for the current type.
1804 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1805 */
Radek Krejci19a96102018-11-15 13:38:09 +01001806static LY_ERR
1807lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
Radek Krejci693262f2019-04-29 15:23:20 +02001808 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01001809{
1810 LY_ERR ret = LY_SUCCESS;
1811 unsigned int u, v, match;
1812 int32_t value = 0;
1813 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001814 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01001815
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001816 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001817 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1818 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1819 return LY_EVALID;
1820 }
1821
1822 LY_ARRAY_FOR(enums_p, u) {
1823 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1824 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001825 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1826 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02001827 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01001828 if (base_enums) {
1829 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1830 LY_ARRAY_FOR(base_enums, v) {
1831 if (!strcmp(e->name, base_enums[v].name)) {
1832 break;
1833 }
1834 }
1835 if (v == LY_ARRAY_SIZE(base_enums)) {
1836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1837 "Invalid %s - derived type adds new item \"%s\".",
1838 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1839 return LY_EVALID;
1840 }
1841 match = v;
1842 }
1843
1844 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02001845 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01001846 if (enums_p[u].flags & LYS_SET_VALUE) {
1847 e->value = (int32_t)enums_p[u].value;
1848 if (!u || e->value >= value) {
1849 value = e->value + 1;
1850 }
1851 /* check collision with other values */
1852 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1853 if (e->value == (*enums)[v].value) {
1854 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1855 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1856 e->value, e->name, (*enums)[v].name);
1857 return LY_EVALID;
1858 }
1859 }
1860 } else if (base_enums) {
1861 /* inherit the assigned value */
1862 e->value = base_enums[match].value;
1863 if (!u || e->value >= value) {
1864 value = e->value + 1;
1865 }
1866 } else {
1867 /* assign value automatically */
1868 if (u && value == INT32_MIN) {
1869 /* counter overflow */
1870 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1871 "Invalid enumeration - it is not possible to auto-assign enum value for "
1872 "\"%s\" since the highest value is already 2147483647.", e->name);
1873 return LY_EVALID;
1874 }
1875 e->value = value++;
1876 }
1877 } else { /* LY_TYPE_BITS */
1878 if (enums_p[u].flags & LYS_SET_VALUE) {
1879 e->value = (int32_t)enums_p[u].value;
1880 if (!u || (uint32_t)e->value >= position) {
1881 position = (uint32_t)e->value + 1;
1882 }
1883 /* check collision with other values */
1884 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1885 if (e->value == (*enums)[v].value) {
1886 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1887 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1888 (uint32_t)e->value, e->name, (*enums)[v].name);
1889 return LY_EVALID;
1890 }
1891 }
1892 } else if (base_enums) {
1893 /* inherit the assigned value */
1894 e->value = base_enums[match].value;
1895 if (!u || (uint32_t)e->value >= position) {
1896 position = (uint32_t)e->value + 1;
1897 }
1898 } else {
1899 /* assign value automatically */
1900 if (u && position == 0) {
1901 /* counter overflow */
1902 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1903 "Invalid bits - it is not possible to auto-assign bit position for "
1904 "\"%s\" since the highest value is already 4294967295.", e->name);
1905 return LY_EVALID;
1906 }
1907 e->value = position++;
1908 }
1909 }
1910
1911 if (base_enums) {
1912 /* the assigned values must not change from the derived type */
1913 if (e->value != base_enums[match].value) {
1914 if (basetype == LY_TYPE_ENUM) {
1915 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1916 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1917 e->name, base_enums[match].value, e->value);
1918 } else {
1919 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1920 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1921 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1922 }
1923 return LY_EVALID;
1924 }
1925 }
1926
1927 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1928 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1929
1930 if (basetype == LY_TYPE_BITS) {
1931 /* keep bits ordered by position */
1932 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1933 if (v != u) {
1934 memcpy(&storage, e, sizeof *e);
1935 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1936 memcpy(&(*enums)[v], &storage, sizeof storage);
1937 }
1938 }
1939 }
1940
1941done:
1942 return ret;
1943}
1944
Radek Krejcia3045382018-11-22 14:30:31 +01001945#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1946 for ((NODE) = (NODE)->parent; \
1947 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1948 (NODE) = (NODE)->parent); \
1949 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1951 TERM; \
1952 }
1953
1954/**
1955 * @brief Validate the predicate(s) from the leafref path.
1956 * @param[in] ctx Compile context
1957 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1958 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001959 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001960 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001961 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001962 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1963 */
1964static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001965lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001966 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001967{
1968 LY_ERR ret = LY_EVALID;
1969 const struct lys_module *mod;
1970 const struct lysc_node *src_node, *dst_node;
1971 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1972 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001973 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001974 const char *start, *end, *pke_end;
1975 struct ly_set keys = {0};
1976 int i;
1977
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001978 assert(path_context);
1979
Radek Krejcia3045382018-11-22 14:30:31 +01001980 while (**predicate == '[') {
1981 start = (*predicate)++;
1982
1983 while (isspace(**predicate)) {
1984 ++(*predicate);
1985 }
1986 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1987 while (isspace(**predicate)) {
1988 ++(*predicate);
1989 }
1990 if (**predicate != '=') {
1991 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001992 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1993 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001994 goto cleanup;
1995 }
1996 ++(*predicate);
1997 while (isspace(**predicate)) {
1998 ++(*predicate);
1999 }
2000
2001 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2003 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2004 goto cleanup;
2005 }
2006 --pke_end;
2007 while (isspace(*pke_end)) {
2008 --pke_end;
2009 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002010 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002011 /* localize path-key-expr */
2012 pke_start = path_key_expr = *predicate;
2013 /* move after the current predicate */
2014 *predicate = end + 1;
2015
2016 /* source (must be leaf or leaf-list) */
2017 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002018 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002019 if (!mod) {
2020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2021 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002022 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002023 goto cleanup;
2024 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002025 if (!mod->implemented) {
2026 /* make the module implemented */
2027 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2028 }
Radek Krejcia3045382018-11-22 14:30:31 +01002029 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002030 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002031 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002032 src_node = NULL;
2033 if (context_node->keys) {
2034 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2035 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2036 src_node = (const struct lysc_node*)context_node->keys[u];
2037 break;
2038 }
2039 }
2040 }
Radek Krejcia3045382018-11-22 14:30:31 +01002041 if (!src_node) {
2042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002043 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002044 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002045 goto cleanup;
2046 }
Radek Krejcia3045382018-11-22 14:30:31 +01002047
2048 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002049 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002050 i = ly_set_add(&keys, (void*)src_node, 0);
2051 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002052 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002053 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002054 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002055 *predicate - start, start, src_node->name);
2056 goto cleanup;
2057 }
2058
2059 /* destination */
2060 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002061 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002062
2063 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2064 if (strncmp(path_key_expr, "current()", 9)) {
2065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2066 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2067 *predicate - start, start);
2068 goto cleanup;
2069 }
2070 path_key_expr += 9;
2071 while (isspace(*path_key_expr)) {
2072 ++path_key_expr;
2073 }
2074
2075 if (*path_key_expr != '/') {
2076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2077 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2078 *predicate - start, start);
2079 goto cleanup;
2080 }
2081 ++path_key_expr;
2082 while (isspace(*path_key_expr)) {
2083 ++path_key_expr;
2084 }
2085
2086 /* rel-path-keyexpr:
2087 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2088 while (!strncmp(path_key_expr, "..", 2)) {
2089 ++dest_parent_times;
2090 path_key_expr += 2;
2091 while (isspace(*path_key_expr)) {
2092 ++path_key_expr;
2093 }
2094 if (*path_key_expr != '/') {
2095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2096 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2097 *predicate - start, start);
2098 goto cleanup;
2099 }
2100 ++path_key_expr;
2101 while (isspace(*path_key_expr)) {
2102 ++path_key_expr;
2103 }
2104
2105 /* path is supposed to be evaluated in data tree, so we have to skip
2106 * all schema nodes that cannot be instantiated in data tree */
2107 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2108 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2109 *predicate - start, start);
2110 }
2111 if (!dest_parent_times) {
2112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2113 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2114 *predicate - start, start);
2115 goto cleanup;
2116 }
2117 if (path_key_expr == pke_end) {
2118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2119 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2120 *predicate - start, start);
2121 goto cleanup;
2122 }
2123
2124 while(path_key_expr != pke_end) {
2125 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002127 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2128 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002129 goto cleanup;
2130 }
2131
2132 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002133 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002134 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002135 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002136 }
2137 if (!mod) {
2138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002139 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002140 *predicate - start, start, dst_len, dst);
2141 goto cleanup;
2142 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002143 if (!mod->implemented) {
2144 /* make the module implemented */
2145 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2146 }
Radek Krejcia3045382018-11-22 14:30:31 +01002147
2148 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2149 if (!dst_node) {
2150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002151 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002152 *predicate - start, start, path_key_expr - pke_start, pke_start);
2153 goto cleanup;
2154 }
2155 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002156 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 +01002157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002158 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002159 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2160 goto cleanup;
2161 }
2162 }
2163
2164 ret = LY_SUCCESS;
2165cleanup:
2166 ly_set_erase(&keys, NULL);
2167 return ret;
2168}
2169
2170/**
2171 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2172 *
2173 * path-arg = absolute-path / relative-path
2174 * absolute-path = 1*("/" (node-identifier *path-predicate))
2175 * relative-path = 1*(".." "/") descendant-path
2176 *
2177 * @param[in,out] path Path to parse.
2178 * @param[out] prefix Prefix of the token, NULL if there is not any.
2179 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2180 * @param[out] name Name of the token.
2181 * @param[out] nam_len Length of the name.
2182 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2183 * must not be changed between consecutive calls. -1 if the
2184 * path is absolute.
2185 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2186 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2187 */
2188static LY_ERR
2189lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2190 int *parent_times, int *has_predicate)
2191{
2192 int par_times = 0;
2193
2194 assert(path && *path);
2195 assert(parent_times);
2196 assert(prefix);
2197 assert(prefix_len);
2198 assert(name);
2199 assert(name_len);
2200 assert(has_predicate);
2201
2202 *prefix = NULL;
2203 *prefix_len = 0;
2204 *name = NULL;
2205 *name_len = 0;
2206 *has_predicate = 0;
2207
2208 if (!*parent_times) {
2209 if (!strncmp(*path, "..", 2)) {
2210 *path += 2;
2211 ++par_times;
2212 while (!strncmp(*path, "/..", 3)) {
2213 *path += 3;
2214 ++par_times;
2215 }
2216 }
2217 if (par_times) {
2218 *parent_times = par_times;
2219 } else {
2220 *parent_times = -1;
2221 }
2222 }
2223
2224 if (**path != '/') {
2225 return LY_EINVAL;
2226 }
2227 /* skip '/' */
2228 ++(*path);
2229
2230 /* node-identifier ([prefix:]name) */
2231 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2232
2233 if ((**path == '/' && (*path)[1]) || !**path) {
2234 /* path continues by another token or this is the last token */
2235 return LY_SUCCESS;
2236 } else if ((*path)[0] != '[') {
2237 /* unexpected character */
2238 return LY_EINVAL;
2239 } else {
2240 /* predicate starting with [ */
2241 *has_predicate = 1;
2242 return LY_SUCCESS;
2243 }
2244}
2245
2246/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002247 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2248 *
2249 * The set of features used for target must be a subset of features used for the leafref.
2250 * This is not a perfect, we should compare the truth tables but it could require too much resources
2251 * and RFC 7950 does not require it explicitely, so we simplify that.
2252 *
2253 * @param[in] refnode The leafref node.
2254 * @param[in] target Tha target node of the leafref.
2255 * @return LY_SUCCESS or LY_EVALID;
2256 */
2257static LY_ERR
2258lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2259{
2260 LY_ERR ret = LY_EVALID;
2261 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002262 unsigned int u, v, count;
2263 struct ly_set features = {0};
2264
2265 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002266 if (iter->iffeatures) {
2267 LY_ARRAY_FOR(iter->iffeatures, u) {
2268 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2269 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002270 }
2271 }
2272 }
2273 }
2274
2275 /* we should have, in features set, a superset of features applicable to the target node.
2276 * So when adding features applicable to the target into the features set, we should not be
2277 * able to actually add any new feature, otherwise it is not a subset of features applicable
2278 * to the leafref itself. */
2279 count = features.count;
2280 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002281 if (iter->iffeatures) {
2282 LY_ARRAY_FOR(iter->iffeatures, u) {
2283 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2284 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002285 /* new feature was added (or LY_EMEM) */
2286 goto cleanup;
2287 }
2288 }
2289 }
2290 }
2291 }
2292 ret = LY_SUCCESS;
2293
2294cleanup:
2295 ly_set_erase(&features, NULL);
2296 return ret;
2297}
2298
2299/**
Radek Krejcia3045382018-11-22 14:30:31 +01002300 * @brief Validate the leafref path.
2301 * @param[in] ctx Compile context
2302 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002303 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002304 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2305 */
2306static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002307lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002308{
2309 const struct lysc_node *node = NULL, *parent = NULL;
2310 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002311 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002312 const char *id, *prefix, *name;
2313 size_t prefix_len, name_len;
2314 int parent_times = 0, has_predicate;
2315 unsigned int iter, u;
2316 LY_ERR ret = LY_SUCCESS;
2317
2318 assert(ctx);
2319 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002320 assert(leafref);
2321
Radek Krejcia3045382018-11-22 14:30:31 +01002322 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002323 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002324 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2325 if (!iter) { /* first iteration */
2326 /* precess ".." in relative paths */
2327 if (parent_times > 0) {
2328 /* move from the context node */
2329 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2330 /* path is supposed to be evaluated in data tree, so we have to skip
2331 * all schema nodes that cannot be instantiated in data tree */
2332 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002333 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002334 }
2335 }
2336 }
2337
2338 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002339 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002340 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002341 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002342 }
2343 if (!mod) {
2344 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002345 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2346 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002347 return LY_EVALID;
2348 }
Radek Krejci3d929562019-02-21 11:25:55 +01002349 if (!mod->implemented) {
2350 /* make the module implemented */
2351 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2352 }
Radek Krejcia3045382018-11-22 14:30:31 +01002353
2354 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2355 if (!node) {
2356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002357 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002358 return LY_EVALID;
2359 }
2360 parent = node;
2361
2362 if (has_predicate) {
2363 /* we have predicate, so the current result must be list */
2364 if (node->nodetype != LYS_LIST) {
2365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2366 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002367 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002368 return LY_EVALID;
2369 }
2370
Radek Krejcibade82a2018-12-05 10:13:48 +01002371 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2372 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002373 }
2374
2375 ++iter;
2376 }
2377 if (ret) {
2378 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002379 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002380 return LY_EVALID;
2381 }
2382
2383 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2384 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2385 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002386 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002387 return LY_EVALID;
2388 }
2389
2390 /* check status */
2391 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2392 return LY_EVALID;
2393 }
2394
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002395 /* check config */
2396 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2397 if (node->flags & LYS_CONFIG_R) {
2398 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2399 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2400 leafref->path);
2401 return LY_EVALID;
2402 }
2403 }
2404
Radek Krejci412ddfa2018-11-23 11:44:11 +01002405 /* store the target's type and check for circular chain of leafrefs */
2406 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2407 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2408 if (type == (struct lysc_type*)leafref) {
2409 /* circular chain detected */
2410 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2411 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2412 return LY_EVALID;
2413 }
2414 }
2415
Radek Krejci58d171e2018-11-23 13:50:55 +01002416 /* check if leafref and its target are under common if-features */
2417 if (lys_compile_leafref_features_validate(startnode, node)) {
2418 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2419 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2420 leafref->path);
2421 return LY_EVALID;
2422 }
2423
Radek Krejcia3045382018-11-22 14:30:31 +01002424 return LY_SUCCESS;
2425}
2426
Radek Krejcicdfecd92018-11-26 11:27:32 +01002427static 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 +01002428 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002429/**
2430 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2431 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002432 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2433 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2434 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2435 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002436 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002437 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002438 * @param[in] basetype Base YANG built-in type of the type to compile.
2439 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2440 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2441 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2442 * @param[out] type Newly created type structure with the filled information about the type.
2443 * @return LY_ERR value.
2444 */
Radek Krejci19a96102018-11-15 13:38:09 +01002445static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002446lys_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,
2447 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2448 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002449{
2450 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002451 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002452 struct lysc_type_bin *bin;
2453 struct lysc_type_num *num;
2454 struct lysc_type_str *str;
2455 struct lysc_type_bits *bits;
2456 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002457 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002458 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002459 struct lysc_type_union *un, *un_aux;
2460 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002461
2462 switch (basetype) {
2463 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002464 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002465
2466 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002467 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002468 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002469 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2470 LY_CHECK_RET(ret);
2471 if (!tpdfname) {
2472 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2473 options, u, lys_compile_ext, ret, done);
2474 }
2475 }
2476
2477 if (tpdfname) {
2478 type_p->compiled = *type;
2479 *type = calloc(1, sizeof(struct lysc_type_bin));
2480 }
2481 break;
2482 case LY_TYPE_BITS:
2483 /* RFC 7950 9.7 - bits */
2484 bits = (struct lysc_type_bits*)(*type);
2485 if (type_p->bits) {
2486 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
Radek Krejci693262f2019-04-29 15:23:20 +02002487 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2488 (struct lysc_type_bitenum_item**)&bits->bits);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002489 LY_CHECK_RET(ret);
2490 }
2491
Radek Krejci555cb5b2018-11-16 14:54:33 +01002492 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002493 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002494 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002496 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002497 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002498 free(*type);
2499 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002500 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002501 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002502 }
2503
2504 if (tpdfname) {
2505 type_p->compiled = *type;
2506 *type = calloc(1, sizeof(struct lysc_type_bits));
2507 }
2508 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002509 case LY_TYPE_DEC64:
2510 dec = (struct lysc_type_dec*)(*type);
2511
2512 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002513 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002514 if (!type_p->fraction_digits) {
2515 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002517 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002519 free(*type);
2520 *type = NULL;
2521 }
2522 return LY_EVALID;
2523 }
2524 } else if (type_p->fraction_digits) {
2525 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002526 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2528 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002529 tpdfname);
2530 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2532 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002533 free(*type);
2534 *type = NULL;
2535 }
2536 return LY_EVALID;
2537 }
2538 dec->fraction_digits = type_p->fraction_digits;
2539
2540 /* RFC 7950 9.2.4 - range */
2541 if (type_p->range) {
2542 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2543 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2544 LY_CHECK_RET(ret);
2545 if (!tpdfname) {
2546 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2547 options, u, lys_compile_ext, ret, done);
2548 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002549 }
2550
2551 if (tpdfname) {
2552 type_p->compiled = *type;
2553 *type = calloc(1, sizeof(struct lysc_type_dec));
2554 }
2555 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002556 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002557 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002558
2559 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002560 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002561 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002562 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2563 LY_CHECK_RET(ret);
2564 if (!tpdfname) {
2565 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2566 options, u, lys_compile_ext, ret, done);
2567 }
2568 } else if (base && ((struct lysc_type_str*)base)->length) {
2569 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2570 }
2571
2572 /* RFC 7950 9.4.5 - pattern */
2573 if (type_p->patterns) {
2574 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2575 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2576 LY_CHECK_RET(ret);
2577 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2578 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2579 }
2580
2581 if (tpdfname) {
2582 type_p->compiled = *type;
2583 *type = calloc(1, sizeof(struct lysc_type_str));
2584 }
2585 break;
2586 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002587 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002588
2589 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002590 if (type_p->enums) {
2591 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2592 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2593 LY_CHECK_RET(ret);
2594 }
2595
Radek Krejci555cb5b2018-11-16 14:54:33 +01002596 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002597 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002598 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002600 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002601 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002602 free(*type);
2603 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002604 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002605 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002606 }
2607
2608 if (tpdfname) {
2609 type_p->compiled = *type;
2610 *type = calloc(1, sizeof(struct lysc_type_enum));
2611 }
2612 break;
2613 case LY_TYPE_INT8:
2614 case LY_TYPE_UINT8:
2615 case LY_TYPE_INT16:
2616 case LY_TYPE_UINT16:
2617 case LY_TYPE_INT32:
2618 case LY_TYPE_UINT32:
2619 case LY_TYPE_INT64:
2620 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002621 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002622
2623 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002624 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002625 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002626 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2627 LY_CHECK_RET(ret);
2628 if (!tpdfname) {
2629 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2630 options, u, lys_compile_ext, ret, done);
2631 }
2632 }
2633
2634 if (tpdfname) {
2635 type_p->compiled = *type;
2636 *type = calloc(1, sizeof(struct lysc_type_num));
2637 }
2638 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002639 case LY_TYPE_IDENT:
2640 idref = (struct lysc_type_identityref*)(*type);
2641
2642 /* RFC 7950 9.10.2 - base */
2643 if (type_p->bases) {
2644 if (base) {
2645 /* only the directly derived identityrefs can contain base specification */
2646 if (tpdfname) {
2647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002648 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002649 tpdfname);
2650 } else {
2651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002652 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002653 free(*type);
2654 *type = NULL;
2655 }
2656 return LY_EVALID;
2657 }
2658 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2659 LY_CHECK_RET(ret);
2660 }
2661
2662 if (!base && !type_p->flags) {
2663 /* type derived from identityref built-in type must contain at least one base */
2664 if (tpdfname) {
2665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2666 } else {
2667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2668 free(*type);
2669 *type = NULL;
2670 }
2671 return LY_EVALID;
2672 }
2673
2674 if (tpdfname) {
2675 type_p->compiled = *type;
2676 *type = calloc(1, sizeof(struct lysc_type_identityref));
2677 }
2678 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002679 case LY_TYPE_LEAFREF:
2680 /* RFC 7950 9.9.3 - require-instance */
2681 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002682 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002683 if (tpdfname) {
2684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2685 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2686 } else {
2687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2688 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2689 free(*type);
2690 *type = NULL;
2691 }
2692 return LY_EVALID;
2693 }
Radek Krejcia3045382018-11-22 14:30:31 +01002694 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002695 } else if (base) {
2696 /* inherit */
2697 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002698 } else {
2699 /* default is true */
2700 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2701 }
2702 if (type_p->path) {
2703 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002704 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002705 } else if (base) {
2706 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002707 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002708 } else if (tpdfname) {
2709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2710 return LY_EVALID;
2711 } else {
2712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2713 free(*type);
2714 *type = NULL;
2715 return LY_EVALID;
2716 }
2717 if (tpdfname) {
2718 type_p->compiled = *type;
2719 *type = calloc(1, sizeof(struct lysc_type_leafref));
2720 }
2721 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002722 case LY_TYPE_INST:
2723 /* RFC 7950 9.9.3 - require-instance */
2724 if (type_p->flags & LYS_SET_REQINST) {
2725 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2726 } else {
2727 /* default is true */
2728 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2729 }
2730
2731 if (tpdfname) {
2732 type_p->compiled = *type;
2733 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2734 }
2735 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002736 case LY_TYPE_UNION:
2737 un = (struct lysc_type_union*)(*type);
2738
2739 /* RFC 7950 7.4 - type */
2740 if (type_p->types) {
2741 if (base) {
2742 /* only the directly derived union can contain types specification */
2743 if (tpdfname) {
2744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2745 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2746 tpdfname);
2747 } else {
2748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2749 "Invalid type substatement for the type not directly derived from union built-in type.");
2750 free(*type);
2751 *type = NULL;
2752 }
2753 return LY_EVALID;
2754 }
2755 /* compile the type */
2756 additional = 0;
2757 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2758 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002759 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 +01002760 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2761 /* add space for additional types from the union subtype */
2762 un_aux = (struct lysc_type_union *)un->types[u + additional];
2763 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)));
2764 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2765 un->types = (void*)((uint32_t*)(p) + 1);
2766
2767 /* copy subtypes of the subtype union */
2768 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2769 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2770 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2771 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2772 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2773 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2774 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2775 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2776 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2777 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2778 /* TODO extensions */
2779
2780 } else {
2781 un->types[u + additional] = un_aux->types[v];
2782 ++un_aux->types[v]->refcount;
2783 }
2784 ++additional;
2785 LY_ARRAY_INCREMENT(un->types);
2786 }
2787 /* compensate u increment in main loop */
2788 --additional;
2789
2790 /* free the replaced union subtype */
2791 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2792 } else {
2793 LY_ARRAY_INCREMENT(un->types);
2794 }
2795 LY_CHECK_RET(ret);
2796 }
2797 }
2798
2799 if (!base && !type_p->flags) {
2800 /* type derived from union built-in type must contain at least one type */
2801 if (tpdfname) {
2802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2803 } else {
2804 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2805 free(*type);
2806 *type = NULL;
2807 }
2808 return LY_EVALID;
2809 }
2810
2811 if (tpdfname) {
2812 type_p->compiled = *type;
2813 *type = calloc(1, sizeof(struct lysc_type_union));
2814 }
2815 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002816 case LY_TYPE_BOOL:
2817 case LY_TYPE_EMPTY:
2818 case LY_TYPE_UNKNOWN: /* just to complete switch */
2819 break;
2820 }
2821 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2822done:
2823 return ret;
2824}
2825
Radek Krejcia3045382018-11-22 14:30:31 +01002826/**
2827 * @brief Compile information about the leaf/leaf-list's type.
2828 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002829 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2830 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2831 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2832 * @param[in] context_name Name of the context node or referencing typedef for logging.
2833 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002834 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2835 * @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 +01002836 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002837 * @return LY_ERR value.
2838 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002839static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002840lys_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 +01002841 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002842{
2843 LY_ERR ret = LY_SUCCESS;
2844 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002845 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002846 struct type_context {
2847 const struct lysp_tpdf *tpdf;
2848 struct lysp_node *node;
2849 struct lysp_module *mod;
2850 } *tctx, *tctx_prev = NULL;
2851 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002852 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002853 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002854 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002855
2856 (*type) = NULL;
2857
2858 tctx = calloc(1, sizeof *tctx);
2859 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002860 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002861 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2862 ret == LY_SUCCESS;
2863 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2864 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2865 if (basetype) {
2866 break;
2867 }
2868
2869 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002870 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2871 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002872 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2873
Radek Krejcicdfecd92018-11-26 11:27:32 +01002874 if (units && !*units) {
2875 /* inherit units */
2876 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2877 }
Radek Krejci01342af2019-01-03 15:18:08 +01002878 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002879 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002880 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002881 }
Radek Krejci01342af2019-01-03 15:18:08 +01002882 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002883 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2884 break;
2885 }
2886
Radek Krejci19a96102018-11-15 13:38:09 +01002887 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002888 /* it is not necessary to continue, the rest of the chain was already compiled,
2889 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002890 basetype = tctx->tpdf->type.compiled->basetype;
2891 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002892 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002893 dummyloops = 1;
2894 goto preparenext;
2895 } else {
2896 tctx = NULL;
2897 break;
2898 }
Radek Krejci19a96102018-11-15 13:38:09 +01002899 }
2900
2901 /* store information for the following processing */
2902 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2903
Radek Krejcicdfecd92018-11-26 11:27:32 +01002904preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002905 /* prepare next loop */
2906 tctx_prev = tctx;
2907 tctx = calloc(1, sizeof *tctx);
2908 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2909 }
2910 free(tctx);
2911
2912 /* allocate type according to the basetype */
2913 switch (basetype) {
2914 case LY_TYPE_BINARY:
2915 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002916 break;
2917 case LY_TYPE_BITS:
2918 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002919 break;
2920 case LY_TYPE_BOOL:
2921 case LY_TYPE_EMPTY:
2922 *type = calloc(1, sizeof(struct lysc_type));
2923 break;
2924 case LY_TYPE_DEC64:
2925 *type = calloc(1, sizeof(struct lysc_type_dec));
2926 break;
2927 case LY_TYPE_ENUM:
2928 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002929 break;
2930 case LY_TYPE_IDENT:
2931 *type = calloc(1, sizeof(struct lysc_type_identityref));
2932 break;
2933 case LY_TYPE_INST:
2934 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2935 break;
2936 case LY_TYPE_LEAFREF:
2937 *type = calloc(1, sizeof(struct lysc_type_leafref));
2938 break;
2939 case LY_TYPE_STRING:
2940 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002941 break;
2942 case LY_TYPE_UNION:
2943 *type = calloc(1, sizeof(struct lysc_type_union));
2944 break;
2945 case LY_TYPE_INT8:
2946 case LY_TYPE_UINT8:
2947 case LY_TYPE_INT16:
2948 case LY_TYPE_UINT16:
2949 case LY_TYPE_INT32:
2950 case LY_TYPE_UINT32:
2951 case LY_TYPE_INT64:
2952 case LY_TYPE_UINT64:
2953 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002954 break;
2955 case LY_TYPE_UNKNOWN:
2956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2957 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2958 ret = LY_EVALID;
2959 goto cleanup;
2960 }
2961 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002962 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002963 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2964 ly_data_type2str[basetype]);
2965 free(*type);
2966 (*type) = NULL;
2967 ret = LY_EVALID;
2968 goto cleanup;
2969 }
2970
2971 /* get restrictions from the referred typedefs */
2972 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2973 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002974 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002975 base = tctx->tpdf->type.compiled;
2976 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002977 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002978 /* no change, just use the type information from the base */
2979 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2980 ++base->refcount;
2981 continue;
2982 }
2983
2984 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002985 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2987 tctx->tpdf->name, ly_data_type2str[basetype]);
2988 ret = LY_EVALID;
2989 goto cleanup;
2990 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2991 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2992 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2993 tctx->tpdf->name, tctx->tpdf->dflt);
2994 ret = LY_EVALID;
2995 goto cleanup;
2996 }
2997
Radek Krejci19a96102018-11-15 13:38:09 +01002998 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002999 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003000 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 +01003001 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
3002 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003003 LY_CHECK_GOTO(ret, cleanup);
3004 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003005 }
3006
Radek Krejcic5c27e52018-11-15 14:38:11 +01003007 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003008 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003009 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003010 (*type)->basetype = basetype;
3011 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01003012 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 +01003013 LY_CHECK_GOTO(ret, cleanup);
3014 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003015 /* no specific restriction in leaf's type definition, copy from the base */
3016 free(*type);
3017 (*type) = base;
3018 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003019 }
Radek Krejci01342af2019-01-03 15:18:08 +01003020 if (!(*type)->dflt) {
3021 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3022 }
Radek Krejci19a96102018-11-15 13:38:09 +01003023
3024 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
3025
3026cleanup:
3027 ly_set_erase(&tpdf_chain, free);
3028 return ret;
3029}
3030
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003031/**
3032 * @brief Compile status information of the given node.
3033 *
3034 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3035 * has the status correctly set during the compilation.
3036 *
3037 * @param[in] ctx Compile context
3038 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3039 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3040 * the compatibility with the parent's status value.
3041 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3042 * @return LY_ERR value.
3043 */
3044static LY_ERR
3045lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3046{
3047 /* status - it is not inherited by specification, but it does not make sense to have
3048 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3049 if (!((*node_flags) & LYS_STATUS_MASK)) {
3050 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3051 if ((parent_flags & 0x3) != 0x3) {
3052 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3053 * combination of bits (0x3) which marks the uses_status value */
3054 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3055 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3056 }
3057 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3058 } else {
3059 (*node_flags) |= LYS_STATUS_CURR;
3060 }
3061 } else if (parent_flags & LYS_STATUS_MASK) {
3062 /* check status compatibility with the parent */
3063 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3064 if ((*node_flags) & LYS_STATUS_CURR) {
3065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3066 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3067 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3068 } else { /* LYS_STATUS_DEPRC */
3069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3070 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3071 }
3072 return LY_EVALID;
3073 }
3074 }
3075 return LY_SUCCESS;
3076}
3077
Radek Krejci8cce8532019-03-05 11:27:45 +01003078/**
3079 * @brief Check uniqness of the node/action/notification name.
3080 *
3081 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3082 * structures, but they share the namespace so we need to check their name collisions.
3083 *
3084 * @param[in] ctx Compile context.
3085 * @param[in] children List (linked list) of data nodes to go through.
3086 * @param[in] actions List (sized array) of actions or RPCs to go through.
3087 * @param[in] notifs List (sized array) of Notifications to go through.
3088 * @param[in] name Name of the item to find in the given lists.
3089 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3090 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3091 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3092 */
3093static LY_ERR
3094lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3095 const struct lysc_action *actions, const struct lysc_notif *notifs,
3096 const char *name, void *exclude)
3097{
3098 const struct lysc_node *iter;
3099 unsigned int u;
3100
3101 LY_LIST_FOR(children, iter) {
3102 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3103 goto error;
3104 }
3105 }
3106 LY_ARRAY_FOR(actions, u) {
3107 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3108 goto error;
3109 }
3110 }
3111 LY_ARRAY_FOR(notifs, u) {
3112 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3113 goto error;
3114 }
3115 }
3116 return LY_SUCCESS;
3117error:
3118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3119 return LY_EEXIST;
3120}
3121
Radek Krejcib1b59152019-01-07 13:21:56 +01003122static 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 +01003123
Radek Krejcia3045382018-11-22 14:30:31 +01003124/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003125 * @brief Compile parsed RPC/action schema node information.
3126 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003127 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003128 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003129 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003130 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3131 * @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).
3132 * Zero means no uses, non-zero value with no status bit set mean the default status.
3133 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3134 */
3135static LY_ERR
3136lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, int options,
3137 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3138{
3139 LY_ERR ret = LY_SUCCESS;
3140 struct lysp_node *child_p;
3141 unsigned int u;
3142
Radek Krejci8cce8532019-03-05 11:27:45 +01003143 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3144 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3145 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3146 action_p->name, action)) {
3147 return LY_EVALID;
3148 }
3149
Radek Krejcifc11bd72019-04-11 16:00:05 +02003150 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003152 "Action \"%s\" is placed inside %s.", action_p->name,
3153 options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003154 return LY_EVALID;
3155 }
3156
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003157 action->nodetype = LYS_ACTION;
3158 action->module = ctx->mod;
3159 action->parent = parent;
3160 if (!(options & LYSC_OPT_FREE_SP)) {
3161 action->sp = action_p;
3162 }
3163 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3164
3165 /* status - it is not inherited by specification, but it does not make sense to have
3166 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3167 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3168
3169 DUP_STRING(ctx->ctx, action_p->name, action->name);
3170 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3171 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
3172 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3173 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, options, u, lys_compile_ext, ret, cleanup);
3174
3175 /* input */
3176 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003177 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, options, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003178 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003179 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_RPC_INPUT, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003180 }
3181
3182 /* output */
3183 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003184 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, options, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003185 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003186 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_RPC_OUTPUT, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003187 }
3188
3189cleanup:
3190 return ret;
3191}
3192
3193/**
Radek Krejci43981a32019-04-12 09:44:11 +02003194 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003195 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003196 * @param[in] notif_p Parsed Notification schema node.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003197 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003198 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3199 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3200 * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags).
Radek Krejcifc11bd72019-04-11 16:00:05 +02003201 * Zero means no uses, non-zero value with no status bit set mean the default status.
3202 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3203 */
3204static LY_ERR
3205lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, int options,
3206 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3207{
3208 LY_ERR ret = LY_SUCCESS;
3209 struct lysp_node *child_p;
3210 unsigned int u;
3211
3212 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3213 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3214 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3215 notif_p->name, notif)) {
3216 return LY_EVALID;
3217 }
3218
3219 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
3220 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3221 "Notification \"%s\" is placed inside %s.", notif_p->name,
3222 options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
3223 return LY_EVALID;
3224 }
3225
3226 notif->nodetype = LYS_NOTIF;
3227 notif->module = ctx->mod;
3228 notif->parent = parent;
3229 if (!(options & LYSC_OPT_FREE_SP)) {
3230 notif->sp = notif_p;
3231 }
3232 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3233
3234 /* status - it is not inherited by specification, but it does not make sense to have
3235 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3236 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3237
3238 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3239 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3240 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
3241 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3242 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, options, u, lys_compile_ext, ret, cleanup);
3243 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, options, u, lys_compile_must, ret, cleanup);
3244
3245 LY_LIST_FOR(notif_p->data, child_p) {
3246 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_NOTIFICATION, (struct lysc_node*)notif, uses_status));
3247 }
3248
3249cleanup:
3250 return ret;
3251}
3252
3253/**
Radek Krejcia3045382018-11-22 14:30:31 +01003254 * @brief Compile parsed container node information.
3255 * @param[in] ctx Compile context
3256 * @param[in] node_p Parsed container node.
3257 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3258 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3259 * is enriched with the container-specific information.
3260 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3261 */
Radek Krejci19a96102018-11-15 13:38:09 +01003262static LY_ERR
3263lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3264{
3265 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3266 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3267 struct lysp_node *child_p;
3268 unsigned int u;
3269 LY_ERR ret = LY_SUCCESS;
3270
Radek Krejcife909632019-02-12 15:34:42 +01003271 if (cont_p->presence) {
3272 cont->flags |= LYS_PRESENCE;
3273 }
3274
Radek Krejci19a96102018-11-15 13:38:09 +01003275 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003276 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003277 }
3278
3279 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003280 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, options, u, lys_compile_action, 0, ret, done);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003281 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 +01003282
3283done:
3284 return ret;
3285}
3286
Radek Krejci33f72892019-02-21 10:36:58 +01003287/*
3288 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3289 * @param[in] ctx Compile context.
3290 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3291 * @param[in] type_p Parsed type to compile.
3292 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3293 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3294 * @return LY_ERR value.
3295 */
3296static LY_ERR
3297lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, int options, struct lysc_node_leaf *leaf)
3298{
3299 unsigned int u, v;
3300 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3301
3302 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type,
3303 leaf->units ? NULL : &leaf->units));
3304 if (leaf->nodetype == LYS_LEAFLIST) {
3305 if (llist->type->dflt && !llist->dflts && !llist->min) {
3306 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3307 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3308 LY_ARRAY_INCREMENT(llist->dflts);
3309 }
3310 } else {
3311 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3312 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3313 }
3314 }
3315 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3316 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3317 ly_set_add(&ctx->unres, leaf, 0);
3318 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3319 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3320 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3321 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3322 ly_set_add(&ctx->unres, leaf, 0);
3323 }
3324 }
3325 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3326 if (leaf->flags & LYS_SET_DFLT) {
3327 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3328 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3329 return LY_EVALID;
3330 }
3331 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3333 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3334 return LY_EVALID;
3335 }
3336 }
3337
3338 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3339 /* configuration data values must be unique - so check the default values */
3340 LY_ARRAY_FOR(llist->dflts, u) {
3341 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3342 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3344 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3345 return LY_EVALID;
3346 }
3347 }
3348 }
3349 }
3350
3351 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3352
3353 return LY_SUCCESS;
3354}
3355
Radek Krejcia3045382018-11-22 14:30:31 +01003356/**
3357 * @brief Compile parsed leaf node information.
3358 * @param[in] ctx Compile context
3359 * @param[in] node_p Parsed leaf node.
3360 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3361 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3362 * is enriched with the leaf-specific information.
3363 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3364 */
Radek Krejci19a96102018-11-15 13:38:09 +01003365static LY_ERR
3366lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3367{
3368 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3369 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3370 unsigned int u;
3371 LY_ERR ret = LY_SUCCESS;
3372
Radek Krejci19a96102018-11-15 13:38:09 +01003373 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003374 if (leaf_p->units) {
3375 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3376 leaf->flags |= LYS_SET_UNITS;
3377 }
3378 if (leaf_p->dflt) {
3379 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003380 leaf->flags |= LYS_SET_DFLT;
3381 }
Radek Krejci43699232018-11-23 14:59:46 +01003382
Radek Krejci33f72892019-02-21 10:36:58 +01003383 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, options, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003384
Radek Krejci19a96102018-11-15 13:38:09 +01003385done:
3386 return ret;
3387}
3388
Radek Krejcia3045382018-11-22 14:30:31 +01003389/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003390 * @brief Compile parsed leaf-list node information.
3391 * @param[in] ctx Compile context
3392 * @param[in] node_p Parsed leaf-list node.
3393 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3394 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3395 * is enriched with the leaf-list-specific information.
3396 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3397 */
3398static LY_ERR
3399lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3400{
3401 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3402 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003403 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003404 LY_ERR ret = LY_SUCCESS;
3405
Radek Krejci0e5d8382018-11-28 16:37:53 +01003406 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003407 if (llist_p->units) {
3408 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3409 llist->flags |= LYS_SET_UNITS;
3410 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003411
3412 if (llist_p->dflts) {
3413 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3414 LY_ARRAY_FOR(llist_p->dflts, u) {
3415 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3416 LY_ARRAY_INCREMENT(llist->dflts);
3417 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003418 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003419 }
3420
3421 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003422 if (llist->min) {
3423 llist->flags |= LYS_MAND_TRUE;
3424 }
Radek Krejcib7408632018-11-28 17:12:11 +01003425 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003426
Radek Krejci33f72892019-02-21 10:36:58 +01003427 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, options, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003428
3429done:
3430 return ret;
3431}
3432
3433/**
Radek Krejci7af64242019-02-18 13:07:53 +01003434 * @brief Compile information about list's uniques.
3435 * @param[in] ctx Compile context.
3436 * @param[in] context_module Module where the prefixes are going to be resolved.
3437 * @param[in] uniques Sized array list of unique statements.
3438 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3439 * @return LY_ERR value.
3440 */
3441static LY_ERR
3442lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3443{
3444 LY_ERR ret = LY_SUCCESS;
3445 struct lysc_node_leaf **key, ***unique;
3446 const char *keystr, *delim;
3447 size_t len;
3448 unsigned int v;
3449 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003450 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003451
3452 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3453 config = -1;
3454 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3455 keystr = uniques[v];
3456 while (keystr) {
3457 delim = strpbrk(keystr, " \t\n");
3458 if (delim) {
3459 len = delim - keystr;
3460 while (isspace(*delim)) {
3461 ++delim;
3462 }
3463 } else {
3464 len = strlen(keystr);
3465 }
3466
3467 /* unique node must be present */
3468 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003469 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3470 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003471 if (ret != LY_SUCCESS) {
3472 if (ret == LY_EDENIED) {
3473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003474 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003475 len, keystr, lys_nodetype2str((*key)->nodetype));
3476 }
3477 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003478 } else if (flags) {
3479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3480 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3481 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3482 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003483 }
3484
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003485
Radek Krejci7af64242019-02-18 13:07:53 +01003486 /* all referenced leafs must be of the same config type */
3487 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3488 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3489 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3490 return LY_EVALID;
3491 } else if ((*key)->flags & LYS_CONFIG_W) {
3492 config = 1;
3493 } else { /* LYS_CONFIG_R */
3494 config = 0;
3495 }
3496
3497 /* check status */
3498 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3499 (*key)->flags, (*key)->module, (*key)->name));
3500
3501 /* mark leaf as unique */
3502 (*key)->flags |= LYS_UNIQUE;
3503
3504 /* next unique value in line */
3505 keystr = delim;
3506 }
3507 /* next unique definition */
3508 }
3509
3510 return LY_SUCCESS;
3511}
3512
3513/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003514 * @brief Compile parsed list node information.
3515 * @param[in] ctx Compile context
3516 * @param[in] node_p Parsed list node.
3517 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3518 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3519 * is enriched with the list-specific information.
3520 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3521 */
3522static LY_ERR
3523lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3524{
3525 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3526 struct lysc_node_list *list = (struct lysc_node_list*)node;
3527 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003528 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003529 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003530 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003531 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003532 LY_ERR ret = LY_SUCCESS;
3533
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003534 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003535 if (list->min) {
3536 list->flags |= LYS_MAND_TRUE;
3537 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003538 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3539
3540 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003541 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003542 }
3543
3544 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
3545
3546 /* keys */
3547 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3548 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3549 return LY_EVALID;
3550 }
3551
3552 /* find all the keys (must be direct children) */
3553 keystr = list_p->key;
3554 while (keystr) {
3555 delim = strpbrk(keystr, " \t\n");
3556 if (delim) {
3557 len = delim - keystr;
3558 while (isspace(*delim)) {
3559 ++delim;
3560 }
3561 } else {
3562 len = strlen(keystr);
3563 }
3564
3565 /* key node must be present */
3566 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3567 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3568 if (!(*key)) {
3569 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3570 "The list's key \"%.*s\" not found.", len, keystr);
3571 return LY_EVALID;
3572 }
3573 /* keys must be unique */
3574 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3575 if (*key == list->keys[u]) {
3576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3577 "Duplicated key identifier \"%.*s\".", len, keystr);
3578 return LY_EVALID;
3579 }
3580 }
3581 /* key must have the same config flag as the list itself */
3582 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3583 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3584 return LY_EVALID;
3585 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003586 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003587 /* YANG 1.0 denies key to be of empty type */
3588 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3590 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3591 return LY_EVALID;
3592 }
3593 } else {
3594 /* when and if-feature are illegal on list keys */
3595 if ((*key)->when) {
3596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3597 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3598 return LY_EVALID;
3599 }
3600 if ((*key)->iffeatures) {
3601 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3602 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3603 return LY_EVALID;
3604 }
3605 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003606
3607 /* check status */
3608 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3609 (*key)->flags, (*key)->module, (*key)->name));
3610
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003611 /* ignore default values of the key */
3612 if ((*key)->dflt) {
3613 lydict_remove(ctx->ctx, (*key)->dflt);
3614 (*key)->dflt = NULL;
3615 }
3616 /* mark leaf as key */
3617 (*key)->flags |= LYS_KEY;
3618
3619 /* next key value */
3620 keystr = delim;
3621 }
3622
3623 /* uniques */
3624 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003625 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003626 }
3627
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003628 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, options, u, lys_compile_action, 0, ret, done);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003629 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 +01003630
3631done:
3632 return ret;
3633}
3634
Radek Krejcib56c7502019-02-13 14:19:54 +01003635/**
3636 * @brief Do some checks and set the default choice's case.
3637 *
3638 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3639 *
3640 * @param[in] ctx Compile context.
3641 * @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,
3642 * not the reference to the imported module.
3643 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3644 * @return LY_ERR value.
3645 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003646static LY_ERR
3647lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3648{
3649 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3650 const char *prefix = NULL, *name;
3651 size_t prefix_len = 0;
3652
3653 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3654 name = strchr(dflt, ':');
3655 if (name) {
3656 prefix = dflt;
3657 prefix_len = name - prefix;
3658 ++name;
3659 } else {
3660 name = dflt;
3661 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003662 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003663 /* prefixed default case make sense only for the prefix of the schema itself */
3664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3665 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3666 prefix_len, prefix);
3667 return LY_EVALID;
3668 }
3669 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3670 if (!ch->dflt) {
3671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3672 "Default case \"%s\" not found.", dflt);
3673 return LY_EVALID;
3674 }
3675 /* no mandatory nodes directly under the default case */
3676 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003677 if (iter->parent != (struct lysc_node*)ch->dflt) {
3678 break;
3679 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003680 if (iter->flags & LYS_MAND_TRUE) {
3681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3682 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3683 return LY_EVALID;
3684 }
3685 }
Radek Krejci01342af2019-01-03 15:18:08 +01003686 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003687 return LY_SUCCESS;
3688}
3689
Radek Krejciccd20f12019-02-15 14:12:27 +01003690static LY_ERR
3691lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3692{
3693 struct lys_module *mod;
3694 const char *prefix = NULL, *name;
3695 size_t prefix_len = 0;
3696 struct lysc_node_case *cs;
3697 struct lysc_node *node;
3698
3699 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3700 name = strchr(dflt, ':');
3701 if (name) {
3702 prefix = dflt;
3703 prefix_len = name - prefix;
3704 ++name;
3705 } else {
3706 name = dflt;
3707 }
3708 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3709 if (prefix) {
3710 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3712 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3713 "The prefix does not match any imported module of the deviation module.",
3714 devnodeid, dflt);
3715 return LY_EVALID;
3716 }
3717 } else {
3718 mod = ctx->mod;
3719 }
3720 /* get the default case */
3721 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3722 if (!cs) {
3723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3724 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3725 devnodeid, dflt);
3726 return LY_EVALID;
3727 }
3728
3729 /* check that there is no mandatory node */
3730 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003731 if (node->parent != (struct lysc_node*)cs) {
3732 break;
3733 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003734 if (node->flags & LYS_MAND_TRUE) {
3735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3736 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3737 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3738 return LY_EVALID;
3739 }
3740 }
3741
3742 /* set the default case in choice */
3743 ch->dflt = cs;
3744 cs->flags |= LYS_SET_DFLT;
3745
3746 return LY_SUCCESS;
3747}
3748
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003749/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003750 * @brief Compile parsed choice node information.
3751 * @param[in] ctx Compile context
3752 * @param[in] node_p Parsed choice node.
3753 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3754 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003755 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003756 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3757 */
3758static LY_ERR
3759lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3760{
3761 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3762 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3763 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003764 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003765 LY_ERR ret = LY_SUCCESS;
3766
Radek Krejci056d0a82018-12-06 16:57:25 +01003767 LY_LIST_FOR(ch_p->child, child_p) {
3768 if (child_p->nodetype == LYS_CASE) {
3769 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003770 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003771 }
3772 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003773 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003774 }
3775 }
3776
3777 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003778 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003779 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003780 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003781
Radek Krejci9800fb82018-12-13 14:26:23 +01003782 return ret;
3783}
3784
3785/**
3786 * @brief Compile parsed anydata or anyxml node information.
3787 * @param[in] ctx Compile context
3788 * @param[in] node_p Parsed anydata or anyxml node.
3789 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3790 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3791 * is enriched with the any-specific information.
3792 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3793 */
3794static LY_ERR
3795lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3796{
3797 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3798 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3799 unsigned int u;
3800 LY_ERR ret = LY_SUCCESS;
3801
3802 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3803
3804 if (any->flags & LYS_CONFIG_W) {
3805 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3806 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3807 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003808done:
3809 return ret;
3810}
3811
Radek Krejcib56c7502019-02-13 14:19:54 +01003812/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003813 * @brief Connect the node into the siblings list and check its name uniqueness.
3814 *
3815 * @param[in] ctx Compile context
3816 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3817 * the choice itself is expected instead of a specific case node.
3818 * @param[in] node Schema node to connect into the list.
Radek Krejci05b774b2019-02-25 13:26:18 +01003819 * @param[in] options Compile options to distinguish input/output when placing node into RPC/action.
Radek Krejci056d0a82018-12-06 16:57:25 +01003820 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3821 */
3822static LY_ERR
Radek Krejci05b774b2019-02-25 13:26:18 +01003823lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node, int options)
Radek Krejci056d0a82018-12-06 16:57:25 +01003824{
3825 struct lysc_node **children;
3826
3827 if (node->nodetype == LYS_CASE) {
3828 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3829 } else {
Radek Krejci05b774b2019-02-25 13:26:18 +01003830 children = lysc_node_children_p(parent, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003831 }
3832 if (children) {
3833 if (!(*children)) {
3834 /* first child */
3835 *children = node;
3836 } else if (*children != node) {
3837 /* by the condition in previous branch we cover the choice/case children
3838 * - the children list is shared by the choice and the the first case, in addition
3839 * the first child of each case must be referenced from the case node. So the node is
3840 * actually always already inserted in case it is the first children - so here such
3841 * a situation actually corresponds to the first branch */
3842 /* insert at the end of the parent's children list */
3843 (*children)->prev->next = node;
3844 node->prev = (*children)->prev;
3845 (*children)->prev = node;
3846
3847 /* check the name uniqueness */
3848 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3849 lysc_node_notifs(parent), node->name, node)) {
3850 return LY_EEXIST;
3851 }
3852 }
3853 }
3854 return LY_SUCCESS;
3855}
3856
Radek Krejci95710c92019-02-11 15:49:55 +01003857/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003858 * @brief Get the XPath context node for the given schema node.
3859 * @param[in] start The schema node where the XPath expression appears.
3860 * @return The context node to evaluate XPath expression in given schema node.
3861 * @return NULL in case the context node is the root node.
3862 */
3863static struct lysc_node *
3864lysc_xpath_context(struct lysc_node *start)
3865{
3866 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3867 start = start->parent);
3868 return start;
3869}
3870
3871/**
3872 * @brief Prepare the case structure in choice node for the new data node.
3873 *
3874 * 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
3875 * created in the choice when the first child was processed.
3876 *
3877 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003878 * @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,
3879 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003880 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3881 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3882 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3883 * @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,
3884 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003885 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003886static struct lysc_node_case*
3887lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3888{
3889 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02003890 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01003891 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003892 unsigned int u;
3893 LY_ERR ret;
3894
Radek Krejci95710c92019-02-11 15:49:55 +01003895#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003896 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003897 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003898 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3899 return NULL; \
3900 } \
3901 }
3902
Radek Krejci95710c92019-02-11 15:49:55 +01003903 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3904 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003905
3906 /* we have to add an implicit case node into the parent choice */
3907 cs = calloc(1, sizeof(struct lysc_node_case));
3908 DUP_STRING(ctx->ctx, child->name, cs->name);
3909 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003910 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003911 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3912 /* the case is already present since the child is not its first children */
3913 return (struct lysc_node_case*)ch->cases->prev;
3914 }
Radek Krejci95710c92019-02-11 15:49:55 +01003915 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003916
3917 /* explicit parent case is not present (this is its first child) */
3918 cs = calloc(1, sizeof(struct lysc_node_case));
3919 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3920 cs->flags = LYS_STATUS_MASK & node_p->flags;
3921 cs->sp = node_p;
3922
Radek Krejcib1b59152019-01-07 13:21:56 +01003923 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejcibae745f2019-04-09 16:28:00 +02003924 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01003925
3926 if (node_p->when) {
3927 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3928 ret = lys_compile_when(ctx, node_p->when, options, when);
3929 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003930 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003931 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003932 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003933 } else {
3934 LOGINT(ctx->ctx);
3935 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003936 }
3937 cs->module = ctx->mod;
3938 cs->prev = (struct lysc_node*)cs;
3939 cs->nodetype = LYS_CASE;
Radek Krejci05b774b2019-02-25 13:26:18 +01003940 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003941 cs->parent = (struct lysc_node*)ch;
3942 cs->child = child;
3943
3944 return cs;
3945error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02003946 if (cs) {
3947 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
3948 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003949 return NULL;
3950
3951#undef UNIQUE_CHECK
3952}
3953
Radek Krejcib56c7502019-02-13 14:19:54 +01003954/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003955 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003956 *
3957 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003958 * @param[in] node Target node where the config is supposed to be changed.
3959 * @param[in] config_flag Node's config flag to be applied to the @p node.
3960 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003961 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3962 * 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 +01003963 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3964 * @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 +01003965 * @return LY_ERR value.
3966 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003967static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003968lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3969 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003970{
3971 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003972 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003973
3974 if (config == (node->flags & LYS_CONFIG_MASK)) {
3975 /* nothing to do */
3976 return LY_SUCCESS;
3977 }
3978
3979 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003980 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003981 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003983 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3984 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003985 return LY_EVALID;
3986 }
Radek Krejci93dcc392019-02-19 10:43:38 +01003987 node->flags |= LYS_SET_CONFIG;
3988 } else {
3989 if (node->flags & LYS_SET_CONFIG) {
3990 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
3991 /* setting config flags, but have node with explicit config true */
3992 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3993 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3994 refine_flag ? "refine" : "deviation", nodeid);
3995 return LY_EVALID;
3996 }
3997 /* do not change config on nodes where the config is explicitely set, this does not apply to
3998 * nodes, which are being changed explicitly (targets of refine or deviation) */
3999 return LY_SUCCESS;
4000 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004001 }
4002 node->flags &= ~LYS_CONFIG_MASK;
4003 node->flags |= config;
4004
4005 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004006 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004007 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004008 }
4009
Radek Krejci76b3e962018-12-14 17:01:25 +01004010 return LY_SUCCESS;
4011}
4012
Radek Krejcib56c7502019-02-13 14:19:54 +01004013/**
4014 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4015 *
4016 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4017 * the flag to such parents from a mandatory children.
4018 *
4019 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4020 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4021 * (mandatory children was removed).
4022 */
Radek Krejcife909632019-02-12 15:34:42 +01004023void
4024lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4025{
4026 struct lysc_node *iter;
4027
4028 if (add) { /* set flag */
4029 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4030 parent = parent->parent) {
4031 parent->flags |= LYS_MAND_TRUE;
4032 }
4033 } else { /* unset flag */
4034 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004035 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004036 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004037 /* there is another mandatory node */
4038 return;
4039 }
4040 }
4041 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4042 parent->flags &= ~LYS_MAND_TRUE;
4043 }
4044 }
4045}
4046
Radek Krejci056d0a82018-12-06 16:57:25 +01004047/**
Radek Krejci3641f562019-02-13 15:38:40 +01004048 * @brief Internal sorting process for the lys_compile_augment_sort().
4049 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4050 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4051 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4052 */
4053static void
4054lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4055{
4056 unsigned int v;
4057 size_t len;
4058
4059 len = strlen(aug_p->nodeid);
4060 LY_ARRAY_FOR(result, v) {
4061 if (strlen(result[v]->nodeid) <= len) {
4062 continue;
4063 }
4064 if (v < LY_ARRAY_SIZE(result)) {
4065 /* move the rest of array */
4066 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4067 break;
4068 }
4069 }
4070 result[v] = aug_p;
4071 LY_ARRAY_INCREMENT(result);
4072}
4073
4074/**
4075 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4076 *
4077 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4078 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4079 *
4080 * @param[in] ctx Compile context.
4081 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4082 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4083 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4084 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4085 * @return LY_ERR value.
4086 */
4087LY_ERR
4088lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4089{
4090 struct lysp_augment **result = NULL;
4091 unsigned int u, v;
4092 size_t count = 0;
4093
4094 assert(augments);
4095
4096 /* get count of the augments in module and all its submodules */
4097 if (aug_p) {
4098 count += LY_ARRAY_SIZE(aug_p);
4099 }
4100 LY_ARRAY_FOR(inc_p, u) {
4101 if (inc_p[u].submodule->augments) {
4102 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4103 }
4104 }
4105
4106 if (!count) {
4107 *augments = NULL;
4108 return LY_SUCCESS;
4109 }
4110 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4111
4112 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4113 * together, so there can be even /z/y betwwen them. */
4114 LY_ARRAY_FOR(aug_p, u) {
4115 lys_compile_augment_sort_(&aug_p[u], result);
4116 }
4117 LY_ARRAY_FOR(inc_p, u) {
4118 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4119 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4120 }
4121 }
4122
4123 *augments = result;
4124 return LY_SUCCESS;
4125}
4126
4127/**
4128 * @brief Compile the parsed augment connecting it into its target.
4129 *
4130 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4131 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4132 * are already implemented and compiled.
4133 *
4134 * @param[in] ctx Compile context.
4135 * @param[in] aug_p Parsed augment to compile.
4136 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4137 * @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
4138 * children in case of the augmenting uses data.
4139 * @return LY_SUCCESS on success.
4140 * @return LY_EVALID on failure.
4141 */
4142LY_ERR
4143lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4144{
4145 LY_ERR ret = LY_SUCCESS;
4146 struct lysp_node *node_p, *case_node_p;
4147 struct lysc_node *target; /* target target of the augment */
4148 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004149 struct lysc_when **when, *when_shared;
4150 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004151 uint16_t flags = 0;
4152 unsigned int u;
Radek Krejci3641f562019-02-13 15:38:40 +01004153
Radek Krejci7af64242019-02-18 13:07:53 +01004154 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004155 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004156 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004157 if (ret != LY_SUCCESS) {
4158 if (ret == LY_EDENIED) {
4159 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4160 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4161 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4162 }
4163 return LY_EVALID;
4164 }
4165
4166 /* check for mandatory nodes
4167 * - new cases augmenting some choice can have mandatory nodes
4168 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4169 */
Radek Krejci733988a2019-02-15 15:12:44 +01004170 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004171 allow_mandatory = 1;
4172 }
4173
4174 when_shared = NULL;
4175 LY_LIST_FOR(aug_p->child, node_p) {
4176 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4177 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4178 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004179 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4180 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004181 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4182 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4183 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4184 return LY_EVALID;
4185 }
4186
4187 /* compile the children */
4188 if (node_p->nodetype != LYS_CASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004189 LY_CHECK_RET(lys_compile_node(ctx, node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004190 } else {
4191 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004192 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004193 }
4194 }
4195
Radek Krejcife13da42019-02-15 14:51:01 +01004196 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4197 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004198 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004199 /* 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 +01004200 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 +01004201 } else if (target->nodetype == LYS_CHOICE) {
4202 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4203 node = ((struct lysc_node_choice*)target)->cases->prev;
4204 } else {
4205 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004206 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004207 }
4208
Radek Krejci733988a2019-02-15 15:12:44 +01004209 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004210 node->flags &= ~LYS_MAND_TRUE;
4211 lys_compile_mandatory_parents(target, 0);
4212 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4213 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4214 aug_p->nodeid, node->name);
4215 return LY_EVALID;
4216 }
4217
4218 /* pass augment's when to all the children */
4219 if (aug_p->when) {
4220 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4221 if (!when_shared) {
4222 ret = lys_compile_when(ctx, aug_p->when, options, when);
4223 LY_CHECK_GOTO(ret, error);
4224 (*when)->context = lysc_xpath_context(target);
4225 when_shared = *when;
4226 } else {
4227 ++when_shared->refcount;
4228 (*when) = when_shared;
4229 }
4230 }
4231 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004232
4233 switch (target->nodetype) {
4234 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004235 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
4236 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004237 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
4238 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004239 break;
4240 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004241 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
4242 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004243 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
4244 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004245 break;
4246 default:
4247 if (aug_p->actions) {
4248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4249 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4250 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4251 return LY_EVALID;
4252 }
4253 if (aug_p->notifs) {
4254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4255 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4256 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4257 return LY_EVALID;
4258 }
4259 }
Radek Krejci3641f562019-02-13 15:38:40 +01004260
4261error:
4262 return ret;
4263}
4264
4265/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004266 * @brief Apply refined or deviated mandatory flag to the target node.
4267 *
4268 * @param[in] ctx Compile context.
4269 * @param[in] node Target node where the mandatory property is supposed to be changed.
4270 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4271 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4272 * @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 +01004273 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4274 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4275 * 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 +01004276 * @return LY_ERR value.
4277 */
4278static LY_ERR
4279lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4280{
4281 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4282 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4283 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4284 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4285 return LY_EVALID;
4286 }
4287
4288 if (mandatory_flag & LYS_MAND_TRUE) {
4289 /* check if node has default value */
4290 if (node->nodetype & LYS_LEAF) {
4291 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004292 if (refine_flag) {
4293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4294 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4295 return LY_EVALID;
4296 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004297 } else {
4298 /* remove the default value taken from the leaf's type */
4299 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4300 ((struct lysc_node_leaf*)node)->dflt = NULL;
4301 }
4302 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004303 if (refine_flag) {
4304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4305 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4306 return LY_EVALID;
4307 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004308 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004309 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004310 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004311 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004312 return LY_EVALID;
4313 }
4314
4315 node->flags &= ~LYS_MAND_FALSE;
4316 node->flags |= LYS_MAND_TRUE;
4317 lys_compile_mandatory_parents(node->parent, 1);
4318 } else {
4319 /* make mandatory false */
4320 node->flags &= ~LYS_MAND_TRUE;
4321 node->flags |= LYS_MAND_FALSE;
4322 lys_compile_mandatory_parents(node->parent, 0);
4323 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4324 /* get the type's default value if any */
4325 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4326 }
4327 }
4328 return LY_SUCCESS;
4329}
4330
4331/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004332 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4333 * If present, also apply uses's modificators.
4334 *
4335 * @param[in] ctx Compile context
4336 * @param[in] uses_p Parsed uses schema node.
4337 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4338 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4339 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4340 * the compile context.
4341 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4342 */
4343static LY_ERR
4344lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
4345{
4346 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004347 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004348 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004349 struct lysc_node_container context_node_fake =
4350 {.nodetype = LYS_CONTAINER,
4351 .module = ctx->mod,
4352 .flags = parent ? parent->flags : 0,
4353 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004354 .prev = (struct lysc_node*)&context_node_fake,
4355 .actions = NULL, .notifs = NULL};
Radek Krejcie86bf772018-12-14 11:39:53 +01004356 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004357 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004358 int found;
4359 const char *id, *name, *prefix;
4360 size_t prefix_len, name_len;
4361 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004362 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004363 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004364 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004365 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004366 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004367 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004368 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004369 unsigned int actions_index, notifs_index;
4370 struct lysc_notif **notifs = NULL;
4371 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004372
4373 /* search for the grouping definition */
4374 found = 0;
4375 id = uses_p->name;
Radek Krejci15e99fc2019-04-08 14:29:39 +02004376 LY_CHECK_RET(lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004377 if (prefix) {
4378 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4379 if (!mod) {
4380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4381 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4382 return LY_EVALID;
4383 }
4384 } else {
4385 mod = ctx->mod_def;
4386 }
4387 if (mod == ctx->mod_def) {
4388 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
4389 grp = lysp_node_groupings(node_p);
4390 LY_ARRAY_FOR(grp, u) {
4391 if (!strcmp(grp[u].name, name)) {
4392 grp = &grp[u];
4393 found = 1;
4394 break;
4395 }
4396 }
4397 }
4398 }
4399 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004400 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004401 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004402 if (grp) {
4403 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4404 if (!strcmp(grp[u].name, name)) {
4405 grp = &grp[u];
4406 found = 1;
4407 }
4408 }
4409 }
4410 if (!found && mod->parsed->includes) {
4411 /* ... and all the submodules */
4412 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4413 grp = mod->parsed->includes[u].submodule->groupings;
4414 if (grp) {
4415 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4416 if (!strcmp(grp[v].name, name)) {
4417 grp = &grp[v];
4418 found = 1;
4419 }
4420 }
4421 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004422 }
4423 }
4424 }
4425 if (!found) {
4426 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4427 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4428 return LY_EVALID;
4429 }
4430
4431 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4432 grp_stack_count = ctx->groupings.count;
4433 ly_set_add(&ctx->groupings, (void*)grp, 0);
4434 if (grp_stack_count == ctx->groupings.count) {
4435 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4437 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4438 return LY_EVALID;
4439 }
4440
4441 /* switch context's mod_def */
4442 mod_old = ctx->mod_def;
4443 ctx->mod_def = mod;
4444
4445 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004446 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup);
Radek Krejcie86bf772018-12-14 11:39:53 +01004447
Radek Krejcifc11bd72019-04-11 16:00:05 +02004448 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004449 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004450 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004451 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004452 child = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01004453
4454 /* some preparation for applying refines */
4455 if (grp->data == node_p) {
4456 /* remember the first child */
4457 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01004458 }
4459 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004460 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004461 LY_LIST_FOR(context_node_fake.child, child) {
4462 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004463
Radek Krejcifc11bd72019-04-11 16:00:05 +02004464 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004465 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004466 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004467 if (!when_shared) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004468 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004469 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004470 when_shared = *when;
4471 } else {
4472 ++when_shared->refcount;
4473 (*when) = when_shared;
4474 }
4475 }
Radek Krejci01342af2019-01-03 15:18:08 +01004476 }
4477 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004478 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004479 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004480 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004481 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 +01004482 }
4483
Radek Krejcifc11bd72019-04-11 16:00:05 +02004484 /* compile actions */
4485 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4486 if (actions) {
4487 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
4488 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, options, u, lys_compile_action, 0, ret, cleanup);
4489 if (*actions && (uses_p->augments || uses_p->refines)) {
4490 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4491 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4492 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4493 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4494 }
4495 }
4496
4497 /* compile notifications */
4498 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4499 if (notifs) {
4500 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
4501 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, options, u, lys_compile_notif, 0, ret, cleanup);
4502 if (*notifs && (uses_p->augments || uses_p->refines)) {
4503 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4504 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4505 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4506 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4507 }
4508 }
4509
4510
Radek Krejci3641f562019-02-13 15:38:40 +01004511 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004512 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004513 LY_ARRAY_FOR(augments, u) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004514 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004515 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004516
Radek Krejcif0089082019-01-07 16:42:01 +01004517 /* reload previous context's mod_def */
4518 ctx->mod_def = mod_old;
4519
Radek Krejci76b3e962018-12-14 17:01:25 +01004520 /* apply refine */
4521 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004522 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 +01004523 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004524 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004525 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004526
4527 /* default value */
4528 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004529 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4531 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4532 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004533 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004534 }
4535 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4537 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4538 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004539 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004540 }
4541 if (node->nodetype == LYS_LEAF) {
4542 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4543 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004544 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004545 /* TODO check the default value according to type */
4546 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004547 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004548 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4549 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02004550 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004551 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004552 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4553 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4554 }
4555 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004556 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004557 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004558 LY_ARRAY_FOR(rfn->dflts, u) {
4559 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4560 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4561 }
4562 /* TODO check the default values according to type */
4563 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004564 if (((struct lysc_node_choice*)node)->dflt) {
4565 /* unset LYS_SET_DFLT from the current default case */
4566 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4567 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004568 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004569 }
4570 }
4571
Radek Krejci12fb9142019-01-08 09:45:30 +01004572 /* description */
4573 if (rfn->dsc) {
4574 FREE_STRING(ctx->ctx, node->dsc);
4575 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4576 }
4577
4578 /* reference */
4579 if (rfn->ref) {
4580 FREE_STRING(ctx->ctx, node->ref);
4581 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4582 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004583
4584 /* config */
4585 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004586 if (!flags) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004587 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004588 } else {
4589 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4590 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4591 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004592 }
4593
4594 /* mandatory */
4595 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004596 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004597 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004598
4599 /* presence */
4600 if (rfn->presence) {
4601 if (node->nodetype != LYS_CONTAINER) {
4602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4603 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4604 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004605 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004606 }
4607 node->flags |= LYS_PRESENCE;
4608 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004609
4610 /* must */
4611 if (rfn->musts) {
4612 switch (node->nodetype) {
4613 case LYS_LEAF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004614 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004615 break;
4616 case LYS_LEAFLIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004617 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004618 break;
4619 case LYS_LIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004620 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004621 break;
4622 case LYS_CONTAINER:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004623 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004624 break;
4625 case LYS_ANYXML:
4626 case LYS_ANYDATA:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004627 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01004628 break;
4629 default:
4630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4631 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4632 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004633 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004634 }
4635 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004636
4637 /* min/max-elements */
4638 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4639 switch (node->nodetype) {
4640 case LYS_LEAFLIST:
4641 if (rfn->flags & LYS_SET_MAX) {
4642 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4643 }
4644 if (rfn->flags & LYS_SET_MIN) {
4645 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004646 if (rfn->min) {
4647 node->flags |= LYS_MAND_TRUE;
4648 lys_compile_mandatory_parents(node->parent, 1);
4649 } else {
4650 node->flags &= ~LYS_MAND_TRUE;
4651 lys_compile_mandatory_parents(node->parent, 0);
4652 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004653 }
4654 break;
4655 case LYS_LIST:
4656 if (rfn->flags & LYS_SET_MAX) {
4657 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4658 }
4659 if (rfn->flags & LYS_SET_MIN) {
4660 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004661 if (rfn->min) {
4662 node->flags |= LYS_MAND_TRUE;
4663 lys_compile_mandatory_parents(node->parent, 1);
4664 } else {
4665 node->flags &= ~LYS_MAND_TRUE;
4666 lys_compile_mandatory_parents(node->parent, 0);
4667 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004668 }
4669 break;
4670 default:
4671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4672 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4673 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004674 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004675 }
4676 }
Radek Krejcif0089082019-01-07 16:42:01 +01004677
4678 /* if-feature */
4679 if (rfn->iffeatures) {
4680 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004681 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004682 }
Radek Krejci01342af2019-01-03 15:18:08 +01004683 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004684
Radek Krejcif2271f12019-01-07 16:42:23 +01004685 /* do some additional checks of the changed nodes when all the refines are applied */
4686 for (u = 0; u < refined.count; ++u) {
4687 node = (struct lysc_node*)refined.objs[u];
4688 rfn = &uses_p->refines[u];
4689
4690 /* check possible conflict with default value (default added, mandatory left true) */
4691 if ((node->flags & LYS_MAND_TRUE) &&
4692 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4693 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4695 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004696 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004697 }
4698
4699 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4700 if (node->nodetype == LYS_LIST) {
4701 min = ((struct lysc_node_list*)node)->min;
4702 max = ((struct lysc_node_list*)node)->max;
4703 } else {
4704 min = ((struct lysc_node_leaflist*)node)->min;
4705 max = ((struct lysc_node_leaflist*)node)->max;
4706 }
4707 if (min > max) {
4708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4709 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4710 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004711 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004712 }
4713 }
4714 }
4715
Radek Krejcie86bf772018-12-14 11:39:53 +01004716 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004717
4718cleanup:
4719 /* fix connection of the children nodes from fake context node back into the parent */
4720 if (context_node_fake.child) {
4721 context_node_fake.child->prev = child;
4722 }
4723 LY_LIST_FOR(context_node_fake.child, child) {
4724 child->parent = parent;
4725 }
4726
4727 if (uses_p->augments || uses_p->refines) {
4728 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004729 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004730 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4731 LY_ARRAY_FREE(context_node_fake.actions);
4732 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004733 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004734 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4735 LY_ARRAY_FREE(context_node_fake.notifs);
4736 }
4737 }
4738
Radek Krejcie86bf772018-12-14 11:39:53 +01004739 /* reload previous context's mod_def */
4740 ctx->mod_def = mod_old;
4741 /* remove the grouping from the stack for circular groupings dependency check */
4742 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4743 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004744 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004745 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004746
4747 return ret;
4748}
4749
Radek Krejcife909632019-02-12 15:34:42 +01004750
Radek Krejcie86bf772018-12-14 11:39:53 +01004751/**
Radek Krejcia3045382018-11-22 14:30:31 +01004752 * @brief Compile parsed schema node information.
4753 * @param[in] ctx Compile context
4754 * @param[in] node_p Parsed schema node.
4755 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4756 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4757 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4758 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004759 * @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).
4760 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004761 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4762 */
Radek Krejci19a96102018-11-15 13:38:09 +01004763static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01004764lys_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 +01004765{
4766 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004767 struct lysc_node *node;
4768 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004769 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004770 unsigned int u;
4771 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
4772
4773 switch (node_p->nodetype) {
4774 case LYS_CONTAINER:
4775 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4776 node_compile_spec = lys_compile_node_container;
4777 break;
4778 case LYS_LEAF:
4779 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4780 node_compile_spec = lys_compile_node_leaf;
4781 break;
4782 case LYS_LIST:
4783 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004784 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004785 break;
4786 case LYS_LEAFLIST:
4787 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004788 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004789 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004790 case LYS_CHOICE:
4791 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004792 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004793 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004794 case LYS_ANYXML:
4795 case LYS_ANYDATA:
4796 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004797 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004798 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004799 case LYS_USES:
4800 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004801 default:
4802 LOGINT(ctx->ctx);
4803 return LY_EINT;
4804 }
4805 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4806 node->nodetype = node_p->nodetype;
4807 node->module = ctx->mod;
4808 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004809 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004810
4811 /* config */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004812 if (options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
4813 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004814 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004815 node->flags |= (options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4816 } else if (options & LYSC_OPT_NOTIFICATION) {
4817 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004818 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004819 node->flags |= LYS_CONFIG_R;
4820 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004821 /* config not explicitely set, inherit it from parent */
4822 if (parent) {
4823 node->flags |= parent->flags & LYS_CONFIG_MASK;
4824 } else {
4825 /* default is config true */
4826 node->flags |= LYS_CONFIG_W;
4827 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004828 } else {
4829 /* config set explicitely */
4830 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004831 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004832 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4834 "Configuration node cannot be child of any state data node.");
4835 goto error;
4836 }
Radek Krejci19a96102018-11-15 13:38:09 +01004837
Radek Krejcia6d57732018-11-29 13:40:37 +01004838 /* *list ordering */
4839 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4840 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004841 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
4842 (options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
4843 (options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01004844 node->flags &= ~LYS_ORDBY_MASK;
4845 node->flags |= LYS_ORDBY_SYSTEM;
4846 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4847 /* default ordering is system */
4848 node->flags |= LYS_ORDBY_SYSTEM;
4849 }
4850 }
4851
Radek Krejci19a96102018-11-15 13:38:09 +01004852 /* status - it is not inherited by specification, but it does not make sense to have
4853 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004854 if (!parent || parent->nodetype != LYS_CHOICE) {
4855 /* in case of choice/case's children, postpone the check to the moment we know if
4856 * 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 +01004857 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 +01004858 }
4859
4860 if (!(options & LYSC_OPT_FREE_SP)) {
4861 node->sp = node_p;
4862 }
4863 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004864 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4865 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004866 if (node_p->when) {
4867 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4868 ret = lys_compile_when(ctx, node_p->when, options, when);
4869 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004870 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004871 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004872 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004873 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
4874
4875 /* nodetype-specific part */
4876 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
4877
Radek Krejcife909632019-02-12 15:34:42 +01004878 /* inherit LYS_MAND_TRUE in parent containers */
4879 if (node->flags & LYS_MAND_TRUE) {
4880 lys_compile_mandatory_parents(parent, 1);
4881 }
4882
Radek Krejci19a96102018-11-15 13:38:09 +01004883 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004884 if (parent) {
4885 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004886 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
4887 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004888 if (uses_status) {
4889
4890 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004891 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004892 * it directly gets the same status flags as the choice;
4893 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004894 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004895 node->parent = (struct lysc_node*)cs;
4896 } else { /* other than choice */
4897 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004898 }
Radek Krejci05b774b2019-02-25 13:26:18 +01004899 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node, options), LY_EVALID);
Radek Krejci19a96102018-11-15 13:38:09 +01004900 } else {
4901 /* top-level element */
4902 if (!ctx->mod->compiled->data) {
4903 ctx->mod->compiled->data = node;
4904 } else {
4905 /* insert at the end of the module's top-level nodes list */
4906 ctx->mod->compiled->data->prev->next = node;
4907 node->prev = ctx->mod->compiled->data->prev;
4908 ctx->mod->compiled->data->prev = node;
4909 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004910 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4911 ctx->mod->compiled->notifs, node->name, node)) {
4912 return LY_EVALID;
4913 }
Radek Krejci19a96102018-11-15 13:38:09 +01004914 }
4915
4916 return LY_SUCCESS;
4917
4918error:
4919 lysc_node_free(ctx->ctx, node);
4920 return ret;
4921}
4922
Radek Krejciccd20f12019-02-15 14:12:27 +01004923static void
4924lysc_disconnect(struct lysc_node *node)
4925{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004926 struct lysc_node *parent, *child, *prev = NULL, *next;
4927 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004928 int remove_cs = 0;
4929
4930 parent = node->parent;
4931
4932 /* parent's first child */
4933 if (!parent) {
4934 return;
4935 }
4936 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004937 cs = (struct lysc_node_case*)node;
4938 } else if (parent->nodetype == LYS_CASE) {
4939 /* disconnecting some node in a case */
4940 cs = (struct lysc_node_case*)parent;
4941 parent = cs->parent;
4942 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4943 if (child == node) {
4944 if (cs->child == child) {
4945 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
4946 /* case with a single child -> remove also the case */
4947 child->parent = NULL;
4948 remove_cs = 1;
4949 } else {
4950 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01004951 }
4952 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004953 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01004954 }
4955 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004956 if (!remove_cs) {
4957 cs = NULL;
4958 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004959 } else if (lysc_node_children(parent, node->flags) == node) {
4960 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004961 }
4962
4963 if (cs) {
4964 if (remove_cs) {
4965 /* cs has only one child which is being also removed */
4966 lysc_disconnect((struct lysc_node*)cs);
4967 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
4968 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01004969 if (((struct lysc_node_choice*)parent)->dflt == cs) {
4970 /* default case removed */
4971 ((struct lysc_node_choice*)parent)->dflt = NULL;
4972 }
4973 if (((struct lysc_node_choice*)parent)->cases == cs) {
4974 /* first case removed */
4975 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
4976 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004977 if (cs->child) {
4978 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
4979 if (cs->child->prev->parent != (struct lysc_node*)cs) {
4980 prev = cs->child->prev;
4981 } /* else all the children are under a single case */
4982 LY_LIST_FOR_SAFE(cs->child, next, child) {
4983 if (child->parent != (struct lysc_node*)cs) {
4984 break;
4985 }
4986 lysc_node_free(node->module->ctx, child);
4987 }
4988 if (prev) {
4989 if (prev->next) {
4990 prev->next = child;
4991 }
4992 if (child) {
4993 child->prev = prev;
4994 } else {
4995 /* link from the first child under the cases */
4996 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
4997 }
4998 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004999 }
5000 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005001 }
5002
5003 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005004 if (node->prev->next) {
5005 node->prev->next = node->next;
5006 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005007 if (node->next) {
5008 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005009 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005010 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005011 if (child) {
5012 child->prev = node->prev;
5013 }
5014 } else if (((struct lysc_node_choice*)parent)->cases) {
5015 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005016 }
5017}
5018
5019LY_ERR
5020lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options)
5021{
5022 LY_ERR ret = LY_EVALID;
5023 struct ly_set devs_p = {0};
5024 struct ly_set targets = {0};
5025 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005026 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005027 struct lysc_action *rpcs;
5028 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005029 struct lysp_deviation *dev;
5030 struct lysp_deviate *d, **dp_new;
5031 struct lysp_deviate_add *d_add;
5032 struct lysp_deviate_del *d_del;
5033 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005034 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005035 struct lysc_deviation {
5036 const char *nodeid;
5037 struct lysc_node *target; /* target node of the deviation */
5038 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005039 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005040 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5041 } **devs = NULL;
5042 int i;
5043 size_t prefix_len, name_len;
5044 const char *prefix, *name, *nodeid;
5045 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005046 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005047 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005048
5049 /* get all deviations from the module and all its submodules ... */
5050 LY_ARRAY_FOR(mod_p->deviations, u) {
5051 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5052 }
5053 LY_ARRAY_FOR(mod_p->includes, v) {
5054 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5055 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5056 }
5057 }
5058 if (!devs_p.count) {
5059 /* nothing to do */
5060 return LY_SUCCESS;
5061 }
5062
5063 /* ... and group them by the target node */
5064 devs = calloc(devs_p.count, sizeof *devs);
5065 for (u = 0; u < devs_p.count; ++u) {
5066 dev = devs_p.objs[u];
5067
5068 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005069 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5070 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005071 if (target->nodetype == LYS_ACTION) {
5072 /* move the target pointer to input/output to make them different from the action and
5073 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5074 * back to the RPC/action node due to a better compatibility and decision code in this function.
5075 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5076 if (flags & LYSC_OPT_RPC_INPUT) {
5077 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5078 flags |= LYSC_OPT_INTERNAL;
5079 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5080 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5081 flags |= LYSC_OPT_INTERNAL;
5082 }
5083 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005084 /* insert into the set of targets with duplicity detection */
5085 i = ly_set_add(&targets, target, 0);
5086 if (!devs[i]) {
5087 /* new record */
5088 devs[i] = calloc(1, sizeof **devs);
5089 devs[i]->target = target;
5090 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005091 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005092 }
5093 /* add deviates into the deviation's list of deviates */
5094 for (d = dev->deviates; d; d = d->next) {
5095 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5096 *dp_new = d;
5097 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5098 devs[i]->not_supported = 1;
5099 }
5100 }
5101 }
5102
5103 /* MACROS for deviates checking */
5104#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5105 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
5106 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
5107 goto cleanup; \
5108 }
5109
5110#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5111 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
5112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
5113 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
5114 goto cleanup; \
5115 }
5116
5117#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005118 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5120 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
5121 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5122 goto cleanup; \
5123 }
5124
Radek Krejci551b12c2019-02-19 16:11:21 +01005125#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5126 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5128 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
5129 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5130 goto cleanup; \
5131 }
5132
Radek Krejciccd20f12019-02-15 14:12:27 +01005133#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5134 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5136 goto cleanup; \
5137 }
5138
Radek Krejci551b12c2019-02-19 16:11:21 +01005139#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5140 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5141 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5142 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5143 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5144 goto cleanup; \
5145 }
5146
Radek Krejciccd20f12019-02-15 14:12:27 +01005147#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5148 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5149 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5151 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5152 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5153 goto cleanup; \
5154 } \
5155 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5156 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5157
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005158#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5159 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5160 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5161 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5162 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 +01005163 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005164 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005165 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5166 "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 +01005167 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005168 goto cleanup; \
5169 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005170 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5171 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5172 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5173 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5174 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005175 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005176 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5177 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5178 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005179 }
5180
5181 /* apply deviations */
5182 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005183 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5184 /* fix the target pointer in case of RPC's/action's input/output */
5185 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5186 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5187 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5188 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5189 }
5190 }
5191
Radek Krejciccd20f12019-02-15 14:12:27 +01005192 /* not-supported */
5193 if (devs[u]->not_supported) {
5194 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5195 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5196 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5197 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005198
5199#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5200 if (devs[u]->target->parent) { \
5201 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5202 } else { \
5203 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5204 } \
5205 LY_ARRAY_FOR(ARRAY, x) { \
5206 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5207 } \
5208 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5209 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5210 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5211 LY_ARRAY_DECREMENT(ARRAY); \
5212 }
5213
Radek Krejcif538ce52019-03-05 10:46:14 +01005214 if (devs[u]->target->nodetype == LYS_ACTION) {
5215 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5216 /* remove RPC's/action's input */
5217 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5218 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005219 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5220 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005221 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5222 /* remove RPC's/action's output */
5223 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5224 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005225 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5226 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005227 } else {
5228 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005229 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005230 }
5231 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005232 /* remove Notification */
5233 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005234 } else {
5235 /* remove the target node */
5236 lysc_disconnect(devs[u]->target);
5237 lysc_node_free(ctx->ctx, devs[u]->target);
5238 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005239
5240 continue;
5241 }
5242
5243 /* list of deviates (not-supported is not present in the list) */
5244 LY_ARRAY_FOR(devs[u]->deviates, v) {
5245 d = devs[u]->deviates[v];
5246
5247 switch (d->mod) {
5248 case LYS_DEV_ADD:
5249 d_add = (struct lysp_deviate_add*)d;
5250 /* [units-stmt] */
5251 if (d_add->units) {
5252 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5253 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5254
5255 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5256 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5257 }
5258
5259 /* *must-stmt */
5260 if (d_add->musts) {
5261 switch (devs[u]->target->nodetype) {
5262 case LYS_CONTAINER:
5263 case LYS_LIST:
5264 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5265 options, x, lys_compile_must, ret, cleanup);
5266 break;
5267 case LYS_LEAF:
5268 case LYS_LEAFLIST:
5269 case LYS_ANYDATA:
5270 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5271 options, x, lys_compile_must, ret, cleanup);
5272 break;
5273 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005274 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5275 options, x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005276 break;
5277 case LYS_ACTION:
5278 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5279 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5280 options, x, lys_compile_must, ret, cleanup);
5281 break;
5282 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5283 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5284 options, x, lys_compile_must, ret, cleanup);
5285 break;
5286 }
5287 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005288 default:
5289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005290 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005291 goto cleanup;
5292 }
5293 }
5294
5295 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005296 if (d_add->uniques) {
5297 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5298 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5299 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005300
5301 /* *default-stmt */
5302 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005303 switch (devs[u]->target->nodetype) {
5304 case LYS_LEAF:
5305 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5306 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5307 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5308 /* first, remove the default value taken from the type */
5309 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5310 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5311 }
5312 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005313 /* mark the new default values as leaf's own */
5314 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005315 break;
5316 case LYS_LEAFLIST:
5317 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5318 /* first, remove the default value taken from the type */
5319 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5320 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5321 }
5322 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5323 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5324 }
5325 /* add new default value(s) */
5326 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5327 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5328 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5329 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5330 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5331 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5332 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005333 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005334 devs[u]->target->flags |= LYS_SET_DFLT;
5335 break;
5336 case LYS_CHOICE:
5337 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5338 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5339 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5340 * to allow making the default case even the augmented case from the deviating module */
5341 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5342 (struct lysc_node_choice*)devs[u]->target)) {
5343 goto cleanup;
5344 }
5345 break;
5346 default:
5347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005348 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005349 goto cleanup;
5350 }
5351 }
5352
5353 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005354 if (d_add->flags & LYS_CONFIG_MASK) {
5355 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5357 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5358 goto cleanup;
5359 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005360 if (devs[u]->flags) {
5361 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5362 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5363 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005364 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5366 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5367 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5368 goto cleanup;
5369 }
5370 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5371 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005372
5373 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005374 if (d_add->flags & LYS_MAND_MASK) {
5375 if (devs[u]->target->flags & LYS_MAND_MASK) {
5376 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5377 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5378 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5379 goto cleanup;
5380 }
5381 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5382 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005383
5384 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005385 if (d_add->flags & LYS_SET_MIN) {
5386 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5387 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5388 /* change value */
5389 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5390 } else if (devs[u]->target->nodetype == LYS_LIST) {
5391 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5392 /* change value */
5393 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5394 } else {
5395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5396 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5397 goto cleanup;
5398 }
5399 if (d_add->min) {
5400 devs[u]->target->flags |= LYS_MAND_TRUE;
5401 }
5402 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005403
5404 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005405 if (d_add->flags & LYS_SET_MAX) {
5406 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5407 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5408 /* change value */
5409 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5410 } else if (devs[u]->target->nodetype == LYS_LIST) {
5411 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5412 /* change value */
5413 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5414 } else {
5415 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5416 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5417 goto cleanup;
5418 }
5419 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005420
5421 break;
5422 case LYS_DEV_DELETE:
5423 d_del = (struct lysp_deviate_del*)d;
5424
5425 /* [units-stmt] */
5426 if (d_del->units) {
5427 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5428 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5429 }
5430
5431 /* *must-stmt */
5432 if (d_del->musts) {
5433 switch (devs[u]->target->nodetype) {
5434 case LYS_CONTAINER:
5435 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005436 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005437 break;
5438 case LYS_LEAF:
5439 case LYS_LEAFLIST:
5440 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005441 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005442 break;
5443 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005444 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005445 break;
5446 case LYS_ACTION:
5447 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5448 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5449 break;
5450 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5451 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5452 break;
5453 }
5454 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005455 default:
5456 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005457 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005458 goto cleanup;
5459 }
5460 }
5461
5462 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005463 if (d_del->uniques) {
5464 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5465 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5466 LY_ARRAY_FOR(d_del->uniques, x) {
5467 LY_ARRAY_FOR(list->uniques, z) {
5468 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5469 nodeid = strpbrk(name, " \t\n");
5470 if (nodeid) {
5471 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5472 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5473 break;
5474 }
5475 while (isspace(*nodeid)) {
5476 ++nodeid;
5477 }
5478 } else {
5479 if (strcmp(name, list->uniques[z][y]->name)) {
5480 break;
5481 }
5482 }
5483 }
5484 if (!name) {
5485 /* complete match - remove the unique */
5486 LY_ARRAY_DECREMENT(list->uniques);
5487 LY_ARRAY_FREE(list->uniques[z]);
5488 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5489 --z;
5490 break;
5491 }
5492 }
5493 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5495 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5496 devs[u]->nodeid, d_del->uniques[x]);
5497 goto cleanup;
5498 }
5499 }
5500 if (!LY_ARRAY_SIZE(list->uniques)) {
5501 LY_ARRAY_FREE(list->uniques);
5502 list->uniques = NULL;
5503 }
5504 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005505
5506 /* *default-stmt */
5507 if (d_del->dflts) {
5508 switch (devs[u]->target->nodetype) {
5509 case LYS_LEAF:
5510 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5511 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5512 dflt, "deleting", "default", d_del->dflts[0]);
5513
5514 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005515 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005516 break;
5517 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005518 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005519 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5520 devs[u]->target->flags &= ~LYS_SET_DFLT;
5521 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005522 break;
5523 case LYS_CHOICE:
5524 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5525 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5526 nodeid = d_del->dflts[0];
5527 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5528 if (prefix) {
5529 /* use module prefixes from the deviation module to match the module of the default case */
5530 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5531 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5532 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5533 "The prefix does not match any imported module of the deviation module.",
5534 devs[u]->nodeid, d_del->dflts[0]);
5535 goto cleanup;
5536 }
5537 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5538 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5539 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5540 "The prefix does not match the default case's module.",
5541 devs[u]->nodeid, d_del->dflts[0]);
5542 goto cleanup;
5543 }
5544 }
5545 /* else {
5546 * strictly, the default prefix would point to the deviation module, but the value should actually
5547 * match the default string in the original module (usually unprefixed), so in this case we do not check
5548 * the module of the default case, just matching its name */
5549 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5551 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5552 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5553 goto cleanup;
5554 }
5555 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5556 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5557 break;
5558 default:
5559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005560 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005561 goto cleanup;
5562 }
5563 }
5564
5565 break;
5566 case LYS_DEV_REPLACE:
5567 d_rpl = (struct lysp_deviate_rpl*)d;
5568
5569 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005570 if (d_rpl->type) {
5571 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5572 /* type is mandatory, so checking for its presence is not necessary */
5573 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
5574 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup);
5575 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005576
5577 /* [units-stmt] */
5578 if (d_rpl->units) {
5579 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5580 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5581 units, "replacing", "units", d_rpl->units);
5582
5583 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5584 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5585 }
5586
5587 /* [default-stmt] */
5588 if (d_rpl->dflt) {
5589 switch (devs[u]->target->nodetype) {
5590 case LYS_LEAF:
5591 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5592 dflt, "replacing", "default", d_rpl->dflt);
5593
5594 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5595 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5596 break;
5597 case LYS_CHOICE:
5598 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5599 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5600 (struct lysc_node_choice*)devs[u]->target)) {
5601 goto cleanup;
5602 }
5603 break;
5604 default:
5605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005606 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005607 goto cleanup;
5608 }
5609 }
5610
5611 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005612 if (d_rpl->flags & LYS_CONFIG_MASK) {
5613 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5615 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5616 goto cleanup;
5617 }
5618 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5620 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5621 goto cleanup;
5622 }
5623 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5624 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005625
5626 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005627 if (d_rpl->flags & LYS_MAND_MASK) {
5628 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5630 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5631 goto cleanup;
5632 }
5633 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5634 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005635
5636 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005637 if (d_rpl->flags & LYS_SET_MIN) {
5638 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5639 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5640 /* change value */
5641 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5642 } else if (devs[u]->target->nodetype == LYS_LIST) {
5643 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5644 /* change value */
5645 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5646 } else {
5647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5648 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5649 goto cleanup;
5650 }
5651 if (d_rpl->min) {
5652 devs[u]->target->flags |= LYS_MAND_TRUE;
5653 }
5654 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005655
5656 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005657 if (d_rpl->flags & LYS_SET_MAX) {
5658 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5659 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5660 /* change value */
5661 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5662 } else if (devs[u]->target->nodetype == LYS_LIST) {
5663 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5664 /* change value */
5665 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5666 } else {
5667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5668 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5669 goto cleanup;
5670 }
5671 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005672
5673 break;
5674 default:
5675 LOGINT(ctx->ctx);
5676 goto cleanup;
5677 }
5678 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005679
Radek Krejci33f72892019-02-21 10:36:58 +01005680 /* final check when all deviations of a single target node are applied */
5681
Radek Krejci551b12c2019-02-19 16:11:21 +01005682 /* check min-max compatibility */
5683 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5684 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5685 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5686 } else if (devs[u]->target->nodetype == LYS_LIST) {
5687 min = ((struct lysc_node_list*)devs[u]->target)->min;
5688 max = ((struct lysc_node_list*)devs[u]->target)->max;
5689 } else {
5690 min = max = 0;
5691 }
5692 if (min > max) {
5693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5694 "after deviation (%s): min value %u is bigger than max value %u.",
5695 devs[u]->nodeid, min, max);
5696 goto cleanup;
5697 }
5698
5699 /* check mandatory - default compatibility */
5700 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5701 && (devs[u]->target->flags & LYS_SET_DFLT)
5702 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5704 "Invalid deviation (%s) combining default value and mandatory %s.",
5705 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5706 goto cleanup;
5707 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5708 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5709 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5711 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5712 goto cleanup;
5713 }
5714 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5715 /* mandatory node under a default case */
5716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5717 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5718 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5719 goto cleanup;
5720 }
Radek Krejci33f72892019-02-21 10:36:58 +01005721
5722 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005723 }
5724
5725 ret = LY_SUCCESS;
5726
5727cleanup:
5728 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5729 LY_ARRAY_FREE(devs[u]->deviates);
5730 free(devs[u]);
5731 }
5732 free(devs);
5733 ly_set_erase(&targets, NULL);
5734 ly_set_erase(&devs_p, NULL);
5735
5736 return ret;
5737}
5738
Radek Krejcib56c7502019-02-13 14:19:54 +01005739/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005740 * @brief Compile the given YANG submodule into the main module.
5741 * @param[in] ctx Compile context
5742 * @param[in] inc Include structure from the main module defining the submodule.
5743 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
5744 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5745 */
5746LY_ERR
5747lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
5748{
5749 unsigned int u;
5750 LY_ERR ret = LY_SUCCESS;
5751 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005752 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005753 struct lysc_module *mainmod = ctx->mod->compiled;
5754
Radek Krejci0af46292019-01-11 16:02:31 +01005755 if (!mainmod->mod->off_features) {
5756 /* features are compiled directly into the compiled module structure,
5757 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5758 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci693262f2019-04-29 15:23:20 +02005759 ret = lys_feature_precompile(ctx->ctx, ctx->mod, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01005760 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5761 LY_CHECK_GOTO(ret, error);
5762 }
5763
Radek Krejcid05cbd92018-12-05 14:26:40 +01005764 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
5765
Radek Krejci8cce8532019-03-05 11:27:45 +01005766 /* TODO data nodes */
5767
5768 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005769 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, options, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01005770
Radek Krejcid05cbd92018-12-05 14:26:40 +01005771error:
5772 return ret;
5773}
5774
Radek Krejci19a96102018-11-15 13:38:09 +01005775LY_ERR
5776lys_compile(struct lys_module *mod, int options)
5777{
5778 struct lysc_ctx ctx = {0};
5779 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005780 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005781 struct lysp_module *sp;
5782 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005783 struct lysp_augment **augments = NULL;
5784 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005785 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005786 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005787
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005788 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005789
5790 if (!mod->implemented) {
5791 /* just imported modules are not compiled */
5792 return LY_SUCCESS;
5793 }
5794
Radek Krejci19a96102018-11-15 13:38:09 +01005795 sp = mod->parsed;
5796
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005797 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005798 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005799 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005800
5801 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005802 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5803 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005804
Radek Krejci19a96102018-11-15 13:38:09 +01005805 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005806 LY_ARRAY_FOR(sp->includes, u) {
5807 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
5808 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5809 }
Radek Krejci0af46292019-01-11 16:02:31 +01005810 if (mod->off_features) {
5811 /* there is already precompiled array of features */
5812 mod_c->features = mod->off_features;
5813 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005814 } else {
5815 /* features are compiled directly into the compiled module structure,
5816 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci693262f2019-04-29 15:23:20 +02005817 ret = lys_feature_precompile(ctx.ctx, ctx.mod, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005818 LY_CHECK_GOTO(ret, error);
5819 }
5820 /* finish feature compilation, not only for the main module, but also for the submodules.
5821 * Due to possible forward references, it must be done when all the features (including submodules)
5822 * are present. */
5823 LY_ARRAY_FOR(sp->features, u) {
5824 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
5825 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5826 }
5827 LY_ARRAY_FOR(sp->includes, v) {
5828 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
5829 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
5830 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5831 }
5832 }
5833
Radek Krejcid05cbd92018-12-05 14:26:40 +01005834 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005835 if (sp->identities) {
5836 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5837 }
5838
Radek Krejci95710c92019-02-11 15:49:55 +01005839 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005840 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005841 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005842 LY_CHECK_GOTO(ret, error);
5843 }
Radek Krejci95710c92019-02-11 15:49:55 +01005844
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005845 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005846 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 +01005847
Radek Krejci95710c92019-02-11 15:49:55 +01005848 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005849 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005850 LY_CHECK_GOTO(ret, error);
5851 LY_ARRAY_FOR(augments, u) {
5852 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
5853 LY_CHECK_GOTO(ret, error);
5854 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005855
5856 /* deviations */
5857 ret = lys_compile_deviations(&ctx, sp, options);
5858 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005859
5860 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
5861
Radek Krejcia3045382018-11-22 14:30:31 +01005862 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005863 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5864 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5865 * 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 +01005866 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005867 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005868 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5869 if (type->basetype == LY_TYPE_LEAFREF) {
5870 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005871 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 +01005872 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005873 } else if (type->basetype == LY_TYPE_UNION) {
5874 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5875 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5876 /* validate the path */
5877 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5878 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5879 LY_CHECK_GOTO(ret, error);
5880 }
5881 }
Radek Krejcia3045382018-11-22 14:30:31 +01005882 }
5883 }
5884 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005885 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005886 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005887 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5888 if (type->basetype == LY_TYPE_LEAFREF) {
5889 /* store pointer to the real type */
5890 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5891 typeiter->basetype == LY_TYPE_LEAFREF;
5892 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5893 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005894 } else if (type->basetype == LY_TYPE_UNION) {
5895 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5896 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5897 /* store pointer to the real type */
5898 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5899 typeiter->basetype == LY_TYPE_LEAFREF;
5900 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5901 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5902 }
5903 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005904 }
5905 }
5906 }
Radek Krejcia3045382018-11-22 14:30:31 +01005907 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005908 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005909 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005910
Radek Krejci19a96102018-11-15 13:38:09 +01005911 if (options & LYSC_OPT_FREE_SP) {
5912 lysp_module_free(mod->parsed);
5913 ((struct lys_module*)mod)->parsed = NULL;
5914 }
5915
Radek Krejci95710c92019-02-11 15:49:55 +01005916 if (!(options & LYSC_OPT_INTERNAL)) {
5917 /* remove flag of the modules implemented by dependency */
5918 for (u = 0; u < ctx.ctx->list.count; ++u) {
5919 m = ctx.ctx->list.objs[u];
5920 if (m->implemented == 2) {
5921 m->implemented = 1;
5922 }
5923 }
5924 }
5925
Radek Krejci19a96102018-11-15 13:38:09 +01005926 ((struct lys_module*)mod)->compiled = mod_c;
5927 return LY_SUCCESS;
5928
5929error:
Radek Krejci95710c92019-02-11 15:49:55 +01005930 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01005931 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005932 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005933 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01005934 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005935 mod->compiled = NULL;
5936
5937 /* revert compilation of modules implemented by dependency */
5938 for (u = 0; u < ctx.ctx->list.count; ++u) {
5939 m = ctx.ctx->list.objs[u];
5940 if (m->implemented == 2) {
5941 /* revert features list to the precompiled state */
5942 lys_feature_precompile_revert(&ctx, m);
5943 /* mark module as imported-only / not-implemented */
5944 m->implemented = 0;
5945 /* free the compiled version of the module */
5946 lysc_module_free(m->compiled, NULL);
5947 m->compiled = NULL;
5948 }
5949 }
5950
Radek Krejci19a96102018-11-15 13:38:09 +01005951 return ret;
5952}