blob: f455dd43747b3e401c6a9e5841e5a38dd0abd57c [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <ctype.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <stdio.h>
Radek Krejci19a96102018-11-15 13:38:09 +010019
20#include "libyang.h"
21#include "context.h"
22#include "tree_schema_internal.h"
23#include "xpath.h"
24
25/**
26 * @brief Duplicate string into dictionary
27 * @param[in] CTX libyang context of the dictionary.
28 * @param[in] ORIG String to duplicate.
29 * @param[out] DUP Where to store the result.
30 */
31#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
32
33#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
34 if (ARRAY_P) { \
35 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010036 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010037 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
38 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010039 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER + __array_offset]); \
40 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
41 } \
42 }
43
Radek Krejci6eeb58f2019-02-22 16:29:37 +010044#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, OPTIONS, ITER, FUNC, USES_STATUS, RET, GOTO) \
45 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
47 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
48 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
50 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
51 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejcid05cbd92018-12-05 14:26:40 +010055#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
56 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
61 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
66#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
67 if (MEMBER_P) { \
68 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
69 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
70 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
71 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 }
73
Radek Krejci00b874b2019-02-12 10:54:50 +010074#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \
75 if (MEMBER_P) { \
76 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
77 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
78 LY_ARRAY_INCREMENT(ARRAY_C); \
79 RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \
80 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
81 }
82
Radek Krejcid05cbd92018-12-05 14:26:40 +010083#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
84 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010085 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
86 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
88 return LY_EVALID; \
89 } \
90 } \
91 }
92
Radek Krejci19a96102018-11-15 13:38:09 +010093static struct lysc_ext_instance *
94lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
95{
96 /* TODO */
97 (void) ctx;
98 (void) orig;
99 return NULL;
100}
101
Radek Krejcib56c7502019-02-13 14:19:54 +0100102/**
103 * @brief Duplicate the compiled pattern structure.
104 *
105 * Instead of duplicating memory, the reference counter in the @p orig is increased.
106 *
107 * @param[in] orig The pattern structure to duplicate.
108 * @return The duplicated structure to use.
109 */
Radek Krejci19a96102018-11-15 13:38:09 +0100110static struct lysc_pattern*
111lysc_pattern_dup(struct lysc_pattern *orig)
112{
113 ++orig->refcount;
114 return orig;
115}
116
Radek Krejcib56c7502019-02-13 14:19:54 +0100117/**
118 * @brief Duplicate the array of compiled patterns.
119 *
120 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
121 *
122 * @param[in] ctx Libyang context for logging.
123 * @param[in] orig The patterns sized array to duplicate.
124 * @return New sized array as a copy of @p orig.
125 * @return NULL in case of memory allocation error.
126 */
Radek Krejci19a96102018-11-15 13:38:09 +0100127static struct lysc_pattern**
128lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
129{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100130 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100131 unsigned int u;
132
Radek Krejcib56c7502019-02-13 14:19:54 +0100133 assert(orig);
134
Radek Krejci19a96102018-11-15 13:38:09 +0100135 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
136 LY_ARRAY_FOR(orig, u) {
137 dup[u] = lysc_pattern_dup(orig[u]);
138 LY_ARRAY_INCREMENT(dup);
139 }
140 return dup;
141}
142
Radek Krejcib56c7502019-02-13 14:19:54 +0100143/**
144 * @brief Duplicate compiled range structure.
145 *
146 * @param[in] ctx Libyang context for logging.
147 * @param[in] orig The range structure to be duplicated.
148 * @return New compiled range structure as a copy of @p orig.
149 * @return NULL in case of memory allocation error.
150 */
Radek Krejci19a96102018-11-15 13:38:09 +0100151struct lysc_range*
152lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
153{
154 struct lysc_range *dup;
155 LY_ERR ret;
156
Radek Krejcib56c7502019-02-13 14:19:54 +0100157 assert(orig);
158
Radek Krejci19a96102018-11-15 13:38:09 +0100159 dup = calloc(1, sizeof *dup);
160 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
161 if (orig->parts) {
162 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
163 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
164 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
165 }
166 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
167 DUP_STRING(ctx, orig->emsg, dup->emsg);
168 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
169
170 return dup;
171cleanup:
172 free(dup);
173 (void) ret; /* set but not used due to the return type */
174 return NULL;
175}
176
Radek Krejcib56c7502019-02-13 14:19:54 +0100177/**
178 * @brief Stack for processing if-feature expressions.
179 */
Radek Krejci19a96102018-11-15 13:38:09 +0100180struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100181 int size; /**< number of items in the stack */
182 int index; /**< first empty item */
183 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100184};
185
Radek Krejcib56c7502019-02-13 14:19:54 +0100186/**
187 * @brief Add @ref ifftokens into the stack.
188 * @param[in] stack The if-feature stack to use.
189 * @param[in] value One of the @ref ifftokens to store in the stack.
190 * @return LY_EMEM in case of memory allocation error
191 * @return LY_ESUCCESS if the value successfully stored.
192 */
Radek Krejci19a96102018-11-15 13:38:09 +0100193static LY_ERR
194iff_stack_push(struct iff_stack *stack, uint8_t value)
195{
196 if (stack->index == stack->size) {
197 stack->size += 4;
198 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
199 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
200 }
201 stack->stack[stack->index++] = value;
202 return LY_SUCCESS;
203}
204
Radek Krejcib56c7502019-02-13 14:19:54 +0100205/**
206 * @brief Get (and remove) the last item form the stack.
207 * @param[in] stack The if-feature stack to use.
208 * @return The value from the top of the stack.
209 */
Radek Krejci19a96102018-11-15 13:38:09 +0100210static uint8_t
211iff_stack_pop(struct iff_stack *stack)
212{
Radek Krejcib56c7502019-02-13 14:19:54 +0100213 assert(stack && stack->index);
214
Radek Krejci19a96102018-11-15 13:38:09 +0100215 stack->index--;
216 return stack->stack[stack->index];
217}
218
Radek Krejcib56c7502019-02-13 14:19:54 +0100219/**
220 * @brief Clean up the stack.
221 * @param[in] stack The if-feature stack to use.
222 */
Radek Krejci19a96102018-11-15 13:38:09 +0100223static void
224iff_stack_clean(struct iff_stack *stack)
225{
226 stack->size = 0;
227 free(stack->stack);
228}
229
Radek Krejcib56c7502019-02-13 14:19:54 +0100230/**
231 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
232 * (libyang format of the if-feature expression).
233 * @param[in,out] list The 2bits array to modify.
234 * @param[in] op The operand (@ref ifftokens) to store.
235 * @param[in] pos Position (0-based) where to store the given @p op.
236 */
Radek Krejci19a96102018-11-15 13:38:09 +0100237static void
238iff_setop(uint8_t *list, uint8_t op, int pos)
239{
240 uint8_t *item;
241 uint8_t mask = 3;
242
243 assert(pos >= 0);
244 assert(op <= 3); /* max 2 bits */
245
246 item = &list[pos / 4];
247 mask = mask << 2 * (pos % 4);
248 *item = (*item) & ~mask;
249 *item = (*item) | (op << 2 * (pos % 4));
250}
251
Radek Krejcib56c7502019-02-13 14:19:54 +0100252#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
253#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100254
Radek Krejci0af46292019-01-11 16:02:31 +0100255/**
256 * @brief Find a feature of the given name and referenced in the given module.
257 *
258 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
259 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
260 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
261 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
262 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
263 * assigned till that time will be still valid.
264 *
265 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
266 * @param[in] name Name of the feature including possible prefix.
267 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
268 * @return Pointer to the feature structure if found, NULL otherwise.
269 */
Radek Krejci19a96102018-11-15 13:38:09 +0100270static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100271lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100272{
273 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100274 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100275
276 for (i = 0; i < len; ++i) {
277 if (name[i] == ':') {
278 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100279 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100280 LY_CHECK_RET(!mod, NULL);
281
282 name = &name[i + 1];
283 len = len - i - 1;
284 }
285 }
286
287 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100288 if (mod->implemented) {
289 /* module is implemented so there is already the compiled schema */
290 flist = mod->compiled->features;
291 } else {
292 flist = mod->off_features;
293 }
294 LY_ARRAY_FOR(flist, i) {
295 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100296 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
297 return f;
298 }
299 }
300
301 return NULL;
302}
303
304static LY_ERR
305lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
306{
307 const char *name;
308 unsigned int u;
309 const struct lys_module *mod;
310 struct lysp_ext *edef = NULL;
311
312 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
313 ext->insubstmt = ext_p->insubstmt;
314 ext->insubstmt_index = ext_p->insubstmt_index;
315
316 /* get module where the extension definition should be placed */
317 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100318 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100319 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
320 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
321 LY_EVALID);
322 LY_CHECK_ERR_RET(!mod->parsed->extensions,
323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
324 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100325 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100326 LY_EVALID);
327 name = &ext_p->name[u + 1];
328 /* find the extension definition there */
329 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
330 if (!strcmp(name, mod->parsed->extensions[u].name)) {
331 edef = &mod->parsed->extensions[u];
332 break;
333 }
334 }
335 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
336 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
337 LY_EVALID);
338 /* TODO plugins */
339
340 return LY_SUCCESS;
341}
342
Radek Krejcib56c7502019-02-13 14:19:54 +0100343/**
344 * @brief Compile information from the if-feature statement
345 * @param[in] ctx Compile context.
346 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
347 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
348 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
349 * @return LY_ERR value.
350 */
Radek Krejci19a96102018-11-15 13:38:09 +0100351static LY_ERR
352lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
353{
354 const char *c = *value;
355 int r, rc = EXIT_FAILURE;
356 int i, j, last_not, checkversion = 0;
357 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
358 uint8_t op;
359 struct iff_stack stack = {0, 0, NULL};
360 struct lysc_feature *f;
361
362 assert(c);
363
364 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
365 for (i = j = last_not = 0; c[i]; i++) {
366 if (c[i] == '(') {
367 j++;
368 checkversion = 1;
369 continue;
370 } else if (c[i] == ')') {
371 j--;
372 continue;
373 } else if (isspace(c[i])) {
374 checkversion = 1;
375 continue;
376 }
377
378 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
379 if (c[i + r] == '\0') {
380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
381 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
382 return LY_EVALID;
383 } else if (!isspace(c[i + r])) {
384 /* feature name starting with the not/and/or */
385 last_not = 0;
386 f_size++;
387 } else if (c[i] == 'n') { /* not operation */
388 if (last_not) {
389 /* double not */
390 expr_size = expr_size - 2;
391 last_not = 0;
392 } else {
393 last_not = 1;
394 }
395 } else { /* and, or */
396 f_exp++;
397 /* not a not operation */
398 last_not = 0;
399 }
400 i += r;
401 } else {
402 f_size++;
403 last_not = 0;
404 }
405 expr_size++;
406
407 while (!isspace(c[i])) {
408 if (!c[i] || c[i] == ')') {
409 i--;
410 break;
411 }
412 i++;
413 }
414 }
415 if (j || f_exp != f_size) {
416 /* not matching count of ( and ) */
417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
418 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
419 return LY_EVALID;
420 }
421
422 if (checkversion || expr_size > 1) {
423 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100424 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
426 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
427 return LY_EVALID;
428 }
429 }
430
431 /* allocate the memory */
432 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
433 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
434 stack.stack = malloc(expr_size * sizeof *stack.stack);
435 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
436
437 stack.size = expr_size;
438 f_size--; expr_size--; /* used as indexes from now */
439
440 for (i--; i >= 0; i--) {
441 if (c[i] == ')') {
442 /* push it on stack */
443 iff_stack_push(&stack, LYS_IFF_RP);
444 continue;
445 } else if (c[i] == '(') {
446 /* pop from the stack into result all operators until ) */
447 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
448 iff_setop(iff->expr, op, expr_size--);
449 }
450 continue;
451 } else if (isspace(c[i])) {
452 continue;
453 }
454
455 /* end of operator or operand -> find beginning and get what is it */
456 j = i + 1;
457 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
458 i--;
459 }
460 i++; /* go back by one step */
461
462 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
463 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
464 /* double not */
465 iff_stack_pop(&stack);
466 } else {
467 /* not has the highest priority, so do not pop from the stack
468 * as in case of AND and OR */
469 iff_stack_push(&stack, LYS_IFF_NOT);
470 }
471 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
472 /* as for OR - pop from the stack all operators with the same or higher
473 * priority and store them to the result, then push the AND to the stack */
474 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
475 op = iff_stack_pop(&stack);
476 iff_setop(iff->expr, op, expr_size--);
477 }
478 iff_stack_push(&stack, LYS_IFF_AND);
479 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
480 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
481 op = iff_stack_pop(&stack);
482 iff_setop(iff->expr, op, expr_size--);
483 }
484 iff_stack_push(&stack, LYS_IFF_OR);
485 } else {
486 /* feature name, length is j - i */
487
488 /* add it to the expression */
489 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
490
491 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100492 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
493 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
494 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
495 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100496 iff->features[f_size] = f;
497 LY_ARRAY_INCREMENT(iff->features);
498 f_size--;
499 }
500 }
501 while (stack.index) {
502 op = iff_stack_pop(&stack);
503 iff_setop(iff->expr, op, expr_size--);
504 }
505
506 if (++expr_size || ++f_size) {
507 /* not all expected operators and operands found */
508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
509 "Invalid value \"%s\" of if-feature - processing error.", *value);
510 rc = LY_EINT;
511 } else {
512 rc = LY_SUCCESS;
513 }
514
515error:
516 /* cleanup */
517 iff_stack_clean(&stack);
518
519 return rc;
520}
521
Radek Krejcib56c7502019-02-13 14:19:54 +0100522/**
523 * @brief Compile information from the when statement
524 * @param[in] ctx Compile context.
525 * @param[in] when_p The parsed when statement structure.
526 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
527 * @param[out] when Pointer where to store pointer to the created compiled when structure.
528 * @return LY_ERR value.
529 */
Radek Krejci19a96102018-11-15 13:38:09 +0100530static LY_ERR
Radek Krejci00b874b2019-02-12 10:54:50 +0100531lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100532{
533 unsigned int u;
534 LY_ERR ret = LY_SUCCESS;
535
Radek Krejci00b874b2019-02-12 10:54:50 +0100536 *when = calloc(1, sizeof **when);
537 (*when)->refcount = 1;
538 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
539 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
540 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
541 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
542 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, options, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100543
544done:
545 return ret;
546}
547
Radek Krejcib56c7502019-02-13 14:19:54 +0100548/**
549 * @brief Compile information from the must statement
550 * @param[in] ctx Compile context.
551 * @param[in] must_p The parsed must statement structure.
552 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
553 * @param[in,out] must Prepared (empty) compiled must structure to fill.
554 * @return LY_ERR value.
555 */
Radek Krejci19a96102018-11-15 13:38:09 +0100556static LY_ERR
557lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
558{
559 unsigned int u;
560 LY_ERR ret = LY_SUCCESS;
561
562 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
563 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
564
565 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
566 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100567 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
568 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100569 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
570
571done:
572 return ret;
573}
574
Radek Krejcib56c7502019-02-13 14:19:54 +0100575/**
576 * @brief Compile information from the import statement
577 * @param[in] ctx Compile context.
578 * @param[in] imp_p The parsed import statement structure.
579 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
580 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
581 * @return LY_ERR value.
582 */
Radek Krejci19a96102018-11-15 13:38:09 +0100583static LY_ERR
584lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
585{
586 unsigned int u;
587 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100588 LY_ERR ret = LY_SUCCESS;
589
590 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
591 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
592 imp->module = imp_p->module;
593
Radek Krejci7f2a5362018-11-28 13:05:37 +0100594 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
595 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100596 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100597 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 /* try to use filepath if present */
599 if (imp->module->filepath) {
600 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
601 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100602 if (mod != imp->module) {
603 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100605 mod = NULL;
606 }
607 }
608 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100609 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100610 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100611 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100612 return LY_ENOTFOUND;
613 }
614 }
Radek Krejci19a96102018-11-15 13:38:09 +0100615 }
616
617done:
618 return ret;
619}
620
Radek Krejcib56c7502019-02-13 14:19:54 +0100621/**
622 * @brief Compile information from the identity statement
623 *
624 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
625 *
626 * @param[in] ctx Compile context.
627 * @param[in] ident_p The parsed identity statement structure.
628 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
629 * @param[in] idents List of so far compiled identities to check the name uniqueness.
630 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
631 * @return LY_ERR value.
632 */
Radek Krejci19a96102018-11-15 13:38:09 +0100633static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100634lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100635{
636 unsigned int u;
637 LY_ERR ret = LY_SUCCESS;
638
Radek Krejcid05cbd92018-12-05 14:26:40 +0100639 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100640 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200641 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
642 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200643 ident->module = ctx->mod;
Radek Krejci19a96102018-11-15 13:38:09 +0100644 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
645 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
646 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
647 ident->flags = ident_p->flags;
648
649done:
650 return ret;
651}
652
Radek Krejcib56c7502019-02-13 14:19:54 +0100653/**
654 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
655 *
656 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
657 *
658 * @param[in] ctx Compile context for logging.
659 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
660 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
661 * @return LY_SUCCESS if everything is ok.
662 * @return LY_EVALID if the identity is derived from itself.
663 */
Radek Krejci38222632019-02-12 16:55:05 +0100664static LY_ERR
665lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
666{
667 LY_ERR ret = LY_EVALID;
668 unsigned int u, v;
669 struct ly_set recursion = {0};
670 struct lysc_ident *drv;
671
672 if (!derived) {
673 return LY_SUCCESS;
674 }
675
676 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
677 if (ident == derived[u]) {
678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
679 "Identity \"%s\" is indirectly derived from itself.", ident->name);
680 goto cleanup;
681 }
682 ly_set_add(&recursion, derived[u], 0);
683 }
684
685 for (v = 0; v < recursion.count; ++v) {
686 drv = recursion.objs[v];
687 if (!drv->derived) {
688 continue;
689 }
690 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
691 if (ident == drv->derived[u]) {
692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
693 "Identity \"%s\" is indirectly derived from itself.", ident->name);
694 goto cleanup;
695 }
696 ly_set_add(&recursion, drv->derived[u], 0);
697 }
698 }
699 ret = LY_SUCCESS;
700
701cleanup:
702 ly_set_erase(&recursion, NULL);
703 return ret;
704}
705
Radek Krejcia3045382018-11-22 14:30:31 +0100706/**
707 * @brief Find and process the referenced base identities from another identity or identityref
708 *
709 * For bases in identity se backlinks to them from the base identities. For identityref, store
710 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
711 * to distinguish these two use cases.
712 *
713 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
714 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
715 * @param[in] ident Referencing identity to work with.
716 * @param[in] bases Array of bases of identityref to fill in.
717 * @return LY_ERR value.
718 */
Radek Krejci19a96102018-11-15 13:38:09 +0100719static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100720lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +0100721{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100722 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100723 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100724 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100725 struct lysc_ident **idref;
726
727 assert(ident || bases);
728
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
731 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
732 return LY_EVALID;
733 }
734
735 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
736 s = strchr(bases_p[u], ':');
737 if (s) {
738 /* prefixed identity */
739 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100740 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100741 } else {
742 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100743 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100744 }
745 if (!mod) {
746 if (ident) {
747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
748 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
749 } else {
750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
751 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
752 }
753 return LY_EVALID;
754 }
755 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100756 if (mod->compiled && mod->compiled->identities) {
757 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
758 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100759 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100760 if (ident == &mod->compiled->identities[v]) {
761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
762 "Identity \"%s\" is derived from itself.", ident->name);
763 return LY_EVALID;
764 }
765 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100766 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100767 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100768 *idref = ident;
769 } else {
770 /* we have match! store the found identity */
771 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100772 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100773 }
774 break;
775 }
776 }
777 }
778 if (!idref || !(*idref)) {
779 if (ident) {
780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
781 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
782 } else {
783 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
784 "Unable to find base (%s) of identityref.", bases_p[u]);
785 }
786 return LY_EVALID;
787 }
788 }
789 return LY_SUCCESS;
790}
791
Radek Krejcia3045382018-11-22 14:30:31 +0100792/**
793 * @brief For the given array of identities, set the backlinks from all their base identities.
794 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
795 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
796 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
797 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
798 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100799static LY_ERR
800lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
801{
802 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100803
804 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
805 if (!idents_p[i].bases) {
806 continue;
807 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100808 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100809 }
810 return LY_SUCCESS;
811}
812
Radek Krejci0af46292019-01-11 16:02:31 +0100813LY_ERR
Radek Krejci693262f2019-04-29 15:23:20 +0200814lys_feature_precompile(struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +0100815{
816 unsigned int offset = 0, u;
817 struct lysc_ctx context = {0};
818
819 assert(ctx);
820 context.ctx = ctx;
821
822 if (!features_p) {
823 return LY_SUCCESS;
824 }
825 if (*features) {
826 offset = LY_ARRAY_SIZE(*features);
827 }
828
829 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
830 LY_ARRAY_FOR(features_p, u) {
831 LY_ARRAY_INCREMENT(*features);
832 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
833 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
834 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
835 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
836 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci693262f2019-04-29 15:23:20 +0200837 (*features)[offset + u].module = module;
Radek Krejci0af46292019-01-11 16:02:31 +0100838 }
839
840 return LY_SUCCESS;
841}
842
Radek Krejcia3045382018-11-22 14:30:31 +0100843/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100844 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100845 *
846 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
847 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100848 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100849 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
850 * being currently processed).
851 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +0100852 * @return LY_SUCCESS if everything is ok.
853 * @return LY_EVALID if the feature references indirectly itself.
854 */
855static LY_ERR
856lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
857{
858 LY_ERR ret = LY_EVALID;
859 unsigned int u, v;
860 struct ly_set recursion = {0};
861 struct lysc_feature *drv;
862
863 if (!depfeatures) {
864 return LY_SUCCESS;
865 }
866
867 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
868 if (feature == depfeatures[u]) {
869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
870 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
871 goto cleanup;
872 }
873 ly_set_add(&recursion, depfeatures[u], 0);
874 }
875
876 for (v = 0; v < recursion.count; ++v) {
877 drv = recursion.objs[v];
878 if (!drv->depfeatures) {
879 continue;
880 }
881 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
882 if (feature == drv->depfeatures[u]) {
883 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
884 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
885 goto cleanup;
886 }
887 ly_set_add(&recursion, drv->depfeatures[u], 0);
888 }
889 }
890 ret = LY_SUCCESS;
891
892cleanup:
893 ly_set_erase(&recursion, NULL);
894 return ret;
895}
896
897/**
Radek Krejci0af46292019-01-11 16:02:31 +0100898 * @brief Create pre-compiled features array.
899 *
900 * See lys_feature_precompile() for more details.
901 *
Radek Krejcia3045382018-11-22 14:30:31 +0100902 * @param[in] ctx Compile context.
903 * @param[in] feature_p Parsed feature definition to compile.
904 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci0af46292019-01-11 16:02:31 +0100905 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100906 * @return LY_ERR value.
907 */
Radek Krejci19a96102018-11-15 13:38:09 +0100908static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100909lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +0100910{
Radek Krejci0af46292019-01-11 16:02:31 +0100911 unsigned int u, v, x;
912 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100913 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100914
Radek Krejci0af46292019-01-11 16:02:31 +0100915 /* find the preprecompiled feature */
916 LY_ARRAY_FOR(features, x) {
917 if (strcmp(features[x].name, feature_p->name)) {
918 continue;
919 }
920 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100921
Radek Krejci0af46292019-01-11 16:02:31 +0100922 /* finish compilation started in lys_feature_precompile() */
923 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
924 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
925 if (feature->iffeatures) {
926 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
927 if (feature->iffeatures[u].features) {
928 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100929 /* check for circular dependency - direct reference first,... */
930 if (feature == feature->iffeatures[u].features[v]) {
931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
932 "Feature \"%s\" is referenced from itself.", feature->name);
933 return LY_EVALID;
934 }
935 /* ... and indirect circular reference */
936 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
937
Radek Krejci0af46292019-01-11 16:02:31 +0100938 /* add itself into the dependants list */
939 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
940 *df = feature;
941 }
Radek Krejci19a96102018-11-15 13:38:09 +0100942 }
Radek Krejci19a96102018-11-15 13:38:09 +0100943 }
944 }
Radek Krejci0af46292019-01-11 16:02:31 +0100945 done:
946 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100947 }
Radek Krejci0af46292019-01-11 16:02:31 +0100948
949 LOGINT(ctx->ctx);
950 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100951}
952
Radek Krejcib56c7502019-02-13 14:19:54 +0100953/**
954 * @brief Revert compiled list of features back to the precompiled state.
955 *
956 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
957 * The features are supposed to be stored again as off_features in ::lys_module structure.
958 *
959 * @param[in] ctx Compilation context.
960 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
961 * and supposed to hold the off_features list.
962 */
Radek Krejci95710c92019-02-11 15:49:55 +0100963static void
964lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
965{
966 unsigned int u, v;
967
968 /* keep the off_features list until the complete lys_module is freed */
969 mod->off_features = mod->compiled->features;
970 mod->compiled->features = NULL;
971
972 /* in the off_features list, remove all the parts (from finished compiling process)
973 * which may points into the data being freed here */
974 LY_ARRAY_FOR(mod->off_features, u) {
975 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
976 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
977 }
978 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
979 mod->off_features[u].iffeatures = NULL;
980
981 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
982 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
983 }
984 LY_ARRAY_FREE(mod->off_features[u].exts);
985 mod->off_features[u].exts = NULL;
986 }
987}
988
Radek Krejcia3045382018-11-22 14:30:31 +0100989/**
990 * @brief Validate and normalize numeric value from a range definition.
991 * @param[in] ctx Compile context.
992 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
993 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
994 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
995 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
996 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
997 * @param[in] value String value of the range boundary.
998 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
999 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1000 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1001 */
Radek Krejci19a96102018-11-15 13:38:09 +01001002static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001003range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001004{
Radek Krejci6cba4292018-11-15 17:33:29 +01001005 size_t fraction = 0, size;
1006
Radek Krejci19a96102018-11-15 13:38:09 +01001007 *len = 0;
1008
1009 assert(value);
1010 /* parse value */
1011 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1012 return LY_EVALID;
1013 }
1014
1015 if ((value[*len] == '-') || (value[*len] == '+')) {
1016 ++(*len);
1017 }
1018
1019 while (isdigit(value[*len])) {
1020 ++(*len);
1021 }
1022
1023 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001024 if (basetype == LY_TYPE_DEC64) {
1025 goto decimal;
1026 } else {
1027 *valcopy = strndup(value, *len);
1028 return LY_SUCCESS;
1029 }
Radek Krejci19a96102018-11-15 13:38:09 +01001030 }
1031 fraction = *len;
1032
1033 ++(*len);
1034 while (isdigit(value[*len])) {
1035 ++(*len);
1036 }
1037
Radek Krejci6cba4292018-11-15 17:33:29 +01001038 if (basetype == LY_TYPE_DEC64) {
1039decimal:
1040 assert(frdigits);
1041 if (*len - 1 - fraction > frdigits) {
1042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1043 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1044 *len, value, frdigits);
1045 return LY_EINVAL;
1046 }
1047 if (fraction) {
1048 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1049 } else {
1050 size = (*len) + frdigits + 1;
1051 }
1052 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001053 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1054
Radek Krejci6cba4292018-11-15 17:33:29 +01001055 (*valcopy)[size - 1] = '\0';
1056 if (fraction) {
1057 memcpy(&(*valcopy)[0], &value[0], fraction);
1058 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1059 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1060 } else {
1061 memcpy(&(*valcopy)[0], &value[0], *len);
1062 memset(&(*valcopy)[*len], '0', frdigits);
1063 }
Radek Krejci19a96102018-11-15 13:38:09 +01001064 }
1065 return LY_SUCCESS;
1066}
1067
Radek Krejcia3045382018-11-22 14:30:31 +01001068/**
1069 * @brief Check that values in range are in ascendant order.
1070 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001071 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1072 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001073 * @param[in] value Current value to check.
1074 * @param[in] prev_value The last seen value.
1075 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1076 */
Radek Krejci19a96102018-11-15 13:38:09 +01001077static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001078range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001079{
1080 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001081 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001082 return LY_EEXIST;
1083 }
1084 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001085 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001086 return LY_EEXIST;
1087 }
1088 }
1089 return LY_SUCCESS;
1090}
1091
Radek Krejcia3045382018-11-22 14:30:31 +01001092/**
1093 * @brief Set min/max value of the range part.
1094 * @param[in] ctx Compile context.
1095 * @param[in] part Range part structure to fill.
1096 * @param[in] max Flag to distinguish if storing min or max value.
1097 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1098 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1099 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1100 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1101 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001102 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001103 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1104 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1105 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1106 * frdigits value), LY_EMEM.
1107 */
Radek Krejci19a96102018-11-15 13:38:09 +01001108static LY_ERR
1109range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001110 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001111{
1112 LY_ERR ret = LY_SUCCESS;
1113 char *valcopy = NULL;
1114 size_t len;
1115
1116 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001117 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001118 LY_CHECK_GOTO(ret, finalize);
1119 }
1120 if (!valcopy && base_range) {
1121 if (max) {
1122 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1123 } else {
1124 part->min_64 = base_range->parts[0].min_64;
1125 }
1126 if (!first) {
1127 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1128 }
1129 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001130 }
1131
1132 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001133 case LY_TYPE_INT8: /* range */
1134 if (valcopy) {
1135 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1136 } else if (max) {
1137 part->max_64 = INT64_C(127);
1138 } else {
1139 part->min_64 = INT64_C(-128);
1140 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001141 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001142 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001143 }
1144 break;
1145 case LY_TYPE_INT16: /* range */
1146 if (valcopy) {
1147 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1148 } else if (max) {
1149 part->max_64 = INT64_C(32767);
1150 } else {
1151 part->min_64 = INT64_C(-32768);
1152 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001153 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001154 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001155 }
1156 break;
1157 case LY_TYPE_INT32: /* range */
1158 if (valcopy) {
1159 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1160 } else if (max) {
1161 part->max_64 = INT64_C(2147483647);
1162 } else {
1163 part->min_64 = INT64_C(-2147483648);
1164 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001165 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001166 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001167 }
1168 break;
1169 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001170 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001171 if (valcopy) {
1172 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1173 max ? &part->max_64 : &part->min_64);
1174 } else if (max) {
1175 part->max_64 = INT64_C(9223372036854775807);
1176 } else {
1177 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1178 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001179 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001180 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001181 }
1182 break;
1183 case LY_TYPE_UINT8: /* range */
1184 if (valcopy) {
1185 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1186 } else if (max) {
1187 part->max_u64 = UINT64_C(255);
1188 } else {
1189 part->min_u64 = UINT64_C(0);
1190 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001191 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001192 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001193 }
1194 break;
1195 case LY_TYPE_UINT16: /* range */
1196 if (valcopy) {
1197 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1198 } else if (max) {
1199 part->max_u64 = UINT64_C(65535);
1200 } else {
1201 part->min_u64 = UINT64_C(0);
1202 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001203 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001204 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001205 }
1206 break;
1207 case LY_TYPE_UINT32: /* range */
1208 if (valcopy) {
1209 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1210 } else if (max) {
1211 part->max_u64 = UINT64_C(4294967295);
1212 } else {
1213 part->min_u64 = UINT64_C(0);
1214 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001215 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001216 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001217 }
1218 break;
1219 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001220 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001221 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001222 if (valcopy) {
1223 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1224 } else if (max) {
1225 part->max_u64 = UINT64_C(18446744073709551615);
1226 } else {
1227 part->min_u64 = UINT64_C(0);
1228 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001229 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001230 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001231 }
1232 break;
1233 default:
1234 LOGINT(ctx->ctx);
1235 ret = LY_EINT;
1236 }
1237
Radek Krejci5969f272018-11-23 10:03:58 +01001238finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001239 if (ret == LY_EDENIED) {
1240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1241 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1242 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1243 } else if (ret == LY_EVALID) {
1244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1245 "Invalid %s restriction - invalid value \"%s\".",
1246 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1247 } else if (ret == LY_EEXIST) {
1248 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1249 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001250 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001251 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001252 } else if (!ret && value) {
1253 *value = *value + len;
1254 }
1255 free(valcopy);
1256 return ret;
1257}
1258
Radek Krejcia3045382018-11-22 14:30:31 +01001259/**
1260 * @brief Compile the parsed range restriction.
1261 * @param[in] ctx Compile context.
1262 * @param[in] range_p Parsed range structure to compile.
1263 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1264 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1265 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1266 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1267 * range restriction must be more restrictive than the base_range.
1268 * @param[in,out] range Pointer to the created current range structure.
1269 * @return LY_ERR value.
1270 */
Radek Krejci19a96102018-11-15 13:38:09 +01001271static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001272lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001273 struct lysc_range *base_range, struct lysc_range **range)
1274{
1275 LY_ERR ret = LY_EVALID;
1276 const char *expr;
1277 struct lysc_range_part *parts = NULL, *part;
1278 int range_expected = 0, uns;
1279 unsigned int parts_done = 0, u, v;
1280
1281 assert(range);
1282 assert(range_p);
1283
1284 expr = range_p->arg;
1285 while(1) {
1286 if (isspace(*expr)) {
1287 ++expr;
1288 } else if (*expr == '\0') {
1289 if (range_expected) {
1290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1291 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1292 length_restr ? "length" : "range", range_p->arg);
1293 goto cleanup;
1294 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1296 "Invalid %s restriction - unexpected end of the expression (%s).",
1297 length_restr ? "length" : "range", range_p->arg);
1298 goto cleanup;
1299 }
1300 parts_done++;
1301 break;
1302 } else if (!strncmp(expr, "min", 3)) {
1303 if (parts) {
1304 /* min cannot be used elsewhere than in the first part */
1305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1306 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1307 expr - range_p->arg, range_p->arg);
1308 goto cleanup;
1309 }
1310 expr += 3;
1311
1312 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001313 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001314 part->max_64 = part->min_64;
1315 } else if (*expr == '|') {
1316 if (!parts || range_expected) {
1317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1318 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1319 goto cleanup;
1320 }
1321 expr++;
1322 parts_done++;
1323 /* process next part of the expression */
1324 } else if (!strncmp(expr, "..", 2)) {
1325 expr += 2;
1326 while (isspace(*expr)) {
1327 expr++;
1328 }
1329 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1331 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1332 goto cleanup;
1333 }
1334 /* continue expecting the upper boundary */
1335 range_expected = 1;
1336 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1337 /* number */
1338 if (range_expected) {
1339 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001340 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001341 range_expected = 0;
1342 } else {
1343 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1344 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001345 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001346 part->max_64 = part->min_64;
1347 }
1348
1349 /* continue with possible another expression part */
1350 } else if (!strncmp(expr, "max", 3)) {
1351 expr += 3;
1352 while (isspace(*expr)) {
1353 expr++;
1354 }
1355 if (*expr != '\0') {
1356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1357 length_restr ? "length" : "range", expr);
1358 goto cleanup;
1359 }
1360 if (range_expected) {
1361 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001362 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001363 range_expected = 0;
1364 } else {
1365 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1366 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001367 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001368 part->min_64 = part->max_64;
1369 }
1370 } else {
1371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1372 length_restr ? "length" : "range", expr);
1373 goto cleanup;
1374 }
1375 }
1376
1377 /* check with the previous range/length restriction */
1378 if (base_range) {
1379 switch (basetype) {
1380 case LY_TYPE_BINARY:
1381 case LY_TYPE_UINT8:
1382 case LY_TYPE_UINT16:
1383 case LY_TYPE_UINT32:
1384 case LY_TYPE_UINT64:
1385 case LY_TYPE_STRING:
1386 uns = 1;
1387 break;
1388 case LY_TYPE_DEC64:
1389 case LY_TYPE_INT8:
1390 case LY_TYPE_INT16:
1391 case LY_TYPE_INT32:
1392 case LY_TYPE_INT64:
1393 uns = 0;
1394 break;
1395 default:
1396 LOGINT(ctx->ctx);
1397 ret = LY_EINT;
1398 goto cleanup;
1399 }
1400 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1401 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1402 goto baseerror;
1403 }
1404 /* current lower bound is not lower than the base */
1405 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1406 /* base has single value */
1407 if (base_range->parts[v].min_64 == parts[u].min_64) {
1408 /* both lower bounds are the same */
1409 if (parts[u].min_64 != parts[u].max_64) {
1410 /* current continues with a range */
1411 goto baseerror;
1412 } else {
1413 /* equal single values, move both forward */
1414 ++v;
1415 continue;
1416 }
1417 } else {
1418 /* base is single value lower than current range, so the
1419 * value from base range is removed in the current,
1420 * move only base and repeat checking */
1421 ++v;
1422 --u;
1423 continue;
1424 }
1425 } else {
1426 /* base is the range */
1427 if (parts[u].min_64 == parts[u].max_64) {
1428 /* current is a single value */
1429 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1430 /* current is behind the base range, so base range is omitted,
1431 * move the base and keep the current for further check */
1432 ++v;
1433 --u;
1434 } /* else it is within the base range, so move the current, but keep the base */
1435 continue;
1436 } else {
1437 /* both are ranges - check the higher bound, the lower was already checked */
1438 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1439 /* higher bound is higher than the current higher bound */
1440 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1441 /* but the current lower bound is also higher, so the base range is omitted,
1442 * continue with the same current, but move the base */
1443 --u;
1444 ++v;
1445 continue;
1446 }
1447 /* current range starts within the base range but end behind it */
1448 goto baseerror;
1449 } else {
1450 /* current range is smaller than the base,
1451 * move current, but stay with the base */
1452 continue;
1453 }
1454 }
1455 }
1456 }
1457 if (u != parts_done) {
1458baseerror:
1459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1460 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1461 length_restr ? "length" : "range", range_p->arg);
1462 goto cleanup;
1463 }
1464 }
1465
1466 if (!(*range)) {
1467 *range = calloc(1, sizeof **range);
1468 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1469 }
1470
Radek Krejcic8b31002019-01-08 10:24:45 +01001471 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001472 if (range_p->eapptag) {
1473 lydict_remove(ctx->ctx, (*range)->eapptag);
1474 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1475 }
1476 if (range_p->emsg) {
1477 lydict_remove(ctx->ctx, (*range)->emsg);
1478 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1479 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001480 if (range_p->dsc) {
1481 lydict_remove(ctx->ctx, (*range)->dsc);
1482 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1483 }
1484 if (range_p->ref) {
1485 lydict_remove(ctx->ctx, (*range)->ref);
1486 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1487 }
Radek Krejci19a96102018-11-15 13:38:09 +01001488 /* extensions are taken only from the last range by the caller */
1489
1490 (*range)->parts = parts;
1491 parts = NULL;
1492 ret = LY_SUCCESS;
1493cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001494 LY_ARRAY_FREE(parts);
1495
1496 return ret;
1497}
1498
1499/**
1500 * @brief Checks pattern syntax.
1501 *
Radek Krejcia3045382018-11-22 14:30:31 +01001502 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001503 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001504 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Radek Krejcia3045382018-11-22 14:30:31 +01001505 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001506 */
1507static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001508lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001509{
Radek Krejci54579462019-04-30 12:47:06 +02001510 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001511 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001512 int err_code;
1513 const char *orig_ptr;
1514 PCRE2_SIZE err_offset;
1515 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001516#define URANGE_LEN 19
1517 char *ublock2urange[][2] = {
1518 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1519 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1520 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1521 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1522 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1523 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1524 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1525 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1526 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1527 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1528 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1529 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1530 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1531 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1532 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1533 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1534 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1535 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1536 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1537 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1538 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1539 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1540 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1541 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1542 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1543 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1544 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1545 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1546 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1547 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1548 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1549 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1550 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1551 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1552 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1553 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1554 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1555 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1556 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1557 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1558 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1559 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1560 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1561 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1562 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1563 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1564 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1565 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1566 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1567 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1568 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1569 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1570 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1571 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1572 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1573 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1574 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1575 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1576 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1577 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1578 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1579 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1580 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1581 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1582 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1583 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1584 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1585 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1586 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1587 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1588 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1589 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1590 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1591 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1592 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1593 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1594 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1595 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1596 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1597 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1598 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1599 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1600 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1601 {NULL, NULL}
1602 };
1603
1604 /* adjust the expression to a Perl equivalent
1605 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1606
1607 /* we need to replace all "$" with "\$", count them now */
1608 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1609
1610 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1611 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1612 perl_regex[0] = '\0';
1613
1614 ptr = perl_regex;
1615
1616 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1617 /* we will add line-end anchoring */
1618 ptr[0] = '(';
1619 ++ptr;
1620 }
1621
1622 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1623 if (orig_ptr[0] == '$') {
1624 ptr += sprintf(ptr, "\\$");
1625 } else if (orig_ptr[0] == '^') {
1626 ptr += sprintf(ptr, "\\^");
1627 } else {
1628 ptr[0] = orig_ptr[0];
1629 ++ptr;
1630 }
1631 }
1632
1633 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1634 ptr += sprintf(ptr, ")$");
1635 } else {
1636 ptr[0] = '\0';
1637 ++ptr;
1638 }
1639
1640 /* substitute Unicode Character Blocks with exact Character Ranges */
1641 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1642 start = ptr - perl_regex;
1643
1644 ptr = strchr(ptr, '}');
1645 if (!ptr) {
1646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1647 pattern, perl_regex + start + 2, "unterminated character property");
1648 free(perl_regex);
1649 return LY_EVALID;
1650 }
1651 end = (ptr - perl_regex) + 1;
1652
1653 /* need more space */
1654 if (end - start < URANGE_LEN) {
1655 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1656 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1657 }
1658
1659 /* find our range */
1660 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1661 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1662 break;
1663 }
1664 }
1665 if (!ublock2urange[idx][0]) {
1666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1667 pattern, perl_regex + start + 5, "unknown block name");
1668 free(perl_regex);
1669 return LY_EVALID;
1670 }
1671
1672 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1673 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1674 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1675 ++count;
1676 }
1677 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1678 --count;
1679 }
1680 }
1681 if (count) {
1682 /* skip brackets */
1683 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1684 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1685 } else {
1686 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1687 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1688 }
1689 }
1690
1691 /* must return 0, already checked during parsing */
Radek Krejci54579462019-04-30 12:47:06 +02001692 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
1693 &err_code, &err_offset, NULL);
1694 if (!code_local) {
1695 PCRE2_UCHAR err_msg[256] = {0};
1696 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1698 free(perl_regex);
1699 return LY_EVALID;
1700 }
1701 free(perl_regex);
1702
Radek Krejci54579462019-04-30 12:47:06 +02001703 if (code) {
1704 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001705 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001706 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001707 }
1708
1709 return LY_SUCCESS;
1710
1711#undef URANGE_LEN
1712}
1713
Radek Krejcia3045382018-11-22 14:30:31 +01001714/**
1715 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1716 * @param[in] ctx Compile context.
1717 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1718 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1719 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1720 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1721 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1722 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1723 */
Radek Krejci19a96102018-11-15 13:38:09 +01001724static LY_ERR
1725lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1726 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1727{
1728 struct lysc_pattern **pattern;
1729 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001730 LY_ERR ret = LY_SUCCESS;
1731
1732 /* first, copy the patterns from the base type */
1733 if (base_patterns) {
1734 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1735 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1736 }
1737
1738 LY_ARRAY_FOR(patterns_p, u) {
1739 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1740 *pattern = calloc(1, sizeof **pattern);
1741 ++(*pattern)->refcount;
1742
Radek Krejci54579462019-04-30 12:47:06 +02001743 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001744 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001745
1746 if (patterns_p[u].arg[0] == 0x15) {
1747 (*pattern)->inverted = 1;
1748 }
Radek Krejci54579462019-04-30 12:47:06 +02001749 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001750 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1751 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001752 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1753 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001754 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1755 options, v, lys_compile_ext, ret, done);
1756 }
1757done:
1758 return ret;
1759}
1760
Radek Krejcia3045382018-11-22 14:30:31 +01001761/**
1762 * @brief map of the possible restrictions combination for the specific built-in type.
1763 */
Radek Krejci19a96102018-11-15 13:38:09 +01001764static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1765 0 /* LY_TYPE_UNKNOWN */,
1766 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001767 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1768 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1769 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1770 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1771 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001772 LYS_SET_BIT /* LY_TYPE_BITS */,
1773 0 /* LY_TYPE_BOOL */,
1774 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1775 0 /* LY_TYPE_EMPTY */,
1776 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1777 LYS_SET_BASE /* LY_TYPE_IDENT */,
1778 LYS_SET_REQINST /* LY_TYPE_INST */,
1779 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001780 LYS_SET_TYPE /* LY_TYPE_UNION */,
1781 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001782 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001783 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001784 LYS_SET_RANGE /* LY_TYPE_INT64 */
1785};
1786
1787/**
1788 * @brief stringification of the YANG built-in data types
1789 */
1790const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1791 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1792 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001793};
1794
Radek Krejcia3045382018-11-22 14:30:31 +01001795/**
1796 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1797 * @param[in] ctx Compile context.
1798 * @param[in] enums_p Array of the parsed enum structures to compile.
1799 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1800 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1801 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1802 * @param[out] enums Newly created array of the compiled enums information for the current type.
1803 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1804 */
Radek Krejci19a96102018-11-15 13:38:09 +01001805static LY_ERR
1806lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
Radek Krejci693262f2019-04-29 15:23:20 +02001807 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01001808{
1809 LY_ERR ret = LY_SUCCESS;
1810 unsigned int u, v, match;
1811 int32_t value = 0;
1812 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001813 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01001814
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001815 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001816 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1817 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1818 return LY_EVALID;
1819 }
1820
1821 LY_ARRAY_FOR(enums_p, u) {
1822 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1823 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001824 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1825 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02001826 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01001827 if (base_enums) {
1828 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1829 LY_ARRAY_FOR(base_enums, v) {
1830 if (!strcmp(e->name, base_enums[v].name)) {
1831 break;
1832 }
1833 }
1834 if (v == LY_ARRAY_SIZE(base_enums)) {
1835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1836 "Invalid %s - derived type adds new item \"%s\".",
1837 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1838 return LY_EVALID;
1839 }
1840 match = v;
1841 }
1842
1843 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02001844 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01001845 if (enums_p[u].flags & LYS_SET_VALUE) {
1846 e->value = (int32_t)enums_p[u].value;
1847 if (!u || e->value >= value) {
1848 value = e->value + 1;
1849 }
1850 /* check collision with other values */
1851 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1852 if (e->value == (*enums)[v].value) {
1853 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1854 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1855 e->value, e->name, (*enums)[v].name);
1856 return LY_EVALID;
1857 }
1858 }
1859 } else if (base_enums) {
1860 /* inherit the assigned value */
1861 e->value = base_enums[match].value;
1862 if (!u || e->value >= value) {
1863 value = e->value + 1;
1864 }
1865 } else {
1866 /* assign value automatically */
1867 if (u && value == INT32_MIN) {
1868 /* counter overflow */
1869 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1870 "Invalid enumeration - it is not possible to auto-assign enum value for "
1871 "\"%s\" since the highest value is already 2147483647.", e->name);
1872 return LY_EVALID;
1873 }
1874 e->value = value++;
1875 }
1876 } else { /* LY_TYPE_BITS */
1877 if (enums_p[u].flags & LYS_SET_VALUE) {
1878 e->value = (int32_t)enums_p[u].value;
1879 if (!u || (uint32_t)e->value >= position) {
1880 position = (uint32_t)e->value + 1;
1881 }
1882 /* check collision with other values */
1883 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1884 if (e->value == (*enums)[v].value) {
1885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1886 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1887 (uint32_t)e->value, e->name, (*enums)[v].name);
1888 return LY_EVALID;
1889 }
1890 }
1891 } else if (base_enums) {
1892 /* inherit the assigned value */
1893 e->value = base_enums[match].value;
1894 if (!u || (uint32_t)e->value >= position) {
1895 position = (uint32_t)e->value + 1;
1896 }
1897 } else {
1898 /* assign value automatically */
1899 if (u && position == 0) {
1900 /* counter overflow */
1901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1902 "Invalid bits - it is not possible to auto-assign bit position for "
1903 "\"%s\" since the highest value is already 4294967295.", e->name);
1904 return LY_EVALID;
1905 }
1906 e->value = position++;
1907 }
1908 }
1909
1910 if (base_enums) {
1911 /* the assigned values must not change from the derived type */
1912 if (e->value != base_enums[match].value) {
1913 if (basetype == LY_TYPE_ENUM) {
1914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1915 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1916 e->name, base_enums[match].value, e->value);
1917 } else {
1918 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1919 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1920 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1921 }
1922 return LY_EVALID;
1923 }
1924 }
1925
1926 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1927 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1928
1929 if (basetype == LY_TYPE_BITS) {
1930 /* keep bits ordered by position */
1931 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1932 if (v != u) {
1933 memcpy(&storage, e, sizeof *e);
1934 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1935 memcpy(&(*enums)[v], &storage, sizeof storage);
1936 }
1937 }
1938 }
1939
1940done:
1941 return ret;
1942}
1943
Radek Krejcia3045382018-11-22 14:30:31 +01001944#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1945 for ((NODE) = (NODE)->parent; \
1946 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1947 (NODE) = (NODE)->parent); \
1948 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1950 TERM; \
1951 }
1952
1953/**
1954 * @brief Validate the predicate(s) from the leafref path.
1955 * @param[in] ctx Compile context
1956 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1957 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001958 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001959 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001960 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001961 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1962 */
1963static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001964lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001965 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001966{
1967 LY_ERR ret = LY_EVALID;
1968 const struct lys_module *mod;
1969 const struct lysc_node *src_node, *dst_node;
1970 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1971 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001972 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001973 const char *start, *end, *pke_end;
1974 struct ly_set keys = {0};
1975 int i;
1976
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001977 assert(path_context);
1978
Radek Krejcia3045382018-11-22 14:30:31 +01001979 while (**predicate == '[') {
1980 start = (*predicate)++;
1981
1982 while (isspace(**predicate)) {
1983 ++(*predicate);
1984 }
1985 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1986 while (isspace(**predicate)) {
1987 ++(*predicate);
1988 }
1989 if (**predicate != '=') {
1990 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001991 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1992 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001993 goto cleanup;
1994 }
1995 ++(*predicate);
1996 while (isspace(**predicate)) {
1997 ++(*predicate);
1998 }
1999
2000 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2002 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2003 goto cleanup;
2004 }
2005 --pke_end;
2006 while (isspace(*pke_end)) {
2007 --pke_end;
2008 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002009 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002010 /* localize path-key-expr */
2011 pke_start = path_key_expr = *predicate;
2012 /* move after the current predicate */
2013 *predicate = end + 1;
2014
2015 /* source (must be leaf or leaf-list) */
2016 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002017 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002018 if (!mod) {
2019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2020 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002021 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002022 goto cleanup;
2023 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002024 if (!mod->implemented) {
2025 /* make the module implemented */
2026 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2027 }
Radek Krejcia3045382018-11-22 14:30:31 +01002028 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002029 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002030 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002031 src_node = NULL;
2032 if (context_node->keys) {
2033 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2034 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2035 src_node = (const struct lysc_node*)context_node->keys[u];
2036 break;
2037 }
2038 }
2039 }
Radek Krejcia3045382018-11-22 14:30:31 +01002040 if (!src_node) {
2041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002042 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002043 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002044 goto cleanup;
2045 }
Radek Krejcia3045382018-11-22 14:30:31 +01002046
2047 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002048 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002049 i = ly_set_add(&keys, (void*)src_node, 0);
2050 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002051 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002053 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002054 *predicate - start, start, src_node->name);
2055 goto cleanup;
2056 }
2057
2058 /* destination */
2059 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002060 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002061
2062 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2063 if (strncmp(path_key_expr, "current()", 9)) {
2064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2065 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2066 *predicate - start, start);
2067 goto cleanup;
2068 }
2069 path_key_expr += 9;
2070 while (isspace(*path_key_expr)) {
2071 ++path_key_expr;
2072 }
2073
2074 if (*path_key_expr != '/') {
2075 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2076 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2077 *predicate - start, start);
2078 goto cleanup;
2079 }
2080 ++path_key_expr;
2081 while (isspace(*path_key_expr)) {
2082 ++path_key_expr;
2083 }
2084
2085 /* rel-path-keyexpr:
2086 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2087 while (!strncmp(path_key_expr, "..", 2)) {
2088 ++dest_parent_times;
2089 path_key_expr += 2;
2090 while (isspace(*path_key_expr)) {
2091 ++path_key_expr;
2092 }
2093 if (*path_key_expr != '/') {
2094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2095 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2096 *predicate - start, start);
2097 goto cleanup;
2098 }
2099 ++path_key_expr;
2100 while (isspace(*path_key_expr)) {
2101 ++path_key_expr;
2102 }
2103
2104 /* path is supposed to be evaluated in data tree, so we have to skip
2105 * all schema nodes that cannot be instantiated in data tree */
2106 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2107 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2108 *predicate - start, start);
2109 }
2110 if (!dest_parent_times) {
2111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2112 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2113 *predicate - start, start);
2114 goto cleanup;
2115 }
2116 if (path_key_expr == pke_end) {
2117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2118 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2119 *predicate - start, start);
2120 goto cleanup;
2121 }
2122
2123 while(path_key_expr != pke_end) {
2124 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002126 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2127 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002128 goto cleanup;
2129 }
2130
2131 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002132 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002133 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002134 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002135 }
2136 if (!mod) {
2137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002138 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002139 *predicate - start, start, dst_len, dst);
2140 goto cleanup;
2141 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002142 if (!mod->implemented) {
2143 /* make the module implemented */
2144 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2145 }
Radek Krejcia3045382018-11-22 14:30:31 +01002146
2147 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2148 if (!dst_node) {
2149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002150 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002151 *predicate - start, start, path_key_expr - pke_start, pke_start);
2152 goto cleanup;
2153 }
2154 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002155 if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcia3045382018-11-22 14:30:31 +01002156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002157 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002158 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2159 goto cleanup;
2160 }
2161 }
2162
2163 ret = LY_SUCCESS;
2164cleanup:
2165 ly_set_erase(&keys, NULL);
2166 return ret;
2167}
2168
2169/**
2170 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2171 *
2172 * path-arg = absolute-path / relative-path
2173 * absolute-path = 1*("/" (node-identifier *path-predicate))
2174 * relative-path = 1*(".." "/") descendant-path
2175 *
2176 * @param[in,out] path Path to parse.
2177 * @param[out] prefix Prefix of the token, NULL if there is not any.
2178 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2179 * @param[out] name Name of the token.
2180 * @param[out] nam_len Length of the name.
2181 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2182 * must not be changed between consecutive calls. -1 if the
2183 * path is absolute.
2184 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2185 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2186 */
2187static LY_ERR
2188lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2189 int *parent_times, int *has_predicate)
2190{
2191 int par_times = 0;
2192
2193 assert(path && *path);
2194 assert(parent_times);
2195 assert(prefix);
2196 assert(prefix_len);
2197 assert(name);
2198 assert(name_len);
2199 assert(has_predicate);
2200
2201 *prefix = NULL;
2202 *prefix_len = 0;
2203 *name = NULL;
2204 *name_len = 0;
2205 *has_predicate = 0;
2206
2207 if (!*parent_times) {
2208 if (!strncmp(*path, "..", 2)) {
2209 *path += 2;
2210 ++par_times;
2211 while (!strncmp(*path, "/..", 3)) {
2212 *path += 3;
2213 ++par_times;
2214 }
2215 }
2216 if (par_times) {
2217 *parent_times = par_times;
2218 } else {
2219 *parent_times = -1;
2220 }
2221 }
2222
2223 if (**path != '/') {
2224 return LY_EINVAL;
2225 }
2226 /* skip '/' */
2227 ++(*path);
2228
2229 /* node-identifier ([prefix:]name) */
2230 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2231
2232 if ((**path == '/' && (*path)[1]) || !**path) {
2233 /* path continues by another token or this is the last token */
2234 return LY_SUCCESS;
2235 } else if ((*path)[0] != '[') {
2236 /* unexpected character */
2237 return LY_EINVAL;
2238 } else {
2239 /* predicate starting with [ */
2240 *has_predicate = 1;
2241 return LY_SUCCESS;
2242 }
2243}
2244
2245/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002246 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2247 *
2248 * The set of features used for target must be a subset of features used for the leafref.
2249 * This is not a perfect, we should compare the truth tables but it could require too much resources
2250 * and RFC 7950 does not require it explicitely, so we simplify that.
2251 *
2252 * @param[in] refnode The leafref node.
2253 * @param[in] target Tha target node of the leafref.
2254 * @return LY_SUCCESS or LY_EVALID;
2255 */
2256static LY_ERR
2257lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2258{
2259 LY_ERR ret = LY_EVALID;
2260 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002261 unsigned int u, v, count;
2262 struct ly_set features = {0};
2263
2264 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002265 if (iter->iffeatures) {
2266 LY_ARRAY_FOR(iter->iffeatures, u) {
2267 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2268 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002269 }
2270 }
2271 }
2272 }
2273
2274 /* we should have, in features set, a superset of features applicable to the target node.
2275 * So when adding features applicable to the target into the features set, we should not be
2276 * able to actually add any new feature, otherwise it is not a subset of features applicable
2277 * to the leafref itself. */
2278 count = features.count;
2279 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002280 if (iter->iffeatures) {
2281 LY_ARRAY_FOR(iter->iffeatures, u) {
2282 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2283 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002284 /* new feature was added (or LY_EMEM) */
2285 goto cleanup;
2286 }
2287 }
2288 }
2289 }
2290 }
2291 ret = LY_SUCCESS;
2292
2293cleanup:
2294 ly_set_erase(&features, NULL);
2295 return ret;
2296}
2297
2298/**
Radek Krejcia3045382018-11-22 14:30:31 +01002299 * @brief Validate the leafref path.
2300 * @param[in] ctx Compile context
2301 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002302 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002303 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2304 */
2305static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002306lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002307{
2308 const struct lysc_node *node = NULL, *parent = NULL;
2309 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002310 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002311 const char *id, *prefix, *name;
2312 size_t prefix_len, name_len;
2313 int parent_times = 0, has_predicate;
2314 unsigned int iter, u;
2315 LY_ERR ret = LY_SUCCESS;
2316
2317 assert(ctx);
2318 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002319 assert(leafref);
2320
Radek Krejcia3045382018-11-22 14:30:31 +01002321 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002322 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002323 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2324 if (!iter) { /* first iteration */
2325 /* precess ".." in relative paths */
2326 if (parent_times > 0) {
2327 /* move from the context node */
2328 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2329 /* path is supposed to be evaluated in data tree, so we have to skip
2330 * all schema nodes that cannot be instantiated in data tree */
2331 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002332 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002333 }
2334 }
2335 }
2336
2337 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002338 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002339 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002340 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002341 }
2342 if (!mod) {
2343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002344 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2345 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002346 return LY_EVALID;
2347 }
Radek Krejci3d929562019-02-21 11:25:55 +01002348 if (!mod->implemented) {
2349 /* make the module implemented */
2350 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2351 }
Radek Krejcia3045382018-11-22 14:30:31 +01002352
2353 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2354 if (!node) {
2355 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002356 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002357 return LY_EVALID;
2358 }
2359 parent = node;
2360
2361 if (has_predicate) {
2362 /* we have predicate, so the current result must be list */
2363 if (node->nodetype != LYS_LIST) {
2364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2365 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002366 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002367 return LY_EVALID;
2368 }
2369
Radek Krejcibade82a2018-12-05 10:13:48 +01002370 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2371 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002372 }
2373
2374 ++iter;
2375 }
2376 if (ret) {
2377 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002378 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002379 return LY_EVALID;
2380 }
2381
2382 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2383 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2384 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002385 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002386 return LY_EVALID;
2387 }
2388
2389 /* check status */
2390 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2391 return LY_EVALID;
2392 }
2393
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002394 /* check config */
2395 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2396 if (node->flags & LYS_CONFIG_R) {
2397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2398 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2399 leafref->path);
2400 return LY_EVALID;
2401 }
2402 }
2403
Radek Krejci412ddfa2018-11-23 11:44:11 +01002404 /* store the target's type and check for circular chain of leafrefs */
2405 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2406 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2407 if (type == (struct lysc_type*)leafref) {
2408 /* circular chain detected */
2409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2410 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2411 return LY_EVALID;
2412 }
2413 }
2414
Radek Krejci58d171e2018-11-23 13:50:55 +01002415 /* check if leafref and its target are under common if-features */
2416 if (lys_compile_leafref_features_validate(startnode, node)) {
2417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2418 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2419 leafref->path);
2420 return LY_EVALID;
2421 }
2422
Radek Krejcia3045382018-11-22 14:30:31 +01002423 return LY_SUCCESS;
2424}
2425
Radek Krejcicdfecd92018-11-26 11:27:32 +01002426static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejci01342af2019-01-03 15:18:08 +01002427 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002428/**
2429 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2430 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002431 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2432 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2433 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2434 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002435 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002436 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002437 * @param[in] basetype Base YANG built-in type of the type to compile.
2438 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2439 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2440 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2441 * @param[out] type Newly created type structure with the filled information about the type.
2442 * @return LY_ERR value.
2443 */
Radek Krejci19a96102018-11-15 13:38:09 +01002444static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002445lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
2446 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2447 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002448{
2449 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002450 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002451 struct lysc_type_bin *bin;
2452 struct lysc_type_num *num;
2453 struct lysc_type_str *str;
2454 struct lysc_type_bits *bits;
2455 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002456 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002457 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002458 struct lysc_type_union *un, *un_aux;
2459 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002460
2461 switch (basetype) {
2462 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002463 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002464
2465 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002466 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002467 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2468 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002469 if (!tpdfname) {
2470 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2471 options, u, lys_compile_ext, ret, done);
2472 }
2473 }
2474
2475 if (tpdfname) {
2476 type_p->compiled = *type;
2477 *type = calloc(1, sizeof(struct lysc_type_bin));
2478 }
2479 break;
2480 case LY_TYPE_BITS:
2481 /* RFC 7950 9.7 - bits */
2482 bits = (struct lysc_type_bits*)(*type);
2483 if (type_p->bits) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002484 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2485 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2486 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002487 }
2488
Radek Krejci555cb5b2018-11-16 14:54:33 +01002489 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002490 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002491 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002492 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002493 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002495 free(*type);
2496 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002497 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002498 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002499 }
2500
2501 if (tpdfname) {
2502 type_p->compiled = *type;
2503 *type = calloc(1, sizeof(struct lysc_type_bits));
2504 }
2505 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002506 case LY_TYPE_DEC64:
2507 dec = (struct lysc_type_dec*)(*type);
2508
2509 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002510 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002511 if (!type_p->fraction_digits) {
2512 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002514 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002515 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002516 free(*type);
2517 *type = NULL;
2518 }
2519 return LY_EVALID;
2520 }
2521 } else if (type_p->fraction_digits) {
2522 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002523 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2525 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002526 tpdfname);
2527 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002528 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2529 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002530 free(*type);
2531 *type = NULL;
2532 }
2533 return LY_EVALID;
2534 }
2535 dec->fraction_digits = type_p->fraction_digits;
2536
2537 /* RFC 7950 9.2.4 - range */
2538 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002539 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2540 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002541 if (!tpdfname) {
2542 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2543 options, u, lys_compile_ext, ret, done);
2544 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002545 }
2546
2547 if (tpdfname) {
2548 type_p->compiled = *type;
2549 *type = calloc(1, sizeof(struct lysc_type_dec));
2550 }
2551 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002552 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002553 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002554
2555 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002556 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002557 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2558 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002559 if (!tpdfname) {
2560 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2561 options, u, lys_compile_ext, ret, done);
2562 }
2563 } else if (base && ((struct lysc_type_str*)base)->length) {
2564 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2565 }
2566
2567 /* RFC 7950 9.4.5 - pattern */
2568 if (type_p->patterns) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002569 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, options,
2570 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002571 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2572 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2573 }
2574
2575 if (tpdfname) {
2576 type_p->compiled = *type;
2577 *type = calloc(1, sizeof(struct lysc_type_str));
2578 }
2579 break;
2580 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002581 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002582
2583 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 if (type_p->enums) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002585 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2586 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002587 }
2588
Radek Krejci555cb5b2018-11-16 14:54:33 +01002589 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002590 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002591 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002592 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002593 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002595 free(*type);
2596 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002597 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002598 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002599 }
2600
2601 if (tpdfname) {
2602 type_p->compiled = *type;
2603 *type = calloc(1, sizeof(struct lysc_type_enum));
2604 }
2605 break;
2606 case LY_TYPE_INT8:
2607 case LY_TYPE_UINT8:
2608 case LY_TYPE_INT16:
2609 case LY_TYPE_UINT16:
2610 case LY_TYPE_INT32:
2611 case LY_TYPE_UINT32:
2612 case LY_TYPE_INT64:
2613 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002614 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002615
2616 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002617 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002618 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2619 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002620 if (!tpdfname) {
2621 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2622 options, u, lys_compile_ext, ret, done);
2623 }
2624 }
2625
2626 if (tpdfname) {
2627 type_p->compiled = *type;
2628 *type = calloc(1, sizeof(struct lysc_type_num));
2629 }
2630 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002631 case LY_TYPE_IDENT:
2632 idref = (struct lysc_type_identityref*)(*type);
2633
2634 /* RFC 7950 9.10.2 - base */
2635 if (type_p->bases) {
2636 if (base) {
2637 /* only the directly derived identityrefs can contain base specification */
2638 if (tpdfname) {
2639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002640 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002641 tpdfname);
2642 } else {
2643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002644 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002645 free(*type);
2646 *type = NULL;
2647 }
2648 return LY_EVALID;
2649 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002650 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002651 }
2652
2653 if (!base && !type_p->flags) {
2654 /* type derived from identityref built-in type must contain at least one base */
2655 if (tpdfname) {
2656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2657 } else {
2658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2659 free(*type);
2660 *type = NULL;
2661 }
2662 return LY_EVALID;
2663 }
2664
2665 if (tpdfname) {
2666 type_p->compiled = *type;
2667 *type = calloc(1, sizeof(struct lysc_type_identityref));
2668 }
2669 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002670 case LY_TYPE_LEAFREF:
2671 /* RFC 7950 9.9.3 - require-instance */
2672 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002673 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002674 if (tpdfname) {
2675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2676 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2677 } else {
2678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2679 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2680 free(*type);
2681 *type = NULL;
2682 }
2683 return LY_EVALID;
2684 }
Radek Krejcia3045382018-11-22 14:30:31 +01002685 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002686 } else if (base) {
2687 /* inherit */
2688 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002689 } else {
2690 /* default is true */
2691 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2692 }
2693 if (type_p->path) {
2694 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002695 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002696 } else if (base) {
2697 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002698 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002699 } else if (tpdfname) {
2700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2701 return LY_EVALID;
2702 } else {
2703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2704 free(*type);
2705 *type = NULL;
2706 return LY_EVALID;
2707 }
2708 if (tpdfname) {
2709 type_p->compiled = *type;
2710 *type = calloc(1, sizeof(struct lysc_type_leafref));
2711 }
2712 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002713 case LY_TYPE_INST:
2714 /* RFC 7950 9.9.3 - require-instance */
2715 if (type_p->flags & LYS_SET_REQINST) {
2716 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2717 } else {
2718 /* default is true */
2719 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2720 }
2721
2722 if (tpdfname) {
2723 type_p->compiled = *type;
2724 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2725 }
2726 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002727 case LY_TYPE_UNION:
2728 un = (struct lysc_type_union*)(*type);
2729
2730 /* RFC 7950 7.4 - type */
2731 if (type_p->types) {
2732 if (base) {
2733 /* only the directly derived union can contain types specification */
2734 if (tpdfname) {
2735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2736 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2737 tpdfname);
2738 } else {
2739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2740 "Invalid type substatement for the type not directly derived from union built-in type.");
2741 free(*type);
2742 *type = NULL;
2743 }
2744 return LY_EVALID;
2745 }
2746 /* compile the type */
2747 additional = 0;
2748 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2749 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002750 LY_CHECK_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 +01002751 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2752 /* add space for additional types from the union subtype */
2753 un_aux = (struct lysc_type_union *)un->types[u + additional];
2754 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)));
2755 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2756 un->types = (void*)((uint32_t*)(p) + 1);
2757
2758 /* copy subtypes of the subtype union */
2759 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2760 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2761 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2762 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2763 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2764 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2765 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2766 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2767 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2768 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2769 /* TODO extensions */
2770
2771 } else {
2772 un->types[u + additional] = un_aux->types[v];
2773 ++un_aux->types[v]->refcount;
2774 }
2775 ++additional;
2776 LY_ARRAY_INCREMENT(un->types);
2777 }
2778 /* compensate u increment in main loop */
2779 --additional;
2780
2781 /* free the replaced union subtype */
2782 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2783 } else {
2784 LY_ARRAY_INCREMENT(un->types);
2785 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002786 }
2787 }
2788
2789 if (!base && !type_p->flags) {
2790 /* type derived from union built-in type must contain at least one type */
2791 if (tpdfname) {
2792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2793 } else {
2794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2795 free(*type);
2796 *type = NULL;
2797 }
2798 return LY_EVALID;
2799 }
2800
2801 if (tpdfname) {
2802 type_p->compiled = *type;
2803 *type = calloc(1, sizeof(struct lysc_type_union));
2804 }
2805 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002806 case LY_TYPE_BOOL:
2807 case LY_TYPE_EMPTY:
2808 case LY_TYPE_UNKNOWN: /* just to complete switch */
2809 break;
2810 }
2811 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2812done:
2813 return ret;
2814}
2815
Radek Krejcia3045382018-11-22 14:30:31 +01002816/**
2817 * @brief Compile information about the leaf/leaf-list's type.
2818 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002819 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2820 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2821 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2822 * @param[in] context_name Name of the context node or referencing typedef for logging.
2823 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002824 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2825 * @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 +01002826 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002827 * @return LY_ERR value.
2828 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002829static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002830lys_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 +01002831 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002832{
2833 LY_ERR ret = LY_SUCCESS;
2834 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002835 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002836 struct type_context {
2837 const struct lysp_tpdf *tpdf;
2838 struct lysp_node *node;
2839 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002840 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002841 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002842 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002843 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002844 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002845
2846 (*type) = NULL;
2847
2848 tctx = calloc(1, sizeof *tctx);
2849 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002850 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002851 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2852 ret == LY_SUCCESS;
2853 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2854 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2855 if (basetype) {
2856 break;
2857 }
2858
2859 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002860 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2861 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002862 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2863
Radek Krejcicdfecd92018-11-26 11:27:32 +01002864 if (units && !*units) {
2865 /* inherit units */
2866 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2867 }
Radek Krejci01342af2019-01-03 15:18:08 +01002868 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002869 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002870 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002871 }
Radek Krejci01342af2019-01-03 15:18:08 +01002872 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002873 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2874 break;
2875 }
2876
Radek Krejci19a96102018-11-15 13:38:09 +01002877 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002878 /* it is not necessary to continue, the rest of the chain was already compiled,
2879 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002880 basetype = tctx->tpdf->type.compiled->basetype;
2881 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002882 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002883 dummyloops = 1;
2884 goto preparenext;
2885 } else {
2886 tctx = NULL;
2887 break;
2888 }
Radek Krejci19a96102018-11-15 13:38:09 +01002889 }
2890
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002891 /* circular typedef reference detection */
2892 for (u = 0; u < tpdf_chain.count; u++) {
2893 /* local part */
2894 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
2895 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2897 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2898 free(tctx);
2899 ret = LY_EVALID;
2900 goto cleanup;
2901 }
2902 }
2903 for (u = 0; u < ctx->tpdf_chain.count; u++) {
2904 /* global part for unions corner case */
2905 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
2906 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
2907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2908 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2909 free(tctx);
2910 ret = LY_EVALID;
2911 goto cleanup;
2912 }
2913 }
2914
Radek Krejci19a96102018-11-15 13:38:09 +01002915 /* store information for the following processing */
2916 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2917
Radek Krejcicdfecd92018-11-26 11:27:32 +01002918preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002919 /* prepare next loop */
2920 tctx_prev = tctx;
2921 tctx = calloc(1, sizeof *tctx);
2922 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2923 }
2924 free(tctx);
2925
2926 /* allocate type according to the basetype */
2927 switch (basetype) {
2928 case LY_TYPE_BINARY:
2929 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002930 break;
2931 case LY_TYPE_BITS:
2932 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002933 break;
2934 case LY_TYPE_BOOL:
2935 case LY_TYPE_EMPTY:
2936 *type = calloc(1, sizeof(struct lysc_type));
2937 break;
2938 case LY_TYPE_DEC64:
2939 *type = calloc(1, sizeof(struct lysc_type_dec));
2940 break;
2941 case LY_TYPE_ENUM:
2942 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002943 break;
2944 case LY_TYPE_IDENT:
2945 *type = calloc(1, sizeof(struct lysc_type_identityref));
2946 break;
2947 case LY_TYPE_INST:
2948 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2949 break;
2950 case LY_TYPE_LEAFREF:
2951 *type = calloc(1, sizeof(struct lysc_type_leafref));
2952 break;
2953 case LY_TYPE_STRING:
2954 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002955 break;
2956 case LY_TYPE_UNION:
2957 *type = calloc(1, sizeof(struct lysc_type_union));
2958 break;
2959 case LY_TYPE_INT8:
2960 case LY_TYPE_UINT8:
2961 case LY_TYPE_INT16:
2962 case LY_TYPE_UINT16:
2963 case LY_TYPE_INT32:
2964 case LY_TYPE_UINT32:
2965 case LY_TYPE_INT64:
2966 case LY_TYPE_UINT64:
2967 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002968 break;
2969 case LY_TYPE_UNKNOWN:
2970 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2971 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2972 ret = LY_EVALID;
2973 goto cleanup;
2974 }
2975 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002976 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002977 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2978 ly_data_type2str[basetype]);
2979 free(*type);
2980 (*type) = NULL;
2981 ret = LY_EVALID;
2982 goto cleanup;
2983 }
2984
2985 /* get restrictions from the referred typedefs */
2986 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2987 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002988
2989 /* remember the typedef context for circular check */
2990 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2991
Radek Krejci43699232018-11-23 14:59:46 +01002992 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002993 base = tctx->tpdf->type.compiled;
2994 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002995 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002996 /* no change, just use the type information from the base */
2997 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2998 ++base->refcount;
2999 continue;
3000 }
3001
3002 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003003 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3004 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3005 tctx->tpdf->name, ly_data_type2str[basetype]);
3006 ret = LY_EVALID;
3007 goto cleanup;
3008 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3009 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3010 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3011 tctx->tpdf->name, tctx->tpdf->dflt);
3012 ret = LY_EVALID;
3013 goto cleanup;
3014 }
3015
Radek Krejci19a96102018-11-15 13:38:09 +01003016 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003017 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003018 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 +01003019 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
3020 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003021 LY_CHECK_GOTO(ret, cleanup);
3022 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003023 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003024 /* remove the processed typedef contexts from the stack for circular check */
3025 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003026
Radek Krejcic5c27e52018-11-15 14:38:11 +01003027 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003028 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003029 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003030 (*type)->basetype = basetype;
3031 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01003032 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 +01003033 LY_CHECK_GOTO(ret, cleanup);
3034 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003035 /* no specific restriction in leaf's type definition, copy from the base */
3036 free(*type);
3037 (*type) = base;
3038 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003039 }
Radek Krejci01342af2019-01-03 15:18:08 +01003040 if (!(*type)->dflt) {
3041 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3042 }
Radek Krejci19a96102018-11-15 13:38:09 +01003043
3044 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
3045
3046cleanup:
3047 ly_set_erase(&tpdf_chain, free);
3048 return ret;
3049}
3050
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003051/**
3052 * @brief Compile status information of the given node.
3053 *
3054 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3055 * has the status correctly set during the compilation.
3056 *
3057 * @param[in] ctx Compile context
3058 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3059 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3060 * the compatibility with the parent's status value.
3061 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3062 * @return LY_ERR value.
3063 */
3064static LY_ERR
3065lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3066{
3067 /* status - it is not inherited by specification, but it does not make sense to have
3068 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3069 if (!((*node_flags) & LYS_STATUS_MASK)) {
3070 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3071 if ((parent_flags & 0x3) != 0x3) {
3072 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3073 * combination of bits (0x3) which marks the uses_status value */
3074 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3075 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3076 }
3077 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3078 } else {
3079 (*node_flags) |= LYS_STATUS_CURR;
3080 }
3081 } else if (parent_flags & LYS_STATUS_MASK) {
3082 /* check status compatibility with the parent */
3083 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3084 if ((*node_flags) & LYS_STATUS_CURR) {
3085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3086 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3087 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3088 } else { /* LYS_STATUS_DEPRC */
3089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3090 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3091 }
3092 return LY_EVALID;
3093 }
3094 }
3095 return LY_SUCCESS;
3096}
3097
Radek Krejci8cce8532019-03-05 11:27:45 +01003098/**
3099 * @brief Check uniqness of the node/action/notification name.
3100 *
3101 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3102 * structures, but they share the namespace so we need to check their name collisions.
3103 *
3104 * @param[in] ctx Compile context.
3105 * @param[in] children List (linked list) of data nodes to go through.
3106 * @param[in] actions List (sized array) of actions or RPCs to go through.
3107 * @param[in] notifs List (sized array) of Notifications to go through.
3108 * @param[in] name Name of the item to find in the given lists.
3109 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3110 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3111 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3112 */
3113static LY_ERR
3114lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3115 const struct lysc_action *actions, const struct lysc_notif *notifs,
3116 const char *name, void *exclude)
3117{
3118 const struct lysc_node *iter;
3119 unsigned int u;
3120
3121 LY_LIST_FOR(children, iter) {
3122 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3123 goto error;
3124 }
3125 }
3126 LY_ARRAY_FOR(actions, u) {
3127 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3128 goto error;
3129 }
3130 }
3131 LY_ARRAY_FOR(notifs, u) {
3132 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3133 goto error;
3134 }
3135 }
3136 return LY_SUCCESS;
3137error:
3138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3139 return LY_EEXIST;
3140}
3141
Radek Krejcib1b59152019-01-07 13:21:56 +01003142static 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 +01003143
Radek Krejcia3045382018-11-22 14:30:31 +01003144/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003145 * @brief Compile parsed RPC/action schema node information.
3146 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003147 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003148 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003149 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003150 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3151 * @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).
3152 * Zero means no uses, non-zero value with no status bit set mean the default status.
3153 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3154 */
3155static LY_ERR
3156lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, int options,
3157 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3158{
3159 LY_ERR ret = LY_SUCCESS;
3160 struct lysp_node *child_p;
3161 unsigned int u;
3162
Radek Krejci8cce8532019-03-05 11:27:45 +01003163 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3164 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3165 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3166 action_p->name, action)) {
3167 return LY_EVALID;
3168 }
3169
Radek Krejcifc11bd72019-04-11 16:00:05 +02003170 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003171 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003172 "Action \"%s\" is placed inside %s.", action_p->name,
3173 options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003174 return LY_EVALID;
3175 }
3176
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003177 action->nodetype = LYS_ACTION;
3178 action->module = ctx->mod;
3179 action->parent = parent;
3180 if (!(options & LYSC_OPT_FREE_SP)) {
3181 action->sp = action_p;
3182 }
3183 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3184
3185 /* status - it is not inherited by specification, but it does not make sense to have
3186 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3187 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3188
3189 DUP_STRING(ctx->ctx, action_p->name, action->name);
3190 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3191 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
3192 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3193 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, options, u, lys_compile_ext, ret, cleanup);
3194
3195 /* input */
3196 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 +02003197 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 +01003198 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003199 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 +01003200 }
3201
3202 /* output */
3203 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 +02003204 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 +01003205 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003206 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 +01003207 }
3208
3209cleanup:
3210 return ret;
3211}
3212
3213/**
Radek Krejci43981a32019-04-12 09:44:11 +02003214 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003215 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003216 * @param[in] notif_p Parsed Notification schema node.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003217 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci43981a32019-04-12 09:44:11 +02003218 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3219 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3220 * @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 +02003221 * Zero means no uses, non-zero value with no status bit set mean the default status.
3222 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3223 */
3224static LY_ERR
3225lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, int options,
3226 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3227{
3228 LY_ERR ret = LY_SUCCESS;
3229 struct lysp_node *child_p;
3230 unsigned int u;
3231
3232 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3233 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3234 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3235 notif_p->name, notif)) {
3236 return LY_EVALID;
3237 }
3238
3239 if (options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
3240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3241 "Notification \"%s\" is placed inside %s.", notif_p->name,
3242 options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
3243 return LY_EVALID;
3244 }
3245
3246 notif->nodetype = LYS_NOTIF;
3247 notif->module = ctx->mod;
3248 notif->parent = parent;
3249 if (!(options & LYSC_OPT_FREE_SP)) {
3250 notif->sp = notif_p;
3251 }
3252 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3253
3254 /* status - it is not inherited by specification, but it does not make sense to have
3255 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3256 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3257
3258 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3259 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3260 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
3261 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3262 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, options, u, lys_compile_ext, ret, cleanup);
3263 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, options, u, lys_compile_must, ret, cleanup);
3264
3265 LY_LIST_FOR(notif_p->data, child_p) {
3266 LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_NOTIFICATION, (struct lysc_node*)notif, uses_status));
3267 }
3268
3269cleanup:
3270 return ret;
3271}
3272
3273/**
Radek Krejcia3045382018-11-22 14:30:31 +01003274 * @brief Compile parsed container node information.
3275 * @param[in] ctx Compile context
3276 * @param[in] node_p Parsed container node.
3277 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3278 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3279 * is enriched with the container-specific information.
3280 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3281 */
Radek Krejci19a96102018-11-15 13:38:09 +01003282static LY_ERR
3283lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3284{
3285 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3286 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3287 struct lysp_node *child_p;
3288 unsigned int u;
3289 LY_ERR ret = LY_SUCCESS;
3290
Radek Krejcife909632019-02-12 15:34:42 +01003291 if (cont_p->presence) {
3292 cont->flags |= LYS_PRESENCE;
3293 }
3294
Radek Krejci19a96102018-11-15 13:38:09 +01003295 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003296 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003297 }
3298
3299 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003300 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 +02003301 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 +01003302
3303done:
3304 return ret;
3305}
3306
Radek Krejci33f72892019-02-21 10:36:58 +01003307/*
3308 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3309 * @param[in] ctx Compile context.
3310 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3311 * @param[in] type_p Parsed type to compile.
3312 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3313 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3314 * @return LY_ERR value.
3315 */
3316static LY_ERR
3317lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, int options, struct lysc_node_leaf *leaf)
3318{
3319 unsigned int u, v;
3320 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3321
3322 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type,
3323 leaf->units ? NULL : &leaf->units));
3324 if (leaf->nodetype == LYS_LEAFLIST) {
3325 if (llist->type->dflt && !llist->dflts && !llist->min) {
3326 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3327 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3328 LY_ARRAY_INCREMENT(llist->dflts);
3329 }
3330 } else {
3331 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3332 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3333 }
3334 }
3335 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3336 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3337 ly_set_add(&ctx->unres, leaf, 0);
3338 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3339 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3340 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3341 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3342 ly_set_add(&ctx->unres, leaf, 0);
3343 }
3344 }
3345 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3346 if (leaf->flags & LYS_SET_DFLT) {
3347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3348 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3349 return LY_EVALID;
3350 }
3351 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3353 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3354 return LY_EVALID;
3355 }
3356 }
3357
3358 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3359 /* configuration data values must be unique - so check the default values */
3360 LY_ARRAY_FOR(llist->dflts, u) {
3361 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3362 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3363 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3364 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3365 return LY_EVALID;
3366 }
3367 }
3368 }
3369 }
3370
3371 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3372
3373 return LY_SUCCESS;
3374}
3375
Radek Krejcia3045382018-11-22 14:30:31 +01003376/**
3377 * @brief Compile parsed leaf node information.
3378 * @param[in] ctx Compile context
3379 * @param[in] node_p Parsed leaf 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-specific information.
3383 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3384 */
Radek Krejci19a96102018-11-15 13:38:09 +01003385static LY_ERR
3386lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3387{
3388 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3389 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3390 unsigned int u;
3391 LY_ERR ret = LY_SUCCESS;
3392
Radek Krejci19a96102018-11-15 13:38:09 +01003393 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003394 if (leaf_p->units) {
3395 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3396 leaf->flags |= LYS_SET_UNITS;
3397 }
3398 if (leaf_p->dflt) {
3399 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003400 leaf->flags |= LYS_SET_DFLT;
3401 }
Radek Krejci43699232018-11-23 14:59:46 +01003402
Radek Krejci33f72892019-02-21 10:36:58 +01003403 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, options, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003404
Radek Krejci19a96102018-11-15 13:38:09 +01003405done:
3406 return ret;
3407}
3408
Radek Krejcia3045382018-11-22 14:30:31 +01003409/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003410 * @brief Compile parsed leaf-list node information.
3411 * @param[in] ctx Compile context
3412 * @param[in] node_p Parsed leaf-list node.
3413 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3414 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3415 * is enriched with the leaf-list-specific information.
3416 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3417 */
3418static LY_ERR
3419lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3420{
3421 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3422 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003423 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003424 LY_ERR ret = LY_SUCCESS;
3425
Radek Krejci0e5d8382018-11-28 16:37:53 +01003426 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003427 if (llist_p->units) {
3428 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3429 llist->flags |= LYS_SET_UNITS;
3430 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003431
3432 if (llist_p->dflts) {
3433 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3434 LY_ARRAY_FOR(llist_p->dflts, u) {
3435 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3436 LY_ARRAY_INCREMENT(llist->dflts);
3437 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003438 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003439 }
3440
3441 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003442 if (llist->min) {
3443 llist->flags |= LYS_MAND_TRUE;
3444 }
Radek Krejcib7408632018-11-28 17:12:11 +01003445 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003446
Radek Krejci33f72892019-02-21 10:36:58 +01003447 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, options, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003448
3449done:
3450 return ret;
3451}
3452
3453/**
Radek Krejci7af64242019-02-18 13:07:53 +01003454 * @brief Compile information about list's uniques.
3455 * @param[in] ctx Compile context.
3456 * @param[in] context_module Module where the prefixes are going to be resolved.
3457 * @param[in] uniques Sized array list of unique statements.
3458 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3459 * @return LY_ERR value.
3460 */
3461static LY_ERR
3462lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3463{
3464 LY_ERR ret = LY_SUCCESS;
3465 struct lysc_node_leaf **key, ***unique;
3466 const char *keystr, *delim;
3467 size_t len;
3468 unsigned int v;
3469 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003470 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003471
3472 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3473 config = -1;
3474 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3475 keystr = uniques[v];
3476 while (keystr) {
3477 delim = strpbrk(keystr, " \t\n");
3478 if (delim) {
3479 len = delim - keystr;
3480 while (isspace(*delim)) {
3481 ++delim;
3482 }
3483 } else {
3484 len = strlen(keystr);
3485 }
3486
3487 /* unique node must be present */
3488 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003489 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3490 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003491 if (ret != LY_SUCCESS) {
3492 if (ret == LY_EDENIED) {
3493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003494 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003495 len, keystr, lys_nodetype2str((*key)->nodetype));
3496 }
3497 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003498 } else if (flags) {
3499 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3500 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3501 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3502 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003503 }
3504
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003505
Radek Krejci7af64242019-02-18 13:07:53 +01003506 /* all referenced leafs must be of the same config type */
3507 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3509 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3510 return LY_EVALID;
3511 } else if ((*key)->flags & LYS_CONFIG_W) {
3512 config = 1;
3513 } else { /* LYS_CONFIG_R */
3514 config = 0;
3515 }
3516
3517 /* check status */
3518 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3519 (*key)->flags, (*key)->module, (*key)->name));
3520
3521 /* mark leaf as unique */
3522 (*key)->flags |= LYS_UNIQUE;
3523
3524 /* next unique value in line */
3525 keystr = delim;
3526 }
3527 /* next unique definition */
3528 }
3529
3530 return LY_SUCCESS;
3531}
3532
3533/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003534 * @brief Compile parsed list node information.
3535 * @param[in] ctx Compile context
3536 * @param[in] node_p Parsed list node.
3537 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3538 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3539 * is enriched with the list-specific information.
3540 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3541 */
3542static LY_ERR
3543lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3544{
3545 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3546 struct lysc_node_list *list = (struct lysc_node_list*)node;
3547 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003548 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003549 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003550 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003551 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003552 LY_ERR ret = LY_SUCCESS;
3553
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003554 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003555 if (list->min) {
3556 list->flags |= LYS_MAND_TRUE;
3557 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003558 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3559
3560 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003561 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003562 }
3563
3564 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
3565
3566 /* keys */
3567 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3569 return LY_EVALID;
3570 }
3571
3572 /* find all the keys (must be direct children) */
3573 keystr = list_p->key;
3574 while (keystr) {
3575 delim = strpbrk(keystr, " \t\n");
3576 if (delim) {
3577 len = delim - keystr;
3578 while (isspace(*delim)) {
3579 ++delim;
3580 }
3581 } else {
3582 len = strlen(keystr);
3583 }
3584
3585 /* key node must be present */
3586 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3587 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3588 if (!(*key)) {
3589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3590 "The list's key \"%.*s\" not found.", len, keystr);
3591 return LY_EVALID;
3592 }
3593 /* keys must be unique */
3594 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3595 if (*key == list->keys[u]) {
3596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3597 "Duplicated key identifier \"%.*s\".", len, keystr);
3598 return LY_EVALID;
3599 }
3600 }
3601 /* key must have the same config flag as the list itself */
3602 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3604 return LY_EVALID;
3605 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003606 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003607 /* YANG 1.0 denies key to be of empty type */
3608 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3610 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3611 return LY_EVALID;
3612 }
3613 } else {
3614 /* when and if-feature are illegal on list keys */
3615 if ((*key)->when) {
3616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3617 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3618 return LY_EVALID;
3619 }
3620 if ((*key)->iffeatures) {
3621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3622 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3623 return LY_EVALID;
3624 }
3625 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003626
3627 /* check status */
3628 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3629 (*key)->flags, (*key)->module, (*key)->name));
3630
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003631 /* ignore default values of the key */
3632 if ((*key)->dflt) {
3633 lydict_remove(ctx->ctx, (*key)->dflt);
3634 (*key)->dflt = NULL;
3635 }
3636 /* mark leaf as key */
3637 (*key)->flags |= LYS_KEY;
3638
3639 /* next key value */
3640 keystr = delim;
3641 }
3642
3643 /* uniques */
3644 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003645 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003646 }
3647
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003648 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 +02003649 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 +01003650
3651done:
3652 return ret;
3653}
3654
Radek Krejcib56c7502019-02-13 14:19:54 +01003655/**
3656 * @brief Do some checks and set the default choice's case.
3657 *
3658 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3659 *
3660 * @param[in] ctx Compile context.
3661 * @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,
3662 * not the reference to the imported module.
3663 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3664 * @return LY_ERR value.
3665 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003666static LY_ERR
3667lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3668{
3669 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3670 const char *prefix = NULL, *name;
3671 size_t prefix_len = 0;
3672
3673 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3674 name = strchr(dflt, ':');
3675 if (name) {
3676 prefix = dflt;
3677 prefix_len = name - prefix;
3678 ++name;
3679 } else {
3680 name = dflt;
3681 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003682 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003683 /* prefixed default case make sense only for the prefix of the schema itself */
3684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3685 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3686 prefix_len, prefix);
3687 return LY_EVALID;
3688 }
3689 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3690 if (!ch->dflt) {
3691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3692 "Default case \"%s\" not found.", dflt);
3693 return LY_EVALID;
3694 }
3695 /* no mandatory nodes directly under the default case */
3696 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003697 if (iter->parent != (struct lysc_node*)ch->dflt) {
3698 break;
3699 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003700 if (iter->flags & LYS_MAND_TRUE) {
3701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3702 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3703 return LY_EVALID;
3704 }
3705 }
Radek Krejci01342af2019-01-03 15:18:08 +01003706 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003707 return LY_SUCCESS;
3708}
3709
Radek Krejciccd20f12019-02-15 14:12:27 +01003710static LY_ERR
3711lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3712{
3713 struct lys_module *mod;
3714 const char *prefix = NULL, *name;
3715 size_t prefix_len = 0;
3716 struct lysc_node_case *cs;
3717 struct lysc_node *node;
3718
3719 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3720 name = strchr(dflt, ':');
3721 if (name) {
3722 prefix = dflt;
3723 prefix_len = name - prefix;
3724 ++name;
3725 } else {
3726 name = dflt;
3727 }
3728 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3729 if (prefix) {
3730 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3732 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3733 "The prefix does not match any imported module of the deviation module.",
3734 devnodeid, dflt);
3735 return LY_EVALID;
3736 }
3737 } else {
3738 mod = ctx->mod;
3739 }
3740 /* get the default case */
3741 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3742 if (!cs) {
3743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3744 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3745 devnodeid, dflt);
3746 return LY_EVALID;
3747 }
3748
3749 /* check that there is no mandatory node */
3750 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003751 if (node->parent != (struct lysc_node*)cs) {
3752 break;
3753 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003754 if (node->flags & LYS_MAND_TRUE) {
3755 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3756 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3757 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3758 return LY_EVALID;
3759 }
3760 }
3761
3762 /* set the default case in choice */
3763 ch->dflt = cs;
3764 cs->flags |= LYS_SET_DFLT;
3765
3766 return LY_SUCCESS;
3767}
3768
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003769/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003770 * @brief Compile parsed choice node information.
3771 * @param[in] ctx Compile context
3772 * @param[in] node_p Parsed choice node.
3773 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3774 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003775 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003776 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3777 */
3778static LY_ERR
3779lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3780{
3781 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3782 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3783 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003784 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003785 LY_ERR ret = LY_SUCCESS;
3786
Radek Krejci056d0a82018-12-06 16:57:25 +01003787 LY_LIST_FOR(ch_p->child, child_p) {
3788 if (child_p->nodetype == LYS_CASE) {
3789 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003790 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003791 }
3792 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003793 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003794 }
3795 }
3796
3797 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003798 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003799 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003800 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003801
Radek Krejci9800fb82018-12-13 14:26:23 +01003802 return ret;
3803}
3804
3805/**
3806 * @brief Compile parsed anydata or anyxml node information.
3807 * @param[in] ctx Compile context
3808 * @param[in] node_p Parsed anydata or anyxml node.
3809 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3810 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3811 * is enriched with the any-specific information.
3812 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3813 */
3814static LY_ERR
3815lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3816{
3817 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3818 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3819 unsigned int u;
3820 LY_ERR ret = LY_SUCCESS;
3821
3822 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3823
3824 if (any->flags & LYS_CONFIG_W) {
3825 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3826 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3827 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003828done:
3829 return ret;
3830}
3831
Radek Krejcib56c7502019-02-13 14:19:54 +01003832/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003833 * @brief Connect the node into the siblings list and check its name uniqueness.
3834 *
3835 * @param[in] ctx Compile context
3836 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3837 * the choice itself is expected instead of a specific case node.
3838 * @param[in] node Schema node to connect into the list.
Radek Krejci05b774b2019-02-25 13:26:18 +01003839 * @param[in] options Compile options to distinguish input/output when placing node into RPC/action.
Radek Krejci056d0a82018-12-06 16:57:25 +01003840 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3841 */
3842static LY_ERR
Radek Krejci05b774b2019-02-25 13:26:18 +01003843lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node, int options)
Radek Krejci056d0a82018-12-06 16:57:25 +01003844{
3845 struct lysc_node **children;
3846
3847 if (node->nodetype == LYS_CASE) {
3848 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3849 } else {
Radek Krejci05b774b2019-02-25 13:26:18 +01003850 children = lysc_node_children_p(parent, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003851 }
3852 if (children) {
3853 if (!(*children)) {
3854 /* first child */
3855 *children = node;
3856 } else if (*children != node) {
3857 /* by the condition in previous branch we cover the choice/case children
3858 * - the children list is shared by the choice and the the first case, in addition
3859 * the first child of each case must be referenced from the case node. So the node is
3860 * actually always already inserted in case it is the first children - so here such
3861 * a situation actually corresponds to the first branch */
3862 /* insert at the end of the parent's children list */
3863 (*children)->prev->next = node;
3864 node->prev = (*children)->prev;
3865 (*children)->prev = node;
3866
3867 /* check the name uniqueness */
3868 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3869 lysc_node_notifs(parent), node->name, node)) {
3870 return LY_EEXIST;
3871 }
3872 }
3873 }
3874 return LY_SUCCESS;
3875}
3876
Radek Krejci95710c92019-02-11 15:49:55 +01003877/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003878 * @brief Get the XPath context node for the given schema node.
3879 * @param[in] start The schema node where the XPath expression appears.
3880 * @return The context node to evaluate XPath expression in given schema node.
3881 * @return NULL in case the context node is the root node.
3882 */
3883static struct lysc_node *
3884lysc_xpath_context(struct lysc_node *start)
3885{
3886 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3887 start = start->parent);
3888 return start;
3889}
3890
3891/**
3892 * @brief Prepare the case structure in choice node for the new data node.
3893 *
3894 * 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
3895 * created in the choice when the first child was processed.
3896 *
3897 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003898 * @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,
3899 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003900 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3901 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3902 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3903 * @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,
3904 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003905 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003906static struct lysc_node_case*
3907lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3908{
3909 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02003910 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01003911 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003912 unsigned int u;
3913 LY_ERR ret;
3914
Radek Krejci95710c92019-02-11 15:49:55 +01003915#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003916 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003917 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003918 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3919 return NULL; \
3920 } \
3921 }
3922
Radek Krejci95710c92019-02-11 15:49:55 +01003923 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3924 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003925
3926 /* we have to add an implicit case node into the parent choice */
3927 cs = calloc(1, sizeof(struct lysc_node_case));
3928 DUP_STRING(ctx->ctx, child->name, cs->name);
3929 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003930 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003931 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3932 /* the case is already present since the child is not its first children */
3933 return (struct lysc_node_case*)ch->cases->prev;
3934 }
Radek Krejci95710c92019-02-11 15:49:55 +01003935 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003936
3937 /* explicit parent case is not present (this is its first child) */
3938 cs = calloc(1, sizeof(struct lysc_node_case));
3939 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3940 cs->flags = LYS_STATUS_MASK & node_p->flags;
3941 cs->sp = node_p;
3942
Radek Krejcib1b59152019-01-07 13:21:56 +01003943 /* 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 +02003944 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01003945
3946 if (node_p->when) {
3947 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3948 ret = lys_compile_when(ctx, node_p->when, options, when);
3949 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003950 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003951 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003952 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003953 } else {
3954 LOGINT(ctx->ctx);
3955 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003956 }
3957 cs->module = ctx->mod;
3958 cs->prev = (struct lysc_node*)cs;
3959 cs->nodetype = LYS_CASE;
Radek Krejci05b774b2019-02-25 13:26:18 +01003960 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003961 cs->parent = (struct lysc_node*)ch;
3962 cs->child = child;
3963
3964 return cs;
3965error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02003966 if (cs) {
3967 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
3968 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003969 return NULL;
3970
3971#undef UNIQUE_CHECK
3972}
3973
Radek Krejcib56c7502019-02-13 14:19:54 +01003974/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003975 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003976 *
3977 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003978 * @param[in] node Target node where the config is supposed to be changed.
3979 * @param[in] config_flag Node's config flag to be applied to the @p node.
3980 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003981 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3982 * 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 +01003983 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3984 * @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 +01003985 * @return LY_ERR value.
3986 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003987static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003988lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3989 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003990{
3991 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003992 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003993
3994 if (config == (node->flags & LYS_CONFIG_MASK)) {
3995 /* nothing to do */
3996 return LY_SUCCESS;
3997 }
3998
3999 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004000 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004001 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01004003 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
4004 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01004005 return LY_EVALID;
4006 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004007 node->flags |= LYS_SET_CONFIG;
4008 } else {
4009 if (node->flags & LYS_SET_CONFIG) {
4010 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4011 /* setting config flags, but have node with explicit config true */
4012 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4013 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
4014 refine_flag ? "refine" : "deviation", nodeid);
4015 return LY_EVALID;
4016 }
4017 /* do not change config on nodes where the config is explicitely set, this does not apply to
4018 * nodes, which are being changed explicitly (targets of refine or deviation) */
4019 return LY_SUCCESS;
4020 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004021 }
4022 node->flags &= ~LYS_CONFIG_MASK;
4023 node->flags |= config;
4024
4025 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004026 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004027 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004028 }
4029
Radek Krejci76b3e962018-12-14 17:01:25 +01004030 return LY_SUCCESS;
4031}
4032
Radek Krejcib56c7502019-02-13 14:19:54 +01004033/**
4034 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4035 *
4036 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4037 * the flag to such parents from a mandatory children.
4038 *
4039 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4040 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4041 * (mandatory children was removed).
4042 */
Radek Krejcife909632019-02-12 15:34:42 +01004043void
4044lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4045{
4046 struct lysc_node *iter;
4047
4048 if (add) { /* set flag */
4049 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4050 parent = parent->parent) {
4051 parent->flags |= LYS_MAND_TRUE;
4052 }
4053 } else { /* unset flag */
4054 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004055 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004056 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004057 /* there is another mandatory node */
4058 return;
4059 }
4060 }
4061 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4062 parent->flags &= ~LYS_MAND_TRUE;
4063 }
4064 }
4065}
4066
Radek Krejci056d0a82018-12-06 16:57:25 +01004067/**
Radek Krejci3641f562019-02-13 15:38:40 +01004068 * @brief Internal sorting process for the lys_compile_augment_sort().
4069 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4070 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4071 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4072 */
4073static void
4074lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4075{
4076 unsigned int v;
4077 size_t len;
4078
4079 len = strlen(aug_p->nodeid);
4080 LY_ARRAY_FOR(result, v) {
4081 if (strlen(result[v]->nodeid) <= len) {
4082 continue;
4083 }
4084 if (v < LY_ARRAY_SIZE(result)) {
4085 /* move the rest of array */
4086 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4087 break;
4088 }
4089 }
4090 result[v] = aug_p;
4091 LY_ARRAY_INCREMENT(result);
4092}
4093
4094/**
4095 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4096 *
4097 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4098 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4099 *
4100 * @param[in] ctx Compile context.
4101 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4102 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4103 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4104 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4105 * @return LY_ERR value.
4106 */
4107LY_ERR
4108lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4109{
4110 struct lysp_augment **result = NULL;
4111 unsigned int u, v;
4112 size_t count = 0;
4113
4114 assert(augments);
4115
4116 /* get count of the augments in module and all its submodules */
4117 if (aug_p) {
4118 count += LY_ARRAY_SIZE(aug_p);
4119 }
4120 LY_ARRAY_FOR(inc_p, u) {
4121 if (inc_p[u].submodule->augments) {
4122 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4123 }
4124 }
4125
4126 if (!count) {
4127 *augments = NULL;
4128 return LY_SUCCESS;
4129 }
4130 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4131
4132 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4133 * together, so there can be even /z/y betwwen them. */
4134 LY_ARRAY_FOR(aug_p, u) {
4135 lys_compile_augment_sort_(&aug_p[u], result);
4136 }
4137 LY_ARRAY_FOR(inc_p, u) {
4138 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4139 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4140 }
4141 }
4142
4143 *augments = result;
4144 return LY_SUCCESS;
4145}
4146
4147/**
4148 * @brief Compile the parsed augment connecting it into its target.
4149 *
4150 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4151 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4152 * are already implemented and compiled.
4153 *
4154 * @param[in] ctx Compile context.
4155 * @param[in] aug_p Parsed augment to compile.
4156 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4157 * @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
4158 * children in case of the augmenting uses data.
4159 * @return LY_SUCCESS on success.
4160 * @return LY_EVALID on failure.
4161 */
4162LY_ERR
4163lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4164{
4165 LY_ERR ret = LY_SUCCESS;
4166 struct lysp_node *node_p, *case_node_p;
4167 struct lysc_node *target; /* target target of the augment */
4168 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004169 struct lysc_when **when, *when_shared;
4170 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004171 uint16_t flags = 0;
4172 unsigned int u;
Radek Krejci3641f562019-02-13 15:38:40 +01004173
Radek Krejci7af64242019-02-18 13:07:53 +01004174 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004175 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004176 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004177 if (ret != LY_SUCCESS) {
4178 if (ret == LY_EDENIED) {
4179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4180 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4181 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4182 }
4183 return LY_EVALID;
4184 }
4185
4186 /* check for mandatory nodes
4187 * - new cases augmenting some choice can have mandatory nodes
4188 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4189 */
Radek Krejci733988a2019-02-15 15:12:44 +01004190 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004191 allow_mandatory = 1;
4192 }
4193
4194 when_shared = NULL;
4195 LY_LIST_FOR(aug_p->child, node_p) {
4196 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4197 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4198 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004199 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4200 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4202 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4203 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4204 return LY_EVALID;
4205 }
4206
4207 /* compile the children */
4208 if (node_p->nodetype != LYS_CASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004209 LY_CHECK_RET(lys_compile_node(ctx, node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004210 } else {
4211 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004212 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004213 }
4214 }
4215
Radek Krejcife13da42019-02-15 14:51:01 +01004216 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4217 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004218 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004219 /* 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 +01004220 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 +01004221 } else if (target->nodetype == LYS_CHOICE) {
4222 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4223 node = ((struct lysc_node_choice*)target)->cases->prev;
4224 } else {
4225 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004226 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004227 }
4228
Radek Krejci733988a2019-02-15 15:12:44 +01004229 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004230 node->flags &= ~LYS_MAND_TRUE;
4231 lys_compile_mandatory_parents(target, 0);
4232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4233 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4234 aug_p->nodeid, node->name);
4235 return LY_EVALID;
4236 }
4237
4238 /* pass augment's when to all the children */
4239 if (aug_p->when) {
4240 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4241 if (!when_shared) {
4242 ret = lys_compile_when(ctx, aug_p->when, options, when);
4243 LY_CHECK_GOTO(ret, error);
4244 (*when)->context = lysc_xpath_context(target);
4245 when_shared = *when;
4246 } else {
4247 ++when_shared->refcount;
4248 (*when) = when_shared;
4249 }
4250 }
4251 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004252
4253 switch (target->nodetype) {
4254 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004255 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
4256 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004257 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
4258 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004259 break;
4260 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004261 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
4262 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004263 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
4264 options | flags, u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004265 break;
4266 default:
4267 if (aug_p->actions) {
4268 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4269 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4270 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4271 return LY_EVALID;
4272 }
4273 if (aug_p->notifs) {
4274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4275 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4276 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4277 return LY_EVALID;
4278 }
4279 }
Radek Krejci3641f562019-02-13 15:38:40 +01004280
4281error:
4282 return ret;
4283}
4284
4285/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004286 * @brief Apply refined or deviated mandatory flag to the target node.
4287 *
4288 * @param[in] ctx Compile context.
4289 * @param[in] node Target node where the mandatory property is supposed to be changed.
4290 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4291 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4292 * @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 +01004293 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4294 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4295 * 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 +01004296 * @return LY_ERR value.
4297 */
4298static LY_ERR
4299lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4300{
4301 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4303 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4304 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4305 return LY_EVALID;
4306 }
4307
4308 if (mandatory_flag & LYS_MAND_TRUE) {
4309 /* check if node has default value */
4310 if (node->nodetype & LYS_LEAF) {
4311 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004312 if (refine_flag) {
4313 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4314 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4315 return LY_EVALID;
4316 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004317 } else {
4318 /* remove the default value taken from the leaf's type */
4319 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4320 ((struct lysc_node_leaf*)node)->dflt = NULL;
4321 }
4322 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004323 if (refine_flag) {
4324 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4325 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4326 return LY_EVALID;
4327 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004328 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004329 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004331 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004332 return LY_EVALID;
4333 }
4334
4335 node->flags &= ~LYS_MAND_FALSE;
4336 node->flags |= LYS_MAND_TRUE;
4337 lys_compile_mandatory_parents(node->parent, 1);
4338 } else {
4339 /* make mandatory false */
4340 node->flags &= ~LYS_MAND_TRUE;
4341 node->flags |= LYS_MAND_FALSE;
4342 lys_compile_mandatory_parents(node->parent, 0);
4343 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4344 /* get the type's default value if any */
4345 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4346 }
4347 }
4348 return LY_SUCCESS;
4349}
4350
4351/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004352 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4353 * If present, also apply uses's modificators.
4354 *
4355 * @param[in] ctx Compile context
4356 * @param[in] uses_p Parsed uses schema node.
4357 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4358 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4359 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4360 * the compile context.
4361 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4362 */
4363static LY_ERR
4364lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
4365{
4366 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004367 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004368 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004369 struct lysc_node_container context_node_fake =
4370 {.nodetype = LYS_CONTAINER,
4371 .module = ctx->mod,
4372 .flags = parent ? parent->flags : 0,
4373 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004374 .prev = (struct lysc_node*)&context_node_fake,
4375 .actions = NULL, .notifs = NULL};
Radek Krejcie86bf772018-12-14 11:39:53 +01004376 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004377 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004378 int found;
4379 const char *id, *name, *prefix;
4380 size_t prefix_len, name_len;
4381 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004382 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004383 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004384 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004385 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004386 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004387 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004388 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004389 unsigned int actions_index, notifs_index;
4390 struct lysc_notif **notifs = NULL;
4391 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004392
4393 /* search for the grouping definition */
4394 found = 0;
4395 id = uses_p->name;
Radek Krejci15e99fc2019-04-08 14:29:39 +02004396 LY_CHECK_RET(lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004397 if (prefix) {
4398 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4399 if (!mod) {
4400 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4401 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4402 return LY_EVALID;
4403 }
4404 } else {
4405 mod = ctx->mod_def;
4406 }
4407 if (mod == ctx->mod_def) {
4408 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
4409 grp = lysp_node_groupings(node_p);
4410 LY_ARRAY_FOR(grp, u) {
4411 if (!strcmp(grp[u].name, name)) {
4412 grp = &grp[u];
4413 found = 1;
4414 break;
4415 }
4416 }
4417 }
4418 }
4419 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004420 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004421 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004422 if (grp) {
4423 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4424 if (!strcmp(grp[u].name, name)) {
4425 grp = &grp[u];
4426 found = 1;
4427 }
4428 }
4429 }
4430 if (!found && mod->parsed->includes) {
4431 /* ... and all the submodules */
4432 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4433 grp = mod->parsed->includes[u].submodule->groupings;
4434 if (grp) {
4435 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4436 if (!strcmp(grp[v].name, name)) {
4437 grp = &grp[v];
4438 found = 1;
4439 }
4440 }
4441 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004442 }
4443 }
4444 }
4445 if (!found) {
4446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4447 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4448 return LY_EVALID;
4449 }
4450
4451 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4452 grp_stack_count = ctx->groupings.count;
4453 ly_set_add(&ctx->groupings, (void*)grp, 0);
4454 if (grp_stack_count == ctx->groupings.count) {
4455 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4456 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4457 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4458 return LY_EVALID;
4459 }
4460
4461 /* switch context's mod_def */
4462 mod_old = ctx->mod_def;
4463 ctx->mod_def = mod;
4464
4465 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004466 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 +01004467
Radek Krejcifc11bd72019-04-11 16:00:05 +02004468 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004469 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004470 /* 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 +02004471 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 +01004472 child = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01004473
4474 /* some preparation for applying refines */
4475 if (grp->data == node_p) {
4476 /* remember the first child */
4477 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01004478 }
4479 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004480 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004481 LY_LIST_FOR(context_node_fake.child, child) {
4482 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004483
Radek Krejcifc11bd72019-04-11 16:00:05 +02004484 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004485 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004486 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004487 if (!when_shared) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004488 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004489 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004490 when_shared = *when;
4491 } else {
4492 ++when_shared->refcount;
4493 (*when) = when_shared;
4494 }
4495 }
Radek Krejci01342af2019-01-03 15:18:08 +01004496 }
4497 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004498 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004499 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004500 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004501 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 +01004502 }
4503
Radek Krejcifc11bd72019-04-11 16:00:05 +02004504 /* compile actions */
4505 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4506 if (actions) {
4507 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
4508 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, options, u, lys_compile_action, 0, ret, cleanup);
4509 if (*actions && (uses_p->augments || uses_p->refines)) {
4510 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4511 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4512 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4513 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4514 }
4515 }
4516
4517 /* compile notifications */
4518 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4519 if (notifs) {
4520 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
4521 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, options, u, lys_compile_notif, 0, ret, cleanup);
4522 if (*notifs && (uses_p->augments || uses_p->refines)) {
4523 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4524 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4525 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4526 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4527 }
4528 }
4529
4530
Radek Krejci3641f562019-02-13 15:38:40 +01004531 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004532 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004533 LY_ARRAY_FOR(augments, u) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004534 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004535 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004536
Radek Krejcif0089082019-01-07 16:42:01 +01004537 /* reload previous context's mod_def */
4538 ctx->mod_def = mod_old;
4539
Radek Krejci76b3e962018-12-14 17:01:25 +01004540 /* apply refine */
4541 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004542 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 +01004543 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004544 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004545 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004546
4547 /* default value */
4548 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004549 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4551 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4552 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004553 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004554 }
4555 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4557 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4558 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004559 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004560 }
4561 if (node->nodetype == LYS_LEAF) {
4562 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4563 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004564 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004565 /* TODO check the default value according to type */
4566 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004567 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4569 "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 +02004570 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004571 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004572 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4573 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4574 }
4575 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004576 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004577 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 +01004578 LY_ARRAY_FOR(rfn->dflts, u) {
4579 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4580 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4581 }
4582 /* TODO check the default values according to type */
4583 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004584 if (((struct lysc_node_choice*)node)->dflt) {
4585 /* unset LYS_SET_DFLT from the current default case */
4586 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4587 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004588 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 +01004589 }
4590 }
4591
Radek Krejci12fb9142019-01-08 09:45:30 +01004592 /* description */
4593 if (rfn->dsc) {
4594 FREE_STRING(ctx->ctx, node->dsc);
4595 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4596 }
4597
4598 /* reference */
4599 if (rfn->ref) {
4600 FREE_STRING(ctx->ctx, node->ref);
4601 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4602 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004603
4604 /* config */
4605 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004606 if (!flags) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004607 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004608 } else {
4609 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4610 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4611 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004612 }
4613
4614 /* mandatory */
4615 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004616 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004617 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004618
4619 /* presence */
4620 if (rfn->presence) {
4621 if (node->nodetype != LYS_CONTAINER) {
4622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4623 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4624 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004625 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004626 }
4627 node->flags |= LYS_PRESENCE;
4628 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004629
4630 /* must */
4631 if (rfn->musts) {
4632 switch (node->nodetype) {
4633 case LYS_LEAF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004634 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 +01004635 break;
4636 case LYS_LEAFLIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004637 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 +01004638 break;
4639 case LYS_LIST:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004640 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 +01004641 break;
4642 case LYS_CONTAINER:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004643 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 +01004644 break;
4645 case LYS_ANYXML:
4646 case LYS_ANYDATA:
Radek Krejcifc11bd72019-04-11 16:00:05 +02004647 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 +01004648 break;
4649 default:
4650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4651 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4652 rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004653 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004654 }
4655 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004656
4657 /* min/max-elements */
4658 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4659 switch (node->nodetype) {
4660 case LYS_LEAFLIST:
4661 if (rfn->flags & LYS_SET_MAX) {
4662 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4663 }
4664 if (rfn->flags & LYS_SET_MIN) {
4665 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004666 if (rfn->min) {
4667 node->flags |= LYS_MAND_TRUE;
4668 lys_compile_mandatory_parents(node->parent, 1);
4669 } else {
4670 node->flags &= ~LYS_MAND_TRUE;
4671 lys_compile_mandatory_parents(node->parent, 0);
4672 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004673 }
4674 break;
4675 case LYS_LIST:
4676 if (rfn->flags & LYS_SET_MAX) {
4677 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4678 }
4679 if (rfn->flags & LYS_SET_MIN) {
4680 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004681 if (rfn->min) {
4682 node->flags |= LYS_MAND_TRUE;
4683 lys_compile_mandatory_parents(node->parent, 1);
4684 } else {
4685 node->flags &= ~LYS_MAND_TRUE;
4686 lys_compile_mandatory_parents(node->parent, 0);
4687 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004688 }
4689 break;
4690 default:
4691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4692 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4693 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004694 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01004695 }
4696 }
Radek Krejcif0089082019-01-07 16:42:01 +01004697
4698 /* if-feature */
4699 if (rfn->iffeatures) {
4700 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004701 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01004702 }
Radek Krejci01342af2019-01-03 15:18:08 +01004703 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004704
Radek Krejcif2271f12019-01-07 16:42:23 +01004705 /* do some additional checks of the changed nodes when all the refines are applied */
4706 for (u = 0; u < refined.count; ++u) {
4707 node = (struct lysc_node*)refined.objs[u];
4708 rfn = &uses_p->refines[u];
4709
4710 /* check possible conflict with default value (default added, mandatory left true) */
4711 if ((node->flags & LYS_MAND_TRUE) &&
4712 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4713 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4714 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4715 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004716 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004717 }
4718
4719 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4720 if (node->nodetype == LYS_LIST) {
4721 min = ((struct lysc_node_list*)node)->min;
4722 max = ((struct lysc_node_list*)node)->max;
4723 } else {
4724 min = ((struct lysc_node_leaflist*)node)->min;
4725 max = ((struct lysc_node_leaflist*)node)->max;
4726 }
4727 if (min > max) {
4728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4729 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4730 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004731 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01004732 }
4733 }
4734 }
4735
Radek Krejcie86bf772018-12-14 11:39:53 +01004736 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004737
4738cleanup:
4739 /* fix connection of the children nodes from fake context node back into the parent */
4740 if (context_node_fake.child) {
4741 context_node_fake.child->prev = child;
4742 }
4743 LY_LIST_FOR(context_node_fake.child, child) {
4744 child->parent = parent;
4745 }
4746
4747 if (uses_p->augments || uses_p->refines) {
4748 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02004749 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004750 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4751 LY_ARRAY_FREE(context_node_fake.actions);
4752 }
Radek Krejci65e20e22019-04-12 09:44:37 +02004753 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004754 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4755 LY_ARRAY_FREE(context_node_fake.notifs);
4756 }
4757 }
4758
Radek Krejcie86bf772018-12-14 11:39:53 +01004759 /* reload previous context's mod_def */
4760 ctx->mod_def = mod_old;
4761 /* remove the grouping from the stack for circular groupings dependency check */
4762 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4763 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004764 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004765 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004766
4767 return ret;
4768}
4769
Radek Krejcife909632019-02-12 15:34:42 +01004770
Radek Krejcie86bf772018-12-14 11:39:53 +01004771/**
Radek Krejcia3045382018-11-22 14:30:31 +01004772 * @brief Compile parsed schema node information.
4773 * @param[in] ctx Compile context
4774 * @param[in] node_p Parsed schema node.
4775 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4776 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4777 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4778 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004779 * @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).
4780 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004781 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4782 */
Radek Krejci19a96102018-11-15 13:38:09 +01004783static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01004784lys_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 +01004785{
4786 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004787 struct lysc_node *node;
4788 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004789 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004790 unsigned int u;
4791 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
4792
4793 switch (node_p->nodetype) {
4794 case LYS_CONTAINER:
4795 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4796 node_compile_spec = lys_compile_node_container;
4797 break;
4798 case LYS_LEAF:
4799 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4800 node_compile_spec = lys_compile_node_leaf;
4801 break;
4802 case LYS_LIST:
4803 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004804 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004805 break;
4806 case LYS_LEAFLIST:
4807 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004808 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004809 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004810 case LYS_CHOICE:
4811 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004812 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004813 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004814 case LYS_ANYXML:
4815 case LYS_ANYDATA:
4816 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004817 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004818 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004819 case LYS_USES:
4820 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004821 default:
4822 LOGINT(ctx->ctx);
4823 return LY_EINT;
4824 }
4825 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4826 node->nodetype = node_p->nodetype;
4827 node->module = ctx->mod;
4828 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004829 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004830
4831 /* config */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004832 if (options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
4833 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004834 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004835 node->flags |= (options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4836 } else if (options & LYSC_OPT_NOTIFICATION) {
4837 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004838 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004839 node->flags |= LYS_CONFIG_R;
4840 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004841 /* config not explicitely set, inherit it from parent */
4842 if (parent) {
4843 node->flags |= parent->flags & LYS_CONFIG_MASK;
4844 } else {
4845 /* default is config true */
4846 node->flags |= LYS_CONFIG_W;
4847 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004848 } else {
4849 /* config set explicitely */
4850 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004851 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004852 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4853 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4854 "Configuration node cannot be child of any state data node.");
4855 goto error;
4856 }
Radek Krejci19a96102018-11-15 13:38:09 +01004857
Radek Krejcia6d57732018-11-29 13:40:37 +01004858 /* *list ordering */
4859 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4860 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004861 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
4862 (options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
4863 (options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01004864 node->flags &= ~LYS_ORDBY_MASK;
4865 node->flags |= LYS_ORDBY_SYSTEM;
4866 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4867 /* default ordering is system */
4868 node->flags |= LYS_ORDBY_SYSTEM;
4869 }
4870 }
4871
Radek Krejci19a96102018-11-15 13:38:09 +01004872 /* status - it is not inherited by specification, but it does not make sense to have
4873 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004874 if (!parent || parent->nodetype != LYS_CHOICE) {
4875 /* in case of choice/case's children, postpone the check to the moment we know if
4876 * 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 +01004877 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 +01004878 }
4879
4880 if (!(options & LYSC_OPT_FREE_SP)) {
4881 node->sp = node_p;
4882 }
4883 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004884 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4885 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004886 if (node_p->when) {
4887 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4888 ret = lys_compile_when(ctx, node_p->when, options, when);
4889 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004890 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004891 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004892 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004893 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
4894
4895 /* nodetype-specific part */
4896 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
4897
Radek Krejcife909632019-02-12 15:34:42 +01004898 /* inherit LYS_MAND_TRUE in parent containers */
4899 if (node->flags & LYS_MAND_TRUE) {
4900 lys_compile_mandatory_parents(parent, 1);
4901 }
4902
Radek Krejci19a96102018-11-15 13:38:09 +01004903 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004904 if (parent) {
4905 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004906 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
4907 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004908 if (uses_status) {
4909
4910 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004911 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004912 * it directly gets the same status flags as the choice;
4913 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004914 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004915 node->parent = (struct lysc_node*)cs;
4916 } else { /* other than choice */
4917 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004918 }
Radek Krejci05b774b2019-02-25 13:26:18 +01004919 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 +01004920 } else {
4921 /* top-level element */
4922 if (!ctx->mod->compiled->data) {
4923 ctx->mod->compiled->data = node;
4924 } else {
4925 /* insert at the end of the module's top-level nodes list */
4926 ctx->mod->compiled->data->prev->next = node;
4927 node->prev = ctx->mod->compiled->data->prev;
4928 ctx->mod->compiled->data->prev = node;
4929 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004930 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4931 ctx->mod->compiled->notifs, node->name, node)) {
4932 return LY_EVALID;
4933 }
Radek Krejci19a96102018-11-15 13:38:09 +01004934 }
4935
4936 return LY_SUCCESS;
4937
4938error:
4939 lysc_node_free(ctx->ctx, node);
4940 return ret;
4941}
4942
Radek Krejciccd20f12019-02-15 14:12:27 +01004943static void
4944lysc_disconnect(struct lysc_node *node)
4945{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004946 struct lysc_node *parent, *child, *prev = NULL, *next;
4947 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004948 int remove_cs = 0;
4949
4950 parent = node->parent;
4951
4952 /* parent's first child */
4953 if (!parent) {
4954 return;
4955 }
4956 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004957 cs = (struct lysc_node_case*)node;
4958 } else if (parent->nodetype == LYS_CASE) {
4959 /* disconnecting some node in a case */
4960 cs = (struct lysc_node_case*)parent;
4961 parent = cs->parent;
4962 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4963 if (child == node) {
4964 if (cs->child == child) {
4965 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
4966 /* case with a single child -> remove also the case */
4967 child->parent = NULL;
4968 remove_cs = 1;
4969 } else {
4970 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01004971 }
4972 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004973 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01004974 }
4975 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004976 if (!remove_cs) {
4977 cs = NULL;
4978 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004979 } else if (lysc_node_children(parent, node->flags) == node) {
4980 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004981 }
4982
4983 if (cs) {
4984 if (remove_cs) {
4985 /* cs has only one child which is being also removed */
4986 lysc_disconnect((struct lysc_node*)cs);
4987 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
4988 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01004989 if (((struct lysc_node_choice*)parent)->dflt == cs) {
4990 /* default case removed */
4991 ((struct lysc_node_choice*)parent)->dflt = NULL;
4992 }
4993 if (((struct lysc_node_choice*)parent)->cases == cs) {
4994 /* first case removed */
4995 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
4996 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004997 if (cs->child) {
4998 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
4999 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5000 prev = cs->child->prev;
5001 } /* else all the children are under a single case */
5002 LY_LIST_FOR_SAFE(cs->child, next, child) {
5003 if (child->parent != (struct lysc_node*)cs) {
5004 break;
5005 }
5006 lysc_node_free(node->module->ctx, child);
5007 }
5008 if (prev) {
5009 if (prev->next) {
5010 prev->next = child;
5011 }
5012 if (child) {
5013 child->prev = prev;
5014 } else {
5015 /* link from the first child under the cases */
5016 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5017 }
5018 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005019 }
5020 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005021 }
5022
5023 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005024 if (node->prev->next) {
5025 node->prev->next = node->next;
5026 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005027 if (node->next) {
5028 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005029 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005030 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005031 if (child) {
5032 child->prev = node->prev;
5033 }
5034 } else if (((struct lysc_node_choice*)parent)->cases) {
5035 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005036 }
5037}
5038
5039LY_ERR
5040lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options)
5041{
5042 LY_ERR ret = LY_EVALID;
5043 struct ly_set devs_p = {0};
5044 struct ly_set targets = {0};
5045 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005046 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005047 struct lysc_action *rpcs;
5048 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005049 struct lysp_deviation *dev;
5050 struct lysp_deviate *d, **dp_new;
5051 struct lysp_deviate_add *d_add;
5052 struct lysp_deviate_del *d_del;
5053 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005054 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005055 struct lysc_deviation {
5056 const char *nodeid;
5057 struct lysc_node *target; /* target node of the deviation */
5058 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005059 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005060 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5061 } **devs = NULL;
5062 int i;
5063 size_t prefix_len, name_len;
5064 const char *prefix, *name, *nodeid;
5065 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005066 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005067 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005068
5069 /* get all deviations from the module and all its submodules ... */
5070 LY_ARRAY_FOR(mod_p->deviations, u) {
5071 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5072 }
5073 LY_ARRAY_FOR(mod_p->includes, v) {
5074 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5075 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5076 }
5077 }
5078 if (!devs_p.count) {
5079 /* nothing to do */
5080 return LY_SUCCESS;
5081 }
5082
5083 /* ... and group them by the target node */
5084 devs = calloc(devs_p.count, sizeof *devs);
5085 for (u = 0; u < devs_p.count; ++u) {
5086 dev = devs_p.objs[u];
5087
5088 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005089 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5090 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005091 if (target->nodetype == LYS_ACTION) {
5092 /* move the target pointer to input/output to make them different from the action and
5093 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5094 * back to the RPC/action node due to a better compatibility and decision code in this function.
5095 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5096 if (flags & LYSC_OPT_RPC_INPUT) {
5097 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5098 flags |= LYSC_OPT_INTERNAL;
5099 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5100 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5101 flags |= LYSC_OPT_INTERNAL;
5102 }
5103 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005104 /* insert into the set of targets with duplicity detection */
5105 i = ly_set_add(&targets, target, 0);
5106 if (!devs[i]) {
5107 /* new record */
5108 devs[i] = calloc(1, sizeof **devs);
5109 devs[i]->target = target;
5110 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005111 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005112 }
5113 /* add deviates into the deviation's list of deviates */
5114 for (d = dev->deviates; d; d = d->next) {
5115 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5116 *dp_new = d;
5117 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5118 devs[i]->not_supported = 1;
5119 }
5120 }
5121 }
5122
5123 /* MACROS for deviates checking */
5124#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5125 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
5126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
5127 goto cleanup; \
5128 }
5129
5130#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5131 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
5132 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
5133 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
5134 goto cleanup; \
5135 }
5136
5137#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005138 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5140 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
5141 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5142 goto cleanup; \
5143 }
5144
Radek Krejci551b12c2019-02-19 16:11:21 +01005145#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5146 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5147 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5148 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
5149 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5150 goto cleanup; \
5151 }
5152
Radek Krejciccd20f12019-02-15 14:12:27 +01005153#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5154 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5155 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5156 goto cleanup; \
5157 }
5158
Radek Krejci551b12c2019-02-19 16:11:21 +01005159#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5160 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5162 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5163 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5164 goto cleanup; \
5165 }
5166
Radek Krejciccd20f12019-02-15 14:12:27 +01005167#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5168 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5169 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5170 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5171 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5172 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5173 goto cleanup; \
5174 } \
5175 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5176 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5177
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005178#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5179 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5180 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5181 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5182 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 +01005183 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005184 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005185 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5186 "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 +01005187 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005188 goto cleanup; \
5189 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005190 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5191 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5192 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5193 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5194 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005195 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005196 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5197 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5198 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005199 }
5200
5201 /* apply deviations */
5202 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005203 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5204 /* fix the target pointer in case of RPC's/action's input/output */
5205 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5206 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5207 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5208 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5209 }
5210 }
5211
Radek Krejciccd20f12019-02-15 14:12:27 +01005212 /* not-supported */
5213 if (devs[u]->not_supported) {
5214 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5215 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5216 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5217 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005218
5219#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5220 if (devs[u]->target->parent) { \
5221 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5222 } else { \
5223 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5224 } \
5225 LY_ARRAY_FOR(ARRAY, x) { \
5226 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5227 } \
5228 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5229 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5230 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5231 LY_ARRAY_DECREMENT(ARRAY); \
5232 }
5233
Radek Krejcif538ce52019-03-05 10:46:14 +01005234 if (devs[u]->target->nodetype == LYS_ACTION) {
5235 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5236 /* remove RPC's/action's input */
5237 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5238 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005239 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5240 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005241 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5242 /* remove RPC's/action's output */
5243 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5244 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005245 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5246 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005247 } else {
5248 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005249 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005250 }
5251 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005252 /* remove Notification */
5253 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005254 } else {
5255 /* remove the target node */
5256 lysc_disconnect(devs[u]->target);
5257 lysc_node_free(ctx->ctx, devs[u]->target);
5258 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005259
5260 continue;
5261 }
5262
5263 /* list of deviates (not-supported is not present in the list) */
5264 LY_ARRAY_FOR(devs[u]->deviates, v) {
5265 d = devs[u]->deviates[v];
5266
5267 switch (d->mod) {
5268 case LYS_DEV_ADD:
5269 d_add = (struct lysp_deviate_add*)d;
5270 /* [units-stmt] */
5271 if (d_add->units) {
5272 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5273 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5274
5275 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5276 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5277 }
5278
5279 /* *must-stmt */
5280 if (d_add->musts) {
5281 switch (devs[u]->target->nodetype) {
5282 case LYS_CONTAINER:
5283 case LYS_LIST:
5284 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5285 options, x, lys_compile_must, ret, cleanup);
5286 break;
5287 case LYS_LEAF:
5288 case LYS_LEAFLIST:
5289 case LYS_ANYDATA:
5290 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5291 options, x, lys_compile_must, ret, cleanup);
5292 break;
5293 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005294 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5295 options, x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005296 break;
5297 case LYS_ACTION:
5298 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5299 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5300 options, x, lys_compile_must, ret, cleanup);
5301 break;
5302 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5303 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5304 options, x, lys_compile_must, ret, cleanup);
5305 break;
5306 }
5307 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005308 default:
5309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005310 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005311 goto cleanup;
5312 }
5313 }
5314
5315 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005316 if (d_add->uniques) {
5317 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5318 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5319 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005320
5321 /* *default-stmt */
5322 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005323 switch (devs[u]->target->nodetype) {
5324 case LYS_LEAF:
5325 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5326 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5327 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5328 /* first, remove the default value taken from the type */
5329 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5330 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5331 }
5332 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005333 /* mark the new default values as leaf's own */
5334 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005335 break;
5336 case LYS_LEAFLIST:
5337 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5338 /* first, remove the default value taken from the type */
5339 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5340 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5341 }
5342 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5343 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5344 }
5345 /* add new default value(s) */
5346 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5347 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5348 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5349 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5350 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5351 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5352 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005353 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005354 devs[u]->target->flags |= LYS_SET_DFLT;
5355 break;
5356 case LYS_CHOICE:
5357 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5358 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5359 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5360 * to allow making the default case even the augmented case from the deviating module */
5361 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5362 (struct lysc_node_choice*)devs[u]->target)) {
5363 goto cleanup;
5364 }
5365 break;
5366 default:
5367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005368 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005369 goto cleanup;
5370 }
5371 }
5372
5373 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005374 if (d_add->flags & LYS_CONFIG_MASK) {
5375 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5376 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5377 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5378 goto cleanup;
5379 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005380 if (devs[u]->flags) {
5381 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5382 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5383 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005384 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5386 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5387 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5388 goto cleanup;
5389 }
5390 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5391 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005392
5393 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005394 if (d_add->flags & LYS_MAND_MASK) {
5395 if (devs[u]->target->flags & LYS_MAND_MASK) {
5396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5397 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5398 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5399 goto cleanup;
5400 }
5401 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5402 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005403
5404 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005405 if (d_add->flags & LYS_SET_MIN) {
5406 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5407 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5408 /* change value */
5409 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5410 } else if (devs[u]->target->nodetype == LYS_LIST) {
5411 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5412 /* change value */
5413 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5414 } else {
5415 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5416 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5417 goto cleanup;
5418 }
5419 if (d_add->min) {
5420 devs[u]->target->flags |= LYS_MAND_TRUE;
5421 }
5422 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005423
5424 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005425 if (d_add->flags & LYS_SET_MAX) {
5426 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5427 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5428 /* change value */
5429 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5430 } else if (devs[u]->target->nodetype == LYS_LIST) {
5431 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5432 /* change value */
5433 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5434 } else {
5435 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5436 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5437 goto cleanup;
5438 }
5439 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005440
5441 break;
5442 case LYS_DEV_DELETE:
5443 d_del = (struct lysp_deviate_del*)d;
5444
5445 /* [units-stmt] */
5446 if (d_del->units) {
5447 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5448 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5449 }
5450
5451 /* *must-stmt */
5452 if (d_del->musts) {
5453 switch (devs[u]->target->nodetype) {
5454 case LYS_CONTAINER:
5455 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005456 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005457 break;
5458 case LYS_LEAF:
5459 case LYS_LEAFLIST:
5460 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005461 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005462 break;
5463 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005464 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005465 break;
5466 case LYS_ACTION:
5467 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5468 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5469 break;
5470 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5471 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5472 break;
5473 }
5474 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005475 default:
5476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005477 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005478 goto cleanup;
5479 }
5480 }
5481
5482 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005483 if (d_del->uniques) {
5484 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5485 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5486 LY_ARRAY_FOR(d_del->uniques, x) {
5487 LY_ARRAY_FOR(list->uniques, z) {
5488 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5489 nodeid = strpbrk(name, " \t\n");
5490 if (nodeid) {
5491 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5492 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5493 break;
5494 }
5495 while (isspace(*nodeid)) {
5496 ++nodeid;
5497 }
5498 } else {
5499 if (strcmp(name, list->uniques[z][y]->name)) {
5500 break;
5501 }
5502 }
5503 }
5504 if (!name) {
5505 /* complete match - remove the unique */
5506 LY_ARRAY_DECREMENT(list->uniques);
5507 LY_ARRAY_FREE(list->uniques[z]);
5508 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5509 --z;
5510 break;
5511 }
5512 }
5513 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5515 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5516 devs[u]->nodeid, d_del->uniques[x]);
5517 goto cleanup;
5518 }
5519 }
5520 if (!LY_ARRAY_SIZE(list->uniques)) {
5521 LY_ARRAY_FREE(list->uniques);
5522 list->uniques = NULL;
5523 }
5524 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005525
5526 /* *default-stmt */
5527 if (d_del->dflts) {
5528 switch (devs[u]->target->nodetype) {
5529 case LYS_LEAF:
5530 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5531 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5532 dflt, "deleting", "default", d_del->dflts[0]);
5533
5534 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005535 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005536 break;
5537 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005538 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005539 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5540 devs[u]->target->flags &= ~LYS_SET_DFLT;
5541 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005542 break;
5543 case LYS_CHOICE:
5544 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5545 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5546 nodeid = d_del->dflts[0];
5547 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5548 if (prefix) {
5549 /* use module prefixes from the deviation module to match the module of the default case */
5550 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5551 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5552 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5553 "The prefix does not match any imported module of the deviation module.",
5554 devs[u]->nodeid, d_del->dflts[0]);
5555 goto cleanup;
5556 }
5557 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5558 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5559 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5560 "The prefix does not match the default case's module.",
5561 devs[u]->nodeid, d_del->dflts[0]);
5562 goto cleanup;
5563 }
5564 }
5565 /* else {
5566 * strictly, the default prefix would point to the deviation module, but the value should actually
5567 * match the default string in the original module (usually unprefixed), so in this case we do not check
5568 * the module of the default case, just matching its name */
5569 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5571 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5572 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5573 goto cleanup;
5574 }
5575 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5576 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5577 break;
5578 default:
5579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005580 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005581 goto cleanup;
5582 }
5583 }
5584
5585 break;
5586 case LYS_DEV_REPLACE:
5587 d_rpl = (struct lysp_deviate_rpl*)d;
5588
5589 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005590 if (d_rpl->type) {
5591 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5592 /* type is mandatory, so checking for its presence is not necessary */
5593 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
5594 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup);
5595 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005596
5597 /* [units-stmt] */
5598 if (d_rpl->units) {
5599 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5600 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5601 units, "replacing", "units", d_rpl->units);
5602
5603 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5604 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5605 }
5606
5607 /* [default-stmt] */
5608 if (d_rpl->dflt) {
5609 switch (devs[u]->target->nodetype) {
5610 case LYS_LEAF:
5611 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5612 dflt, "replacing", "default", d_rpl->dflt);
5613
5614 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5615 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5616 break;
5617 case LYS_CHOICE:
5618 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5619 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5620 (struct lysc_node_choice*)devs[u]->target)) {
5621 goto cleanup;
5622 }
5623 break;
5624 default:
5625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005626 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005627 goto cleanup;
5628 }
5629 }
5630
5631 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005632 if (d_rpl->flags & LYS_CONFIG_MASK) {
5633 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5635 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5636 goto cleanup;
5637 }
5638 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5640 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5641 goto cleanup;
5642 }
5643 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5644 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005645
5646 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005647 if (d_rpl->flags & LYS_MAND_MASK) {
5648 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5650 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5651 goto cleanup;
5652 }
5653 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5654 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005655
5656 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005657 if (d_rpl->flags & LYS_SET_MIN) {
5658 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5659 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5660 /* change value */
5661 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5662 } else if (devs[u]->target->nodetype == LYS_LIST) {
5663 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5664 /* change value */
5665 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5666 } else {
5667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5668 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5669 goto cleanup;
5670 }
5671 if (d_rpl->min) {
5672 devs[u]->target->flags |= LYS_MAND_TRUE;
5673 }
5674 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005675
5676 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005677 if (d_rpl->flags & LYS_SET_MAX) {
5678 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5679 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5680 /* change value */
5681 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5682 } else if (devs[u]->target->nodetype == LYS_LIST) {
5683 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5684 /* change value */
5685 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5686 } else {
5687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5688 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5689 goto cleanup;
5690 }
5691 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005692
5693 break;
5694 default:
5695 LOGINT(ctx->ctx);
5696 goto cleanup;
5697 }
5698 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005699
Radek Krejci33f72892019-02-21 10:36:58 +01005700 /* final check when all deviations of a single target node are applied */
5701
Radek Krejci551b12c2019-02-19 16:11:21 +01005702 /* check min-max compatibility */
5703 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5704 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5705 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5706 } else if (devs[u]->target->nodetype == LYS_LIST) {
5707 min = ((struct lysc_node_list*)devs[u]->target)->min;
5708 max = ((struct lysc_node_list*)devs[u]->target)->max;
5709 } else {
5710 min = max = 0;
5711 }
5712 if (min > max) {
5713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5714 "after deviation (%s): min value %u is bigger than max value %u.",
5715 devs[u]->nodeid, min, max);
5716 goto cleanup;
5717 }
5718
5719 /* check mandatory - default compatibility */
5720 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5721 && (devs[u]->target->flags & LYS_SET_DFLT)
5722 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5724 "Invalid deviation (%s) combining default value and mandatory %s.",
5725 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5726 goto cleanup;
5727 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5728 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5729 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5731 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5732 goto cleanup;
5733 }
5734 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5735 /* mandatory node under a default case */
5736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5737 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5738 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5739 goto cleanup;
5740 }
Radek Krejci33f72892019-02-21 10:36:58 +01005741
5742 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005743 }
5744
5745 ret = LY_SUCCESS;
5746
5747cleanup:
5748 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5749 LY_ARRAY_FREE(devs[u]->deviates);
5750 free(devs[u]);
5751 }
5752 free(devs);
5753 ly_set_erase(&targets, NULL);
5754 ly_set_erase(&devs_p, NULL);
5755
5756 return ret;
5757}
5758
Radek Krejcib56c7502019-02-13 14:19:54 +01005759/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005760 * @brief Compile the given YANG submodule into the main module.
5761 * @param[in] ctx Compile context
5762 * @param[in] inc Include structure from the main module defining the submodule.
5763 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
5764 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5765 */
5766LY_ERR
5767lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
5768{
5769 unsigned int u;
5770 LY_ERR ret = LY_SUCCESS;
5771 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005772 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005773 struct lysc_module *mainmod = ctx->mod->compiled;
5774
Radek Krejci0af46292019-01-11 16:02:31 +01005775 if (!mainmod->mod->off_features) {
5776 /* features are compiled directly into the compiled module structure,
5777 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5778 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci693262f2019-04-29 15:23:20 +02005779 ret = lys_feature_precompile(ctx->ctx, ctx->mod, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01005780 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5781 LY_CHECK_GOTO(ret, error);
5782 }
5783
Radek Krejcid05cbd92018-12-05 14:26:40 +01005784 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
5785
Radek Krejci8cce8532019-03-05 11:27:45 +01005786 /* TODO data nodes */
5787
5788 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005789 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, options, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01005790
Radek Krejcid05cbd92018-12-05 14:26:40 +01005791error:
5792 return ret;
5793}
5794
Radek Krejci19a96102018-11-15 13:38:09 +01005795LY_ERR
5796lys_compile(struct lys_module *mod, int options)
5797{
5798 struct lysc_ctx ctx = {0};
5799 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005800 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005801 struct lysp_module *sp;
5802 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005803 struct lysp_augment **augments = NULL;
5804 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005805 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005806 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005807
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005808 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005809
5810 if (!mod->implemented) {
5811 /* just imported modules are not compiled */
5812 return LY_SUCCESS;
5813 }
5814
Radek Krejci19a96102018-11-15 13:38:09 +01005815 sp = mod->parsed;
5816
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005817 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005818 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005819 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005820
5821 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005822 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5823 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005824
Radek Krejci19a96102018-11-15 13:38:09 +01005825 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005826 LY_ARRAY_FOR(sp->includes, u) {
5827 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
5828 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5829 }
Radek Krejci0af46292019-01-11 16:02:31 +01005830 if (mod->off_features) {
5831 /* there is already precompiled array of features */
5832 mod_c->features = mod->off_features;
5833 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005834 } else {
5835 /* features are compiled directly into the compiled module structure,
5836 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci693262f2019-04-29 15:23:20 +02005837 ret = lys_feature_precompile(ctx.ctx, ctx.mod, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01005838 LY_CHECK_GOTO(ret, error);
5839 }
5840 /* finish feature compilation, not only for the main module, but also for the submodules.
5841 * Due to possible forward references, it must be done when all the features (including submodules)
5842 * are present. */
5843 LY_ARRAY_FOR(sp->features, u) {
5844 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
5845 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5846 }
5847 LY_ARRAY_FOR(sp->includes, v) {
5848 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
5849 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
5850 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5851 }
5852 }
5853
Radek Krejcid05cbd92018-12-05 14:26:40 +01005854 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005855 if (sp->identities) {
5856 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5857 }
5858
Radek Krejci95710c92019-02-11 15:49:55 +01005859 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005860 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005861 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005862 LY_CHECK_GOTO(ret, error);
5863 }
Radek Krejci95710c92019-02-11 15:49:55 +01005864
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005865 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 +02005866 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 +01005867
Radek Krejci95710c92019-02-11 15:49:55 +01005868 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005869 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005870 LY_CHECK_GOTO(ret, error);
5871 LY_ARRAY_FOR(augments, u) {
5872 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
5873 LY_CHECK_GOTO(ret, error);
5874 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005875
5876 /* deviations */
5877 ret = lys_compile_deviations(&ctx, sp, options);
5878 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005879
5880 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
5881
Radek Krejcia3045382018-11-22 14:30:31 +01005882 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005883 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5884 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5885 * 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 +01005886 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005887 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005888 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5889 if (type->basetype == LY_TYPE_LEAFREF) {
5890 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005891 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 +01005892 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005893 } else if (type->basetype == LY_TYPE_UNION) {
5894 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5895 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5896 /* validate the path */
5897 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5898 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5899 LY_CHECK_GOTO(ret, error);
5900 }
5901 }
Radek Krejcia3045382018-11-22 14:30:31 +01005902 }
5903 }
5904 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005905 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005906 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005907 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5908 if (type->basetype == LY_TYPE_LEAFREF) {
5909 /* store pointer to the real type */
5910 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5911 typeiter->basetype == LY_TYPE_LEAFREF;
5912 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5913 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005914 } else if (type->basetype == LY_TYPE_UNION) {
5915 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5916 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5917 /* store pointer to the real type */
5918 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5919 typeiter->basetype == LY_TYPE_LEAFREF;
5920 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5921 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5922 }
5923 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005924 }
5925 }
5926 }
Radek Krejcia3045382018-11-22 14:30:31 +01005927 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005928 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02005929 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005930 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005931
Radek Krejci19a96102018-11-15 13:38:09 +01005932 if (options & LYSC_OPT_FREE_SP) {
5933 lysp_module_free(mod->parsed);
5934 ((struct lys_module*)mod)->parsed = NULL;
5935 }
5936
Radek Krejci95710c92019-02-11 15:49:55 +01005937 if (!(options & LYSC_OPT_INTERNAL)) {
5938 /* remove flag of the modules implemented by dependency */
5939 for (u = 0; u < ctx.ctx->list.count; ++u) {
5940 m = ctx.ctx->list.objs[u];
5941 if (m->implemented == 2) {
5942 m->implemented = 1;
5943 }
5944 }
5945 }
5946
Radek Krejci19a96102018-11-15 13:38:09 +01005947 ((struct lys_module*)mod)->compiled = mod_c;
5948 return LY_SUCCESS;
5949
5950error:
Radek Krejci95710c92019-02-11 15:49:55 +01005951 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01005952 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005953 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02005954 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005955 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01005956 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005957 mod->compiled = NULL;
5958
5959 /* revert compilation of modules implemented by dependency */
5960 for (u = 0; u < ctx.ctx->list.count; ++u) {
5961 m = ctx.ctx->list.objs[u];
5962 if (m->implemented == 2) {
5963 /* revert features list to the precompiled state */
5964 lys_feature_precompile_revert(&ctx, m);
5965 /* mark module as imported-only / not-implemented */
5966 m->implemented = 0;
5967 /* free the compiled version of the module */
5968 lysc_module_free(m->compiled, NULL);
5969 m->compiled = NULL;
5970 }
5971 }
5972
Radek Krejci19a96102018-11-15 13:38:09 +01005973 return ret;
5974}