blob: f2326aa5724c088b29d8f45b2659cbec1ee4a798 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <ctype.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <stdio.h>
Radek Krejci19a96102018-11-15 13:38:09 +010019
20#include "libyang.h"
21#include "context.h"
22#include "tree_schema_internal.h"
23#include "xpath.h"
24
25/**
26 * @brief Duplicate string into dictionary
27 * @param[in] CTX libyang context of the dictionary.
28 * @param[in] ORIG String to duplicate.
29 * @param[out] DUP Where to store the result.
30 */
31#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
32
33#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
34 if (ARRAY_P) { \
35 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010036 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010037 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
38 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010039 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER + __array_offset]); \
40 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
41 } \
42 }
43
Radek Krejci6eeb58f2019-02-22 16:29:37 +010044#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, OPTIONS, ITER, FUNC, USES_STATUS, RET, GOTO) \
45 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
47 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
48 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
50 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
51 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejcid05cbd92018-12-05 14:26:40 +010055#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
56 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
61 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
66#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
67 if (MEMBER_P) { \
68 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
69 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
70 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
71 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 }
73
Radek Krejci00b874b2019-02-12 10:54:50 +010074#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \
75 if (MEMBER_P) { \
76 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
77 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
78 LY_ARRAY_INCREMENT(ARRAY_C); \
79 RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \
80 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
81 }
82
Radek Krejcid05cbd92018-12-05 14:26:40 +010083#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
84 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010085 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
86 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
88 return LY_EVALID; \
89 } \
90 } \
91 }
92
Radek Krejci19a96102018-11-15 13:38:09 +010093static struct lysc_ext_instance *
94lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
95{
96 /* TODO */
97 (void) ctx;
98 (void) orig;
99 return NULL;
100}
101
Radek Krejcib56c7502019-02-13 14:19:54 +0100102/**
103 * @brief Duplicate the compiled pattern structure.
104 *
105 * Instead of duplicating memory, the reference counter in the @p orig is increased.
106 *
107 * @param[in] orig The pattern structure to duplicate.
108 * @return The duplicated structure to use.
109 */
Radek Krejci19a96102018-11-15 13:38:09 +0100110static struct lysc_pattern*
111lysc_pattern_dup(struct lysc_pattern *orig)
112{
113 ++orig->refcount;
114 return orig;
115}
116
Radek Krejcib56c7502019-02-13 14:19:54 +0100117/**
118 * @brief Duplicate the array of compiled patterns.
119 *
120 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
121 *
122 * @param[in] ctx Libyang context for logging.
123 * @param[in] orig The patterns sized array to duplicate.
124 * @return New sized array as a copy of @p orig.
125 * @return NULL in case of memory allocation error.
126 */
Radek Krejci19a96102018-11-15 13:38:09 +0100127static struct lysc_pattern**
128lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
129{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100130 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100131 unsigned int u;
132
Radek Krejcib56c7502019-02-13 14:19:54 +0100133 assert(orig);
134
Radek Krejci19a96102018-11-15 13:38:09 +0100135 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
136 LY_ARRAY_FOR(orig, u) {
137 dup[u] = lysc_pattern_dup(orig[u]);
138 LY_ARRAY_INCREMENT(dup);
139 }
140 return dup;
141}
142
Radek Krejcib56c7502019-02-13 14:19:54 +0100143/**
144 * @brief Duplicate compiled range structure.
145 *
146 * @param[in] ctx Libyang context for logging.
147 * @param[in] orig The range structure to be duplicated.
148 * @return New compiled range structure as a copy of @p orig.
149 * @return NULL in case of memory allocation error.
150 */
Radek Krejci19a96102018-11-15 13:38:09 +0100151struct lysc_range*
152lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
153{
154 struct lysc_range *dup;
155 LY_ERR ret;
156
Radek Krejcib56c7502019-02-13 14:19:54 +0100157 assert(orig);
158
Radek Krejci19a96102018-11-15 13:38:09 +0100159 dup = calloc(1, sizeof *dup);
160 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
161 if (orig->parts) {
162 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
163 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
164 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
165 }
166 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
167 DUP_STRING(ctx, orig->emsg, dup->emsg);
168 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
169
170 return dup;
171cleanup:
172 free(dup);
173 (void) ret; /* set but not used due to the return type */
174 return NULL;
175}
176
Radek Krejcib56c7502019-02-13 14:19:54 +0100177/**
178 * @brief Stack for processing if-feature expressions.
179 */
Radek Krejci19a96102018-11-15 13:38:09 +0100180struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100181 int size; /**< number of items in the stack */
182 int index; /**< first empty item */
183 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100184};
185
Radek Krejcib56c7502019-02-13 14:19:54 +0100186/**
187 * @brief Add @ref ifftokens into the stack.
188 * @param[in] stack The if-feature stack to use.
189 * @param[in] value One of the @ref ifftokens to store in the stack.
190 * @return LY_EMEM in case of memory allocation error
191 * @return LY_ESUCCESS if the value successfully stored.
192 */
Radek Krejci19a96102018-11-15 13:38:09 +0100193static LY_ERR
194iff_stack_push(struct iff_stack *stack, uint8_t value)
195{
196 if (stack->index == stack->size) {
197 stack->size += 4;
198 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
199 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
200 }
201 stack->stack[stack->index++] = value;
202 return LY_SUCCESS;
203}
204
Radek Krejcib56c7502019-02-13 14:19:54 +0100205/**
206 * @brief Get (and remove) the last item form the stack.
207 * @param[in] stack The if-feature stack to use.
208 * @return The value from the top of the stack.
209 */
Radek Krejci19a96102018-11-15 13:38:09 +0100210static uint8_t
211iff_stack_pop(struct iff_stack *stack)
212{
Radek Krejcib56c7502019-02-13 14:19:54 +0100213 assert(stack && stack->index);
214
Radek Krejci19a96102018-11-15 13:38:09 +0100215 stack->index--;
216 return stack->stack[stack->index];
217}
218
Radek Krejcib56c7502019-02-13 14:19:54 +0100219/**
220 * @brief Clean up the stack.
221 * @param[in] stack The if-feature stack to use.
222 */
Radek Krejci19a96102018-11-15 13:38:09 +0100223static void
224iff_stack_clean(struct iff_stack *stack)
225{
226 stack->size = 0;
227 free(stack->stack);
228}
229
Radek Krejcib56c7502019-02-13 14:19:54 +0100230/**
231 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
232 * (libyang format of the if-feature expression).
233 * @param[in,out] list The 2bits array to modify.
234 * @param[in] op The operand (@ref ifftokens) to store.
235 * @param[in] pos Position (0-based) where to store the given @p op.
236 */
Radek Krejci19a96102018-11-15 13:38:09 +0100237static void
238iff_setop(uint8_t *list, uint8_t op, int pos)
239{
240 uint8_t *item;
241 uint8_t mask = 3;
242
243 assert(pos >= 0);
244 assert(op <= 3); /* max 2 bits */
245
246 item = &list[pos / 4];
247 mask = mask << 2 * (pos % 4);
248 *item = (*item) & ~mask;
249 *item = (*item) | (op << 2 * (pos % 4));
250}
251
Radek Krejcib56c7502019-02-13 14:19:54 +0100252#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
253#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100254
Radek Krejci0af46292019-01-11 16:02:31 +0100255/**
256 * @brief Find a feature of the given name and referenced in the given module.
257 *
258 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
259 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
260 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
261 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
262 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
263 * assigned till that time will be still valid.
264 *
265 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
266 * @param[in] name Name of the feature including possible prefix.
267 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
268 * @return Pointer to the feature structure if found, NULL otherwise.
269 */
Radek Krejci19a96102018-11-15 13:38:09 +0100270static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100271lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100272{
273 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100274 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100275
276 for (i = 0; i < len; ++i) {
277 if (name[i] == ':') {
278 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100279 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100280 LY_CHECK_RET(!mod, NULL);
281
282 name = &name[i + 1];
283 len = len - i - 1;
284 }
285 }
286
287 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100288 if (mod->implemented) {
289 /* module is implemented so there is already the compiled schema */
290 flist = mod->compiled->features;
291 } else {
292 flist = mod->off_features;
293 }
294 LY_ARRAY_FOR(flist, i) {
295 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100296 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
297 return f;
298 }
299 }
300
301 return NULL;
302}
303
304static LY_ERR
305lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
306{
307 const char *name;
308 unsigned int u;
309 const struct lys_module *mod;
310 struct lysp_ext *edef = NULL;
311
312 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
313 ext->insubstmt = ext_p->insubstmt;
314 ext->insubstmt_index = ext_p->insubstmt_index;
315
316 /* get module where the extension definition should be placed */
317 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100318 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100319 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
320 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
321 LY_EVALID);
322 LY_CHECK_ERR_RET(!mod->parsed->extensions,
323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
324 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100325 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100326 LY_EVALID);
327 name = &ext_p->name[u + 1];
328 /* find the extension definition there */
329 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
330 if (!strcmp(name, mod->parsed->extensions[u].name)) {
331 edef = &mod->parsed->extensions[u];
332 break;
333 }
334 }
335 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
336 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
337 LY_EVALID);
338 /* TODO plugins */
339
340 return LY_SUCCESS;
341}
342
Radek Krejcib56c7502019-02-13 14:19:54 +0100343/**
344 * @brief Compile information from the if-feature statement
345 * @param[in] ctx Compile context.
346 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
347 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
348 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
349 * @return LY_ERR value.
350 */
Radek Krejci19a96102018-11-15 13:38:09 +0100351static LY_ERR
352lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
353{
354 const char *c = *value;
355 int r, rc = EXIT_FAILURE;
356 int i, j, last_not, checkversion = 0;
357 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
358 uint8_t op;
359 struct iff_stack stack = {0, 0, NULL};
360 struct lysc_feature *f;
361
362 assert(c);
363
364 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
365 for (i = j = last_not = 0; c[i]; i++) {
366 if (c[i] == '(') {
367 j++;
368 checkversion = 1;
369 continue;
370 } else if (c[i] == ')') {
371 j--;
372 continue;
373 } else if (isspace(c[i])) {
374 checkversion = 1;
375 continue;
376 }
377
378 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
379 if (c[i + r] == '\0') {
380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
381 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
382 return LY_EVALID;
383 } else if (!isspace(c[i + r])) {
384 /* feature name starting with the not/and/or */
385 last_not = 0;
386 f_size++;
387 } else if (c[i] == 'n') { /* not operation */
388 if (last_not) {
389 /* double not */
390 expr_size = expr_size - 2;
391 last_not = 0;
392 } else {
393 last_not = 1;
394 }
395 } else { /* and, or */
396 f_exp++;
397 /* not a not operation */
398 last_not = 0;
399 }
400 i += r;
401 } else {
402 f_size++;
403 last_not = 0;
404 }
405 expr_size++;
406
407 while (!isspace(c[i])) {
408 if (!c[i] || c[i] == ')') {
409 i--;
410 break;
411 }
412 i++;
413 }
414 }
415 if (j || f_exp != f_size) {
416 /* not matching count of ( and ) */
417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
418 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
419 return LY_EVALID;
420 }
421
422 if (checkversion || expr_size > 1) {
423 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100424 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
426 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
427 return LY_EVALID;
428 }
429 }
430
431 /* allocate the memory */
432 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
433 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
434 stack.stack = malloc(expr_size * sizeof *stack.stack);
435 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
436
437 stack.size = expr_size;
438 f_size--; expr_size--; /* used as indexes from now */
439
440 for (i--; i >= 0; i--) {
441 if (c[i] == ')') {
442 /* push it on stack */
443 iff_stack_push(&stack, LYS_IFF_RP);
444 continue;
445 } else if (c[i] == '(') {
446 /* pop from the stack into result all operators until ) */
447 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
448 iff_setop(iff->expr, op, expr_size--);
449 }
450 continue;
451 } else if (isspace(c[i])) {
452 continue;
453 }
454
455 /* end of operator or operand -> find beginning and get what is it */
456 j = i + 1;
457 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
458 i--;
459 }
460 i++; /* go back by one step */
461
462 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
463 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
464 /* double not */
465 iff_stack_pop(&stack);
466 } else {
467 /* not has the highest priority, so do not pop from the stack
468 * as in case of AND and OR */
469 iff_stack_push(&stack, LYS_IFF_NOT);
470 }
471 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
472 /* as for OR - pop from the stack all operators with the same or higher
473 * priority and store them to the result, then push the AND to the stack */
474 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
475 op = iff_stack_pop(&stack);
476 iff_setop(iff->expr, op, expr_size--);
477 }
478 iff_stack_push(&stack, LYS_IFF_AND);
479 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
480 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
481 op = iff_stack_pop(&stack);
482 iff_setop(iff->expr, op, expr_size--);
483 }
484 iff_stack_push(&stack, LYS_IFF_OR);
485 } else {
486 /* feature name, length is j - i */
487
488 /* add it to the expression */
489 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
490
491 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100492 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
493 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
494 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
495 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100496 iff->features[f_size] = f;
497 LY_ARRAY_INCREMENT(iff->features);
498 f_size--;
499 }
500 }
501 while (stack.index) {
502 op = iff_stack_pop(&stack);
503 iff_setop(iff->expr, op, expr_size--);
504 }
505
506 if (++expr_size || ++f_size) {
507 /* not all expected operators and operands found */
508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
509 "Invalid value \"%s\" of if-feature - processing error.", *value);
510 rc = LY_EINT;
511 } else {
512 rc = LY_SUCCESS;
513 }
514
515error:
516 /* cleanup */
517 iff_stack_clean(&stack);
518
519 return rc;
520}
521
Radek Krejcib56c7502019-02-13 14:19:54 +0100522/**
523 * @brief Compile information from the when statement
524 * @param[in] ctx Compile context.
525 * @param[in] when_p The parsed when statement structure.
526 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
527 * @param[out] when Pointer where to store pointer to the created compiled when structure.
528 * @return LY_ERR value.
529 */
Radek Krejci19a96102018-11-15 13:38:09 +0100530static LY_ERR
Radek Krejci00b874b2019-02-12 10:54:50 +0100531lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100532{
533 unsigned int u;
534 LY_ERR ret = LY_SUCCESS;
535
Radek Krejci00b874b2019-02-12 10:54:50 +0100536 *when = calloc(1, sizeof **when);
537 (*when)->refcount = 1;
538 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
539 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
540 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
541 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
542 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, options, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100543
544done:
545 return ret;
546}
547
Radek Krejcib56c7502019-02-13 14:19:54 +0100548/**
549 * @brief Compile information from the must statement
550 * @param[in] ctx Compile context.
551 * @param[in] must_p The parsed must statement structure.
552 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
553 * @param[in,out] must Prepared (empty) compiled must structure to fill.
554 * @return LY_ERR value.
555 */
Radek Krejci19a96102018-11-15 13:38:09 +0100556static LY_ERR
557lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
558{
559 unsigned int u;
560 LY_ERR ret = LY_SUCCESS;
561
562 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
563 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
564
565 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
566 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100567 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
568 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100569 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
570
571done:
572 return ret;
573}
574
Radek Krejcib56c7502019-02-13 14:19:54 +0100575/**
576 * @brief Compile information from the import statement
577 * @param[in] ctx Compile context.
578 * @param[in] imp_p The parsed import statement structure.
579 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
580 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
581 * @return LY_ERR value.
582 */
Radek Krejci19a96102018-11-15 13:38:09 +0100583static LY_ERR
584lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
585{
586 unsigned int u;
587 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100588 LY_ERR ret = LY_SUCCESS;
589
590 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
591 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
592 imp->module = imp_p->module;
593
Radek Krejci7f2a5362018-11-28 13:05:37 +0100594 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
595 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100596 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100597 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 /* try to use filepath if present */
599 if (imp->module->filepath) {
600 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
601 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100602 if (mod != imp->module) {
603 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100605 mod = NULL;
606 }
607 }
608 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100609 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100610 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100611 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100612 return LY_ENOTFOUND;
613 }
614 }
Radek Krejci19a96102018-11-15 13:38:09 +0100615 }
616
617done:
618 return ret;
619}
620
Radek Krejcib56c7502019-02-13 14:19:54 +0100621/**
622 * @brief Compile information from the identity statement
623 *
624 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
625 *
626 * @param[in] ctx Compile context.
627 * @param[in] ident_p The parsed identity statement structure.
628 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
629 * @param[in] idents List of so far compiled identities to check the name uniqueness.
630 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
631 * @return LY_ERR value.
632 */
Radek Krejci19a96102018-11-15 13:38:09 +0100633static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100634lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100635{
636 unsigned int u;
637 LY_ERR ret = LY_SUCCESS;
638
Radek Krejcid05cbd92018-12-05 14:26:40 +0100639 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100640 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200641 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
642 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100643 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
644 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
645 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
646 ident->flags = ident_p->flags;
647
648done:
649 return ret;
650}
651
Radek Krejcib56c7502019-02-13 14:19:54 +0100652/**
653 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
654 *
655 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
656 *
657 * @param[in] ctx Compile context for logging.
658 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
659 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
660 * @return LY_SUCCESS if everything is ok.
661 * @return LY_EVALID if the identity is derived from itself.
662 */
Radek Krejci38222632019-02-12 16:55:05 +0100663static LY_ERR
664lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
665{
666 LY_ERR ret = LY_EVALID;
667 unsigned int u, v;
668 struct ly_set recursion = {0};
669 struct lysc_ident *drv;
670
671 if (!derived) {
672 return LY_SUCCESS;
673 }
674
675 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
676 if (ident == derived[u]) {
677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
678 "Identity \"%s\" is indirectly derived from itself.", ident->name);
679 goto cleanup;
680 }
681 ly_set_add(&recursion, derived[u], 0);
682 }
683
684 for (v = 0; v < recursion.count; ++v) {
685 drv = recursion.objs[v];
686 if (!drv->derived) {
687 continue;
688 }
689 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
690 if (ident == drv->derived[u]) {
691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
692 "Identity \"%s\" is indirectly derived from itself.", ident->name);
693 goto cleanup;
694 }
695 ly_set_add(&recursion, drv->derived[u], 0);
696 }
697 }
698 ret = LY_SUCCESS;
699
700cleanup:
701 ly_set_erase(&recursion, NULL);
702 return ret;
703}
704
Radek Krejcia3045382018-11-22 14:30:31 +0100705/**
706 * @brief Find and process the referenced base identities from another identity or identityref
707 *
708 * For bases in identity se backlinks to them from the base identities. For identityref, store
709 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
710 * to distinguish these two use cases.
711 *
712 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
713 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
714 * @param[in] ident Referencing identity to work with.
715 * @param[in] bases Array of bases of identityref to fill in.
716 * @return LY_ERR value.
717 */
Radek Krejci19a96102018-11-15 13:38:09 +0100718static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100719lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +0100720{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100721 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100722 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100723 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100724 struct lysc_ident **idref;
725
726 assert(ident || bases);
727
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100728 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
730 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
731 return LY_EVALID;
732 }
733
734 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
735 s = strchr(bases_p[u], ':');
736 if (s) {
737 /* prefixed identity */
738 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100739 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100740 } else {
741 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100742 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100743 }
744 if (!mod) {
745 if (ident) {
746 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
747 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
748 } else {
749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
750 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
751 }
752 return LY_EVALID;
753 }
754 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100755 if (mod->compiled && mod->compiled->identities) {
756 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
757 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100758 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100759 if (ident == &mod->compiled->identities[v]) {
760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
761 "Identity \"%s\" is derived from itself.", ident->name);
762 return LY_EVALID;
763 }
764 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100765 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100766 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100767 *idref = ident;
768 } else {
769 /* we have match! store the found identity */
770 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100771 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100772 }
773 break;
774 }
775 }
776 }
777 if (!idref || !(*idref)) {
778 if (ident) {
779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
780 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
781 } else {
782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
783 "Unable to find base (%s) of identityref.", bases_p[u]);
784 }
785 return LY_EVALID;
786 }
787 }
788 return LY_SUCCESS;
789}
790
Radek Krejcia3045382018-11-22 14:30:31 +0100791/**
792 * @brief For the given array of identities, set the backlinks from all their base identities.
793 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
794 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
795 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
796 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
797 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100798static LY_ERR
799lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
800{
801 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100802
803 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
804 if (!idents_p[i].bases) {
805 continue;
806 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100807 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100808 }
809 return LY_SUCCESS;
810}
811
Radek Krejci0af46292019-01-11 16:02:31 +0100812LY_ERR
813lys_feature_precompile(struct ly_ctx *ctx, struct lysp_feature *features_p, struct lysc_feature **features)
814{
815 unsigned int offset = 0, u;
816 struct lysc_ctx context = {0};
817
818 assert(ctx);
819 context.ctx = ctx;
820
821 if (!features_p) {
822 return LY_SUCCESS;
823 }
824 if (*features) {
825 offset = LY_ARRAY_SIZE(*features);
826 }
827
828 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
829 LY_ARRAY_FOR(features_p, u) {
830 LY_ARRAY_INCREMENT(*features);
831 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
832 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
833 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
834 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
835 (*features)[offset + u].flags = features_p[u].flags;
836 }
837
838 return LY_SUCCESS;
839}
840
Radek Krejcia3045382018-11-22 14:30:31 +0100841/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100842 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100843 *
844 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
845 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100846 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100847 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
848 * being currently processed).
849 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +0100850 * @return LY_SUCCESS if everything is ok.
851 * @return LY_EVALID if the feature references indirectly itself.
852 */
853static LY_ERR
854lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
855{
856 LY_ERR ret = LY_EVALID;
857 unsigned int u, v;
858 struct ly_set recursion = {0};
859 struct lysc_feature *drv;
860
861 if (!depfeatures) {
862 return LY_SUCCESS;
863 }
864
865 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
866 if (feature == depfeatures[u]) {
867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
868 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
869 goto cleanup;
870 }
871 ly_set_add(&recursion, depfeatures[u], 0);
872 }
873
874 for (v = 0; v < recursion.count; ++v) {
875 drv = recursion.objs[v];
876 if (!drv->depfeatures) {
877 continue;
878 }
879 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
880 if (feature == drv->depfeatures[u]) {
881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
882 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
883 goto cleanup;
884 }
885 ly_set_add(&recursion, drv->depfeatures[u], 0);
886 }
887 }
888 ret = LY_SUCCESS;
889
890cleanup:
891 ly_set_erase(&recursion, NULL);
892 return ret;
893}
894
895/**
Radek Krejci0af46292019-01-11 16:02:31 +0100896 * @brief Create pre-compiled features array.
897 *
898 * See lys_feature_precompile() for more details.
899 *
Radek Krejcia3045382018-11-22 14:30:31 +0100900 * @param[in] ctx Compile context.
901 * @param[in] feature_p Parsed feature definition to compile.
902 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci0af46292019-01-11 16:02:31 +0100903 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100904 * @return LY_ERR value.
905 */
Radek Krejci19a96102018-11-15 13:38:09 +0100906static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100907lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +0100908{
Radek Krejci0af46292019-01-11 16:02:31 +0100909 unsigned int u, v, x;
910 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100911 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100912
Radek Krejci0af46292019-01-11 16:02:31 +0100913 /* find the preprecompiled feature */
914 LY_ARRAY_FOR(features, x) {
915 if (strcmp(features[x].name, feature_p->name)) {
916 continue;
917 }
918 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100919
Radek Krejci0af46292019-01-11 16:02:31 +0100920 /* finish compilation started in lys_feature_precompile() */
921 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
922 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
923 if (feature->iffeatures) {
924 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
925 if (feature->iffeatures[u].features) {
926 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100927 /* check for circular dependency - direct reference first,... */
928 if (feature == feature->iffeatures[u].features[v]) {
929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
930 "Feature \"%s\" is referenced from itself.", feature->name);
931 return LY_EVALID;
932 }
933 /* ... and indirect circular reference */
934 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
935
Radek Krejci0af46292019-01-11 16:02:31 +0100936 /* add itself into the dependants list */
937 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
938 *df = feature;
939 }
Radek Krejci19a96102018-11-15 13:38:09 +0100940 }
Radek Krejci19a96102018-11-15 13:38:09 +0100941 }
942 }
Radek Krejci0af46292019-01-11 16:02:31 +0100943 done:
944 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100945 }
Radek Krejci0af46292019-01-11 16:02:31 +0100946
947 LOGINT(ctx->ctx);
948 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100949}
950
Radek Krejcib56c7502019-02-13 14:19:54 +0100951/**
952 * @brief Revert compiled list of features back to the precompiled state.
953 *
954 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
955 * The features are supposed to be stored again as off_features in ::lys_module structure.
956 *
957 * @param[in] ctx Compilation context.
958 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
959 * and supposed to hold the off_features list.
960 */
Radek Krejci95710c92019-02-11 15:49:55 +0100961static void
962lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
963{
964 unsigned int u, v;
965
966 /* keep the off_features list until the complete lys_module is freed */
967 mod->off_features = mod->compiled->features;
968 mod->compiled->features = NULL;
969
970 /* in the off_features list, remove all the parts (from finished compiling process)
971 * which may points into the data being freed here */
972 LY_ARRAY_FOR(mod->off_features, u) {
973 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
974 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
975 }
976 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
977 mod->off_features[u].iffeatures = NULL;
978
979 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
980 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
981 }
982 LY_ARRAY_FREE(mod->off_features[u].exts);
983 mod->off_features[u].exts = NULL;
984 }
985}
986
Radek Krejcia3045382018-11-22 14:30:31 +0100987/**
988 * @brief Validate and normalize numeric value from a range definition.
989 * @param[in] ctx Compile context.
990 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
991 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
992 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
993 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
994 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
995 * @param[in] value String value of the range boundary.
996 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
997 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
998 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
999 */
Radek Krejci19a96102018-11-15 13:38:09 +01001000static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001001range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001002{
Radek Krejci6cba4292018-11-15 17:33:29 +01001003 size_t fraction = 0, size;
1004
Radek Krejci19a96102018-11-15 13:38:09 +01001005 *len = 0;
1006
1007 assert(value);
1008 /* parse value */
1009 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1010 return LY_EVALID;
1011 }
1012
1013 if ((value[*len] == '-') || (value[*len] == '+')) {
1014 ++(*len);
1015 }
1016
1017 while (isdigit(value[*len])) {
1018 ++(*len);
1019 }
1020
1021 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001022 if (basetype == LY_TYPE_DEC64) {
1023 goto decimal;
1024 } else {
1025 *valcopy = strndup(value, *len);
1026 return LY_SUCCESS;
1027 }
Radek Krejci19a96102018-11-15 13:38:09 +01001028 }
1029 fraction = *len;
1030
1031 ++(*len);
1032 while (isdigit(value[*len])) {
1033 ++(*len);
1034 }
1035
Radek Krejci6cba4292018-11-15 17:33:29 +01001036 if (basetype == LY_TYPE_DEC64) {
1037decimal:
1038 assert(frdigits);
1039 if (*len - 1 - fraction > frdigits) {
1040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1041 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1042 *len, value, frdigits);
1043 return LY_EINVAL;
1044 }
1045 if (fraction) {
1046 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1047 } else {
1048 size = (*len) + frdigits + 1;
1049 }
1050 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001051 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1052
Radek Krejci6cba4292018-11-15 17:33:29 +01001053 (*valcopy)[size - 1] = '\0';
1054 if (fraction) {
1055 memcpy(&(*valcopy)[0], &value[0], fraction);
1056 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1057 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1058 } else {
1059 memcpy(&(*valcopy)[0], &value[0], *len);
1060 memset(&(*valcopy)[*len], '0', frdigits);
1061 }
Radek Krejci19a96102018-11-15 13:38:09 +01001062 }
1063 return LY_SUCCESS;
1064}
1065
Radek Krejcia3045382018-11-22 14:30:31 +01001066/**
1067 * @brief Check that values in range are in ascendant order.
1068 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001069 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1070 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001071 * @param[in] value Current value to check.
1072 * @param[in] prev_value The last seen value.
1073 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1074 */
Radek Krejci19a96102018-11-15 13:38:09 +01001075static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001076range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001077{
1078 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001079 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001080 return LY_EEXIST;
1081 }
1082 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001083 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001084 return LY_EEXIST;
1085 }
1086 }
1087 return LY_SUCCESS;
1088}
1089
Radek Krejcia3045382018-11-22 14:30:31 +01001090/**
1091 * @brief Set min/max value of the range part.
1092 * @param[in] ctx Compile context.
1093 * @param[in] part Range part structure to fill.
1094 * @param[in] max Flag to distinguish if storing min or max value.
1095 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1096 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1097 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1098 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1099 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001100 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001101 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1102 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1103 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1104 * frdigits value), LY_EMEM.
1105 */
Radek Krejci19a96102018-11-15 13:38:09 +01001106static LY_ERR
1107range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001108 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001109{
1110 LY_ERR ret = LY_SUCCESS;
1111 char *valcopy = NULL;
1112 size_t len;
1113
1114 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001115 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001116 LY_CHECK_GOTO(ret, finalize);
1117 }
1118 if (!valcopy && base_range) {
1119 if (max) {
1120 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1121 } else {
1122 part->min_64 = base_range->parts[0].min_64;
1123 }
1124 if (!first) {
1125 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1126 }
1127 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001128 }
1129
1130 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001131 case LY_TYPE_INT8: /* range */
1132 if (valcopy) {
1133 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1134 } else if (max) {
1135 part->max_64 = INT64_C(127);
1136 } else {
1137 part->min_64 = INT64_C(-128);
1138 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001139 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001140 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001141 }
1142 break;
1143 case LY_TYPE_INT16: /* range */
1144 if (valcopy) {
1145 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1146 } else if (max) {
1147 part->max_64 = INT64_C(32767);
1148 } else {
1149 part->min_64 = INT64_C(-32768);
1150 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001151 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001152 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001153 }
1154 break;
1155 case LY_TYPE_INT32: /* range */
1156 if (valcopy) {
1157 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1158 } else if (max) {
1159 part->max_64 = INT64_C(2147483647);
1160 } else {
1161 part->min_64 = INT64_C(-2147483648);
1162 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001163 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001164 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001165 }
1166 break;
1167 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001168 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001169 if (valcopy) {
1170 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1171 max ? &part->max_64 : &part->min_64);
1172 } else if (max) {
1173 part->max_64 = INT64_C(9223372036854775807);
1174 } else {
1175 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1176 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001177 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001178 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001179 }
1180 break;
1181 case LY_TYPE_UINT8: /* range */
1182 if (valcopy) {
1183 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1184 } else if (max) {
1185 part->max_u64 = UINT64_C(255);
1186 } else {
1187 part->min_u64 = UINT64_C(0);
1188 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001189 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001190 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001191 }
1192 break;
1193 case LY_TYPE_UINT16: /* range */
1194 if (valcopy) {
1195 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1196 } else if (max) {
1197 part->max_u64 = UINT64_C(65535);
1198 } else {
1199 part->min_u64 = UINT64_C(0);
1200 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001201 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001202 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001203 }
1204 break;
1205 case LY_TYPE_UINT32: /* range */
1206 if (valcopy) {
1207 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1208 } else if (max) {
1209 part->max_u64 = UINT64_C(4294967295);
1210 } else {
1211 part->min_u64 = UINT64_C(0);
1212 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001213 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001214 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001215 }
1216 break;
1217 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001218 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001219 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001220 if (valcopy) {
1221 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1222 } else if (max) {
1223 part->max_u64 = UINT64_C(18446744073709551615);
1224 } else {
1225 part->min_u64 = UINT64_C(0);
1226 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001227 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001228 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001229 }
1230 break;
1231 default:
1232 LOGINT(ctx->ctx);
1233 ret = LY_EINT;
1234 }
1235
Radek Krejci5969f272018-11-23 10:03:58 +01001236finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001237 if (ret == LY_EDENIED) {
1238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1239 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1240 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1241 } else if (ret == LY_EVALID) {
1242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1243 "Invalid %s restriction - invalid value \"%s\".",
1244 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1245 } else if (ret == LY_EEXIST) {
1246 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1247 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001248 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001249 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001250 } else if (!ret && value) {
1251 *value = *value + len;
1252 }
1253 free(valcopy);
1254 return ret;
1255}
1256
Radek Krejcia3045382018-11-22 14:30:31 +01001257/**
1258 * @brief Compile the parsed range restriction.
1259 * @param[in] ctx Compile context.
1260 * @param[in] range_p Parsed range structure to compile.
1261 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1262 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1263 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1264 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1265 * range restriction must be more restrictive than the base_range.
1266 * @param[in,out] range Pointer to the created current range structure.
1267 * @return LY_ERR value.
1268 */
Radek Krejci19a96102018-11-15 13:38:09 +01001269static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001270lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001271 struct lysc_range *base_range, struct lysc_range **range)
1272{
1273 LY_ERR ret = LY_EVALID;
1274 const char *expr;
1275 struct lysc_range_part *parts = NULL, *part;
1276 int range_expected = 0, uns;
1277 unsigned int parts_done = 0, u, v;
1278
1279 assert(range);
1280 assert(range_p);
1281
1282 expr = range_p->arg;
1283 while(1) {
1284 if (isspace(*expr)) {
1285 ++expr;
1286 } else if (*expr == '\0') {
1287 if (range_expected) {
1288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1289 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1290 length_restr ? "length" : "range", range_p->arg);
1291 goto cleanup;
1292 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1294 "Invalid %s restriction - unexpected end of the expression (%s).",
1295 length_restr ? "length" : "range", range_p->arg);
1296 goto cleanup;
1297 }
1298 parts_done++;
1299 break;
1300 } else if (!strncmp(expr, "min", 3)) {
1301 if (parts) {
1302 /* min cannot be used elsewhere than in the first part */
1303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1304 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1305 expr - range_p->arg, range_p->arg);
1306 goto cleanup;
1307 }
1308 expr += 3;
1309
1310 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001311 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001312 part->max_64 = part->min_64;
1313 } else if (*expr == '|') {
1314 if (!parts || range_expected) {
1315 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1316 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1317 goto cleanup;
1318 }
1319 expr++;
1320 parts_done++;
1321 /* process next part of the expression */
1322 } else if (!strncmp(expr, "..", 2)) {
1323 expr += 2;
1324 while (isspace(*expr)) {
1325 expr++;
1326 }
1327 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1328 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1329 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1330 goto cleanup;
1331 }
1332 /* continue expecting the upper boundary */
1333 range_expected = 1;
1334 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1335 /* number */
1336 if (range_expected) {
1337 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001338 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001339 range_expected = 0;
1340 } else {
1341 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1342 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001343 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001344 part->max_64 = part->min_64;
1345 }
1346
1347 /* continue with possible another expression part */
1348 } else if (!strncmp(expr, "max", 3)) {
1349 expr += 3;
1350 while (isspace(*expr)) {
1351 expr++;
1352 }
1353 if (*expr != '\0') {
1354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1355 length_restr ? "length" : "range", expr);
1356 goto cleanup;
1357 }
1358 if (range_expected) {
1359 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001360 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001361 range_expected = 0;
1362 } else {
1363 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1364 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001365 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001366 part->min_64 = part->max_64;
1367 }
1368 } else {
1369 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1370 length_restr ? "length" : "range", expr);
1371 goto cleanup;
1372 }
1373 }
1374
1375 /* check with the previous range/length restriction */
1376 if (base_range) {
1377 switch (basetype) {
1378 case LY_TYPE_BINARY:
1379 case LY_TYPE_UINT8:
1380 case LY_TYPE_UINT16:
1381 case LY_TYPE_UINT32:
1382 case LY_TYPE_UINT64:
1383 case LY_TYPE_STRING:
1384 uns = 1;
1385 break;
1386 case LY_TYPE_DEC64:
1387 case LY_TYPE_INT8:
1388 case LY_TYPE_INT16:
1389 case LY_TYPE_INT32:
1390 case LY_TYPE_INT64:
1391 uns = 0;
1392 break;
1393 default:
1394 LOGINT(ctx->ctx);
1395 ret = LY_EINT;
1396 goto cleanup;
1397 }
1398 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1399 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1400 goto baseerror;
1401 }
1402 /* current lower bound is not lower than the base */
1403 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1404 /* base has single value */
1405 if (base_range->parts[v].min_64 == parts[u].min_64) {
1406 /* both lower bounds are the same */
1407 if (parts[u].min_64 != parts[u].max_64) {
1408 /* current continues with a range */
1409 goto baseerror;
1410 } else {
1411 /* equal single values, move both forward */
1412 ++v;
1413 continue;
1414 }
1415 } else {
1416 /* base is single value lower than current range, so the
1417 * value from base range is removed in the current,
1418 * move only base and repeat checking */
1419 ++v;
1420 --u;
1421 continue;
1422 }
1423 } else {
1424 /* base is the range */
1425 if (parts[u].min_64 == parts[u].max_64) {
1426 /* current is a single value */
1427 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1428 /* current is behind the base range, so base range is omitted,
1429 * move the base and keep the current for further check */
1430 ++v;
1431 --u;
1432 } /* else it is within the base range, so move the current, but keep the base */
1433 continue;
1434 } else {
1435 /* both are ranges - check the higher bound, the lower was already checked */
1436 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1437 /* higher bound is higher than the current higher bound */
1438 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1439 /* but the current lower bound is also higher, so the base range is omitted,
1440 * continue with the same current, but move the base */
1441 --u;
1442 ++v;
1443 continue;
1444 }
1445 /* current range starts within the base range but end behind it */
1446 goto baseerror;
1447 } else {
1448 /* current range is smaller than the base,
1449 * move current, but stay with the base */
1450 continue;
1451 }
1452 }
1453 }
1454 }
1455 if (u != parts_done) {
1456baseerror:
1457 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1458 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1459 length_restr ? "length" : "range", range_p->arg);
1460 goto cleanup;
1461 }
1462 }
1463
1464 if (!(*range)) {
1465 *range = calloc(1, sizeof **range);
1466 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1467 }
1468
Radek Krejcic8b31002019-01-08 10:24:45 +01001469 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001470 if (range_p->eapptag) {
1471 lydict_remove(ctx->ctx, (*range)->eapptag);
1472 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1473 }
1474 if (range_p->emsg) {
1475 lydict_remove(ctx->ctx, (*range)->emsg);
1476 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1477 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001478 if (range_p->dsc) {
1479 lydict_remove(ctx->ctx, (*range)->dsc);
1480 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1481 }
1482 if (range_p->ref) {
1483 lydict_remove(ctx->ctx, (*range)->ref);
1484 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1485 }
Radek Krejci19a96102018-11-15 13:38:09 +01001486 /* extensions are taken only from the last range by the caller */
1487
1488 (*range)->parts = parts;
1489 parts = NULL;
1490 ret = LY_SUCCESS;
1491cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001492 LY_ARRAY_FREE(parts);
1493
1494 return ret;
1495}
1496
1497/**
1498 * @brief Checks pattern syntax.
1499 *
Radek Krejcia3045382018-11-22 14:30:31 +01001500 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001501 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001502 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1503 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001504 */
1505static LY_ERR
1506lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1507{
1508 int idx, idx2, start, end, err_offset, count;
1509 char *perl_regex, *ptr;
1510 const char *err_msg, *orig_ptr;
1511 pcre *precomp;
1512#define URANGE_LEN 19
1513 char *ublock2urange[][2] = {
1514 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1515 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1516 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1517 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1518 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1519 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1520 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1521 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1522 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1523 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1524 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1525 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1526 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1527 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1528 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1529 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1530 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1531 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1532 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1533 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1534 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1535 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1536 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1537 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1538 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1539 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1540 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1541 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1542 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1543 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1544 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1545 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1546 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1547 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1548 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1549 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1550 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1551 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1552 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1553 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1554 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1555 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1556 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1557 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1558 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1559 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1560 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1561 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1562 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1563 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1564 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1565 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1566 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1567 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1568 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1569 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1570 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1571 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1572 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1573 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1574 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1575 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1576 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1577 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1578 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1579 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1580 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1581 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1582 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1583 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1584 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1585 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1586 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1587 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1588 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1589 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1590 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1591 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1592 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1593 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1594 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1595 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1596 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1597 {NULL, NULL}
1598 };
1599
1600 /* adjust the expression to a Perl equivalent
1601 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1602
1603 /* we need to replace all "$" with "\$", count them now */
1604 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1605
1606 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1607 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1608 perl_regex[0] = '\0';
1609
1610 ptr = perl_regex;
1611
1612 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1613 /* we will add line-end anchoring */
1614 ptr[0] = '(';
1615 ++ptr;
1616 }
1617
1618 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1619 if (orig_ptr[0] == '$') {
1620 ptr += sprintf(ptr, "\\$");
1621 } else if (orig_ptr[0] == '^') {
1622 ptr += sprintf(ptr, "\\^");
1623 } else {
1624 ptr[0] = orig_ptr[0];
1625 ++ptr;
1626 }
1627 }
1628
1629 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1630 ptr += sprintf(ptr, ")$");
1631 } else {
1632 ptr[0] = '\0';
1633 ++ptr;
1634 }
1635
1636 /* substitute Unicode Character Blocks with exact Character Ranges */
1637 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1638 start = ptr - perl_regex;
1639
1640 ptr = strchr(ptr, '}');
1641 if (!ptr) {
1642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1643 pattern, perl_regex + start + 2, "unterminated character property");
1644 free(perl_regex);
1645 return LY_EVALID;
1646 }
1647 end = (ptr - perl_regex) + 1;
1648
1649 /* need more space */
1650 if (end - start < URANGE_LEN) {
1651 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1652 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1653 }
1654
1655 /* find our range */
1656 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1657 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1658 break;
1659 }
1660 }
1661 if (!ublock2urange[idx][0]) {
1662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1663 pattern, perl_regex + start + 5, "unknown block name");
1664 free(perl_regex);
1665 return LY_EVALID;
1666 }
1667
1668 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1669 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1670 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1671 ++count;
1672 }
1673 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1674 --count;
1675 }
1676 }
1677 if (count) {
1678 /* skip brackets */
1679 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1680 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1681 } else {
1682 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1683 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1684 }
1685 }
1686
1687 /* must return 0, already checked during parsing */
1688 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1689 &err_msg, &err_offset, NULL);
1690 if (!precomp) {
1691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1692 free(perl_regex);
1693 return LY_EVALID;
1694 }
1695 free(perl_regex);
1696
1697 if (pcre_precomp) {
1698 *pcre_precomp = precomp;
1699 } else {
1700 free(precomp);
1701 }
1702
1703 return LY_SUCCESS;
1704
1705#undef URANGE_LEN
1706}
1707
Radek Krejcia3045382018-11-22 14:30:31 +01001708/**
1709 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1710 * @param[in] ctx Compile context.
1711 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1712 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1713 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1714 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1715 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1716 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1717 */
Radek Krejci19a96102018-11-15 13:38:09 +01001718static LY_ERR
1719lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1720 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1721{
1722 struct lysc_pattern **pattern;
1723 unsigned int u, v;
1724 const char *err_msg;
1725 LY_ERR ret = LY_SUCCESS;
1726
1727 /* first, copy the patterns from the base type */
1728 if (base_patterns) {
1729 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1730 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1731 }
1732
1733 LY_ARRAY_FOR(patterns_p, u) {
1734 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1735 *pattern = calloc(1, sizeof **pattern);
1736 ++(*pattern)->refcount;
1737
1738 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1739 LY_CHECK_RET(ret);
1740 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1741 if (err_msg) {
1742 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1743 }
1744
1745 if (patterns_p[u].arg[0] == 0x15) {
1746 (*pattern)->inverted = 1;
1747 }
1748 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1749 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001750 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1751 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001752 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1753 options, v, lys_compile_ext, ret, done);
1754 }
1755done:
1756 return ret;
1757}
1758
Radek Krejcia3045382018-11-22 14:30:31 +01001759/**
1760 * @brief map of the possible restrictions combination for the specific built-in type.
1761 */
Radek Krejci19a96102018-11-15 13:38:09 +01001762static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1763 0 /* LY_TYPE_UNKNOWN */,
1764 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001765 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1766 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1767 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1768 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1769 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001770 LYS_SET_BIT /* LY_TYPE_BITS */,
1771 0 /* LY_TYPE_BOOL */,
1772 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1773 0 /* LY_TYPE_EMPTY */,
1774 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1775 LYS_SET_BASE /* LY_TYPE_IDENT */,
1776 LYS_SET_REQINST /* LY_TYPE_INST */,
1777 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001778 LYS_SET_TYPE /* LY_TYPE_UNION */,
1779 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001780 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001781 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001782 LYS_SET_RANGE /* LY_TYPE_INT64 */
1783};
1784
1785/**
1786 * @brief stringification of the YANG built-in data types
1787 */
1788const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1789 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1790 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001791};
1792
Radek Krejcia3045382018-11-22 14:30:31 +01001793/**
1794 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1795 * @param[in] ctx Compile context.
1796 * @param[in] enums_p Array of the parsed enum structures to compile.
1797 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1798 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1799 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1800 * @param[out] enums Newly created array of the compiled enums information for the current type.
1801 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1802 */
Radek Krejci19a96102018-11-15 13:38:09 +01001803static LY_ERR
1804lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1805 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1806{
1807 LY_ERR ret = LY_SUCCESS;
1808 unsigned int u, v, match;
1809 int32_t value = 0;
1810 uint32_t position = 0;
1811 struct lysc_type_enum_item *e, storage;
1812
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001813 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1815 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1816 return LY_EVALID;
1817 }
1818
1819 LY_ARRAY_FOR(enums_p, u) {
1820 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1821 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001822 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1823 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001824 if (base_enums) {
1825 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1826 LY_ARRAY_FOR(base_enums, v) {
1827 if (!strcmp(e->name, base_enums[v].name)) {
1828 break;
1829 }
1830 }
1831 if (v == LY_ARRAY_SIZE(base_enums)) {
1832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1833 "Invalid %s - derived type adds new item \"%s\".",
1834 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1835 return LY_EVALID;
1836 }
1837 match = v;
1838 }
1839
1840 if (basetype == LY_TYPE_ENUM) {
1841 if (enums_p[u].flags & LYS_SET_VALUE) {
1842 e->value = (int32_t)enums_p[u].value;
1843 if (!u || e->value >= value) {
1844 value = e->value + 1;
1845 }
1846 /* check collision with other values */
1847 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1848 if (e->value == (*enums)[v].value) {
1849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1850 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1851 e->value, e->name, (*enums)[v].name);
1852 return LY_EVALID;
1853 }
1854 }
1855 } else if (base_enums) {
1856 /* inherit the assigned value */
1857 e->value = base_enums[match].value;
1858 if (!u || e->value >= value) {
1859 value = e->value + 1;
1860 }
1861 } else {
1862 /* assign value automatically */
1863 if (u && value == INT32_MIN) {
1864 /* counter overflow */
1865 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1866 "Invalid enumeration - it is not possible to auto-assign enum value for "
1867 "\"%s\" since the highest value is already 2147483647.", e->name);
1868 return LY_EVALID;
1869 }
1870 e->value = value++;
1871 }
1872 } else { /* LY_TYPE_BITS */
1873 if (enums_p[u].flags & LYS_SET_VALUE) {
1874 e->value = (int32_t)enums_p[u].value;
1875 if (!u || (uint32_t)e->value >= position) {
1876 position = (uint32_t)e->value + 1;
1877 }
1878 /* check collision with other values */
1879 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1880 if (e->value == (*enums)[v].value) {
1881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1882 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1883 (uint32_t)e->value, e->name, (*enums)[v].name);
1884 return LY_EVALID;
1885 }
1886 }
1887 } else if (base_enums) {
1888 /* inherit the assigned value */
1889 e->value = base_enums[match].value;
1890 if (!u || (uint32_t)e->value >= position) {
1891 position = (uint32_t)e->value + 1;
1892 }
1893 } else {
1894 /* assign value automatically */
1895 if (u && position == 0) {
1896 /* counter overflow */
1897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1898 "Invalid bits - it is not possible to auto-assign bit position for "
1899 "\"%s\" since the highest value is already 4294967295.", e->name);
1900 return LY_EVALID;
1901 }
1902 e->value = position++;
1903 }
1904 }
1905
1906 if (base_enums) {
1907 /* the assigned values must not change from the derived type */
1908 if (e->value != base_enums[match].value) {
1909 if (basetype == LY_TYPE_ENUM) {
1910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1911 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1912 e->name, base_enums[match].value, e->value);
1913 } else {
1914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1915 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1916 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1917 }
1918 return LY_EVALID;
1919 }
1920 }
1921
1922 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1923 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1924
1925 if (basetype == LY_TYPE_BITS) {
1926 /* keep bits ordered by position */
1927 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1928 if (v != u) {
1929 memcpy(&storage, e, sizeof *e);
1930 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1931 memcpy(&(*enums)[v], &storage, sizeof storage);
1932 }
1933 }
1934 }
1935
1936done:
1937 return ret;
1938}
1939
Radek Krejcia3045382018-11-22 14:30:31 +01001940#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1941 for ((NODE) = (NODE)->parent; \
1942 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1943 (NODE) = (NODE)->parent); \
1944 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1945 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1946 TERM; \
1947 }
1948
1949/**
1950 * @brief Validate the predicate(s) from the leafref path.
1951 * @param[in] ctx Compile context
1952 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1953 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001954 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001955 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001956 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001957 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1958 */
1959static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001960lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001961 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001962{
1963 LY_ERR ret = LY_EVALID;
1964 const struct lys_module *mod;
1965 const struct lysc_node *src_node, *dst_node;
1966 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1967 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001968 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001969 const char *start, *end, *pke_end;
1970 struct ly_set keys = {0};
1971 int i;
1972
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001973 assert(path_context);
1974
Radek Krejcia3045382018-11-22 14:30:31 +01001975 while (**predicate == '[') {
1976 start = (*predicate)++;
1977
1978 while (isspace(**predicate)) {
1979 ++(*predicate);
1980 }
1981 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1982 while (isspace(**predicate)) {
1983 ++(*predicate);
1984 }
1985 if (**predicate != '=') {
1986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001987 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1988 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001989 goto cleanup;
1990 }
1991 ++(*predicate);
1992 while (isspace(**predicate)) {
1993 ++(*predicate);
1994 }
1995
1996 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1998 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1999 goto cleanup;
2000 }
2001 --pke_end;
2002 while (isspace(*pke_end)) {
2003 --pke_end;
2004 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002005 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002006 /* localize path-key-expr */
2007 pke_start = path_key_expr = *predicate;
2008 /* move after the current predicate */
2009 *predicate = end + 1;
2010
2011 /* source (must be leaf or leaf-list) */
2012 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002013 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002014 if (!mod) {
2015 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2016 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002017 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002018 goto cleanup;
2019 }
Radek Krejcia3045382018-11-22 14:30:31 +01002020 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002021 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002022 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002023 src_node = NULL;
2024 if (context_node->keys) {
2025 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2026 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2027 src_node = (const struct lysc_node*)context_node->keys[u];
2028 break;
2029 }
2030 }
2031 }
Radek Krejcia3045382018-11-22 14:30:31 +01002032 if (!src_node) {
2033 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002034 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002035 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002036 goto cleanup;
2037 }
Radek Krejcia3045382018-11-22 14:30:31 +01002038
2039 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002040 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002041 i = ly_set_add(&keys, (void*)src_node, 0);
2042 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002043 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002045 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002046 *predicate - start, start, src_node->name);
2047 goto cleanup;
2048 }
2049
2050 /* destination */
2051 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002052 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002053
2054 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2055 if (strncmp(path_key_expr, "current()", 9)) {
2056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2057 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2058 *predicate - start, start);
2059 goto cleanup;
2060 }
2061 path_key_expr += 9;
2062 while (isspace(*path_key_expr)) {
2063 ++path_key_expr;
2064 }
2065
2066 if (*path_key_expr != '/') {
2067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2068 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2069 *predicate - start, start);
2070 goto cleanup;
2071 }
2072 ++path_key_expr;
2073 while (isspace(*path_key_expr)) {
2074 ++path_key_expr;
2075 }
2076
2077 /* rel-path-keyexpr:
2078 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2079 while (!strncmp(path_key_expr, "..", 2)) {
2080 ++dest_parent_times;
2081 path_key_expr += 2;
2082 while (isspace(*path_key_expr)) {
2083 ++path_key_expr;
2084 }
2085 if (*path_key_expr != '/') {
2086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2087 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2088 *predicate - start, start);
2089 goto cleanup;
2090 }
2091 ++path_key_expr;
2092 while (isspace(*path_key_expr)) {
2093 ++path_key_expr;
2094 }
2095
2096 /* path is supposed to be evaluated in data tree, so we have to skip
2097 * all schema nodes that cannot be instantiated in data tree */
2098 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2099 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2100 *predicate - start, start);
2101 }
2102 if (!dest_parent_times) {
2103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2104 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2105 *predicate - start, start);
2106 goto cleanup;
2107 }
2108 if (path_key_expr == pke_end) {
2109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2110 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2111 *predicate - start, start);
2112 goto cleanup;
2113 }
2114
2115 while(path_key_expr != pke_end) {
2116 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002118 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2119 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002120 goto cleanup;
2121 }
2122
2123 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002124 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002125 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002126 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002127 }
2128 if (!mod) {
2129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2130 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
2131 *predicate - start, start, dst_len, dst);
2132 goto cleanup;
2133 }
2134
2135 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2136 if (!dst_node) {
2137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2138 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
2139 *predicate - start, start, path_key_expr - pke_start, pke_start);
2140 goto cleanup;
2141 }
2142 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002143 if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcia3045382018-11-22 14:30:31 +01002144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002145 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002146 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2147 goto cleanup;
2148 }
2149 }
2150
2151 ret = LY_SUCCESS;
2152cleanup:
2153 ly_set_erase(&keys, NULL);
2154 return ret;
2155}
2156
2157/**
2158 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2159 *
2160 * path-arg = absolute-path / relative-path
2161 * absolute-path = 1*("/" (node-identifier *path-predicate))
2162 * relative-path = 1*(".." "/") descendant-path
2163 *
2164 * @param[in,out] path Path to parse.
2165 * @param[out] prefix Prefix of the token, NULL if there is not any.
2166 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2167 * @param[out] name Name of the token.
2168 * @param[out] nam_len Length of the name.
2169 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2170 * must not be changed between consecutive calls. -1 if the
2171 * path is absolute.
2172 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2173 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2174 */
2175static LY_ERR
2176lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2177 int *parent_times, int *has_predicate)
2178{
2179 int par_times = 0;
2180
2181 assert(path && *path);
2182 assert(parent_times);
2183 assert(prefix);
2184 assert(prefix_len);
2185 assert(name);
2186 assert(name_len);
2187 assert(has_predicate);
2188
2189 *prefix = NULL;
2190 *prefix_len = 0;
2191 *name = NULL;
2192 *name_len = 0;
2193 *has_predicate = 0;
2194
2195 if (!*parent_times) {
2196 if (!strncmp(*path, "..", 2)) {
2197 *path += 2;
2198 ++par_times;
2199 while (!strncmp(*path, "/..", 3)) {
2200 *path += 3;
2201 ++par_times;
2202 }
2203 }
2204 if (par_times) {
2205 *parent_times = par_times;
2206 } else {
2207 *parent_times = -1;
2208 }
2209 }
2210
2211 if (**path != '/') {
2212 return LY_EINVAL;
2213 }
2214 /* skip '/' */
2215 ++(*path);
2216
2217 /* node-identifier ([prefix:]name) */
2218 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2219
2220 if ((**path == '/' && (*path)[1]) || !**path) {
2221 /* path continues by another token or this is the last token */
2222 return LY_SUCCESS;
2223 } else if ((*path)[0] != '[') {
2224 /* unexpected character */
2225 return LY_EINVAL;
2226 } else {
2227 /* predicate starting with [ */
2228 *has_predicate = 1;
2229 return LY_SUCCESS;
2230 }
2231}
2232
2233/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002234 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2235 *
2236 * The set of features used for target must be a subset of features used for the leafref.
2237 * This is not a perfect, we should compare the truth tables but it could require too much resources
2238 * and RFC 7950 does not require it explicitely, so we simplify that.
2239 *
2240 * @param[in] refnode The leafref node.
2241 * @param[in] target Tha target node of the leafref.
2242 * @return LY_SUCCESS or LY_EVALID;
2243 */
2244static LY_ERR
2245lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2246{
2247 LY_ERR ret = LY_EVALID;
2248 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002249 unsigned int u, v, count;
2250 struct ly_set features = {0};
2251
2252 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002253 if (iter->iffeatures) {
2254 LY_ARRAY_FOR(iter->iffeatures, u) {
2255 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2256 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002257 }
2258 }
2259 }
2260 }
2261
2262 /* we should have, in features set, a superset of features applicable to the target node.
2263 * So when adding features applicable to the target into the features set, we should not be
2264 * able to actually add any new feature, otherwise it is not a subset of features applicable
2265 * to the leafref itself. */
2266 count = features.count;
2267 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002268 if (iter->iffeatures) {
2269 LY_ARRAY_FOR(iter->iffeatures, u) {
2270 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2271 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002272 /* new feature was added (or LY_EMEM) */
2273 goto cleanup;
2274 }
2275 }
2276 }
2277 }
2278 }
2279 ret = LY_SUCCESS;
2280
2281cleanup:
2282 ly_set_erase(&features, NULL);
2283 return ret;
2284}
2285
2286/**
Radek Krejcia3045382018-11-22 14:30:31 +01002287 * @brief Validate the leafref path.
2288 * @param[in] ctx Compile context
2289 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002290 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002291 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2292 */
2293static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002294lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002295{
2296 const struct lysc_node *node = NULL, *parent = NULL;
2297 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002298 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002299 const char *id, *prefix, *name;
2300 size_t prefix_len, name_len;
2301 int parent_times = 0, has_predicate;
2302 unsigned int iter, u;
2303 LY_ERR ret = LY_SUCCESS;
2304
2305 assert(ctx);
2306 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002307 assert(leafref);
2308
Radek Krejcia3045382018-11-22 14:30:31 +01002309 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002310 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002311 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2312 if (!iter) { /* first iteration */
2313 /* precess ".." in relative paths */
2314 if (parent_times > 0) {
2315 /* move from the context node */
2316 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2317 /* path is supposed to be evaluated in data tree, so we have to skip
2318 * all schema nodes that cannot be instantiated in data tree */
2319 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002320 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002321 }
2322 }
2323 }
2324
2325 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002326 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002327 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002328 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002329 }
2330 if (!mod) {
2331 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002332 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2333 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002334 return LY_EVALID;
2335 }
Radek Krejci3d929562019-02-21 11:25:55 +01002336 if (!mod->implemented) {
2337 /* make the module implemented */
2338 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2339 }
Radek Krejcia3045382018-11-22 14:30:31 +01002340
2341 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2342 if (!node) {
2343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002344 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002345 return LY_EVALID;
2346 }
2347 parent = node;
2348
2349 if (has_predicate) {
2350 /* we have predicate, so the current result must be list */
2351 if (node->nodetype != LYS_LIST) {
2352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2353 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002354 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002355 return LY_EVALID;
2356 }
2357
Radek Krejcibade82a2018-12-05 10:13:48 +01002358 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2359 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002360 }
2361
2362 ++iter;
2363 }
2364 if (ret) {
2365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002366 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002367 return LY_EVALID;
2368 }
2369
2370 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2372 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002373 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002374 return LY_EVALID;
2375 }
2376
2377 /* check status */
2378 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2379 return LY_EVALID;
2380 }
2381
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002382 /* check config */
2383 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2384 if (node->flags & LYS_CONFIG_R) {
2385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2386 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2387 leafref->path);
2388 return LY_EVALID;
2389 }
2390 }
2391
Radek Krejci412ddfa2018-11-23 11:44:11 +01002392 /* store the target's type and check for circular chain of leafrefs */
2393 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2394 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2395 if (type == (struct lysc_type*)leafref) {
2396 /* circular chain detected */
2397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2398 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2399 return LY_EVALID;
2400 }
2401 }
2402
Radek Krejci58d171e2018-11-23 13:50:55 +01002403 /* check if leafref and its target are under common if-features */
2404 if (lys_compile_leafref_features_validate(startnode, node)) {
2405 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2406 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2407 leafref->path);
2408 return LY_EVALID;
2409 }
2410
Radek Krejcia3045382018-11-22 14:30:31 +01002411 return LY_SUCCESS;
2412}
2413
Radek Krejcicdfecd92018-11-26 11:27:32 +01002414static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejci01342af2019-01-03 15:18:08 +01002415 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002416/**
2417 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2418 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002419 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2420 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2421 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2422 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002423 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002424 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002425 * @param[in] basetype Base YANG built-in type of the type to compile.
2426 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2427 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2428 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2429 * @param[out] type Newly created type structure with the filled information about the type.
2430 * @return LY_ERR value.
2431 */
Radek Krejci19a96102018-11-15 13:38:09 +01002432static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002433lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
2434 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2435 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002436{
2437 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002438 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002439 struct lysc_type_bin *bin;
2440 struct lysc_type_num *num;
2441 struct lysc_type_str *str;
2442 struct lysc_type_bits *bits;
2443 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002444 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002445 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002446 struct lysc_type_union *un, *un_aux;
2447 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002448
2449 switch (basetype) {
2450 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002451 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002452
2453 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002454 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002455 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002456 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2457 LY_CHECK_RET(ret);
2458 if (!tpdfname) {
2459 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2460 options, u, lys_compile_ext, ret, done);
2461 }
2462 }
2463
2464 if (tpdfname) {
2465 type_p->compiled = *type;
2466 *type = calloc(1, sizeof(struct lysc_type_bin));
2467 }
2468 break;
2469 case LY_TYPE_BITS:
2470 /* RFC 7950 9.7 - bits */
2471 bits = (struct lysc_type_bits*)(*type);
2472 if (type_p->bits) {
2473 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2474 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2475 (struct lysc_type_enum_item**)&bits->bits);
2476 LY_CHECK_RET(ret);
2477 }
2478
Radek Krejci555cb5b2018-11-16 14:54:33 +01002479 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002480 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002481 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002483 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002485 free(*type);
2486 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002487 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002488 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002489 }
2490
2491 if (tpdfname) {
2492 type_p->compiled = *type;
2493 *type = calloc(1, sizeof(struct lysc_type_bits));
2494 }
2495 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002496 case LY_TYPE_DEC64:
2497 dec = (struct lysc_type_dec*)(*type);
2498
2499 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002500 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002501 if (!type_p->fraction_digits) {
2502 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002504 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002506 free(*type);
2507 *type = NULL;
2508 }
2509 return LY_EVALID;
2510 }
2511 } else if (type_p->fraction_digits) {
2512 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002513 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2515 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002516 tpdfname);
2517 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2519 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002520 free(*type);
2521 *type = NULL;
2522 }
2523 return LY_EVALID;
2524 }
2525 dec->fraction_digits = type_p->fraction_digits;
2526
2527 /* RFC 7950 9.2.4 - range */
2528 if (type_p->range) {
2529 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2530 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2531 LY_CHECK_RET(ret);
2532 if (!tpdfname) {
2533 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2534 options, u, lys_compile_ext, ret, done);
2535 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002536 }
2537
2538 if (tpdfname) {
2539 type_p->compiled = *type;
2540 *type = calloc(1, sizeof(struct lysc_type_dec));
2541 }
2542 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002543 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002544 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002545
2546 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002547 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002548 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002549 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2550 LY_CHECK_RET(ret);
2551 if (!tpdfname) {
2552 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2553 options, u, lys_compile_ext, ret, done);
2554 }
2555 } else if (base && ((struct lysc_type_str*)base)->length) {
2556 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2557 }
2558
2559 /* RFC 7950 9.4.5 - pattern */
2560 if (type_p->patterns) {
2561 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2562 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2563 LY_CHECK_RET(ret);
2564 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2565 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2566 }
2567
2568 if (tpdfname) {
2569 type_p->compiled = *type;
2570 *type = calloc(1, sizeof(struct lysc_type_str));
2571 }
2572 break;
2573 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002574 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002575
2576 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002577 if (type_p->enums) {
2578 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2579 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2580 LY_CHECK_RET(ret);
2581 }
2582
Radek Krejci555cb5b2018-11-16 14:54:33 +01002583 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002585 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002587 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002589 free(*type);
2590 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002592 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 }
2594
2595 if (tpdfname) {
2596 type_p->compiled = *type;
2597 *type = calloc(1, sizeof(struct lysc_type_enum));
2598 }
2599 break;
2600 case LY_TYPE_INT8:
2601 case LY_TYPE_UINT8:
2602 case LY_TYPE_INT16:
2603 case LY_TYPE_UINT16:
2604 case LY_TYPE_INT32:
2605 case LY_TYPE_UINT32:
2606 case LY_TYPE_INT64:
2607 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002608 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002609
2610 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002611 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002612 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002613 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2614 LY_CHECK_RET(ret);
2615 if (!tpdfname) {
2616 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2617 options, u, lys_compile_ext, ret, done);
2618 }
2619 }
2620
2621 if (tpdfname) {
2622 type_p->compiled = *type;
2623 *type = calloc(1, sizeof(struct lysc_type_num));
2624 }
2625 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002626 case LY_TYPE_IDENT:
2627 idref = (struct lysc_type_identityref*)(*type);
2628
2629 /* RFC 7950 9.10.2 - base */
2630 if (type_p->bases) {
2631 if (base) {
2632 /* only the directly derived identityrefs can contain base specification */
2633 if (tpdfname) {
2634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002635 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002636 tpdfname);
2637 } else {
2638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002639 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002640 free(*type);
2641 *type = NULL;
2642 }
2643 return LY_EVALID;
2644 }
2645 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2646 LY_CHECK_RET(ret);
2647 }
2648
2649 if (!base && !type_p->flags) {
2650 /* type derived from identityref built-in type must contain at least one base */
2651 if (tpdfname) {
2652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2653 } else {
2654 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2655 free(*type);
2656 *type = NULL;
2657 }
2658 return LY_EVALID;
2659 }
2660
2661 if (tpdfname) {
2662 type_p->compiled = *type;
2663 *type = calloc(1, sizeof(struct lysc_type_identityref));
2664 }
2665 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002666 case LY_TYPE_LEAFREF:
2667 /* RFC 7950 9.9.3 - require-instance */
2668 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002669 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002670 if (tpdfname) {
2671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2672 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2673 } else {
2674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2675 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2676 free(*type);
2677 *type = NULL;
2678 }
2679 return LY_EVALID;
2680 }
Radek Krejcia3045382018-11-22 14:30:31 +01002681 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002682 } else if (base) {
2683 /* inherit */
2684 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002685 } else {
2686 /* default is true */
2687 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2688 }
2689 if (type_p->path) {
2690 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002691 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002692 } else if (base) {
2693 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002694 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002695 } else if (tpdfname) {
2696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2697 return LY_EVALID;
2698 } else {
2699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2700 free(*type);
2701 *type = NULL;
2702 return LY_EVALID;
2703 }
2704 if (tpdfname) {
2705 type_p->compiled = *type;
2706 *type = calloc(1, sizeof(struct lysc_type_leafref));
2707 }
2708 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002709 case LY_TYPE_INST:
2710 /* RFC 7950 9.9.3 - require-instance */
2711 if (type_p->flags & LYS_SET_REQINST) {
2712 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2713 } else {
2714 /* default is true */
2715 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2716 }
2717
2718 if (tpdfname) {
2719 type_p->compiled = *type;
2720 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2721 }
2722 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002723 case LY_TYPE_UNION:
2724 un = (struct lysc_type_union*)(*type);
2725
2726 /* RFC 7950 7.4 - type */
2727 if (type_p->types) {
2728 if (base) {
2729 /* only the directly derived union can contain types specification */
2730 if (tpdfname) {
2731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2732 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2733 tpdfname);
2734 } else {
2735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2736 "Invalid type substatement for the type not directly derived from union built-in type.");
2737 free(*type);
2738 *type = NULL;
2739 }
2740 return LY_EVALID;
2741 }
2742 /* compile the type */
2743 additional = 0;
2744 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2745 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002746 ret = lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], options, &un->types[u + additional], NULL);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002747 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2748 /* add space for additional types from the union subtype */
2749 un_aux = (struct lysc_type_union *)un->types[u + additional];
2750 p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types)));
2751 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2752 un->types = (void*)((uint32_t*)(p) + 1);
2753
2754 /* copy subtypes of the subtype union */
2755 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2756 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2757 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2758 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2759 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2760 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2761 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2762 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2763 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2764 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2765 /* TODO extensions */
2766
2767 } else {
2768 un->types[u + additional] = un_aux->types[v];
2769 ++un_aux->types[v]->refcount;
2770 }
2771 ++additional;
2772 LY_ARRAY_INCREMENT(un->types);
2773 }
2774 /* compensate u increment in main loop */
2775 --additional;
2776
2777 /* free the replaced union subtype */
2778 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2779 } else {
2780 LY_ARRAY_INCREMENT(un->types);
2781 }
2782 LY_CHECK_RET(ret);
2783 }
2784 }
2785
2786 if (!base && !type_p->flags) {
2787 /* type derived from union built-in type must contain at least one type */
2788 if (tpdfname) {
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2790 } else {
2791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2792 free(*type);
2793 *type = NULL;
2794 }
2795 return LY_EVALID;
2796 }
2797
2798 if (tpdfname) {
2799 type_p->compiled = *type;
2800 *type = calloc(1, sizeof(struct lysc_type_union));
2801 }
2802 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002803 case LY_TYPE_BOOL:
2804 case LY_TYPE_EMPTY:
2805 case LY_TYPE_UNKNOWN: /* just to complete switch */
2806 break;
2807 }
2808 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2809done:
2810 return ret;
2811}
2812
Radek Krejcia3045382018-11-22 14:30:31 +01002813/**
2814 * @brief Compile information about the leaf/leaf-list's type.
2815 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002816 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2817 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2818 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2819 * @param[in] context_name Name of the context node or referencing typedef for logging.
2820 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002821 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2822 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002823 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002824 * @return LY_ERR value.
2825 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002826static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002827lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejci01342af2019-01-03 15:18:08 +01002828 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002829{
2830 LY_ERR ret = LY_SUCCESS;
2831 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002832 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002833 struct type_context {
2834 const struct lysp_tpdf *tpdf;
2835 struct lysp_node *node;
2836 struct lysp_module *mod;
2837 } *tctx, *tctx_prev = NULL;
2838 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002839 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002840 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002841 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002842
2843 (*type) = NULL;
2844
2845 tctx = calloc(1, sizeof *tctx);
2846 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002847 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002848 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2849 ret == LY_SUCCESS;
2850 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2851 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2852 if (basetype) {
2853 break;
2854 }
2855
2856 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002857 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2858 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002859 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2860
Radek Krejcicdfecd92018-11-26 11:27:32 +01002861 if (units && !*units) {
2862 /* inherit units */
2863 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2864 }
Radek Krejci01342af2019-01-03 15:18:08 +01002865 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002866 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002867 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002868 }
Radek Krejci01342af2019-01-03 15:18:08 +01002869 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002870 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2871 break;
2872 }
2873
Radek Krejci19a96102018-11-15 13:38:09 +01002874 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002875 /* it is not necessary to continue, the rest of the chain was already compiled,
2876 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002877 basetype = tctx->tpdf->type.compiled->basetype;
2878 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002879 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002880 dummyloops = 1;
2881 goto preparenext;
2882 } else {
2883 tctx = NULL;
2884 break;
2885 }
Radek Krejci19a96102018-11-15 13:38:09 +01002886 }
2887
2888 /* store information for the following processing */
2889 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2890
Radek Krejcicdfecd92018-11-26 11:27:32 +01002891preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002892 /* prepare next loop */
2893 tctx_prev = tctx;
2894 tctx = calloc(1, sizeof *tctx);
2895 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2896 }
2897 free(tctx);
2898
2899 /* allocate type according to the basetype */
2900 switch (basetype) {
2901 case LY_TYPE_BINARY:
2902 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002903 break;
2904 case LY_TYPE_BITS:
2905 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002906 break;
2907 case LY_TYPE_BOOL:
2908 case LY_TYPE_EMPTY:
2909 *type = calloc(1, sizeof(struct lysc_type));
2910 break;
2911 case LY_TYPE_DEC64:
2912 *type = calloc(1, sizeof(struct lysc_type_dec));
2913 break;
2914 case LY_TYPE_ENUM:
2915 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002916 break;
2917 case LY_TYPE_IDENT:
2918 *type = calloc(1, sizeof(struct lysc_type_identityref));
2919 break;
2920 case LY_TYPE_INST:
2921 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2922 break;
2923 case LY_TYPE_LEAFREF:
2924 *type = calloc(1, sizeof(struct lysc_type_leafref));
2925 break;
2926 case LY_TYPE_STRING:
2927 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002928 break;
2929 case LY_TYPE_UNION:
2930 *type = calloc(1, sizeof(struct lysc_type_union));
2931 break;
2932 case LY_TYPE_INT8:
2933 case LY_TYPE_UINT8:
2934 case LY_TYPE_INT16:
2935 case LY_TYPE_UINT16:
2936 case LY_TYPE_INT32:
2937 case LY_TYPE_UINT32:
2938 case LY_TYPE_INT64:
2939 case LY_TYPE_UINT64:
2940 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002941 break;
2942 case LY_TYPE_UNKNOWN:
2943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2944 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2945 ret = LY_EVALID;
2946 goto cleanup;
2947 }
2948 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002949 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2951 ly_data_type2str[basetype]);
2952 free(*type);
2953 (*type) = NULL;
2954 ret = LY_EVALID;
2955 goto cleanup;
2956 }
2957
2958 /* get restrictions from the referred typedefs */
2959 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2960 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002961 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002962 base = tctx->tpdf->type.compiled;
2963 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002964 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002965 /* no change, just use the type information from the base */
2966 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2967 ++base->refcount;
2968 continue;
2969 }
2970
2971 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002972 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2974 tctx->tpdf->name, ly_data_type2str[basetype]);
2975 ret = LY_EVALID;
2976 goto cleanup;
2977 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2979 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2980 tctx->tpdf->name, tctx->tpdf->dflt);
2981 ret = LY_EVALID;
2982 goto cleanup;
2983 }
2984
Radek Krejci19a96102018-11-15 13:38:09 +01002985 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002986 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002987 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002988 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2989 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002990 LY_CHECK_GOTO(ret, cleanup);
2991 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002992 }
2993
Radek Krejcic5c27e52018-11-15 14:38:11 +01002994 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002995 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002996 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002997 (*type)->basetype = basetype;
2998 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01002999 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, options, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003000 LY_CHECK_GOTO(ret, cleanup);
3001 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003002 /* no specific restriction in leaf's type definition, copy from the base */
3003 free(*type);
3004 (*type) = base;
3005 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003006 }
Radek Krejci01342af2019-01-03 15:18:08 +01003007 if (!(*type)->dflt) {
3008 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3009 }
Radek Krejci19a96102018-11-15 13:38:09 +01003010
3011 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
3012
3013cleanup:
3014 ly_set_erase(&tpdf_chain, free);
3015 return ret;
3016}
3017
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003018/**
3019 * @brief Compile status information of the given node.
3020 *
3021 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3022 * has the status correctly set during the compilation.
3023 *
3024 * @param[in] ctx Compile context
3025 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3026 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3027 * the compatibility with the parent's status value.
3028 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3029 * @return LY_ERR value.
3030 */
3031static LY_ERR
3032lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3033{
3034 /* status - it is not inherited by specification, but it does not make sense to have
3035 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3036 if (!((*node_flags) & LYS_STATUS_MASK)) {
3037 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3038 if ((parent_flags & 0x3) != 0x3) {
3039 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3040 * combination of bits (0x3) which marks the uses_status value */
3041 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3042 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3043 }
3044 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3045 } else {
3046 (*node_flags) |= LYS_STATUS_CURR;
3047 }
3048 } else if (parent_flags & LYS_STATUS_MASK) {
3049 /* check status compatibility with the parent */
3050 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3051 if ((*node_flags) & LYS_STATUS_CURR) {
3052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3053 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3054 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3055 } else { /* LYS_STATUS_DEPRC */
3056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3057 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3058 }
3059 return LY_EVALID;
3060 }
3061 }
3062 return LY_SUCCESS;
3063}
3064
Radek Krejci8cce8532019-03-05 11:27:45 +01003065/**
3066 * @brief Check uniqness of the node/action/notification name.
3067 *
3068 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3069 * structures, but they share the namespace so we need to check their name collisions.
3070 *
3071 * @param[in] ctx Compile context.
3072 * @param[in] children List (linked list) of data nodes to go through.
3073 * @param[in] actions List (sized array) of actions or RPCs to go through.
3074 * @param[in] notifs List (sized array) of Notifications to go through.
3075 * @param[in] name Name of the item to find in the given lists.
3076 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3077 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3078 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3079 */
3080static LY_ERR
3081lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3082 const struct lysc_action *actions, const struct lysc_notif *notifs,
3083 const char *name, void *exclude)
3084{
3085 const struct lysc_node *iter;
3086 unsigned int u;
3087
3088 LY_LIST_FOR(children, iter) {
3089 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3090 goto error;
3091 }
3092 }
3093 LY_ARRAY_FOR(actions, u) {
3094 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3095 goto error;
3096 }
3097 }
3098 LY_ARRAY_FOR(notifs, u) {
3099 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3100 goto error;
3101 }
3102 }
3103 return LY_SUCCESS;
3104error:
3105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3106 return LY_EEXIST;
3107}
3108
Radek Krejcib1b59152019-01-07 13:21:56 +01003109static 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 +01003110
Radek Krejcia3045382018-11-22 14:30:31 +01003111/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003112 * @brief Compile parsed RPC/action schema node information.
3113 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003114 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003115 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003116 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003117 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3118 * @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).
3119 * Zero means no uses, non-zero value with no status bit set mean the default status.
3120 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3121 */
3122static LY_ERR
3123lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, int options,
3124 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3125{
3126 LY_ERR ret = LY_SUCCESS;
3127 struct lysp_node *child_p;
3128 unsigned int u;
3129
Radek Krejci8cce8532019-03-05 11:27:45 +01003130 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3131 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3132 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3133 action_p->name, action)) {
3134 return LY_EVALID;
3135 }
3136
Radek Krejcifc11bd72019-04-11 16:00:05 +02003137 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003139 "Action \"%s\" is placed inside %s.", action_p->name,
3140 options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003141 return LY_EVALID;
3142 }
3143
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003144 action->nodetype = LYS_ACTION;
3145 action->module = ctx->mod;
3146 action->parent = parent;
3147 if (!(options & LYSC_OPT_FREE_SP)) {
3148 action->sp = action_p;
3149 }
3150 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3151
3152 /* status - it is not inherited by specification, but it does not make sense to have
3153 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3154 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3155
3156 DUP_STRING(ctx->ctx, action_p->name, action->name);
3157 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3158 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
3159 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3160 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, options, u, lys_compile_ext, ret, cleanup);
3161
3162 /* input */
3163 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 +02003164 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 +01003165 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003166 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 +01003167 }
3168
3169 /* output */
3170 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 +02003171 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 +01003172 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003173 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 +01003174 }
3175
3176cleanup:
3177 return ret;
3178}
3179
3180/**
Radek Krejci43981a32019-04-12 09:44:11 +02003181 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003182 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003183 * @param[in] notif_p Parsed Notification schema node.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003184 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003185 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3186 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3187 * @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 +02003188 * Zero means no uses, non-zero value with no status bit set mean the default status.
3189 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3190 */
3191static LY_ERR
3192lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, int options,
3193 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3194{
3195 LY_ERR ret = LY_SUCCESS;
3196 struct lysp_node *child_p;
3197 unsigned int u;
3198
3199 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3200 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3201 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3202 notif_p->name, notif)) {
3203 return LY_EVALID;
3204 }
3205
3206 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
3207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3208 "Notification \"%s\" is placed inside %s.", notif_p->name,
3209 options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
3210 return LY_EVALID;
3211 }
3212
3213 notif->nodetype = LYS_NOTIF;
3214 notif->module = ctx->mod;
3215 notif->parent = parent;
3216 if (!(options & LYSC_OPT_FREE_SP)) {
3217 notif->sp = notif_p;
3218 }
3219 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3220
3221 /* status - it is not inherited by specification, but it does not make sense to have
3222 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3223 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3224
3225 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3226 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3227 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
3228 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3229 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, options, u, lys_compile_ext, ret, cleanup);
3230 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, options, u, lys_compile_must, ret, cleanup);
3231
3232 LY_LIST_FOR(notif_p->data, child_p) {
3233 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_NOTIFICATION, (struct lysc_node*)notif, uses_status));
3234 }
3235
3236cleanup:
3237 return ret;
3238}
3239
3240/**
Radek Krejcia3045382018-11-22 14:30:31 +01003241 * @brief Compile parsed container node information.
3242 * @param[in] ctx Compile context
3243 * @param[in] node_p Parsed container node.
3244 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3245 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3246 * is enriched with the container-specific information.
3247 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3248 */
Radek Krejci19a96102018-11-15 13:38:09 +01003249static LY_ERR
3250lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3251{
3252 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3253 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3254 struct lysp_node *child_p;
3255 unsigned int u;
3256 LY_ERR ret = LY_SUCCESS;
3257
Radek Krejcife909632019-02-12 15:34:42 +01003258 if (cont_p->presence) {
3259 cont->flags |= LYS_PRESENCE;
3260 }
3261
Radek Krejci19a96102018-11-15 13:38:09 +01003262 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003263 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003264 }
3265
3266 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003267 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 +02003268 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 +01003269
3270done:
3271 return ret;
3272}
3273
Radek Krejci33f72892019-02-21 10:36:58 +01003274/*
3275 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3276 * @param[in] ctx Compile context.
3277 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3278 * @param[in] type_p Parsed type to compile.
3279 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3280 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3281 * @return LY_ERR value.
3282 */
3283static LY_ERR
3284lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, int options, struct lysc_node_leaf *leaf)
3285{
3286 unsigned int u, v;
3287 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3288
3289 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type,
3290 leaf->units ? NULL : &leaf->units));
3291 if (leaf->nodetype == LYS_LEAFLIST) {
3292 if (llist->type->dflt && !llist->dflts && !llist->min) {
3293 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3294 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3295 LY_ARRAY_INCREMENT(llist->dflts);
3296 }
3297 } else {
3298 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3299 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3300 }
3301 }
3302 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3303 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3304 ly_set_add(&ctx->unres, leaf, 0);
3305 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3306 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3307 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3308 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3309 ly_set_add(&ctx->unres, leaf, 0);
3310 }
3311 }
3312 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3313 if (leaf->flags & LYS_SET_DFLT) {
3314 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3315 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3316 return LY_EVALID;
3317 }
3318 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3320 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3321 return LY_EVALID;
3322 }
3323 }
3324
3325 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3326 /* configuration data values must be unique - so check the default values */
3327 LY_ARRAY_FOR(llist->dflts, u) {
3328 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3329 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3331 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3332 return LY_EVALID;
3333 }
3334 }
3335 }
3336 }
3337
3338 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3339
3340 return LY_SUCCESS;
3341}
3342
Radek Krejcia3045382018-11-22 14:30:31 +01003343/**
3344 * @brief Compile parsed leaf node information.
3345 * @param[in] ctx Compile context
3346 * @param[in] node_p Parsed leaf node.
3347 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3348 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3349 * is enriched with the leaf-specific information.
3350 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3351 */
Radek Krejci19a96102018-11-15 13:38:09 +01003352static LY_ERR
3353lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3354{
3355 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3356 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3357 unsigned int u;
3358 LY_ERR ret = LY_SUCCESS;
3359
Radek Krejci19a96102018-11-15 13:38:09 +01003360 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003361 if (leaf_p->units) {
3362 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3363 leaf->flags |= LYS_SET_UNITS;
3364 }
3365 if (leaf_p->dflt) {
3366 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003367 leaf->flags |= LYS_SET_DFLT;
3368 }
Radek Krejci43699232018-11-23 14:59:46 +01003369
Radek Krejci33f72892019-02-21 10:36:58 +01003370 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, options, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003371
Radek Krejci19a96102018-11-15 13:38:09 +01003372done:
3373 return ret;
3374}
3375
Radek Krejcia3045382018-11-22 14:30:31 +01003376/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003377 * @brief Compile parsed leaf-list node information.
3378 * @param[in] ctx Compile context
3379 * @param[in] node_p Parsed leaf-list node.
3380 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3381 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3382 * is enriched with the leaf-list-specific information.
3383 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3384 */
3385static LY_ERR
3386lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3387{
3388 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3389 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003390 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003391 LY_ERR ret = LY_SUCCESS;
3392
Radek Krejci0e5d8382018-11-28 16:37:53 +01003393 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003394 if (llist_p->units) {
3395 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3396 llist->flags |= LYS_SET_UNITS;
3397 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003398
3399 if (llist_p->dflts) {
3400 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3401 LY_ARRAY_FOR(llist_p->dflts, u) {
3402 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3403 LY_ARRAY_INCREMENT(llist->dflts);
3404 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003405 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003406 }
3407
3408 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003409 if (llist->min) {
3410 llist->flags |= LYS_MAND_TRUE;
3411 }
Radek Krejcib7408632018-11-28 17:12:11 +01003412 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003413
Radek Krejci33f72892019-02-21 10:36:58 +01003414 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, options, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003415
3416done:
3417 return ret;
3418}
3419
3420/**
Radek Krejci7af64242019-02-18 13:07:53 +01003421 * @brief Compile information about list's uniques.
3422 * @param[in] ctx Compile context.
3423 * @param[in] context_module Module where the prefixes are going to be resolved.
3424 * @param[in] uniques Sized array list of unique statements.
3425 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3426 * @return LY_ERR value.
3427 */
3428static LY_ERR
3429lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3430{
3431 LY_ERR ret = LY_SUCCESS;
3432 struct lysc_node_leaf **key, ***unique;
3433 const char *keystr, *delim;
3434 size_t len;
3435 unsigned int v;
3436 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003437 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003438
3439 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3440 config = -1;
3441 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3442 keystr = uniques[v];
3443 while (keystr) {
3444 delim = strpbrk(keystr, " \t\n");
3445 if (delim) {
3446 len = delim - keystr;
3447 while (isspace(*delim)) {
3448 ++delim;
3449 }
3450 } else {
3451 len = strlen(keystr);
3452 }
3453
3454 /* unique node must be present */
3455 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003456 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3457 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003458 if (ret != LY_SUCCESS) {
3459 if (ret == LY_EDENIED) {
3460 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003461 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003462 len, keystr, lys_nodetype2str((*key)->nodetype));
3463 }
3464 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003465 } else if (flags) {
3466 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3467 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3468 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3469 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003470 }
3471
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003472
Radek Krejci7af64242019-02-18 13:07:53 +01003473 /* all referenced leafs must be of the same config type */
3474 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3475 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3476 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3477 return LY_EVALID;
3478 } else if ((*key)->flags & LYS_CONFIG_W) {
3479 config = 1;
3480 } else { /* LYS_CONFIG_R */
3481 config = 0;
3482 }
3483
3484 /* check status */
3485 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3486 (*key)->flags, (*key)->module, (*key)->name));
3487
3488 /* mark leaf as unique */
3489 (*key)->flags |= LYS_UNIQUE;
3490
3491 /* next unique value in line */
3492 keystr = delim;
3493 }
3494 /* next unique definition */
3495 }
3496
3497 return LY_SUCCESS;
3498}
3499
3500/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003501 * @brief Compile parsed list node information.
3502 * @param[in] ctx Compile context
3503 * @param[in] node_p Parsed list node.
3504 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3505 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3506 * is enriched with the list-specific information.
3507 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3508 */
3509static LY_ERR
3510lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3511{
3512 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3513 struct lysc_node_list *list = (struct lysc_node_list*)node;
3514 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003515 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003516 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003517 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003518 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003519 LY_ERR ret = LY_SUCCESS;
3520
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003521 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003522 if (list->min) {
3523 list->flags |= LYS_MAND_TRUE;
3524 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003525 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3526
3527 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003528 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003529 }
3530
3531 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
3532
3533 /* keys */
3534 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3536 return LY_EVALID;
3537 }
3538
3539 /* find all the keys (must be direct children) */
3540 keystr = list_p->key;
3541 while (keystr) {
3542 delim = strpbrk(keystr, " \t\n");
3543 if (delim) {
3544 len = delim - keystr;
3545 while (isspace(*delim)) {
3546 ++delim;
3547 }
3548 } else {
3549 len = strlen(keystr);
3550 }
3551
3552 /* key node must be present */
3553 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3554 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3555 if (!(*key)) {
3556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3557 "The list's key \"%.*s\" not found.", len, keystr);
3558 return LY_EVALID;
3559 }
3560 /* keys must be unique */
3561 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3562 if (*key == list->keys[u]) {
3563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3564 "Duplicated key identifier \"%.*s\".", len, keystr);
3565 return LY_EVALID;
3566 }
3567 }
3568 /* key must have the same config flag as the list itself */
3569 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3571 return LY_EVALID;
3572 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003573 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003574 /* YANG 1.0 denies key to be of empty type */
3575 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3577 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3578 return LY_EVALID;
3579 }
3580 } else {
3581 /* when and if-feature are illegal on list keys */
3582 if ((*key)->when) {
3583 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3584 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3585 return LY_EVALID;
3586 }
3587 if ((*key)->iffeatures) {
3588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3589 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3590 return LY_EVALID;
3591 }
3592 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003593
3594 /* check status */
3595 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3596 (*key)->flags, (*key)->module, (*key)->name));
3597
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003598 /* ignore default values of the key */
3599 if ((*key)->dflt) {
3600 lydict_remove(ctx->ctx, (*key)->dflt);
3601 (*key)->dflt = NULL;
3602 }
3603 /* mark leaf as key */
3604 (*key)->flags |= LYS_KEY;
3605
3606 /* next key value */
3607 keystr = delim;
3608 }
3609
3610 /* uniques */
3611 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003612 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003613 }
3614
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003615 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 +02003616 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 +01003617
3618done:
3619 return ret;
3620}
3621
Radek Krejcib56c7502019-02-13 14:19:54 +01003622/**
3623 * @brief Do some checks and set the default choice's case.
3624 *
3625 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3626 *
3627 * @param[in] ctx Compile context.
3628 * @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,
3629 * not the reference to the imported module.
3630 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3631 * @return LY_ERR value.
3632 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003633static LY_ERR
3634lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3635{
3636 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3637 const char *prefix = NULL, *name;
3638 size_t prefix_len = 0;
3639
3640 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3641 name = strchr(dflt, ':');
3642 if (name) {
3643 prefix = dflt;
3644 prefix_len = name - prefix;
3645 ++name;
3646 } else {
3647 name = dflt;
3648 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003649 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003650 /* prefixed default case make sense only for the prefix of the schema itself */
3651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3652 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3653 prefix_len, prefix);
3654 return LY_EVALID;
3655 }
3656 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3657 if (!ch->dflt) {
3658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3659 "Default case \"%s\" not found.", dflt);
3660 return LY_EVALID;
3661 }
3662 /* no mandatory nodes directly under the default case */
3663 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003664 if (iter->parent != (struct lysc_node*)ch->dflt) {
3665 break;
3666 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003667 if (iter->flags & LYS_MAND_TRUE) {
3668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3669 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3670 return LY_EVALID;
3671 }
3672 }
Radek Krejci01342af2019-01-03 15:18:08 +01003673 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003674 return LY_SUCCESS;
3675}
3676
Radek Krejciccd20f12019-02-15 14:12:27 +01003677static LY_ERR
3678lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3679{
3680 struct lys_module *mod;
3681 const char *prefix = NULL, *name;
3682 size_t prefix_len = 0;
3683 struct lysc_node_case *cs;
3684 struct lysc_node *node;
3685
3686 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3687 name = strchr(dflt, ':');
3688 if (name) {
3689 prefix = dflt;
3690 prefix_len = name - prefix;
3691 ++name;
3692 } else {
3693 name = dflt;
3694 }
3695 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3696 if (prefix) {
3697 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3699 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3700 "The prefix does not match any imported module of the deviation module.",
3701 devnodeid, dflt);
3702 return LY_EVALID;
3703 }
3704 } else {
3705 mod = ctx->mod;
3706 }
3707 /* get the default case */
3708 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3709 if (!cs) {
3710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3711 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3712 devnodeid, dflt);
3713 return LY_EVALID;
3714 }
3715
3716 /* check that there is no mandatory node */
3717 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003718 if (node->parent != (struct lysc_node*)cs) {
3719 break;
3720 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003721 if (node->flags & LYS_MAND_TRUE) {
3722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3723 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3724 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3725 return LY_EVALID;
3726 }
3727 }
3728
3729 /* set the default case in choice */
3730 ch->dflt = cs;
3731 cs->flags |= LYS_SET_DFLT;
3732
3733 return LY_SUCCESS;
3734}
3735
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003736/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003737 * @brief Compile parsed choice node information.
3738 * @param[in] ctx Compile context
3739 * @param[in] node_p Parsed choice node.
3740 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3741 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003742 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003743 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3744 */
3745static LY_ERR
3746lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3747{
3748 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3749 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3750 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003751 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003752 LY_ERR ret = LY_SUCCESS;
3753
Radek Krejci056d0a82018-12-06 16:57:25 +01003754 LY_LIST_FOR(ch_p->child, child_p) {
3755 if (child_p->nodetype == LYS_CASE) {
3756 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003757 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003758 }
3759 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003760 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003761 }
3762 }
3763
3764 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003765 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003766 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003767 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003768
Radek Krejci9800fb82018-12-13 14:26:23 +01003769 return ret;
3770}
3771
3772/**
3773 * @brief Compile parsed anydata or anyxml node information.
3774 * @param[in] ctx Compile context
3775 * @param[in] node_p Parsed anydata or anyxml node.
3776 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3777 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3778 * is enriched with the any-specific information.
3779 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3780 */
3781static LY_ERR
3782lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3783{
3784 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3785 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3786 unsigned int u;
3787 LY_ERR ret = LY_SUCCESS;
3788
3789 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3790
3791 if (any->flags & LYS_CONFIG_W) {
3792 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3793 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3794 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003795done:
3796 return ret;
3797}
3798
Radek Krejcib56c7502019-02-13 14:19:54 +01003799/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003800 * @brief Connect the node into the siblings list and check its name uniqueness.
3801 *
3802 * @param[in] ctx Compile context
3803 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3804 * the choice itself is expected instead of a specific case node.
3805 * @param[in] node Schema node to connect into the list.
Radek Krejci05b774b2019-02-25 13:26:18 +01003806 * @param[in] options Compile options to distinguish input/output when placing node into RPC/action.
Radek Krejci056d0a82018-12-06 16:57:25 +01003807 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3808 */
3809static LY_ERR
Radek Krejci05b774b2019-02-25 13:26:18 +01003810lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node, int options)
Radek Krejci056d0a82018-12-06 16:57:25 +01003811{
3812 struct lysc_node **children;
3813
3814 if (node->nodetype == LYS_CASE) {
3815 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3816 } else {
Radek Krejci05b774b2019-02-25 13:26:18 +01003817 children = lysc_node_children_p(parent, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003818 }
3819 if (children) {
3820 if (!(*children)) {
3821 /* first child */
3822 *children = node;
3823 } else if (*children != node) {
3824 /* by the condition in previous branch we cover the choice/case children
3825 * - the children list is shared by the choice and the the first case, in addition
3826 * the first child of each case must be referenced from the case node. So the node is
3827 * actually always already inserted in case it is the first children - so here such
3828 * a situation actually corresponds to the first branch */
3829 /* insert at the end of the parent's children list */
3830 (*children)->prev->next = node;
3831 node->prev = (*children)->prev;
3832 (*children)->prev = node;
3833
3834 /* check the name uniqueness */
3835 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3836 lysc_node_notifs(parent), node->name, node)) {
3837 return LY_EEXIST;
3838 }
3839 }
3840 }
3841 return LY_SUCCESS;
3842}
3843
Radek Krejci95710c92019-02-11 15:49:55 +01003844/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003845 * @brief Get the XPath context node for the given schema node.
3846 * @param[in] start The schema node where the XPath expression appears.
3847 * @return The context node to evaluate XPath expression in given schema node.
3848 * @return NULL in case the context node is the root node.
3849 */
3850static struct lysc_node *
3851lysc_xpath_context(struct lysc_node *start)
3852{
3853 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3854 start = start->parent);
3855 return start;
3856}
3857
3858/**
3859 * @brief Prepare the case structure in choice node for the new data node.
3860 *
3861 * 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
3862 * created in the choice when the first child was processed.
3863 *
3864 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003865 * @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,
3866 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003867 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3868 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3869 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3870 * @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,
3871 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003872 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003873static struct lysc_node_case*
3874lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3875{
3876 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02003877 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01003878 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003879 unsigned int u;
3880 LY_ERR ret;
3881
Radek Krejci95710c92019-02-11 15:49:55 +01003882#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003883 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003884 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3886 return NULL; \
3887 } \
3888 }
3889
Radek Krejci95710c92019-02-11 15:49:55 +01003890 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3891 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003892
3893 /* we have to add an implicit case node into the parent choice */
3894 cs = calloc(1, sizeof(struct lysc_node_case));
3895 DUP_STRING(ctx->ctx, child->name, cs->name);
3896 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003897 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003898 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3899 /* the case is already present since the child is not its first children */
3900 return (struct lysc_node_case*)ch->cases->prev;
3901 }
Radek Krejci95710c92019-02-11 15:49:55 +01003902 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003903
3904 /* explicit parent case is not present (this is its first child) */
3905 cs = calloc(1, sizeof(struct lysc_node_case));
3906 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3907 cs->flags = LYS_STATUS_MASK & node_p->flags;
3908 cs->sp = node_p;
3909
Radek Krejcib1b59152019-01-07 13:21:56 +01003910 /* 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 +02003911 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01003912
3913 if (node_p->when) {
3914 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3915 ret = lys_compile_when(ctx, node_p->when, options, when);
3916 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003917 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003918 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003919 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003920 } else {
3921 LOGINT(ctx->ctx);
3922 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003923 }
3924 cs->module = ctx->mod;
3925 cs->prev = (struct lysc_node*)cs;
3926 cs->nodetype = LYS_CASE;
Radek Krejci05b774b2019-02-25 13:26:18 +01003927 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003928 cs->parent = (struct lysc_node*)ch;
3929 cs->child = child;
3930
3931 return cs;
3932error:
Radek Krejcibb9b1982019-04-08 14:24:59 +02003933 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01003934 return NULL;
3935
3936#undef UNIQUE_CHECK
3937}
3938
Radek Krejcib56c7502019-02-13 14:19:54 +01003939/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003940 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003941 *
3942 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003943 * @param[in] node Target node where the config is supposed to be changed.
3944 * @param[in] config_flag Node's config flag to be applied to the @p node.
3945 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003946 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3947 * 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 +01003948 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3949 * @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 +01003950 * @return LY_ERR value.
3951 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003952static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003953lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3954 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003955{
3956 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003957 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003958
3959 if (config == (node->flags & LYS_CONFIG_MASK)) {
3960 /* nothing to do */
3961 return LY_SUCCESS;
3962 }
3963
3964 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003965 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003966 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3967 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003968 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3969 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003970 return LY_EVALID;
3971 }
Radek Krejci93dcc392019-02-19 10:43:38 +01003972 node->flags |= LYS_SET_CONFIG;
3973 } else {
3974 if (node->flags & LYS_SET_CONFIG) {
3975 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
3976 /* setting config flags, but have node with explicit config true */
3977 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3978 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3979 refine_flag ? "refine" : "deviation", nodeid);
3980 return LY_EVALID;
3981 }
3982 /* do not change config on nodes where the config is explicitely set, this does not apply to
3983 * nodes, which are being changed explicitly (targets of refine or deviation) */
3984 return LY_SUCCESS;
3985 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003986 }
3987 node->flags &= ~LYS_CONFIG_MASK;
3988 node->flags |= config;
3989
3990 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003991 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003992 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01003993 }
3994
Radek Krejci76b3e962018-12-14 17:01:25 +01003995 return LY_SUCCESS;
3996}
3997
Radek Krejcib56c7502019-02-13 14:19:54 +01003998/**
3999 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4000 *
4001 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4002 * the flag to such parents from a mandatory children.
4003 *
4004 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4005 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4006 * (mandatory children was removed).
4007 */
Radek Krejcife909632019-02-12 15:34:42 +01004008void
4009lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4010{
4011 struct lysc_node *iter;
4012
4013 if (add) { /* set flag */
4014 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4015 parent = parent->parent) {
4016 parent->flags |= LYS_MAND_TRUE;
4017 }
4018 } else { /* unset flag */
4019 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004020 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004021 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004022 /* there is another mandatory node */
4023 return;
4024 }
4025 }
4026 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4027 parent->flags &= ~LYS_MAND_TRUE;
4028 }
4029 }
4030}
4031
Radek Krejci056d0a82018-12-06 16:57:25 +01004032/**
Radek Krejci3641f562019-02-13 15:38:40 +01004033 * @brief Internal sorting process for the lys_compile_augment_sort().
4034 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4035 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4036 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4037 */
4038static void
4039lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4040{
4041 unsigned int v;
4042 size_t len;
4043
4044 len = strlen(aug_p->nodeid);
4045 LY_ARRAY_FOR(result, v) {
4046 if (strlen(result[v]->nodeid) <= len) {
4047 continue;
4048 }
4049 if (v < LY_ARRAY_SIZE(result)) {
4050 /* move the rest of array */
4051 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4052 break;
4053 }
4054 }
4055 result[v] = aug_p;
4056 LY_ARRAY_INCREMENT(result);
4057}
4058
4059/**
4060 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4061 *
4062 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4063 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4064 *
4065 * @param[in] ctx Compile context.
4066 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4067 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4068 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4069 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4070 * @return LY_ERR value.
4071 */
4072LY_ERR
4073lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4074{
4075 struct lysp_augment **result = NULL;
4076 unsigned int u, v;
4077 size_t count = 0;
4078
4079 assert(augments);
4080
4081 /* get count of the augments in module and all its submodules */
4082 if (aug_p) {
4083 count += LY_ARRAY_SIZE(aug_p);
4084 }
4085 LY_ARRAY_FOR(inc_p, u) {
4086 if (inc_p[u].submodule->augments) {
4087 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4088 }
4089 }
4090
4091 if (!count) {
4092 *augments = NULL;
4093 return LY_SUCCESS;
4094 }
4095 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4096
4097 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4098 * together, so there can be even /z/y betwwen them. */
4099 LY_ARRAY_FOR(aug_p, u) {
4100 lys_compile_augment_sort_(&aug_p[u], result);
4101 }
4102 LY_ARRAY_FOR(inc_p, u) {
4103 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4104 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4105 }
4106 }
4107
4108 *augments = result;
4109 return LY_SUCCESS;
4110}
4111
4112/**
4113 * @brief Compile the parsed augment connecting it into its target.
4114 *
4115 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4116 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4117 * are already implemented and compiled.
4118 *
4119 * @param[in] ctx Compile context.
4120 * @param[in] aug_p Parsed augment to compile.
4121 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4122 * @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
4123 * children in case of the augmenting uses data.
4124 * @return LY_SUCCESS on success.
4125 * @return LY_EVALID on failure.
4126 */
4127LY_ERR
4128lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4129{
4130 LY_ERR ret = LY_SUCCESS;
4131 struct lysp_node *node_p, *case_node_p;
4132 struct lysc_node *target; /* target target of the augment */
4133 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004134 struct lysc_when **when, *when_shared;
4135 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004136 uint16_t flags = 0;
4137 unsigned int u;
Radek Krejci3641f562019-02-13 15:38:40 +01004138
Radek Krejci7af64242019-02-18 13:07:53 +01004139 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004140 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004141 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004142 if (ret != LY_SUCCESS) {
4143 if (ret == LY_EDENIED) {
4144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4145 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4146 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4147 }
4148 return LY_EVALID;
4149 }
4150
4151 /* check for mandatory nodes
4152 * - new cases augmenting some choice can have mandatory nodes
4153 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4154 */
Radek Krejci733988a2019-02-15 15:12:44 +01004155 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004156 allow_mandatory = 1;
4157 }
4158
4159 when_shared = NULL;
4160 LY_LIST_FOR(aug_p->child, node_p) {
4161 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4162 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4163 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004164 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4165 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004166 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4167 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4168 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4169 return LY_EVALID;
4170 }
4171
4172 /* compile the children */
4173 if (node_p->nodetype != LYS_CASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004174 LY_CHECK_RET(lys_compile_node(ctx, node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004175 } else {
4176 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004177 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004178 }
4179 }
4180
Radek Krejcife13da42019-02-15 14:51:01 +01004181 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4182 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004183 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004184 /* 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 +01004185 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 +01004186 } else if (target->nodetype == LYS_CHOICE) {
4187 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4188 node = ((struct lysc_node_choice*)target)->cases->prev;
4189 } else {
4190 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004191 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004192 }
4193
Radek Krejci733988a2019-02-15 15:12:44 +01004194 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004195 node->flags &= ~LYS_MAND_TRUE;
4196 lys_compile_mandatory_parents(target, 0);
4197 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4198 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4199 aug_p->nodeid, node->name);
4200 return LY_EVALID;
4201 }
4202
4203 /* pass augment's when to all the children */
4204 if (aug_p->when) {
4205 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4206 if (!when_shared) {
4207 ret = lys_compile_when(ctx, aug_p->when, options, when);
4208 LY_CHECK_GOTO(ret, error);
4209 (*when)->context = lysc_xpath_context(target);
4210 when_shared = *when;
4211 } else {
4212 ++when_shared->refcount;
4213 (*when) = when_shared;
4214 }
4215 }
4216 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004217
4218 switch (target->nodetype) {
4219 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004220 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
4221 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004222 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
4223 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004224 break;
4225 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004226 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
4227 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004228 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
4229 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004230 break;
4231 default:
4232 if (aug_p->actions) {
4233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4234 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4235 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4236 return LY_EVALID;
4237 }
4238 if (aug_p->notifs) {
4239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4240 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4241 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4242 return LY_EVALID;
4243 }
4244 }
Radek Krejci3641f562019-02-13 15:38:40 +01004245
4246error:
4247 return ret;
4248}
4249
4250/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004251 * @brief Apply refined or deviated mandatory flag to the target node.
4252 *
4253 * @param[in] ctx Compile context.
4254 * @param[in] node Target node where the mandatory property is supposed to be changed.
4255 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4256 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4257 * @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 +01004258 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4259 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4260 * 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 +01004261 * @return LY_ERR value.
4262 */
4263static LY_ERR
4264lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4265{
4266 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4268 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4269 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4270 return LY_EVALID;
4271 }
4272
4273 if (mandatory_flag & LYS_MAND_TRUE) {
4274 /* check if node has default value */
4275 if (node->nodetype & LYS_LEAF) {
4276 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004277 if (refine_flag) {
4278 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4279 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4280 return LY_EVALID;
4281 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004282 } else {
4283 /* remove the default value taken from the leaf's type */
4284 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4285 ((struct lysc_node_leaf*)node)->dflt = NULL;
4286 }
4287 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004288 if (refine_flag) {
4289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4290 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4291 return LY_EVALID;
4292 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004293 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004294 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004296 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004297 return LY_EVALID;
4298 }
4299
4300 node->flags &= ~LYS_MAND_FALSE;
4301 node->flags |= LYS_MAND_TRUE;
4302 lys_compile_mandatory_parents(node->parent, 1);
4303 } else {
4304 /* make mandatory false */
4305 node->flags &= ~LYS_MAND_TRUE;
4306 node->flags |= LYS_MAND_FALSE;
4307 lys_compile_mandatory_parents(node->parent, 0);
4308 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4309 /* get the type's default value if any */
4310 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4311 }
4312 }
4313 return LY_SUCCESS;
4314}
4315
4316/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004317 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4318 * If present, also apply uses's modificators.
4319 *
4320 * @param[in] ctx Compile context
4321 * @param[in] uses_p Parsed uses schema node.
4322 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4323 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4324 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4325 * the compile context.
4326 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4327 */
4328static LY_ERR
4329lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
4330{
4331 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004332 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004333 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004334 struct lysc_node_container context_node_fake =
4335 {.nodetype = LYS_CONTAINER,
4336 .module = ctx->mod,
4337 .flags = parent ? parent->flags : 0,
4338 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004339 .prev = (struct lysc_node*)&context_node_fake,
4340 .actions = NULL, .notifs = NULL};
Radek Krejcie86bf772018-12-14 11:39:53 +01004341 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004342 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004343 int found;
4344 const char *id, *name, *prefix;
4345 size_t prefix_len, name_len;
4346 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004347 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004348 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004349 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004350 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004351 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004352 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004353 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004354 unsigned int actions_index, notifs_index;
4355 struct lysc_notif **notifs = NULL;
4356 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004357
4358 /* search for the grouping definition */
4359 found = 0;
4360 id = uses_p->name;
Radek Krejci15e99fc2019-04-08 14:29:39 +02004361 LY_CHECK_RET(lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004362 if (prefix) {
4363 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4364 if (!mod) {
4365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4366 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4367 return LY_EVALID;
4368 }
4369 } else {
4370 mod = ctx->mod_def;
4371 }
4372 if (mod == ctx->mod_def) {
4373 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
4374 grp = lysp_node_groupings(node_p);
4375 LY_ARRAY_FOR(grp, u) {
4376 if (!strcmp(grp[u].name, name)) {
4377 grp = &grp[u];
4378 found = 1;
4379 break;
4380 }
4381 }
4382 }
4383 }
4384 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004385 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004386 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004387 if (grp) {
4388 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4389 if (!strcmp(grp[u].name, name)) {
4390 grp = &grp[u];
4391 found = 1;
4392 }
4393 }
4394 }
4395 if (!found && mod->parsed->includes) {
4396 /* ... and all the submodules */
4397 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4398 grp = mod->parsed->includes[u].submodule->groupings;
4399 if (grp) {
4400 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4401 if (!strcmp(grp[v].name, name)) {
4402 grp = &grp[v];
4403 found = 1;
4404 }
4405 }
4406 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004407 }
4408 }
4409 }
4410 if (!found) {
4411 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4412 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4413 return LY_EVALID;
4414 }
4415
4416 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4417 grp_stack_count = ctx->groupings.count;
4418 ly_set_add(&ctx->groupings, (void*)grp, 0);
4419 if (grp_stack_count == ctx->groupings.count) {
4420 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4422 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4423 return LY_EVALID;
4424 }
4425
4426 /* switch context's mod_def */
4427 mod_old = ctx->mod_def;
4428 ctx->mod_def = mod;
4429
4430 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004431 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 +01004432
Radek Krejcifc11bd72019-04-11 16:00:05 +02004433 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004434 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004435 /* 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 +02004436 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 +01004437 child = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01004438
4439 /* some preparation for applying refines */
4440 if (grp->data == node_p) {
4441 /* remember the first child */
4442 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01004443 }
4444 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004445 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004446 LY_LIST_FOR(context_node_fake.child, child) {
4447 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004448
Radek Krejcifc11bd72019-04-11 16:00:05 +02004449 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004450 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004451 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004452 if (!when_shared) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004453 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004454 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004455 when_shared = *when;
4456 } else {
4457 ++when_shared->refcount;
4458 (*when) = when_shared;
4459 }
4460 }
Radek Krejci01342af2019-01-03 15:18:08 +01004461 }
4462 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004463 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004464 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004465 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004466 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 +01004467 }
4468
Radek Krejcifc11bd72019-04-11 16:00:05 +02004469 /* compile actions */
4470 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4471 if (actions) {
4472 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
4473 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, options, u, lys_compile_action, 0, ret, cleanup);
4474 if (*actions && (uses_p->augments || uses_p->refines)) {
4475 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4476 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4477 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4478 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4479 }
4480 }
4481
4482 /* compile notifications */
4483 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4484 if (notifs) {
4485 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
4486 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, options, u, lys_compile_notif, 0, ret, cleanup);
4487 if (*notifs && (uses_p->augments || uses_p->refines)) {
4488 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4489 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4490 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4491 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4492 }
4493 }
4494
4495
Radek Krejci3641f562019-02-13 15:38:40 +01004496 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004497 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004498 LY_ARRAY_FOR(augments, u) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004499 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004500 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004501
Radek Krejcif0089082019-01-07 16:42:01 +01004502 /* reload previous context's mod_def */
4503 ctx->mod_def = mod_old;
4504
Radek Krejci76b3e962018-12-14 17:01:25 +01004505 /* apply refine */
4506 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004507 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 +01004508 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004509 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004510 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004511
4512 /* default value */
4513 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004514 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004515 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4516 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4517 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004518 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004519 }
4520 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4521 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4522 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4523 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004524 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004525 }
4526 if (node->nodetype == LYS_LEAF) {
4527 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4528 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004529 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004530 /* TODO check the default value according to type */
4531 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004532 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4534 "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 +02004535 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004536 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004537 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4538 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4539 }
4540 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004541 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004542 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 +01004543 LY_ARRAY_FOR(rfn->dflts, u) {
4544 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4545 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4546 }
4547 /* TODO check the default values according to type */
4548 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004549 if (((struct lysc_node_choice*)node)->dflt) {
4550 /* unset LYS_SET_DFLT from the current default case */
4551 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4552 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004553 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 +01004554 }
4555 }
4556
Radek Krejci12fb9142019-01-08 09:45:30 +01004557 /* description */
4558 if (rfn->dsc) {
4559 FREE_STRING(ctx->ctx, node->dsc);
4560 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4561 }
4562
4563 /* reference */
4564 if (rfn->ref) {
4565 FREE_STRING(ctx->ctx, node->ref);
4566 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4567 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004568
4569 /* config */
4570 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004571 if (!flags) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004572 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004573 } else {
4574 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4575 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4576 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004577 }
4578
4579 /* mandatory */
4580 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004581 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004582 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004583
4584 /* presence */
4585 if (rfn->presence) {
4586 if (node->nodetype != LYS_CONTAINER) {
4587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4588 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4589 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004590 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004591 }
4592 node->flags |= LYS_PRESENCE;
4593 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004594
4595 /* must */
4596 if (rfn->musts) {
4597 switch (node->nodetype) {
4598 case LYS_LEAF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004599 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 +01004600 break;
4601 case LYS_LEAFLIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004602 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 +01004603 break;
4604 case LYS_LIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004605 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 +01004606 break;
4607 case LYS_CONTAINER:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004608 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 +01004609 break;
4610 case LYS_ANYXML:
4611 case LYS_ANYDATA:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004612 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 +01004613 break;
4614 default:
4615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4616 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4617 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004618 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004619 }
4620 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004621
4622 /* min/max-elements */
4623 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4624 switch (node->nodetype) {
4625 case LYS_LEAFLIST:
4626 if (rfn->flags & LYS_SET_MAX) {
4627 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4628 }
4629 if (rfn->flags & LYS_SET_MIN) {
4630 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004631 if (rfn->min) {
4632 node->flags |= LYS_MAND_TRUE;
4633 lys_compile_mandatory_parents(node->parent, 1);
4634 } else {
4635 node->flags &= ~LYS_MAND_TRUE;
4636 lys_compile_mandatory_parents(node->parent, 0);
4637 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004638 }
4639 break;
4640 case LYS_LIST:
4641 if (rfn->flags & LYS_SET_MAX) {
4642 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4643 }
4644 if (rfn->flags & LYS_SET_MIN) {
4645 ((struct lysc_node_list*)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 default:
4656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4657 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4658 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004659 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004660 }
4661 }
Radek Krejcif0089082019-01-07 16:42:01 +01004662
4663 /* if-feature */
4664 if (rfn->iffeatures) {
4665 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004666 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004667 }
Radek Krejci01342af2019-01-03 15:18:08 +01004668 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004669
Radek Krejcif2271f12019-01-07 16:42:23 +01004670 /* do some additional checks of the changed nodes when all the refines are applied */
4671 for (u = 0; u < refined.count; ++u) {
4672 node = (struct lysc_node*)refined.objs[u];
4673 rfn = &uses_p->refines[u];
4674
4675 /* check possible conflict with default value (default added, mandatory left true) */
4676 if ((node->flags & LYS_MAND_TRUE) &&
4677 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4678 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4680 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004681 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004682 }
4683
4684 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4685 if (node->nodetype == LYS_LIST) {
4686 min = ((struct lysc_node_list*)node)->min;
4687 max = ((struct lysc_node_list*)node)->max;
4688 } else {
4689 min = ((struct lysc_node_leaflist*)node)->min;
4690 max = ((struct lysc_node_leaflist*)node)->max;
4691 }
4692 if (min > max) {
4693 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4694 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4695 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004696 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004697 }
4698 }
4699 }
4700
Radek Krejcie86bf772018-12-14 11:39:53 +01004701 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004702
4703cleanup:
4704 /* fix connection of the children nodes from fake context node back into the parent */
4705 if (context_node_fake.child) {
4706 context_node_fake.child->prev = child;
4707 }
4708 LY_LIST_FOR(context_node_fake.child, child) {
4709 child->parent = parent;
4710 }
4711
4712 if (uses_p->augments || uses_p->refines) {
4713 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004714 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004715 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4716 LY_ARRAY_FREE(context_node_fake.actions);
4717 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004718 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004719 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4720 LY_ARRAY_FREE(context_node_fake.notifs);
4721 }
4722 }
4723
Radek Krejcie86bf772018-12-14 11:39:53 +01004724 /* reload previous context's mod_def */
4725 ctx->mod_def = mod_old;
4726 /* remove the grouping from the stack for circular groupings dependency check */
4727 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4728 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004729 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004730 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004731
4732 return ret;
4733}
4734
Radek Krejcife909632019-02-12 15:34:42 +01004735
Radek Krejcie86bf772018-12-14 11:39:53 +01004736/**
Radek Krejcia3045382018-11-22 14:30:31 +01004737 * @brief Compile parsed schema node information.
4738 * @param[in] ctx Compile context
4739 * @param[in] node_p Parsed schema node.
4740 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4741 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4742 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4743 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004744 * @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).
4745 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004746 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4747 */
Radek Krejci19a96102018-11-15 13:38:09 +01004748static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01004749lys_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 +01004750{
4751 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004752 struct lysc_node *node;
4753 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004754 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004755 unsigned int u;
4756 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
4757
4758 switch (node_p->nodetype) {
4759 case LYS_CONTAINER:
4760 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4761 node_compile_spec = lys_compile_node_container;
4762 break;
4763 case LYS_LEAF:
4764 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4765 node_compile_spec = lys_compile_node_leaf;
4766 break;
4767 case LYS_LIST:
4768 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004769 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004770 break;
4771 case LYS_LEAFLIST:
4772 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004773 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004774 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004775 case LYS_CHOICE:
4776 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004777 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004778 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004779 case LYS_ANYXML:
4780 case LYS_ANYDATA:
4781 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004782 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004783 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004784 case LYS_USES:
4785 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004786 default:
4787 LOGINT(ctx->ctx);
4788 return LY_EINT;
4789 }
4790 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4791 node->nodetype = node_p->nodetype;
4792 node->module = ctx->mod;
4793 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004794 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004795
4796 /* config */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004797 if (options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
4798 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004799 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004800 node->flags |= (options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4801 } else if (options & LYSC_OPT_NOTIFICATION) {
4802 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004803 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004804 node->flags |= LYS_CONFIG_R;
4805 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004806 /* config not explicitely set, inherit it from parent */
4807 if (parent) {
4808 node->flags |= parent->flags & LYS_CONFIG_MASK;
4809 } else {
4810 /* default is config true */
4811 node->flags |= LYS_CONFIG_W;
4812 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004813 } else {
4814 /* config set explicitely */
4815 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004816 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004817 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4818 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4819 "Configuration node cannot be child of any state data node.");
4820 goto error;
4821 }
Radek Krejci19a96102018-11-15 13:38:09 +01004822
Radek Krejcia6d57732018-11-29 13:40:37 +01004823 /* *list ordering */
4824 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4825 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004826 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
4827 (options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
4828 (options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01004829 node->flags &= ~LYS_ORDBY_MASK;
4830 node->flags |= LYS_ORDBY_SYSTEM;
4831 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4832 /* default ordering is system */
4833 node->flags |= LYS_ORDBY_SYSTEM;
4834 }
4835 }
4836
Radek Krejci19a96102018-11-15 13:38:09 +01004837 /* status - it is not inherited by specification, but it does not make sense to have
4838 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004839 if (!parent || parent->nodetype != LYS_CHOICE) {
4840 /* in case of choice/case's children, postpone the check to the moment we know if
4841 * 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 +01004842 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 +01004843 }
4844
4845 if (!(options & LYSC_OPT_FREE_SP)) {
4846 node->sp = node_p;
4847 }
4848 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004849 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4850 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004851 if (node_p->when) {
4852 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4853 ret = lys_compile_when(ctx, node_p->when, options, when);
4854 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004855 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004856 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004857 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004858 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
4859
4860 /* nodetype-specific part */
4861 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
4862
Radek Krejcife909632019-02-12 15:34:42 +01004863 /* inherit LYS_MAND_TRUE in parent containers */
4864 if (node->flags & LYS_MAND_TRUE) {
4865 lys_compile_mandatory_parents(parent, 1);
4866 }
4867
Radek Krejci19a96102018-11-15 13:38:09 +01004868 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004869 if (parent) {
4870 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004871 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
4872 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004873 if (uses_status) {
4874
4875 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004876 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004877 * it directly gets the same status flags as the choice;
4878 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004879 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004880 node->parent = (struct lysc_node*)cs;
4881 } else { /* other than choice */
4882 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004883 }
Radek Krejci05b774b2019-02-25 13:26:18 +01004884 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 +01004885 } else {
4886 /* top-level element */
4887 if (!ctx->mod->compiled->data) {
4888 ctx->mod->compiled->data = node;
4889 } else {
4890 /* insert at the end of the module's top-level nodes list */
4891 ctx->mod->compiled->data->prev->next = node;
4892 node->prev = ctx->mod->compiled->data->prev;
4893 ctx->mod->compiled->data->prev = node;
4894 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004895 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4896 ctx->mod->compiled->notifs, node->name, node)) {
4897 return LY_EVALID;
4898 }
Radek Krejci19a96102018-11-15 13:38:09 +01004899 }
4900
4901 return LY_SUCCESS;
4902
4903error:
4904 lysc_node_free(ctx->ctx, node);
4905 return ret;
4906}
4907
Radek Krejciccd20f12019-02-15 14:12:27 +01004908static void
4909lysc_disconnect(struct lysc_node *node)
4910{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004911 struct lysc_node *parent, *child, *prev = NULL, *next;
4912 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004913 int remove_cs = 0;
4914
4915 parent = node->parent;
4916
4917 /* parent's first child */
4918 if (!parent) {
4919 return;
4920 }
4921 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004922 cs = (struct lysc_node_case*)node;
4923 } else if (parent->nodetype == LYS_CASE) {
4924 /* disconnecting some node in a case */
4925 cs = (struct lysc_node_case*)parent;
4926 parent = cs->parent;
4927 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4928 if (child == node) {
4929 if (cs->child == child) {
4930 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
4931 /* case with a single child -> remove also the case */
4932 child->parent = NULL;
4933 remove_cs = 1;
4934 } else {
4935 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01004936 }
4937 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004938 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01004939 }
4940 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004941 if (!remove_cs) {
4942 cs = NULL;
4943 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004944 } else if (lysc_node_children(parent, node->flags) == node) {
4945 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004946 }
4947
4948 if (cs) {
4949 if (remove_cs) {
4950 /* cs has only one child which is being also removed */
4951 lysc_disconnect((struct lysc_node*)cs);
4952 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
4953 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01004954 if (((struct lysc_node_choice*)parent)->dflt == cs) {
4955 /* default case removed */
4956 ((struct lysc_node_choice*)parent)->dflt = NULL;
4957 }
4958 if (((struct lysc_node_choice*)parent)->cases == cs) {
4959 /* first case removed */
4960 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
4961 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004962 if (cs->child) {
4963 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
4964 if (cs->child->prev->parent != (struct lysc_node*)cs) {
4965 prev = cs->child->prev;
4966 } /* else all the children are under a single case */
4967 LY_LIST_FOR_SAFE(cs->child, next, child) {
4968 if (child->parent != (struct lysc_node*)cs) {
4969 break;
4970 }
4971 lysc_node_free(node->module->ctx, child);
4972 }
4973 if (prev) {
4974 if (prev->next) {
4975 prev->next = child;
4976 }
4977 if (child) {
4978 child->prev = prev;
4979 } else {
4980 /* link from the first child under the cases */
4981 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
4982 }
4983 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004984 }
4985 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004986 }
4987
4988 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01004989 if (node->prev->next) {
4990 node->prev->next = node->next;
4991 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004992 if (node->next) {
4993 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004994 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004995 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01004996 if (child) {
4997 child->prev = node->prev;
4998 }
4999 } else if (((struct lysc_node_choice*)parent)->cases) {
5000 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005001 }
5002}
5003
5004LY_ERR
5005lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options)
5006{
5007 LY_ERR ret = LY_EVALID;
5008 struct ly_set devs_p = {0};
5009 struct ly_set targets = {0};
5010 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005011 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005012 struct lysc_action *rpcs;
5013 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005014 struct lysp_deviation *dev;
5015 struct lysp_deviate *d, **dp_new;
5016 struct lysp_deviate_add *d_add;
5017 struct lysp_deviate_del *d_del;
5018 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005019 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005020 struct lysc_deviation {
5021 const char *nodeid;
5022 struct lysc_node *target; /* target node of the deviation */
5023 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005024 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005025 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5026 } **devs = NULL;
5027 int i;
5028 size_t prefix_len, name_len;
5029 const char *prefix, *name, *nodeid;
5030 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005031 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005032 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005033
5034 /* get all deviations from the module and all its submodules ... */
5035 LY_ARRAY_FOR(mod_p->deviations, u) {
5036 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5037 }
5038 LY_ARRAY_FOR(mod_p->includes, v) {
5039 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5040 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5041 }
5042 }
5043 if (!devs_p.count) {
5044 /* nothing to do */
5045 return LY_SUCCESS;
5046 }
5047
5048 /* ... and group them by the target node */
5049 devs = calloc(devs_p.count, sizeof *devs);
5050 for (u = 0; u < devs_p.count; ++u) {
5051 dev = devs_p.objs[u];
5052
5053 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005054 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5055 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005056 if (target->nodetype == LYS_ACTION) {
5057 /* move the target pointer to input/output to make them different from the action and
5058 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5059 * back to the RPC/action node due to a better compatibility and decision code in this function.
5060 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5061 if (flags & LYSC_OPT_RPC_INPUT) {
5062 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5063 flags |= LYSC_OPT_INTERNAL;
5064 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5065 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5066 flags |= LYSC_OPT_INTERNAL;
5067 }
5068 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005069 /* insert into the set of targets with duplicity detection */
5070 i = ly_set_add(&targets, target, 0);
5071 if (!devs[i]) {
5072 /* new record */
5073 devs[i] = calloc(1, sizeof **devs);
5074 devs[i]->target = target;
5075 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005076 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005077 }
5078 /* add deviates into the deviation's list of deviates */
5079 for (d = dev->deviates; d; d = d->next) {
5080 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5081 *dp_new = d;
5082 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5083 devs[i]->not_supported = 1;
5084 }
5085 }
5086 }
5087
5088 /* MACROS for deviates checking */
5089#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5090 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
5091 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
5092 goto cleanup; \
5093 }
5094
5095#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5096 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
5097 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
5098 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
5099 goto cleanup; \
5100 }
5101
5102#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005103 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005104 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5105 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
5106 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5107 goto cleanup; \
5108 }
5109
Radek Krejci551b12c2019-02-19 16:11:21 +01005110#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5111 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5113 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
5114 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5115 goto cleanup; \
5116 }
5117
Radek Krejciccd20f12019-02-15 14:12:27 +01005118#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5119 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5120 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5121 goto cleanup; \
5122 }
5123
Radek Krejci551b12c2019-02-19 16:11:21 +01005124#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5125 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5127 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5128 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5129 goto cleanup; \
5130 }
5131
Radek Krejciccd20f12019-02-15 14:12:27 +01005132#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5133 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5134 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5136 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5137 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5138 goto cleanup; \
5139 } \
5140 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5141 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5142
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005143#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5144 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5145 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5146 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5147 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 +01005148 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005149 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5151 "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 +01005152 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005153 goto cleanup; \
5154 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005155 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5156 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5157 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5158 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5159 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005160 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005161 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5162 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5163 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005164 }
5165
5166 /* apply deviations */
5167 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005168 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5169 /* fix the target pointer in case of RPC's/action's input/output */
5170 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5171 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5172 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5173 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5174 }
5175 }
5176
Radek Krejciccd20f12019-02-15 14:12:27 +01005177 /* not-supported */
5178 if (devs[u]->not_supported) {
5179 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5180 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5181 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5182 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005183
5184#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5185 if (devs[u]->target->parent) { \
5186 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5187 } else { \
5188 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5189 } \
5190 LY_ARRAY_FOR(ARRAY, x) { \
5191 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5192 } \
5193 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5194 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5195 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5196 LY_ARRAY_DECREMENT(ARRAY); \
5197 }
5198
Radek Krejcif538ce52019-03-05 10:46:14 +01005199 if (devs[u]->target->nodetype == LYS_ACTION) {
5200 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5201 /* remove RPC's/action's input */
5202 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5203 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005204 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5205 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005206 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5207 /* remove RPC's/action's output */
5208 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5209 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005210 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5211 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005212 } else {
5213 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005214 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005215 }
5216 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005217 /* remove Notification */
5218 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005219 } else {
5220 /* remove the target node */
5221 lysc_disconnect(devs[u]->target);
5222 lysc_node_free(ctx->ctx, devs[u]->target);
5223 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005224
5225 continue;
5226 }
5227
5228 /* list of deviates (not-supported is not present in the list) */
5229 LY_ARRAY_FOR(devs[u]->deviates, v) {
5230 d = devs[u]->deviates[v];
5231
5232 switch (d->mod) {
5233 case LYS_DEV_ADD:
5234 d_add = (struct lysp_deviate_add*)d;
5235 /* [units-stmt] */
5236 if (d_add->units) {
5237 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5238 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5239
5240 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5241 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5242 }
5243
5244 /* *must-stmt */
5245 if (d_add->musts) {
5246 switch (devs[u]->target->nodetype) {
5247 case LYS_CONTAINER:
5248 case LYS_LIST:
5249 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5250 options, x, lys_compile_must, ret, cleanup);
5251 break;
5252 case LYS_LEAF:
5253 case LYS_LEAFLIST:
5254 case LYS_ANYDATA:
5255 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5256 options, x, lys_compile_must, ret, cleanup);
5257 break;
5258 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005259 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5260 options, x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005261 break;
5262 case LYS_ACTION:
5263 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5264 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5265 options, x, lys_compile_must, ret, cleanup);
5266 break;
5267 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5268 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5269 options, x, lys_compile_must, ret, cleanup);
5270 break;
5271 }
5272 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005273 default:
5274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005275 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005276 goto cleanup;
5277 }
5278 }
5279
5280 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005281 if (d_add->uniques) {
5282 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5283 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5284 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005285
5286 /* *default-stmt */
5287 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005288 switch (devs[u]->target->nodetype) {
5289 case LYS_LEAF:
5290 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5291 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5292 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5293 /* first, remove the default value taken from the type */
5294 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5295 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5296 }
5297 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005298 /* mark the new default values as leaf's own */
5299 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005300 break;
5301 case LYS_LEAFLIST:
5302 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5303 /* first, remove the default value taken from the type */
5304 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5305 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5306 }
5307 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5308 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5309 }
5310 /* add new default value(s) */
5311 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5312 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5313 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5314 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5315 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5316 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5317 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005318 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005319 devs[u]->target->flags |= LYS_SET_DFLT;
5320 break;
5321 case LYS_CHOICE:
5322 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5323 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5324 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5325 * to allow making the default case even the augmented case from the deviating module */
5326 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5327 (struct lysc_node_choice*)devs[u]->target)) {
5328 goto cleanup;
5329 }
5330 break;
5331 default:
5332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005333 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005334 goto cleanup;
5335 }
5336 }
5337
5338 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005339 if (d_add->flags & LYS_CONFIG_MASK) {
5340 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5342 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5343 goto cleanup;
5344 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005345 if (devs[u]->flags) {
5346 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5347 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5348 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005349 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5351 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5352 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5353 goto cleanup;
5354 }
5355 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5356 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005357
5358 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005359 if (d_add->flags & LYS_MAND_MASK) {
5360 if (devs[u]->target->flags & LYS_MAND_MASK) {
5361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5362 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5363 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5364 goto cleanup;
5365 }
5366 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5367 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005368
5369 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005370 if (d_add->flags & LYS_SET_MIN) {
5371 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5372 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5373 /* change value */
5374 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5375 } else if (devs[u]->target->nodetype == LYS_LIST) {
5376 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5377 /* change value */
5378 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5379 } else {
5380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5381 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5382 goto cleanup;
5383 }
5384 if (d_add->min) {
5385 devs[u]->target->flags |= LYS_MAND_TRUE;
5386 }
5387 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005388
5389 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005390 if (d_add->flags & LYS_SET_MAX) {
5391 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5392 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5393 /* change value */
5394 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5395 } else if (devs[u]->target->nodetype == LYS_LIST) {
5396 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5397 /* change value */
5398 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5399 } else {
5400 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5401 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5402 goto cleanup;
5403 }
5404 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005405
5406 break;
5407 case LYS_DEV_DELETE:
5408 d_del = (struct lysp_deviate_del*)d;
5409
5410 /* [units-stmt] */
5411 if (d_del->units) {
5412 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5413 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5414 }
5415
5416 /* *must-stmt */
5417 if (d_del->musts) {
5418 switch (devs[u]->target->nodetype) {
5419 case LYS_CONTAINER:
5420 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005421 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005422 break;
5423 case LYS_LEAF:
5424 case LYS_LEAFLIST:
5425 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005426 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005427 break;
5428 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005429 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005430 break;
5431 case LYS_ACTION:
5432 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5433 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5434 break;
5435 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5436 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5437 break;
5438 }
5439 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005440 default:
5441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005442 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005443 goto cleanup;
5444 }
5445 }
5446
5447 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005448 if (d_del->uniques) {
5449 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5450 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5451 LY_ARRAY_FOR(d_del->uniques, x) {
5452 LY_ARRAY_FOR(list->uniques, z) {
5453 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5454 nodeid = strpbrk(name, " \t\n");
5455 if (nodeid) {
5456 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5457 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5458 break;
5459 }
5460 while (isspace(*nodeid)) {
5461 ++nodeid;
5462 }
5463 } else {
5464 if (strcmp(name, list->uniques[z][y]->name)) {
5465 break;
5466 }
5467 }
5468 }
5469 if (!name) {
5470 /* complete match - remove the unique */
5471 LY_ARRAY_DECREMENT(list->uniques);
5472 LY_ARRAY_FREE(list->uniques[z]);
5473 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5474 --z;
5475 break;
5476 }
5477 }
5478 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5480 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5481 devs[u]->nodeid, d_del->uniques[x]);
5482 goto cleanup;
5483 }
5484 }
5485 if (!LY_ARRAY_SIZE(list->uniques)) {
5486 LY_ARRAY_FREE(list->uniques);
5487 list->uniques = NULL;
5488 }
5489 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005490
5491 /* *default-stmt */
5492 if (d_del->dflts) {
5493 switch (devs[u]->target->nodetype) {
5494 case LYS_LEAF:
5495 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5496 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5497 dflt, "deleting", "default", d_del->dflts[0]);
5498
5499 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005500 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005501 break;
5502 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005503 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005504 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5505 devs[u]->target->flags &= ~LYS_SET_DFLT;
5506 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005507 break;
5508 case LYS_CHOICE:
5509 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5510 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5511 nodeid = d_del->dflts[0];
5512 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5513 if (prefix) {
5514 /* use module prefixes from the deviation module to match the module of the default case */
5515 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5516 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5517 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5518 "The prefix does not match any imported module of the deviation module.",
5519 devs[u]->nodeid, d_del->dflts[0]);
5520 goto cleanup;
5521 }
5522 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5523 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5524 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5525 "The prefix does not match the default case's module.",
5526 devs[u]->nodeid, d_del->dflts[0]);
5527 goto cleanup;
5528 }
5529 }
5530 /* else {
5531 * strictly, the default prefix would point to the deviation module, but the value should actually
5532 * match the default string in the original module (usually unprefixed), so in this case we do not check
5533 * the module of the default case, just matching its name */
5534 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5536 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5537 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5538 goto cleanup;
5539 }
5540 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5541 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5542 break;
5543 default:
5544 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005545 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005546 goto cleanup;
5547 }
5548 }
5549
5550 break;
5551 case LYS_DEV_REPLACE:
5552 d_rpl = (struct lysp_deviate_rpl*)d;
5553
5554 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005555 if (d_rpl->type) {
5556 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5557 /* type is mandatory, so checking for its presence is not necessary */
5558 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
5559 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup);
5560 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005561
5562 /* [units-stmt] */
5563 if (d_rpl->units) {
5564 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5565 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5566 units, "replacing", "units", d_rpl->units);
5567
5568 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5569 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5570 }
5571
5572 /* [default-stmt] */
5573 if (d_rpl->dflt) {
5574 switch (devs[u]->target->nodetype) {
5575 case LYS_LEAF:
5576 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5577 dflt, "replacing", "default", d_rpl->dflt);
5578
5579 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5580 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5581 break;
5582 case LYS_CHOICE:
5583 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5584 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5585 (struct lysc_node_choice*)devs[u]->target)) {
5586 goto cleanup;
5587 }
5588 break;
5589 default:
5590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005591 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005592 goto cleanup;
5593 }
5594 }
5595
5596 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005597 if (d_rpl->flags & LYS_CONFIG_MASK) {
5598 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5600 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5601 goto cleanup;
5602 }
5603 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5605 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5606 goto cleanup;
5607 }
5608 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5609 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005610
5611 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005612 if (d_rpl->flags & LYS_MAND_MASK) {
5613 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5615 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5616 goto cleanup;
5617 }
5618 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5619 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005620
5621 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005622 if (d_rpl->flags & LYS_SET_MIN) {
5623 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5624 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5625 /* change value */
5626 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5627 } else if (devs[u]->target->nodetype == LYS_LIST) {
5628 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5629 /* change value */
5630 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5631 } else {
5632 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5633 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5634 goto cleanup;
5635 }
5636 if (d_rpl->min) {
5637 devs[u]->target->flags |= LYS_MAND_TRUE;
5638 }
5639 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005640
5641 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005642 if (d_rpl->flags & LYS_SET_MAX) {
5643 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5644 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5645 /* change value */
5646 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5647 } else if (devs[u]->target->nodetype == LYS_LIST) {
5648 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5649 /* change value */
5650 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5651 } else {
5652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5653 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5654 goto cleanup;
5655 }
5656 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005657
5658 break;
5659 default:
5660 LOGINT(ctx->ctx);
5661 goto cleanup;
5662 }
5663 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005664
Radek Krejci33f72892019-02-21 10:36:58 +01005665 /* final check when all deviations of a single target node are applied */
5666
Radek Krejci551b12c2019-02-19 16:11:21 +01005667 /* check min-max compatibility */
5668 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5669 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5670 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5671 } else if (devs[u]->target->nodetype == LYS_LIST) {
5672 min = ((struct lysc_node_list*)devs[u]->target)->min;
5673 max = ((struct lysc_node_list*)devs[u]->target)->max;
5674 } else {
5675 min = max = 0;
5676 }
5677 if (min > max) {
5678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5679 "after deviation (%s): min value %u is bigger than max value %u.",
5680 devs[u]->nodeid, min, max);
5681 goto cleanup;
5682 }
5683
5684 /* check mandatory - default compatibility */
5685 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5686 && (devs[u]->target->flags & LYS_SET_DFLT)
5687 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5688 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5689 "Invalid deviation (%s) combining default value and mandatory %s.",
5690 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5691 goto cleanup;
5692 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5693 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5694 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5696 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5697 goto cleanup;
5698 }
5699 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5700 /* mandatory node under a default case */
5701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5702 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5703 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5704 goto cleanup;
5705 }
Radek Krejci33f72892019-02-21 10:36:58 +01005706
5707 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005708 }
5709
5710 ret = LY_SUCCESS;
5711
5712cleanup:
5713 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5714 LY_ARRAY_FREE(devs[u]->deviates);
5715 free(devs[u]);
5716 }
5717 free(devs);
5718 ly_set_erase(&targets, NULL);
5719 ly_set_erase(&devs_p, NULL);
5720
5721 return ret;
5722}
5723
Radek Krejcib56c7502019-02-13 14:19:54 +01005724/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005725 * @brief Compile the given YANG submodule into the main module.
5726 * @param[in] ctx Compile context
5727 * @param[in] inc Include structure from the main module defining the submodule.
5728 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
5729 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5730 */
5731LY_ERR
5732lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
5733{
5734 unsigned int u;
5735 LY_ERR ret = LY_SUCCESS;
5736 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005737 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005738 struct lysc_module *mainmod = ctx->mod->compiled;
5739
Radek Krejci0af46292019-01-11 16:02:31 +01005740 if (!mainmod->mod->off_features) {
5741 /* features are compiled directly into the compiled module structure,
5742 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5743 * The features compilation is finished in the main module (lys_compile()). */
5744 ret = lys_feature_precompile(ctx->ctx, submod->features,
5745 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5746 LY_CHECK_GOTO(ret, error);
5747 }
5748
Radek Krejcid05cbd92018-12-05 14:26:40 +01005749 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
5750
Radek Krejci8cce8532019-03-05 11:27:45 +01005751 /* TODO data nodes */
5752
5753 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005754 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, options, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01005755
Radek Krejcid05cbd92018-12-05 14:26:40 +01005756error:
5757 return ret;
5758}
5759
Radek Krejci19a96102018-11-15 13:38:09 +01005760LY_ERR
5761lys_compile(struct lys_module *mod, int options)
5762{
5763 struct lysc_ctx ctx = {0};
5764 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005765 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005766 struct lysp_module *sp;
5767 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005768 struct lysp_augment **augments = NULL;
5769 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005770 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005771 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005772
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005773 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005774
5775 if (!mod->implemented) {
5776 /* just imported modules are not compiled */
5777 return LY_SUCCESS;
5778 }
5779
Radek Krejci19a96102018-11-15 13:38:09 +01005780 sp = mod->parsed;
5781
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005782 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005783 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005784 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005785
5786 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005787 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5788 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005789
Radek Krejci19a96102018-11-15 13:38:09 +01005790 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005791 LY_ARRAY_FOR(sp->includes, u) {
5792 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
5793 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5794 }
Radek Krejci0af46292019-01-11 16:02:31 +01005795 if (mod->off_features) {
5796 /* there is already precompiled array of features */
5797 mod_c->features = mod->off_features;
5798 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005799 } else {
5800 /* features are compiled directly into the compiled module structure,
5801 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
5802 ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features);
5803 LY_CHECK_GOTO(ret, error);
5804 }
5805 /* finish feature compilation, not only for the main module, but also for the submodules.
5806 * Due to possible forward references, it must be done when all the features (including submodules)
5807 * are present. */
5808 LY_ARRAY_FOR(sp->features, u) {
5809 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
5810 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5811 }
5812 LY_ARRAY_FOR(sp->includes, v) {
5813 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
5814 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
5815 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5816 }
5817 }
5818
Radek Krejcid05cbd92018-12-05 14:26:40 +01005819 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005820 if (sp->identities) {
5821 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5822 }
5823
Radek Krejci95710c92019-02-11 15:49:55 +01005824 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005825 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005826 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005827 LY_CHECK_GOTO(ret, error);
5828 }
Radek Krejci95710c92019-02-11 15:49:55 +01005829
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005830 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 +02005831 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 +01005832
Radek Krejci95710c92019-02-11 15:49:55 +01005833 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005834 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005835 LY_CHECK_GOTO(ret, error);
5836 LY_ARRAY_FOR(augments, u) {
5837 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
5838 LY_CHECK_GOTO(ret, error);
5839 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005840
5841 /* deviations */
5842 ret = lys_compile_deviations(&ctx, sp, options);
5843 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005844
5845 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
5846
Radek Krejcia3045382018-11-22 14:30:31 +01005847 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005848 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5849 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5850 * 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 +01005851 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005852 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005853 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5854 if (type->basetype == LY_TYPE_LEAFREF) {
5855 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005856 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 +01005857 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005858 } else if (type->basetype == LY_TYPE_UNION) {
5859 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5860 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5861 /* validate the path */
5862 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5863 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5864 LY_CHECK_GOTO(ret, error);
5865 }
5866 }
Radek Krejcia3045382018-11-22 14:30:31 +01005867 }
5868 }
5869 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005870 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005871 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005872 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5873 if (type->basetype == LY_TYPE_LEAFREF) {
5874 /* store pointer to the real type */
5875 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5876 typeiter->basetype == LY_TYPE_LEAFREF;
5877 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5878 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005879 } else if (type->basetype == LY_TYPE_UNION) {
5880 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5881 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5882 /* store pointer to the real type */
5883 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5884 typeiter->basetype == LY_TYPE_LEAFREF;
5885 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5886 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5887 }
5888 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005889 }
5890 }
5891 }
Radek Krejcia3045382018-11-22 14:30:31 +01005892 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005893 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005894 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005895
Radek Krejci19a96102018-11-15 13:38:09 +01005896 if (options & LYSC_OPT_FREE_SP) {
5897 lysp_module_free(mod->parsed);
5898 ((struct lys_module*)mod)->parsed = NULL;
5899 }
5900
Radek Krejci95710c92019-02-11 15:49:55 +01005901 if (!(options & LYSC_OPT_INTERNAL)) {
5902 /* remove flag of the modules implemented by dependency */
5903 for (u = 0; u < ctx.ctx->list.count; ++u) {
5904 m = ctx.ctx->list.objs[u];
5905 if (m->implemented == 2) {
5906 m->implemented = 1;
5907 }
5908 }
5909 }
5910
Radek Krejci19a96102018-11-15 13:38:09 +01005911 ((struct lys_module*)mod)->compiled = mod_c;
5912 return LY_SUCCESS;
5913
5914error:
Radek Krejci95710c92019-02-11 15:49:55 +01005915 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01005916 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005917 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005918 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01005919 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005920 mod->compiled = NULL;
5921
5922 /* revert compilation of modules implemented by dependency */
5923 for (u = 0; u < ctx.ctx->list.count; ++u) {
5924 m = ctx.ctx->list.objs[u];
5925 if (m->implemented == 2) {
5926 /* revert features list to the precompiled state */
5927 lys_feature_precompile_revert(&ctx, m);
5928 /* mark module as imported-only / not-implemented */
5929 m->implemented = 0;
5930 /* free the compiled version of the module */
5931 lysc_module_free(m->compiled, NULL);
5932 m->compiled = NULL;
5933 }
5934 }
5935
Radek Krejci19a96102018-11-15 13:38:09 +01005936 return ret;
5937}