blob: 2c4ea542536af0fee2df4c6855ad56a8dbe5ac43 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <ctype.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <stdio.h>
Radek Krejci19a96102018-11-15 13:38:09 +010019
20#include "libyang.h"
21#include "context.h"
22#include "tree_schema_internal.h"
23#include "xpath.h"
24
25/**
26 * @brief Duplicate string into dictionary
27 * @param[in] CTX libyang context of the dictionary.
28 * @param[in] ORIG String to duplicate.
29 * @param[out] DUP Where to store the result.
30 */
31#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
32
33#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
34 if (ARRAY_P) { \
35 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010036 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010037 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
38 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010039 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER + __array_offset]); \
40 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
41 } \
42 }
43
Radek Krejci6eeb58f2019-02-22 16:29:37 +010044#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, OPTIONS, ITER, FUNC, USES_STATUS, RET, GOTO) \
45 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
47 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
48 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
50 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
51 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejcid05cbd92018-12-05 14:26:40 +010055#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
56 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
61 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
66#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
67 if (MEMBER_P) { \
68 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
69 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
70 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
71 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 }
73
Radek Krejci00b874b2019-02-12 10:54:50 +010074#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \
75 if (MEMBER_P) { \
76 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
77 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
78 LY_ARRAY_INCREMENT(ARRAY_C); \
79 RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \
80 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
81 }
82
Radek Krejcid05cbd92018-12-05 14:26:40 +010083#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
84 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010085 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
86 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010087 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
88 return LY_EVALID; \
89 } \
90 } \
91 }
92
Radek Krejci19a96102018-11-15 13:38:09 +010093static struct lysc_ext_instance *
94lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
95{
96 /* TODO */
97 (void) ctx;
98 (void) orig;
99 return NULL;
100}
101
Radek Krejcib56c7502019-02-13 14:19:54 +0100102/**
103 * @brief Duplicate the compiled pattern structure.
104 *
105 * Instead of duplicating memory, the reference counter in the @p orig is increased.
106 *
107 * @param[in] orig The pattern structure to duplicate.
108 * @return The duplicated structure to use.
109 */
Radek Krejci19a96102018-11-15 13:38:09 +0100110static struct lysc_pattern*
111lysc_pattern_dup(struct lysc_pattern *orig)
112{
113 ++orig->refcount;
114 return orig;
115}
116
Radek Krejcib56c7502019-02-13 14:19:54 +0100117/**
118 * @brief Duplicate the array of compiled patterns.
119 *
120 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
121 *
122 * @param[in] ctx Libyang context for logging.
123 * @param[in] orig The patterns sized array to duplicate.
124 * @return New sized array as a copy of @p orig.
125 * @return NULL in case of memory allocation error.
126 */
Radek Krejci19a96102018-11-15 13:38:09 +0100127static struct lysc_pattern**
128lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
129{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100130 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100131 unsigned int u;
132
Radek Krejcib56c7502019-02-13 14:19:54 +0100133 assert(orig);
134
Radek Krejci19a96102018-11-15 13:38:09 +0100135 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
136 LY_ARRAY_FOR(orig, u) {
137 dup[u] = lysc_pattern_dup(orig[u]);
138 LY_ARRAY_INCREMENT(dup);
139 }
140 return dup;
141}
142
Radek Krejcib56c7502019-02-13 14:19:54 +0100143/**
144 * @brief Duplicate compiled range structure.
145 *
146 * @param[in] ctx Libyang context for logging.
147 * @param[in] orig The range structure to be duplicated.
148 * @return New compiled range structure as a copy of @p orig.
149 * @return NULL in case of memory allocation error.
150 */
Radek Krejci19a96102018-11-15 13:38:09 +0100151struct lysc_range*
152lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
153{
154 struct lysc_range *dup;
155 LY_ERR ret;
156
Radek Krejcib56c7502019-02-13 14:19:54 +0100157 assert(orig);
158
Radek Krejci19a96102018-11-15 13:38:09 +0100159 dup = calloc(1, sizeof *dup);
160 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
161 if (orig->parts) {
162 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
163 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
164 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
165 }
166 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
167 DUP_STRING(ctx, orig->emsg, dup->emsg);
168 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
169
170 return dup;
171cleanup:
172 free(dup);
173 (void) ret; /* set but not used due to the return type */
174 return NULL;
175}
176
Radek Krejcib56c7502019-02-13 14:19:54 +0100177/**
178 * @brief Stack for processing if-feature expressions.
179 */
Radek Krejci19a96102018-11-15 13:38:09 +0100180struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100181 int size; /**< number of items in the stack */
182 int index; /**< first empty item */
183 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100184};
185
Radek Krejcib56c7502019-02-13 14:19:54 +0100186/**
187 * @brief Add @ref ifftokens into the stack.
188 * @param[in] stack The if-feature stack to use.
189 * @param[in] value One of the @ref ifftokens to store in the stack.
190 * @return LY_EMEM in case of memory allocation error
191 * @return LY_ESUCCESS if the value successfully stored.
192 */
Radek Krejci19a96102018-11-15 13:38:09 +0100193static LY_ERR
194iff_stack_push(struct iff_stack *stack, uint8_t value)
195{
196 if (stack->index == stack->size) {
197 stack->size += 4;
198 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
199 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
200 }
201 stack->stack[stack->index++] = value;
202 return LY_SUCCESS;
203}
204
Radek Krejcib56c7502019-02-13 14:19:54 +0100205/**
206 * @brief Get (and remove) the last item form the stack.
207 * @param[in] stack The if-feature stack to use.
208 * @return The value from the top of the stack.
209 */
Radek Krejci19a96102018-11-15 13:38:09 +0100210static uint8_t
211iff_stack_pop(struct iff_stack *stack)
212{
Radek Krejcib56c7502019-02-13 14:19:54 +0100213 assert(stack && stack->index);
214
Radek Krejci19a96102018-11-15 13:38:09 +0100215 stack->index--;
216 return stack->stack[stack->index];
217}
218
Radek Krejcib56c7502019-02-13 14:19:54 +0100219/**
220 * @brief Clean up the stack.
221 * @param[in] stack The if-feature stack to use.
222 */
Radek Krejci19a96102018-11-15 13:38:09 +0100223static void
224iff_stack_clean(struct iff_stack *stack)
225{
226 stack->size = 0;
227 free(stack->stack);
228}
229
Radek Krejcib56c7502019-02-13 14:19:54 +0100230/**
231 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
232 * (libyang format of the if-feature expression).
233 * @param[in,out] list The 2bits array to modify.
234 * @param[in] op The operand (@ref ifftokens) to store.
235 * @param[in] pos Position (0-based) where to store the given @p op.
236 */
Radek Krejci19a96102018-11-15 13:38:09 +0100237static void
238iff_setop(uint8_t *list, uint8_t op, int pos)
239{
240 uint8_t *item;
241 uint8_t mask = 3;
242
243 assert(pos >= 0);
244 assert(op <= 3); /* max 2 bits */
245
246 item = &list[pos / 4];
247 mask = mask << 2 * (pos % 4);
248 *item = (*item) & ~mask;
249 *item = (*item) | (op << 2 * (pos % 4));
250}
251
Radek Krejcib56c7502019-02-13 14:19:54 +0100252#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
253#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100254
Radek Krejci0af46292019-01-11 16:02:31 +0100255/**
256 * @brief Find a feature of the given name and referenced in the given module.
257 *
258 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
259 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
260 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
261 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
262 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
263 * assigned till that time will be still valid.
264 *
265 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
266 * @param[in] name Name of the feature including possible prefix.
267 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
268 * @return Pointer to the feature structure if found, NULL otherwise.
269 */
Radek Krejci19a96102018-11-15 13:38:09 +0100270static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100271lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100272{
273 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100274 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100275
276 for (i = 0; i < len; ++i) {
277 if (name[i] == ':') {
278 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100279 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100280 LY_CHECK_RET(!mod, NULL);
281
282 name = &name[i + 1];
283 len = len - i - 1;
284 }
285 }
286
287 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100288 if (mod->implemented) {
289 /* module is implemented so there is already the compiled schema */
290 flist = mod->compiled->features;
291 } else {
292 flist = mod->off_features;
293 }
294 LY_ARRAY_FOR(flist, i) {
295 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100296 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
297 return f;
298 }
299 }
300
301 return NULL;
302}
303
304static LY_ERR
305lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
306{
307 const char *name;
308 unsigned int u;
309 const struct lys_module *mod;
310 struct lysp_ext *edef = NULL;
311
312 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
313 ext->insubstmt = ext_p->insubstmt;
314 ext->insubstmt_index = ext_p->insubstmt_index;
315
316 /* get module where the extension definition should be placed */
317 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100318 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100319 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
320 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
321 LY_EVALID);
322 LY_CHECK_ERR_RET(!mod->parsed->extensions,
323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
324 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100325 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100326 LY_EVALID);
327 name = &ext_p->name[u + 1];
328 /* find the extension definition there */
329 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
330 if (!strcmp(name, mod->parsed->extensions[u].name)) {
331 edef = &mod->parsed->extensions[u];
332 break;
333 }
334 }
335 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
336 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
337 LY_EVALID);
338 /* TODO plugins */
339
340 return LY_SUCCESS;
341}
342
Radek Krejcib56c7502019-02-13 14:19:54 +0100343/**
344 * @brief Compile information from the if-feature statement
345 * @param[in] ctx Compile context.
346 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
347 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
348 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
349 * @return LY_ERR value.
350 */
Radek Krejci19a96102018-11-15 13:38:09 +0100351static LY_ERR
352lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
353{
354 const char *c = *value;
355 int r, rc = EXIT_FAILURE;
356 int i, j, last_not, checkversion = 0;
357 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
358 uint8_t op;
359 struct iff_stack stack = {0, 0, NULL};
360 struct lysc_feature *f;
361
362 assert(c);
363
364 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
365 for (i = j = last_not = 0; c[i]; i++) {
366 if (c[i] == '(') {
367 j++;
368 checkversion = 1;
369 continue;
370 } else if (c[i] == ')') {
371 j--;
372 continue;
373 } else if (isspace(c[i])) {
374 checkversion = 1;
375 continue;
376 }
377
378 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
379 if (c[i + r] == '\0') {
380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
381 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
382 return LY_EVALID;
383 } else if (!isspace(c[i + r])) {
384 /* feature name starting with the not/and/or */
385 last_not = 0;
386 f_size++;
387 } else if (c[i] == 'n') { /* not operation */
388 if (last_not) {
389 /* double not */
390 expr_size = expr_size - 2;
391 last_not = 0;
392 } else {
393 last_not = 1;
394 }
395 } else { /* and, or */
396 f_exp++;
397 /* not a not operation */
398 last_not = 0;
399 }
400 i += r;
401 } else {
402 f_size++;
403 last_not = 0;
404 }
405 expr_size++;
406
407 while (!isspace(c[i])) {
408 if (!c[i] || c[i] == ')') {
409 i--;
410 break;
411 }
412 i++;
413 }
414 }
415 if (j || f_exp != f_size) {
416 /* not matching count of ( and ) */
417 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
418 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
419 return LY_EVALID;
420 }
421
422 if (checkversion || expr_size > 1) {
423 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100424 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
426 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
427 return LY_EVALID;
428 }
429 }
430
431 /* allocate the memory */
432 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
433 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
434 stack.stack = malloc(expr_size * sizeof *stack.stack);
435 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
436
437 stack.size = expr_size;
438 f_size--; expr_size--; /* used as indexes from now */
439
440 for (i--; i >= 0; i--) {
441 if (c[i] == ')') {
442 /* push it on stack */
443 iff_stack_push(&stack, LYS_IFF_RP);
444 continue;
445 } else if (c[i] == '(') {
446 /* pop from the stack into result all operators until ) */
447 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
448 iff_setop(iff->expr, op, expr_size--);
449 }
450 continue;
451 } else if (isspace(c[i])) {
452 continue;
453 }
454
455 /* end of operator or operand -> find beginning and get what is it */
456 j = i + 1;
457 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
458 i--;
459 }
460 i++; /* go back by one step */
461
462 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
463 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
464 /* double not */
465 iff_stack_pop(&stack);
466 } else {
467 /* not has the highest priority, so do not pop from the stack
468 * as in case of AND and OR */
469 iff_stack_push(&stack, LYS_IFF_NOT);
470 }
471 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
472 /* as for OR - pop from the stack all operators with the same or higher
473 * priority and store them to the result, then push the AND to the stack */
474 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
475 op = iff_stack_pop(&stack);
476 iff_setop(iff->expr, op, expr_size--);
477 }
478 iff_stack_push(&stack, LYS_IFF_AND);
479 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
480 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
481 op = iff_stack_pop(&stack);
482 iff_setop(iff->expr, op, expr_size--);
483 }
484 iff_stack_push(&stack, LYS_IFF_OR);
485 } else {
486 /* feature name, length is j - i */
487
488 /* add it to the expression */
489 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
490
491 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100492 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
493 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
494 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
495 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100496 iff->features[f_size] = f;
497 LY_ARRAY_INCREMENT(iff->features);
498 f_size--;
499 }
500 }
501 while (stack.index) {
502 op = iff_stack_pop(&stack);
503 iff_setop(iff->expr, op, expr_size--);
504 }
505
506 if (++expr_size || ++f_size) {
507 /* not all expected operators and operands found */
508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
509 "Invalid value \"%s\" of if-feature - processing error.", *value);
510 rc = LY_EINT;
511 } else {
512 rc = LY_SUCCESS;
513 }
514
515error:
516 /* cleanup */
517 iff_stack_clean(&stack);
518
519 return rc;
520}
521
Radek Krejcib56c7502019-02-13 14:19:54 +0100522/**
523 * @brief Compile information from the when statement
524 * @param[in] ctx Compile context.
525 * @param[in] when_p The parsed when statement structure.
526 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
527 * @param[out] when Pointer where to store pointer to the created compiled when structure.
528 * @return LY_ERR value.
529 */
Radek Krejci19a96102018-11-15 13:38:09 +0100530static LY_ERR
Radek Krejci00b874b2019-02-12 10:54:50 +0100531lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100532{
533 unsigned int u;
534 LY_ERR ret = LY_SUCCESS;
535
Radek Krejci00b874b2019-02-12 10:54:50 +0100536 *when = calloc(1, sizeof **when);
537 (*when)->refcount = 1;
538 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
539 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
540 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
541 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
542 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, options, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100543
544done:
545 return ret;
546}
547
Radek Krejcib56c7502019-02-13 14:19:54 +0100548/**
549 * @brief Compile information from the must statement
550 * @param[in] ctx Compile context.
551 * @param[in] must_p The parsed must statement structure.
552 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
553 * @param[in,out] must Prepared (empty) compiled must structure to fill.
554 * @return LY_ERR value.
555 */
Radek Krejci19a96102018-11-15 13:38:09 +0100556static LY_ERR
557lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
558{
559 unsigned int u;
560 LY_ERR ret = LY_SUCCESS;
561
562 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
563 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
564
565 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
566 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100567 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
568 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100569 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
570
571done:
572 return ret;
573}
574
Radek Krejcib56c7502019-02-13 14:19:54 +0100575/**
576 * @brief Compile information from the import statement
577 * @param[in] ctx Compile context.
578 * @param[in] imp_p The parsed import statement structure.
579 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
580 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
581 * @return LY_ERR value.
582 */
Radek Krejci19a96102018-11-15 13:38:09 +0100583static LY_ERR
584lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
585{
586 unsigned int u;
587 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100588 LY_ERR ret = LY_SUCCESS;
589
590 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
591 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
592 imp->module = imp_p->module;
593
Radek Krejci7f2a5362018-11-28 13:05:37 +0100594 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
595 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100596 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100597 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 /* try to use filepath if present */
599 if (imp->module->filepath) {
600 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
601 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100602 if (mod != imp->module) {
603 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100605 mod = NULL;
606 }
607 }
608 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100609 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100610 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100611 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100612 return LY_ENOTFOUND;
613 }
614 }
Radek Krejci19a96102018-11-15 13:38:09 +0100615 }
616
617done:
618 return ret;
619}
620
Radek Krejcib56c7502019-02-13 14:19:54 +0100621/**
622 * @brief Compile information from the identity statement
623 *
624 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
625 *
626 * @param[in] ctx Compile context.
627 * @param[in] ident_p The parsed identity statement structure.
628 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
629 * @param[in] idents List of so far compiled identities to check the name uniqueness.
630 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
631 * @return LY_ERR value.
632 */
Radek Krejci19a96102018-11-15 13:38:09 +0100633static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100634lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100635{
636 unsigned int u;
637 LY_ERR ret = LY_SUCCESS;
638
Radek Krejcid05cbd92018-12-05 14:26:40 +0100639 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100640 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100641 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
642 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
Radek Krejci19a96102018-11-15 13:38:09 +0100643 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
644 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
645 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
646 ident->flags = ident_p->flags;
647
648done:
649 return ret;
650}
651
Radek Krejcib56c7502019-02-13 14:19:54 +0100652/**
653 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
654 *
655 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
656 *
657 * @param[in] ctx Compile context for logging.
658 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
659 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
660 * @return LY_SUCCESS if everything is ok.
661 * @return LY_EVALID if the identity is derived from itself.
662 */
Radek Krejci38222632019-02-12 16:55:05 +0100663static LY_ERR
664lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
665{
666 LY_ERR ret = LY_EVALID;
667 unsigned int u, v;
668 struct ly_set recursion = {0};
669 struct lysc_ident *drv;
670
671 if (!derived) {
672 return LY_SUCCESS;
673 }
674
675 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
676 if (ident == derived[u]) {
677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
678 "Identity \"%s\" is indirectly derived from itself.", ident->name);
679 goto cleanup;
680 }
681 ly_set_add(&recursion, derived[u], 0);
682 }
683
684 for (v = 0; v < recursion.count; ++v) {
685 drv = recursion.objs[v];
686 if (!drv->derived) {
687 continue;
688 }
689 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
690 if (ident == drv->derived[u]) {
691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
692 "Identity \"%s\" is indirectly derived from itself.", ident->name);
693 goto cleanup;
694 }
695 ly_set_add(&recursion, drv->derived[u], 0);
696 }
697 }
698 ret = LY_SUCCESS;
699
700cleanup:
701 ly_set_erase(&recursion, NULL);
702 return ret;
703}
704
Radek Krejcia3045382018-11-22 14:30:31 +0100705/**
706 * @brief Find and process the referenced base identities from another identity or identityref
707 *
708 * For bases in identity se backlinks to them from the base identities. For identityref, store
709 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
710 * to distinguish these two use cases.
711 *
712 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
713 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
714 * @param[in] ident Referencing identity to work with.
715 * @param[in] bases Array of bases of identityref to fill in.
716 * @return LY_ERR value.
717 */
Radek Krejci19a96102018-11-15 13:38:09 +0100718static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100719lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +0100720{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100721 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100722 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100723 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100724 struct lysc_ident **idref;
725
726 assert(ident || bases);
727
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100728 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
730 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
731 return LY_EVALID;
732 }
733
734 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
735 s = strchr(bases_p[u], ':');
736 if (s) {
737 /* prefixed identity */
738 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100739 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100740 } else {
741 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100742 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100743 }
744 if (!mod) {
745 if (ident) {
746 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
747 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
748 } else {
749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
750 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
751 }
752 return LY_EVALID;
753 }
754 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100755 if (mod->compiled && mod->compiled->identities) {
756 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
757 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100758 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100759 if (ident == &mod->compiled->identities[v]) {
760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
761 "Identity \"%s\" is derived from itself.", ident->name);
762 return LY_EVALID;
763 }
764 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100765 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100766 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100767 *idref = ident;
768 } else {
769 /* we have match! store the found identity */
770 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100771 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100772 }
773 break;
774 }
775 }
776 }
777 if (!idref || !(*idref)) {
778 if (ident) {
779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
780 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
781 } else {
782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
783 "Unable to find base (%s) of identityref.", bases_p[u]);
784 }
785 return LY_EVALID;
786 }
787 }
788 return LY_SUCCESS;
789}
790
Radek Krejcia3045382018-11-22 14:30:31 +0100791/**
792 * @brief For the given array of identities, set the backlinks from all their base identities.
793 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
794 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
795 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
796 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
797 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100798static LY_ERR
799lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
800{
801 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100802
803 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
804 if (!idents_p[i].bases) {
805 continue;
806 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100807 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100808 }
809 return LY_SUCCESS;
810}
811
Radek Krejci0af46292019-01-11 16:02:31 +0100812LY_ERR
813lys_feature_precompile(struct ly_ctx *ctx, struct lysp_feature *features_p, struct lysc_feature **features)
814{
815 unsigned int offset = 0, u;
816 struct lysc_ctx context = {0};
817
818 assert(ctx);
819 context.ctx = ctx;
820
821 if (!features_p) {
822 return LY_SUCCESS;
823 }
824 if (*features) {
825 offset = LY_ARRAY_SIZE(*features);
826 }
827
828 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
829 LY_ARRAY_FOR(features_p, u) {
830 LY_ARRAY_INCREMENT(*features);
831 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
832 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
833 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
834 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
835 (*features)[offset + u].flags = features_p[u].flags;
836 }
837
838 return LY_SUCCESS;
839}
840
Radek Krejcia3045382018-11-22 14:30:31 +0100841/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100842 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100843 *
844 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
845 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100846 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100847 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
848 * being currently processed).
849 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +0100850 * @return LY_SUCCESS if everything is ok.
851 * @return LY_EVALID if the feature references indirectly itself.
852 */
853static LY_ERR
854lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
855{
856 LY_ERR ret = LY_EVALID;
857 unsigned int u, v;
858 struct ly_set recursion = {0};
859 struct lysc_feature *drv;
860
861 if (!depfeatures) {
862 return LY_SUCCESS;
863 }
864
865 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
866 if (feature == depfeatures[u]) {
867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
868 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
869 goto cleanup;
870 }
871 ly_set_add(&recursion, depfeatures[u], 0);
872 }
873
874 for (v = 0; v < recursion.count; ++v) {
875 drv = recursion.objs[v];
876 if (!drv->depfeatures) {
877 continue;
878 }
879 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
880 if (feature == drv->depfeatures[u]) {
881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
882 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
883 goto cleanup;
884 }
885 ly_set_add(&recursion, drv->depfeatures[u], 0);
886 }
887 }
888 ret = LY_SUCCESS;
889
890cleanup:
891 ly_set_erase(&recursion, NULL);
892 return ret;
893}
894
895/**
Radek Krejci0af46292019-01-11 16:02:31 +0100896 * @brief Create pre-compiled features array.
897 *
898 * See lys_feature_precompile() for more details.
899 *
Radek Krejcia3045382018-11-22 14:30:31 +0100900 * @param[in] ctx Compile context.
901 * @param[in] feature_p Parsed feature definition to compile.
902 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci0af46292019-01-11 16:02:31 +0100903 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100904 * @return LY_ERR value.
905 */
Radek Krejci19a96102018-11-15 13:38:09 +0100906static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100907lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +0100908{
Radek Krejci0af46292019-01-11 16:02:31 +0100909 unsigned int u, v, x;
910 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100911 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100912
Radek Krejci0af46292019-01-11 16:02:31 +0100913 /* find the preprecompiled feature */
914 LY_ARRAY_FOR(features, x) {
915 if (strcmp(features[x].name, feature_p->name)) {
916 continue;
917 }
918 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100919
Radek Krejci0af46292019-01-11 16:02:31 +0100920 /* finish compilation started in lys_feature_precompile() */
921 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
922 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
923 if (feature->iffeatures) {
924 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
925 if (feature->iffeatures[u].features) {
926 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100927 /* check for circular dependency - direct reference first,... */
928 if (feature == feature->iffeatures[u].features[v]) {
929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
930 "Feature \"%s\" is referenced from itself.", feature->name);
931 return LY_EVALID;
932 }
933 /* ... and indirect circular reference */
934 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
935
Radek Krejci0af46292019-01-11 16:02:31 +0100936 /* add itself into the dependants list */
937 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
938 *df = feature;
939 }
Radek Krejci19a96102018-11-15 13:38:09 +0100940 }
Radek Krejci19a96102018-11-15 13:38:09 +0100941 }
942 }
Radek Krejci0af46292019-01-11 16:02:31 +0100943 done:
944 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100945 }
Radek Krejci0af46292019-01-11 16:02:31 +0100946
947 LOGINT(ctx->ctx);
948 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100949}
950
Radek Krejcib56c7502019-02-13 14:19:54 +0100951/**
952 * @brief Revert compiled list of features back to the precompiled state.
953 *
954 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
955 * The features are supposed to be stored again as off_features in ::lys_module structure.
956 *
957 * @param[in] ctx Compilation context.
958 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
959 * and supposed to hold the off_features list.
960 */
Radek Krejci95710c92019-02-11 15:49:55 +0100961static void
962lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
963{
964 unsigned int u, v;
965
966 /* keep the off_features list until the complete lys_module is freed */
967 mod->off_features = mod->compiled->features;
968 mod->compiled->features = NULL;
969
970 /* in the off_features list, remove all the parts (from finished compiling process)
971 * which may points into the data being freed here */
972 LY_ARRAY_FOR(mod->off_features, u) {
973 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
974 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
975 }
976 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
977 mod->off_features[u].iffeatures = NULL;
978
979 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
980 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
981 }
982 LY_ARRAY_FREE(mod->off_features[u].exts);
983 mod->off_features[u].exts = NULL;
984 }
985}
986
Radek Krejcia3045382018-11-22 14:30:31 +0100987/**
988 * @brief Validate and normalize numeric value from a range definition.
989 * @param[in] ctx Compile context.
990 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
991 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
992 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
993 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
994 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
995 * @param[in] value String value of the range boundary.
996 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
997 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
998 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
999 */
Radek Krejci19a96102018-11-15 13:38:09 +01001000static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001001range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001002{
Radek Krejci6cba4292018-11-15 17:33:29 +01001003 size_t fraction = 0, size;
1004
Radek Krejci19a96102018-11-15 13:38:09 +01001005 *len = 0;
1006
1007 assert(value);
1008 /* parse value */
1009 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1010 return LY_EVALID;
1011 }
1012
1013 if ((value[*len] == '-') || (value[*len] == '+')) {
1014 ++(*len);
1015 }
1016
1017 while (isdigit(value[*len])) {
1018 ++(*len);
1019 }
1020
1021 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001022 if (basetype == LY_TYPE_DEC64) {
1023 goto decimal;
1024 } else {
1025 *valcopy = strndup(value, *len);
1026 return LY_SUCCESS;
1027 }
Radek Krejci19a96102018-11-15 13:38:09 +01001028 }
1029 fraction = *len;
1030
1031 ++(*len);
1032 while (isdigit(value[*len])) {
1033 ++(*len);
1034 }
1035
Radek Krejci6cba4292018-11-15 17:33:29 +01001036 if (basetype == LY_TYPE_DEC64) {
1037decimal:
1038 assert(frdigits);
1039 if (*len - 1 - fraction > frdigits) {
1040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1041 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1042 *len, value, frdigits);
1043 return LY_EINVAL;
1044 }
1045 if (fraction) {
1046 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1047 } else {
1048 size = (*len) + frdigits + 1;
1049 }
1050 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001051 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1052
Radek Krejci6cba4292018-11-15 17:33:29 +01001053 (*valcopy)[size - 1] = '\0';
1054 if (fraction) {
1055 memcpy(&(*valcopy)[0], &value[0], fraction);
1056 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1057 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1058 } else {
1059 memcpy(&(*valcopy)[0], &value[0], *len);
1060 memset(&(*valcopy)[*len], '0', frdigits);
1061 }
Radek Krejci19a96102018-11-15 13:38:09 +01001062 }
1063 return LY_SUCCESS;
1064}
1065
Radek Krejcia3045382018-11-22 14:30:31 +01001066/**
1067 * @brief Check that values in range are in ascendant order.
1068 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001069 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1070 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001071 * @param[in] value Current value to check.
1072 * @param[in] prev_value The last seen value.
1073 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1074 */
Radek Krejci19a96102018-11-15 13:38:09 +01001075static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001076range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001077{
1078 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001079 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001080 return LY_EEXIST;
1081 }
1082 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001083 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001084 return LY_EEXIST;
1085 }
1086 }
1087 return LY_SUCCESS;
1088}
1089
Radek Krejcia3045382018-11-22 14:30:31 +01001090/**
1091 * @brief Set min/max value of the range part.
1092 * @param[in] ctx Compile context.
1093 * @param[in] part Range part structure to fill.
1094 * @param[in] max Flag to distinguish if storing min or max value.
1095 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1096 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1097 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1098 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1099 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001100 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001101 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1102 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1103 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1104 * frdigits value), LY_EMEM.
1105 */
Radek Krejci19a96102018-11-15 13:38:09 +01001106static LY_ERR
1107range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001108 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001109{
1110 LY_ERR ret = LY_SUCCESS;
1111 char *valcopy = NULL;
1112 size_t len;
1113
1114 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001115 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001116 LY_CHECK_GOTO(ret, finalize);
1117 }
1118 if (!valcopy && base_range) {
1119 if (max) {
1120 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1121 } else {
1122 part->min_64 = base_range->parts[0].min_64;
1123 }
1124 if (!first) {
1125 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1126 }
1127 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001128 }
1129
1130 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001131 case LY_TYPE_INT8: /* range */
1132 if (valcopy) {
1133 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1134 } else if (max) {
1135 part->max_64 = INT64_C(127);
1136 } else {
1137 part->min_64 = INT64_C(-128);
1138 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001139 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001140 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001141 }
1142 break;
1143 case LY_TYPE_INT16: /* range */
1144 if (valcopy) {
1145 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1146 } else if (max) {
1147 part->max_64 = INT64_C(32767);
1148 } else {
1149 part->min_64 = INT64_C(-32768);
1150 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001151 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001152 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001153 }
1154 break;
1155 case LY_TYPE_INT32: /* range */
1156 if (valcopy) {
1157 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1158 } else if (max) {
1159 part->max_64 = INT64_C(2147483647);
1160 } else {
1161 part->min_64 = INT64_C(-2147483648);
1162 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001163 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001164 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001165 }
1166 break;
1167 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001168 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001169 if (valcopy) {
1170 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1171 max ? &part->max_64 : &part->min_64);
1172 } else if (max) {
1173 part->max_64 = INT64_C(9223372036854775807);
1174 } else {
1175 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1176 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001177 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001178 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001179 }
1180 break;
1181 case LY_TYPE_UINT8: /* range */
1182 if (valcopy) {
1183 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1184 } else if (max) {
1185 part->max_u64 = UINT64_C(255);
1186 } else {
1187 part->min_u64 = UINT64_C(0);
1188 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001189 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001190 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001191 }
1192 break;
1193 case LY_TYPE_UINT16: /* range */
1194 if (valcopy) {
1195 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1196 } else if (max) {
1197 part->max_u64 = UINT64_C(65535);
1198 } else {
1199 part->min_u64 = UINT64_C(0);
1200 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001201 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001202 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001203 }
1204 break;
1205 case LY_TYPE_UINT32: /* range */
1206 if (valcopy) {
1207 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1208 } else if (max) {
1209 part->max_u64 = UINT64_C(4294967295);
1210 } else {
1211 part->min_u64 = UINT64_C(0);
1212 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001213 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001214 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001215 }
1216 break;
1217 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001218 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001219 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001220 if (valcopy) {
1221 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1222 } else if (max) {
1223 part->max_u64 = UINT64_C(18446744073709551615);
1224 } else {
1225 part->min_u64 = UINT64_C(0);
1226 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001227 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001228 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001229 }
1230 break;
1231 default:
1232 LOGINT(ctx->ctx);
1233 ret = LY_EINT;
1234 }
1235
Radek Krejci5969f272018-11-23 10:03:58 +01001236finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001237 if (ret == LY_EDENIED) {
1238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1239 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1240 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1241 } else if (ret == LY_EVALID) {
1242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1243 "Invalid %s restriction - invalid value \"%s\".",
1244 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1245 } else if (ret == LY_EEXIST) {
1246 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1247 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001248 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001249 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001250 } else if (!ret && value) {
1251 *value = *value + len;
1252 }
1253 free(valcopy);
1254 return ret;
1255}
1256
Radek Krejcia3045382018-11-22 14:30:31 +01001257/**
1258 * @brief Compile the parsed range restriction.
1259 * @param[in] ctx Compile context.
1260 * @param[in] range_p Parsed range structure to compile.
1261 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1262 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1263 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1264 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1265 * range restriction must be more restrictive than the base_range.
1266 * @param[in,out] range Pointer to the created current range structure.
1267 * @return LY_ERR value.
1268 */
Radek Krejci19a96102018-11-15 13:38:09 +01001269static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001270lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001271 struct lysc_range *base_range, struct lysc_range **range)
1272{
1273 LY_ERR ret = LY_EVALID;
1274 const char *expr;
1275 struct lysc_range_part *parts = NULL, *part;
1276 int range_expected = 0, uns;
1277 unsigned int parts_done = 0, u, v;
1278
1279 assert(range);
1280 assert(range_p);
1281
1282 expr = range_p->arg;
1283 while(1) {
1284 if (isspace(*expr)) {
1285 ++expr;
1286 } else if (*expr == '\0') {
1287 if (range_expected) {
1288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1289 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1290 length_restr ? "length" : "range", range_p->arg);
1291 goto cleanup;
1292 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1294 "Invalid %s restriction - unexpected end of the expression (%s).",
1295 length_restr ? "length" : "range", range_p->arg);
1296 goto cleanup;
1297 }
1298 parts_done++;
1299 break;
1300 } else if (!strncmp(expr, "min", 3)) {
1301 if (parts) {
1302 /* min cannot be used elsewhere than in the first part */
1303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1304 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1305 expr - range_p->arg, range_p->arg);
1306 goto cleanup;
1307 }
1308 expr += 3;
1309
1310 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001311 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001312 part->max_64 = part->min_64;
1313 } else if (*expr == '|') {
1314 if (!parts || range_expected) {
1315 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1316 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1317 goto cleanup;
1318 }
1319 expr++;
1320 parts_done++;
1321 /* process next part of the expression */
1322 } else if (!strncmp(expr, "..", 2)) {
1323 expr += 2;
1324 while (isspace(*expr)) {
1325 expr++;
1326 }
1327 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1328 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1329 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1330 goto cleanup;
1331 }
1332 /* continue expecting the upper boundary */
1333 range_expected = 1;
1334 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1335 /* number */
1336 if (range_expected) {
1337 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001338 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001339 range_expected = 0;
1340 } else {
1341 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1342 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001343 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001344 part->max_64 = part->min_64;
1345 }
1346
1347 /* continue with possible another expression part */
1348 } else if (!strncmp(expr, "max", 3)) {
1349 expr += 3;
1350 while (isspace(*expr)) {
1351 expr++;
1352 }
1353 if (*expr != '\0') {
1354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1355 length_restr ? "length" : "range", expr);
1356 goto cleanup;
1357 }
1358 if (range_expected) {
1359 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001360 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001361 range_expected = 0;
1362 } else {
1363 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1364 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001365 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001366 part->min_64 = part->max_64;
1367 }
1368 } else {
1369 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1370 length_restr ? "length" : "range", expr);
1371 goto cleanup;
1372 }
1373 }
1374
1375 /* check with the previous range/length restriction */
1376 if (base_range) {
1377 switch (basetype) {
1378 case LY_TYPE_BINARY:
1379 case LY_TYPE_UINT8:
1380 case LY_TYPE_UINT16:
1381 case LY_TYPE_UINT32:
1382 case LY_TYPE_UINT64:
1383 case LY_TYPE_STRING:
1384 uns = 1;
1385 break;
1386 case LY_TYPE_DEC64:
1387 case LY_TYPE_INT8:
1388 case LY_TYPE_INT16:
1389 case LY_TYPE_INT32:
1390 case LY_TYPE_INT64:
1391 uns = 0;
1392 break;
1393 default:
1394 LOGINT(ctx->ctx);
1395 ret = LY_EINT;
1396 goto cleanup;
1397 }
1398 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1399 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1400 goto baseerror;
1401 }
1402 /* current lower bound is not lower than the base */
1403 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1404 /* base has single value */
1405 if (base_range->parts[v].min_64 == parts[u].min_64) {
1406 /* both lower bounds are the same */
1407 if (parts[u].min_64 != parts[u].max_64) {
1408 /* current continues with a range */
1409 goto baseerror;
1410 } else {
1411 /* equal single values, move both forward */
1412 ++v;
1413 continue;
1414 }
1415 } else {
1416 /* base is single value lower than current range, so the
1417 * value from base range is removed in the current,
1418 * move only base and repeat checking */
1419 ++v;
1420 --u;
1421 continue;
1422 }
1423 } else {
1424 /* base is the range */
1425 if (parts[u].min_64 == parts[u].max_64) {
1426 /* current is a single value */
1427 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1428 /* current is behind the base range, so base range is omitted,
1429 * move the base and keep the current for further check */
1430 ++v;
1431 --u;
1432 } /* else it is within the base range, so move the current, but keep the base */
1433 continue;
1434 } else {
1435 /* both are ranges - check the higher bound, the lower was already checked */
1436 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1437 /* higher bound is higher than the current higher bound */
1438 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1439 /* but the current lower bound is also higher, so the base range is omitted,
1440 * continue with the same current, but move the base */
1441 --u;
1442 ++v;
1443 continue;
1444 }
1445 /* current range starts within the base range but end behind it */
1446 goto baseerror;
1447 } else {
1448 /* current range is smaller than the base,
1449 * move current, but stay with the base */
1450 continue;
1451 }
1452 }
1453 }
1454 }
1455 if (u != parts_done) {
1456baseerror:
1457 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1458 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1459 length_restr ? "length" : "range", range_p->arg);
1460 goto cleanup;
1461 }
1462 }
1463
1464 if (!(*range)) {
1465 *range = calloc(1, sizeof **range);
1466 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1467 }
1468
Radek Krejcic8b31002019-01-08 10:24:45 +01001469 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001470 if (range_p->eapptag) {
1471 lydict_remove(ctx->ctx, (*range)->eapptag);
1472 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1473 }
1474 if (range_p->emsg) {
1475 lydict_remove(ctx->ctx, (*range)->emsg);
1476 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1477 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001478 if (range_p->dsc) {
1479 lydict_remove(ctx->ctx, (*range)->dsc);
1480 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1481 }
1482 if (range_p->ref) {
1483 lydict_remove(ctx->ctx, (*range)->ref);
1484 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1485 }
Radek Krejci19a96102018-11-15 13:38:09 +01001486 /* extensions are taken only from the last range by the caller */
1487
1488 (*range)->parts = parts;
1489 parts = NULL;
1490 ret = LY_SUCCESS;
1491cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001492 LY_ARRAY_FREE(parts);
1493
1494 return ret;
1495}
1496
1497/**
1498 * @brief Checks pattern syntax.
1499 *
Radek Krejcia3045382018-11-22 14:30:31 +01001500 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001501 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001502 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1503 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001504 */
1505static LY_ERR
1506lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1507{
1508 int idx, idx2, start, end, err_offset, count;
1509 char *perl_regex, *ptr;
1510 const char *err_msg, *orig_ptr;
1511 pcre *precomp;
1512#define URANGE_LEN 19
1513 char *ublock2urange[][2] = {
1514 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1515 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1516 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1517 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1518 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1519 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1520 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1521 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1522 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1523 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1524 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1525 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1526 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1527 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1528 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1529 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1530 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1531 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1532 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1533 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1534 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1535 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1536 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1537 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1538 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1539 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1540 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1541 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1542 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1543 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1544 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1545 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1546 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1547 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1548 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1549 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1550 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1551 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1552 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1553 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1554 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1555 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1556 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1557 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1558 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1559 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1560 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1561 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1562 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1563 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1564 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1565 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1566 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1567 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1568 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1569 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1570 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1571 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1572 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1573 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1574 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1575 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1576 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1577 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1578 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1579 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1580 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1581 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1582 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1583 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1584 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1585 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1586 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1587 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1588 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1589 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1590 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1591 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1592 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1593 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1594 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1595 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1596 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1597 {NULL, NULL}
1598 };
1599
1600 /* adjust the expression to a Perl equivalent
1601 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1602
1603 /* we need to replace all "$" with "\$", count them now */
1604 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1605
1606 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1607 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1608 perl_regex[0] = '\0';
1609
1610 ptr = perl_regex;
1611
1612 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1613 /* we will add line-end anchoring */
1614 ptr[0] = '(';
1615 ++ptr;
1616 }
1617
1618 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1619 if (orig_ptr[0] == '$') {
1620 ptr += sprintf(ptr, "\\$");
1621 } else if (orig_ptr[0] == '^') {
1622 ptr += sprintf(ptr, "\\^");
1623 } else {
1624 ptr[0] = orig_ptr[0];
1625 ++ptr;
1626 }
1627 }
1628
1629 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1630 ptr += sprintf(ptr, ")$");
1631 } else {
1632 ptr[0] = '\0';
1633 ++ptr;
1634 }
1635
1636 /* substitute Unicode Character Blocks with exact Character Ranges */
1637 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1638 start = ptr - perl_regex;
1639
1640 ptr = strchr(ptr, '}');
1641 if (!ptr) {
1642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1643 pattern, perl_regex + start + 2, "unterminated character property");
1644 free(perl_regex);
1645 return LY_EVALID;
1646 }
1647 end = (ptr - perl_regex) + 1;
1648
1649 /* need more space */
1650 if (end - start < URANGE_LEN) {
1651 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1652 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1653 }
1654
1655 /* find our range */
1656 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1657 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1658 break;
1659 }
1660 }
1661 if (!ublock2urange[idx][0]) {
1662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1663 pattern, perl_regex + start + 5, "unknown block name");
1664 free(perl_regex);
1665 return LY_EVALID;
1666 }
1667
1668 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1669 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1670 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1671 ++count;
1672 }
1673 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1674 --count;
1675 }
1676 }
1677 if (count) {
1678 /* skip brackets */
1679 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1680 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1681 } else {
1682 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1683 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1684 }
1685 }
1686
1687 /* must return 0, already checked during parsing */
1688 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1689 &err_msg, &err_offset, NULL);
1690 if (!precomp) {
1691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1692 free(perl_regex);
1693 return LY_EVALID;
1694 }
1695 free(perl_regex);
1696
1697 if (pcre_precomp) {
1698 *pcre_precomp = precomp;
1699 } else {
1700 free(precomp);
1701 }
1702
1703 return LY_SUCCESS;
1704
1705#undef URANGE_LEN
1706}
1707
Radek Krejcia3045382018-11-22 14:30:31 +01001708/**
1709 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1710 * @param[in] ctx Compile context.
1711 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1712 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1713 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1714 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1715 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1716 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1717 */
Radek Krejci19a96102018-11-15 13:38:09 +01001718static LY_ERR
1719lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1720 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1721{
1722 struct lysc_pattern **pattern;
1723 unsigned int u, v;
1724 const char *err_msg;
1725 LY_ERR ret = LY_SUCCESS;
1726
1727 /* first, copy the patterns from the base type */
1728 if (base_patterns) {
1729 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1730 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1731 }
1732
1733 LY_ARRAY_FOR(patterns_p, u) {
1734 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1735 *pattern = calloc(1, sizeof **pattern);
1736 ++(*pattern)->refcount;
1737
1738 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1739 LY_CHECK_RET(ret);
1740 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1741 if (err_msg) {
1742 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1743 }
1744
1745 if (patterns_p[u].arg[0] == 0x15) {
1746 (*pattern)->inverted = 1;
1747 }
1748 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1749 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001750 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1751 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001752 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1753 options, v, lys_compile_ext, ret, done);
1754 }
1755done:
1756 return ret;
1757}
1758
Radek Krejcia3045382018-11-22 14:30:31 +01001759/**
1760 * @brief map of the possible restrictions combination for the specific built-in type.
1761 */
Radek Krejci19a96102018-11-15 13:38:09 +01001762static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1763 0 /* LY_TYPE_UNKNOWN */,
1764 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001765 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1766 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1767 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1768 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1769 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001770 LYS_SET_BIT /* LY_TYPE_BITS */,
1771 0 /* LY_TYPE_BOOL */,
1772 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1773 0 /* LY_TYPE_EMPTY */,
1774 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1775 LYS_SET_BASE /* LY_TYPE_IDENT */,
1776 LYS_SET_REQINST /* LY_TYPE_INST */,
1777 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001778 LYS_SET_TYPE /* LY_TYPE_UNION */,
1779 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001780 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001781 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001782 LYS_SET_RANGE /* LY_TYPE_INT64 */
1783};
1784
1785/**
1786 * @brief stringification of the YANG built-in data types
1787 */
1788const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1789 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1790 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001791};
1792
Radek Krejcia3045382018-11-22 14:30:31 +01001793/**
1794 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1795 * @param[in] ctx Compile context.
1796 * @param[in] enums_p Array of the parsed enum structures to compile.
1797 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1798 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1799 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1800 * @param[out] enums Newly created array of the compiled enums information for the current type.
1801 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1802 */
Radek Krejci19a96102018-11-15 13:38:09 +01001803static LY_ERR
1804lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1805 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1806{
1807 LY_ERR ret = LY_SUCCESS;
1808 unsigned int u, v, match;
1809 int32_t value = 0;
1810 uint32_t position = 0;
1811 struct lysc_type_enum_item *e, storage;
1812
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001813 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1815 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1816 return LY_EVALID;
1817 }
1818
1819 LY_ARRAY_FOR(enums_p, u) {
1820 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1821 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001822 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1823 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001824 if (base_enums) {
1825 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1826 LY_ARRAY_FOR(base_enums, v) {
1827 if (!strcmp(e->name, base_enums[v].name)) {
1828 break;
1829 }
1830 }
1831 if (v == LY_ARRAY_SIZE(base_enums)) {
1832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1833 "Invalid %s - derived type adds new item \"%s\".",
1834 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1835 return LY_EVALID;
1836 }
1837 match = v;
1838 }
1839
1840 if (basetype == LY_TYPE_ENUM) {
1841 if (enums_p[u].flags & LYS_SET_VALUE) {
1842 e->value = (int32_t)enums_p[u].value;
1843 if (!u || e->value >= value) {
1844 value = e->value + 1;
1845 }
1846 /* check collision with other values */
1847 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1848 if (e->value == (*enums)[v].value) {
1849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1850 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1851 e->value, e->name, (*enums)[v].name);
1852 return LY_EVALID;
1853 }
1854 }
1855 } else if (base_enums) {
1856 /* inherit the assigned value */
1857 e->value = base_enums[match].value;
1858 if (!u || e->value >= value) {
1859 value = e->value + 1;
1860 }
1861 } else {
1862 /* assign value automatically */
1863 if (u && value == INT32_MIN) {
1864 /* counter overflow */
1865 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1866 "Invalid enumeration - it is not possible to auto-assign enum value for "
1867 "\"%s\" since the highest value is already 2147483647.", e->name);
1868 return LY_EVALID;
1869 }
1870 e->value = value++;
1871 }
1872 } else { /* LY_TYPE_BITS */
1873 if (enums_p[u].flags & LYS_SET_VALUE) {
1874 e->value = (int32_t)enums_p[u].value;
1875 if (!u || (uint32_t)e->value >= position) {
1876 position = (uint32_t)e->value + 1;
1877 }
1878 /* check collision with other values */
1879 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1880 if (e->value == (*enums)[v].value) {
1881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1882 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1883 (uint32_t)e->value, e->name, (*enums)[v].name);
1884 return LY_EVALID;
1885 }
1886 }
1887 } else if (base_enums) {
1888 /* inherit the assigned value */
1889 e->value = base_enums[match].value;
1890 if (!u || (uint32_t)e->value >= position) {
1891 position = (uint32_t)e->value + 1;
1892 }
1893 } else {
1894 /* assign value automatically */
1895 if (u && position == 0) {
1896 /* counter overflow */
1897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1898 "Invalid bits - it is not possible to auto-assign bit position for "
1899 "\"%s\" since the highest value is already 4294967295.", e->name);
1900 return LY_EVALID;
1901 }
1902 e->value = position++;
1903 }
1904 }
1905
1906 if (base_enums) {
1907 /* the assigned values must not change from the derived type */
1908 if (e->value != base_enums[match].value) {
1909 if (basetype == LY_TYPE_ENUM) {
1910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1911 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1912 e->name, base_enums[match].value, e->value);
1913 } else {
1914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1915 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1916 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1917 }
1918 return LY_EVALID;
1919 }
1920 }
1921
1922 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1923 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1924
1925 if (basetype == LY_TYPE_BITS) {
1926 /* keep bits ordered by position */
1927 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1928 if (v != u) {
1929 memcpy(&storage, e, sizeof *e);
1930 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1931 memcpy(&(*enums)[v], &storage, sizeof storage);
1932 }
1933 }
1934 }
1935
1936done:
1937 return ret;
1938}
1939
Radek Krejcia3045382018-11-22 14:30:31 +01001940#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1941 for ((NODE) = (NODE)->parent; \
1942 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1943 (NODE) = (NODE)->parent); \
1944 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1945 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1946 TERM; \
1947 }
1948
1949/**
1950 * @brief Validate the predicate(s) from the leafref path.
1951 * @param[in] ctx Compile context
1952 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1953 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001954 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001955 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001956 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001957 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1958 */
1959static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001960lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001961 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001962{
1963 LY_ERR ret = LY_EVALID;
1964 const struct lys_module *mod;
1965 const struct lysc_node *src_node, *dst_node;
1966 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1967 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001968 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001969 const char *start, *end, *pke_end;
1970 struct ly_set keys = {0};
1971 int i;
1972
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001973 assert(path_context);
1974
Radek Krejcia3045382018-11-22 14:30:31 +01001975 while (**predicate == '[') {
1976 start = (*predicate)++;
1977
1978 while (isspace(**predicate)) {
1979 ++(*predicate);
1980 }
1981 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1982 while (isspace(**predicate)) {
1983 ++(*predicate);
1984 }
1985 if (**predicate != '=') {
1986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001987 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1988 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001989 goto cleanup;
1990 }
1991 ++(*predicate);
1992 while (isspace(**predicate)) {
1993 ++(*predicate);
1994 }
1995
1996 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1998 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1999 goto cleanup;
2000 }
2001 --pke_end;
2002 while (isspace(*pke_end)) {
2003 --pke_end;
2004 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002005 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002006 /* localize path-key-expr */
2007 pke_start = path_key_expr = *predicate;
2008 /* move after the current predicate */
2009 *predicate = end + 1;
2010
2011 /* source (must be leaf or leaf-list) */
2012 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002013 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002014 if (!mod) {
2015 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2016 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002017 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002018 goto cleanup;
2019 }
Radek Krejcia3045382018-11-22 14:30:31 +01002020 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002021 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002022 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002023 src_node = NULL;
2024 if (context_node->keys) {
2025 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2026 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2027 src_node = (const struct lysc_node*)context_node->keys[u];
2028 break;
2029 }
2030 }
2031 }
Radek Krejcia3045382018-11-22 14:30:31 +01002032 if (!src_node) {
2033 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002034 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002035 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002036 goto cleanup;
2037 }
Radek Krejcia3045382018-11-22 14:30:31 +01002038
2039 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002040 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002041 i = ly_set_add(&keys, (void*)src_node, 0);
2042 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002043 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002045 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002046 *predicate - start, start, src_node->name);
2047 goto cleanup;
2048 }
2049
2050 /* destination */
2051 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002052 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002053
2054 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2055 if (strncmp(path_key_expr, "current()", 9)) {
2056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2057 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2058 *predicate - start, start);
2059 goto cleanup;
2060 }
2061 path_key_expr += 9;
2062 while (isspace(*path_key_expr)) {
2063 ++path_key_expr;
2064 }
2065
2066 if (*path_key_expr != '/') {
2067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2068 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2069 *predicate - start, start);
2070 goto cleanup;
2071 }
2072 ++path_key_expr;
2073 while (isspace(*path_key_expr)) {
2074 ++path_key_expr;
2075 }
2076
2077 /* rel-path-keyexpr:
2078 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2079 while (!strncmp(path_key_expr, "..", 2)) {
2080 ++dest_parent_times;
2081 path_key_expr += 2;
2082 while (isspace(*path_key_expr)) {
2083 ++path_key_expr;
2084 }
2085 if (*path_key_expr != '/') {
2086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2087 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2088 *predicate - start, start);
2089 goto cleanup;
2090 }
2091 ++path_key_expr;
2092 while (isspace(*path_key_expr)) {
2093 ++path_key_expr;
2094 }
2095
2096 /* path is supposed to be evaluated in data tree, so we have to skip
2097 * all schema nodes that cannot be instantiated in data tree */
2098 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2099 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2100 *predicate - start, start);
2101 }
2102 if (!dest_parent_times) {
2103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2104 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2105 *predicate - start, start);
2106 goto cleanup;
2107 }
2108 if (path_key_expr == pke_end) {
2109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2110 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2111 *predicate - start, start);
2112 goto cleanup;
2113 }
2114
2115 while(path_key_expr != pke_end) {
2116 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002118 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2119 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002120 goto cleanup;
2121 }
2122
2123 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002124 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002125 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002126 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002127 }
2128 if (!mod) {
2129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2130 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
2131 *predicate - start, start, dst_len, dst);
2132 goto cleanup;
2133 }
2134
2135 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2136 if (!dst_node) {
2137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2138 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
2139 *predicate - start, start, path_key_expr - pke_start, pke_start);
2140 goto cleanup;
2141 }
2142 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002143 if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcia3045382018-11-22 14:30:31 +01002144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002145 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002146 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2147 goto cleanup;
2148 }
2149 }
2150
2151 ret = LY_SUCCESS;
2152cleanup:
2153 ly_set_erase(&keys, NULL);
2154 return ret;
2155}
2156
2157/**
2158 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2159 *
2160 * path-arg = absolute-path / relative-path
2161 * absolute-path = 1*("/" (node-identifier *path-predicate))
2162 * relative-path = 1*(".." "/") descendant-path
2163 *
2164 * @param[in,out] path Path to parse.
2165 * @param[out] prefix Prefix of the token, NULL if there is not any.
2166 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2167 * @param[out] name Name of the token.
2168 * @param[out] nam_len Length of the name.
2169 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2170 * must not be changed between consecutive calls. -1 if the
2171 * path is absolute.
2172 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2173 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2174 */
2175static LY_ERR
2176lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2177 int *parent_times, int *has_predicate)
2178{
2179 int par_times = 0;
2180
2181 assert(path && *path);
2182 assert(parent_times);
2183 assert(prefix);
2184 assert(prefix_len);
2185 assert(name);
2186 assert(name_len);
2187 assert(has_predicate);
2188
2189 *prefix = NULL;
2190 *prefix_len = 0;
2191 *name = NULL;
2192 *name_len = 0;
2193 *has_predicate = 0;
2194
2195 if (!*parent_times) {
2196 if (!strncmp(*path, "..", 2)) {
2197 *path += 2;
2198 ++par_times;
2199 while (!strncmp(*path, "/..", 3)) {
2200 *path += 3;
2201 ++par_times;
2202 }
2203 }
2204 if (par_times) {
2205 *parent_times = par_times;
2206 } else {
2207 *parent_times = -1;
2208 }
2209 }
2210
2211 if (**path != '/') {
2212 return LY_EINVAL;
2213 }
2214 /* skip '/' */
2215 ++(*path);
2216
2217 /* node-identifier ([prefix:]name) */
2218 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2219
2220 if ((**path == '/' && (*path)[1]) || !**path) {
2221 /* path continues by another token or this is the last token */
2222 return LY_SUCCESS;
2223 } else if ((*path)[0] != '[') {
2224 /* unexpected character */
2225 return LY_EINVAL;
2226 } else {
2227 /* predicate starting with [ */
2228 *has_predicate = 1;
2229 return LY_SUCCESS;
2230 }
2231}
2232
2233/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002234 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2235 *
2236 * The set of features used for target must be a subset of features used for the leafref.
2237 * This is not a perfect, we should compare the truth tables but it could require too much resources
2238 * and RFC 7950 does not require it explicitely, so we simplify that.
2239 *
2240 * @param[in] refnode The leafref node.
2241 * @param[in] target Tha target node of the leafref.
2242 * @return LY_SUCCESS or LY_EVALID;
2243 */
2244static LY_ERR
2245lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2246{
2247 LY_ERR ret = LY_EVALID;
2248 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002249 unsigned int u, v, count;
2250 struct ly_set features = {0};
2251
2252 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002253 if (iter->iffeatures) {
2254 LY_ARRAY_FOR(iter->iffeatures, u) {
2255 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2256 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002257 }
2258 }
2259 }
2260 }
2261
2262 /* we should have, in features set, a superset of features applicable to the target node.
2263 * So when adding features applicable to the target into the features set, we should not be
2264 * able to actually add any new feature, otherwise it is not a subset of features applicable
2265 * to the leafref itself. */
2266 count = features.count;
2267 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002268 if (iter->iffeatures) {
2269 LY_ARRAY_FOR(iter->iffeatures, u) {
2270 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2271 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002272 /* new feature was added (or LY_EMEM) */
2273 goto cleanup;
2274 }
2275 }
2276 }
2277 }
2278 }
2279 ret = LY_SUCCESS;
2280
2281cleanup:
2282 ly_set_erase(&features, NULL);
2283 return ret;
2284}
2285
2286/**
Radek Krejcia3045382018-11-22 14:30:31 +01002287 * @brief Validate the leafref path.
2288 * @param[in] ctx Compile context
2289 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002290 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002291 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2292 */
2293static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002294lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002295{
2296 const struct lysc_node *node = NULL, *parent = NULL;
2297 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002298 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002299 const char *id, *prefix, *name;
2300 size_t prefix_len, name_len;
2301 int parent_times = 0, has_predicate;
2302 unsigned int iter, u;
2303 LY_ERR ret = LY_SUCCESS;
2304
2305 assert(ctx);
2306 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002307 assert(leafref);
2308
Radek Krejcia3045382018-11-22 14:30:31 +01002309 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002310 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002311 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2312 if (!iter) { /* first iteration */
2313 /* precess ".." in relative paths */
2314 if (parent_times > 0) {
2315 /* move from the context node */
2316 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2317 /* path is supposed to be evaluated in data tree, so we have to skip
2318 * all schema nodes that cannot be instantiated in data tree */
2319 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002320 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002321 }
2322 }
2323 }
2324
2325 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002326 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002327 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002328 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002329 }
2330 if (!mod) {
2331 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002332 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2333 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002334 return LY_EVALID;
2335 }
Radek Krejci3d929562019-02-21 11:25:55 +01002336 if (!mod->implemented) {
2337 /* make the module implemented */
2338 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2339 }
Radek Krejcia3045382018-11-22 14:30:31 +01002340
2341 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2342 if (!node) {
2343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002344 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002345 return LY_EVALID;
2346 }
2347 parent = node;
2348
2349 if (has_predicate) {
2350 /* we have predicate, so the current result must be list */
2351 if (node->nodetype != LYS_LIST) {
2352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2353 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002354 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002355 return LY_EVALID;
2356 }
2357
Radek Krejcibade82a2018-12-05 10:13:48 +01002358 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2359 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002360 }
2361
2362 ++iter;
2363 }
2364 if (ret) {
2365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002366 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002367 return LY_EVALID;
2368 }
2369
2370 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2372 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002373 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002374 return LY_EVALID;
2375 }
2376
2377 /* check status */
2378 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2379 return LY_EVALID;
2380 }
2381
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002382 /* check config */
2383 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2384 if (node->flags & LYS_CONFIG_R) {
2385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2386 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2387 leafref->path);
2388 return LY_EVALID;
2389 }
2390 }
2391
Radek Krejci412ddfa2018-11-23 11:44:11 +01002392 /* store the target's type and check for circular chain of leafrefs */
2393 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2394 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2395 if (type == (struct lysc_type*)leafref) {
2396 /* circular chain detected */
2397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2398 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2399 return LY_EVALID;
2400 }
2401 }
2402
Radek Krejci58d171e2018-11-23 13:50:55 +01002403 /* check if leafref and its target are under common if-features */
2404 if (lys_compile_leafref_features_validate(startnode, node)) {
2405 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2406 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2407 leafref->path);
2408 return LY_EVALID;
2409 }
2410
Radek Krejcia3045382018-11-22 14:30:31 +01002411 return LY_SUCCESS;
2412}
2413
Radek Krejcicdfecd92018-11-26 11:27:32 +01002414static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejci01342af2019-01-03 15:18:08 +01002415 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002416/**
2417 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2418 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002419 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2420 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2421 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2422 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002423 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002424 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002425 * @param[in] basetype Base YANG built-in type of the type to compile.
2426 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2427 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2428 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2429 * @param[out] type Newly created type structure with the filled information about the type.
2430 * @return LY_ERR value.
2431 */
Radek Krejci19a96102018-11-15 13:38:09 +01002432static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002433lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
2434 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2435 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002436{
2437 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002438 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002439 struct lysc_type_bin *bin;
2440 struct lysc_type_num *num;
2441 struct lysc_type_str *str;
2442 struct lysc_type_bits *bits;
2443 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002444 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002445 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002446 struct lysc_type_union *un, *un_aux;
2447 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002448
2449 switch (basetype) {
2450 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002451 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002452
2453 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002454 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002455 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002456 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2457 LY_CHECK_RET(ret);
2458 if (!tpdfname) {
2459 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2460 options, u, lys_compile_ext, ret, done);
2461 }
2462 }
2463
2464 if (tpdfname) {
2465 type_p->compiled = *type;
2466 *type = calloc(1, sizeof(struct lysc_type_bin));
2467 }
2468 break;
2469 case LY_TYPE_BITS:
2470 /* RFC 7950 9.7 - bits */
2471 bits = (struct lysc_type_bits*)(*type);
2472 if (type_p->bits) {
2473 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2474 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2475 (struct lysc_type_enum_item**)&bits->bits);
2476 LY_CHECK_RET(ret);
2477 }
2478
Radek Krejci555cb5b2018-11-16 14:54:33 +01002479 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002480 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002481 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002483 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002485 free(*type);
2486 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002487 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002488 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002489 }
2490
2491 if (tpdfname) {
2492 type_p->compiled = *type;
2493 *type = calloc(1, sizeof(struct lysc_type_bits));
2494 }
2495 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002496 case LY_TYPE_DEC64:
2497 dec = (struct lysc_type_dec*)(*type);
2498
2499 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002500 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002501 if (!type_p->fraction_digits) {
2502 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002504 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002506 free(*type);
2507 *type = NULL;
2508 }
2509 return LY_EVALID;
2510 }
2511 } else if (type_p->fraction_digits) {
2512 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002513 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2515 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002516 tpdfname);
2517 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2519 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002520 free(*type);
2521 *type = NULL;
2522 }
2523 return LY_EVALID;
2524 }
2525 dec->fraction_digits = type_p->fraction_digits;
2526
2527 /* RFC 7950 9.2.4 - range */
2528 if (type_p->range) {
2529 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2530 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2531 LY_CHECK_RET(ret);
2532 if (!tpdfname) {
2533 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2534 options, u, lys_compile_ext, ret, done);
2535 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002536 }
2537
2538 if (tpdfname) {
2539 type_p->compiled = *type;
2540 *type = calloc(1, sizeof(struct lysc_type_dec));
2541 }
2542 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002543 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002544 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002545
2546 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002547 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002548 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002549 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2550 LY_CHECK_RET(ret);
2551 if (!tpdfname) {
2552 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2553 options, u, lys_compile_ext, ret, done);
2554 }
2555 } else if (base && ((struct lysc_type_str*)base)->length) {
2556 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2557 }
2558
2559 /* RFC 7950 9.4.5 - pattern */
2560 if (type_p->patterns) {
2561 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2562 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2563 LY_CHECK_RET(ret);
2564 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2565 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2566 }
2567
2568 if (tpdfname) {
2569 type_p->compiled = *type;
2570 *type = calloc(1, sizeof(struct lysc_type_str));
2571 }
2572 break;
2573 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002574 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002575
2576 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002577 if (type_p->enums) {
2578 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2579 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2580 LY_CHECK_RET(ret);
2581 }
2582
Radek Krejci555cb5b2018-11-16 14:54:33 +01002583 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002585 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002587 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002589 free(*type);
2590 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002592 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593 }
2594
2595 if (tpdfname) {
2596 type_p->compiled = *type;
2597 *type = calloc(1, sizeof(struct lysc_type_enum));
2598 }
2599 break;
2600 case LY_TYPE_INT8:
2601 case LY_TYPE_UINT8:
2602 case LY_TYPE_INT16:
2603 case LY_TYPE_UINT16:
2604 case LY_TYPE_INT32:
2605 case LY_TYPE_UINT32:
2606 case LY_TYPE_INT64:
2607 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002608 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002609
2610 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002611 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002612 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002613 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2614 LY_CHECK_RET(ret);
2615 if (!tpdfname) {
2616 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2617 options, u, lys_compile_ext, ret, done);
2618 }
2619 }
2620
2621 if (tpdfname) {
2622 type_p->compiled = *type;
2623 *type = calloc(1, sizeof(struct lysc_type_num));
2624 }
2625 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002626 case LY_TYPE_IDENT:
2627 idref = (struct lysc_type_identityref*)(*type);
2628
2629 /* RFC 7950 9.10.2 - base */
2630 if (type_p->bases) {
2631 if (base) {
2632 /* only the directly derived identityrefs can contain base specification */
2633 if (tpdfname) {
2634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002635 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002636 tpdfname);
2637 } else {
2638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002639 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002640 free(*type);
2641 *type = NULL;
2642 }
2643 return LY_EVALID;
2644 }
2645 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2646 LY_CHECK_RET(ret);
2647 }
2648
2649 if (!base && !type_p->flags) {
2650 /* type derived from identityref built-in type must contain at least one base */
2651 if (tpdfname) {
2652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2653 } else {
2654 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2655 free(*type);
2656 *type = NULL;
2657 }
2658 return LY_EVALID;
2659 }
2660
2661 if (tpdfname) {
2662 type_p->compiled = *type;
2663 *type = calloc(1, sizeof(struct lysc_type_identityref));
2664 }
2665 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002666 case LY_TYPE_LEAFREF:
2667 /* RFC 7950 9.9.3 - require-instance */
2668 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002669 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002670 if (tpdfname) {
2671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2672 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2673 } else {
2674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2675 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2676 free(*type);
2677 *type = NULL;
2678 }
2679 return LY_EVALID;
2680 }
Radek Krejcia3045382018-11-22 14:30:31 +01002681 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002682 } else if (base) {
2683 /* inherit */
2684 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002685 } else {
2686 /* default is true */
2687 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2688 }
2689 if (type_p->path) {
2690 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002691 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002692 } else if (base) {
2693 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002694 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002695 } else if (tpdfname) {
2696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2697 return LY_EVALID;
2698 } else {
2699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2700 free(*type);
2701 *type = NULL;
2702 return LY_EVALID;
2703 }
2704 if (tpdfname) {
2705 type_p->compiled = *type;
2706 *type = calloc(1, sizeof(struct lysc_type_leafref));
2707 }
2708 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002709 case LY_TYPE_INST:
2710 /* RFC 7950 9.9.3 - require-instance */
2711 if (type_p->flags & LYS_SET_REQINST) {
2712 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2713 } else {
2714 /* default is true */
2715 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2716 }
2717
2718 if (tpdfname) {
2719 type_p->compiled = *type;
2720 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2721 }
2722 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002723 case LY_TYPE_UNION:
2724 un = (struct lysc_type_union*)(*type);
2725
2726 /* RFC 7950 7.4 - type */
2727 if (type_p->types) {
2728 if (base) {
2729 /* only the directly derived union can contain types specification */
2730 if (tpdfname) {
2731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2732 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2733 tpdfname);
2734 } else {
2735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2736 "Invalid type substatement for the type not directly derived from union built-in type.");
2737 free(*type);
2738 *type = NULL;
2739 }
2740 return LY_EVALID;
2741 }
2742 /* compile the type */
2743 additional = 0;
2744 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2745 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002746 ret = lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], options, &un->types[u + additional], NULL);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002747 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2748 /* add space for additional types from the union subtype */
2749 un_aux = (struct lysc_type_union *)un->types[u + additional];
2750 p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types)));
2751 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2752 un->types = (void*)((uint32_t*)(p) + 1);
2753
2754 /* copy subtypes of the subtype union */
2755 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2756 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2757 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2758 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2759 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2760 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2761 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2762 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2763 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2764 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2765 /* TODO extensions */
2766
2767 } else {
2768 un->types[u + additional] = un_aux->types[v];
2769 ++un_aux->types[v]->refcount;
2770 }
2771 ++additional;
2772 LY_ARRAY_INCREMENT(un->types);
2773 }
2774 /* compensate u increment in main loop */
2775 --additional;
2776
2777 /* free the replaced union subtype */
2778 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2779 } else {
2780 LY_ARRAY_INCREMENT(un->types);
2781 }
2782 LY_CHECK_RET(ret);
2783 }
2784 }
2785
2786 if (!base && !type_p->flags) {
2787 /* type derived from union built-in type must contain at least one type */
2788 if (tpdfname) {
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2790 } else {
2791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2792 free(*type);
2793 *type = NULL;
2794 }
2795 return LY_EVALID;
2796 }
2797
2798 if (tpdfname) {
2799 type_p->compiled = *type;
2800 *type = calloc(1, sizeof(struct lysc_type_union));
2801 }
2802 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002803 case LY_TYPE_BOOL:
2804 case LY_TYPE_EMPTY:
2805 case LY_TYPE_UNKNOWN: /* just to complete switch */
2806 break;
2807 }
2808 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2809done:
2810 return ret;
2811}
2812
Radek Krejcia3045382018-11-22 14:30:31 +01002813/**
2814 * @brief Compile information about the leaf/leaf-list's type.
2815 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002816 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2817 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2818 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2819 * @param[in] context_name Name of the context node or referencing typedef for logging.
2820 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002821 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2822 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002823 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002824 * @return LY_ERR value.
2825 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002826static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002827lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejci01342af2019-01-03 15:18:08 +01002828 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002829{
2830 LY_ERR ret = LY_SUCCESS;
2831 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002832 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002833 struct type_context {
2834 const struct lysp_tpdf *tpdf;
2835 struct lysp_node *node;
2836 struct lysp_module *mod;
2837 } *tctx, *tctx_prev = NULL;
2838 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002839 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002840 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002841 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002842
2843 (*type) = NULL;
2844
2845 tctx = calloc(1, sizeof *tctx);
2846 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002847 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002848 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2849 ret == LY_SUCCESS;
2850 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2851 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2852 if (basetype) {
2853 break;
2854 }
2855
2856 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002857 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2858 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002859 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2860
Radek Krejcicdfecd92018-11-26 11:27:32 +01002861 if (units && !*units) {
2862 /* inherit units */
2863 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2864 }
Radek Krejci01342af2019-01-03 15:18:08 +01002865 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002866 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002867 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002868 }
Radek Krejci01342af2019-01-03 15:18:08 +01002869 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002870 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2871 break;
2872 }
2873
Radek Krejci19a96102018-11-15 13:38:09 +01002874 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002875 /* it is not necessary to continue, the rest of the chain was already compiled,
2876 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002877 basetype = tctx->tpdf->type.compiled->basetype;
2878 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002879 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002880 dummyloops = 1;
2881 goto preparenext;
2882 } else {
2883 tctx = NULL;
2884 break;
2885 }
Radek Krejci19a96102018-11-15 13:38:09 +01002886 }
2887
2888 /* store information for the following processing */
2889 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2890
Radek Krejcicdfecd92018-11-26 11:27:32 +01002891preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002892 /* prepare next loop */
2893 tctx_prev = tctx;
2894 tctx = calloc(1, sizeof *tctx);
2895 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2896 }
2897 free(tctx);
2898
2899 /* allocate type according to the basetype */
2900 switch (basetype) {
2901 case LY_TYPE_BINARY:
2902 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002903 break;
2904 case LY_TYPE_BITS:
2905 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002906 break;
2907 case LY_TYPE_BOOL:
2908 case LY_TYPE_EMPTY:
2909 *type = calloc(1, sizeof(struct lysc_type));
2910 break;
2911 case LY_TYPE_DEC64:
2912 *type = calloc(1, sizeof(struct lysc_type_dec));
2913 break;
2914 case LY_TYPE_ENUM:
2915 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002916 break;
2917 case LY_TYPE_IDENT:
2918 *type = calloc(1, sizeof(struct lysc_type_identityref));
2919 break;
2920 case LY_TYPE_INST:
2921 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2922 break;
2923 case LY_TYPE_LEAFREF:
2924 *type = calloc(1, sizeof(struct lysc_type_leafref));
2925 break;
2926 case LY_TYPE_STRING:
2927 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002928 break;
2929 case LY_TYPE_UNION:
2930 *type = calloc(1, sizeof(struct lysc_type_union));
2931 break;
2932 case LY_TYPE_INT8:
2933 case LY_TYPE_UINT8:
2934 case LY_TYPE_INT16:
2935 case LY_TYPE_UINT16:
2936 case LY_TYPE_INT32:
2937 case LY_TYPE_UINT32:
2938 case LY_TYPE_INT64:
2939 case LY_TYPE_UINT64:
2940 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002941 break;
2942 case LY_TYPE_UNKNOWN:
2943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2944 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2945 ret = LY_EVALID;
2946 goto cleanup;
2947 }
2948 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002949 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2951 ly_data_type2str[basetype]);
2952 free(*type);
2953 (*type) = NULL;
2954 ret = LY_EVALID;
2955 goto cleanup;
2956 }
2957
2958 /* get restrictions from the referred typedefs */
2959 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2960 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002961 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002962 base = tctx->tpdf->type.compiled;
2963 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002964 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002965 /* no change, just use the type information from the base */
2966 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2967 ++base->refcount;
2968 continue;
2969 }
2970
2971 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002972 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2974 tctx->tpdf->name, ly_data_type2str[basetype]);
2975 ret = LY_EVALID;
2976 goto cleanup;
2977 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2979 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2980 tctx->tpdf->name, tctx->tpdf->dflt);
2981 ret = LY_EVALID;
2982 goto cleanup;
2983 }
2984
Radek Krejci19a96102018-11-15 13:38:09 +01002985 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002986 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002987 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002988 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2989 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002990 LY_CHECK_GOTO(ret, cleanup);
2991 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002992 }
2993
Radek Krejcic5c27e52018-11-15 14:38:11 +01002994 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002995 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002996 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002997 (*type)->basetype = basetype;
2998 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01002999 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, options, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003000 LY_CHECK_GOTO(ret, cleanup);
3001 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003002 /* no specific restriction in leaf's type definition, copy from the base */
3003 free(*type);
3004 (*type) = base;
3005 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003006 }
Radek Krejci01342af2019-01-03 15:18:08 +01003007 if (!(*type)->dflt) {
3008 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
3009 }
Radek Krejci19a96102018-11-15 13:38:09 +01003010
3011 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
3012
3013cleanup:
3014 ly_set_erase(&tpdf_chain, free);
3015 return ret;
3016}
3017
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003018/**
3019 * @brief Compile status information of the given node.
3020 *
3021 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3022 * has the status correctly set during the compilation.
3023 *
3024 * @param[in] ctx Compile context
3025 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3026 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3027 * the compatibility with the parent's status value.
3028 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3029 * @return LY_ERR value.
3030 */
3031static LY_ERR
3032lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3033{
3034 /* status - it is not inherited by specification, but it does not make sense to have
3035 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3036 if (!((*node_flags) & LYS_STATUS_MASK)) {
3037 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3038 if ((parent_flags & 0x3) != 0x3) {
3039 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3040 * combination of bits (0x3) which marks the uses_status value */
3041 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3042 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3043 }
3044 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3045 } else {
3046 (*node_flags) |= LYS_STATUS_CURR;
3047 }
3048 } else if (parent_flags & LYS_STATUS_MASK) {
3049 /* check status compatibility with the parent */
3050 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3051 if ((*node_flags) & LYS_STATUS_CURR) {
3052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3053 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3054 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3055 } else { /* LYS_STATUS_DEPRC */
3056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3057 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3058 }
3059 return LY_EVALID;
3060 }
3061 }
3062 return LY_SUCCESS;
3063}
3064
Radek Krejci8cce8532019-03-05 11:27:45 +01003065/**
3066 * @brief Check uniqness of the node/action/notification name.
3067 *
3068 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3069 * structures, but they share the namespace so we need to check their name collisions.
3070 *
3071 * @param[in] ctx Compile context.
3072 * @param[in] children List (linked list) of data nodes to go through.
3073 * @param[in] actions List (sized array) of actions or RPCs to go through.
3074 * @param[in] notifs List (sized array) of Notifications to go through.
3075 * @param[in] name Name of the item to find in the given lists.
3076 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3077 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3078 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3079 */
3080static LY_ERR
3081lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3082 const struct lysc_action *actions, const struct lysc_notif *notifs,
3083 const char *name, void *exclude)
3084{
3085 const struct lysc_node *iter;
3086 unsigned int u;
3087
3088 LY_LIST_FOR(children, iter) {
3089 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3090 goto error;
3091 }
3092 }
3093 LY_ARRAY_FOR(actions, u) {
3094 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3095 goto error;
3096 }
3097 }
3098 LY_ARRAY_FOR(notifs, u) {
3099 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3100 goto error;
3101 }
3102 }
3103 return LY_SUCCESS;
3104error:
3105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3106 return LY_EEXIST;
3107}
3108
Radek Krejcib1b59152019-01-07 13:21:56 +01003109static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01003110
Radek Krejcia3045382018-11-22 14:30:31 +01003111/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003112 * @brief Compile parsed RPC/action schema node information.
3113 * @param[in] ctx Compile context
3114 * @param[in] node_p Parsed RPC/action schema node.
3115 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3116 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3117 * @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).
3118 * Zero means no uses, non-zero value with no status bit set mean the default status.
3119 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3120 */
3121static LY_ERR
3122lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, int options,
3123 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3124{
3125 LY_ERR ret = LY_SUCCESS;
3126 struct lysp_node *child_p;
3127 unsigned int u;
3128
Radek Krejci8cce8532019-03-05 11:27:45 +01003129 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3130 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3131 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3132 action_p->name, action)) {
3133 return LY_EVALID;
3134 }
3135
Radek Krejci05b774b2019-02-25 13:26:18 +01003136 if (options & LYSC_OPT_RPC_MASK) {
3137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3138 "Action \"%s\" is placed inside another RPC/action.", action_p->name);
3139 return LY_EVALID;
3140 }
3141
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003142 action->nodetype = LYS_ACTION;
3143 action->module = ctx->mod;
3144 action->parent = parent;
3145 if (!(options & LYSC_OPT_FREE_SP)) {
3146 action->sp = action_p;
3147 }
3148 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3149
3150 /* status - it is not inherited by specification, but it does not make sense to have
3151 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3152 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3153
3154 DUP_STRING(ctx->ctx, action_p->name, action->name);
3155 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3156 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
3157 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, options, u, lys_compile_iffeature, ret, cleanup);
3158 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, options, u, lys_compile_ext, ret, cleanup);
3159
3160 /* input */
3161 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, options, u, lys_compile_must, ret, cleanup);
3162 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input.exts, options, u, lys_compile_ext, ret, cleanup);
3163 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003164 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 +01003165 }
3166
3167 /* output */
3168 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, options, u, lys_compile_must, ret, cleanup);
3169 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output.exts, options, u, lys_compile_ext, ret, cleanup);
3170 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003171 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 +01003172 }
3173
3174cleanup:
3175 return ret;
3176}
3177
3178/**
Radek Krejcia3045382018-11-22 14:30:31 +01003179 * @brief Compile parsed container node information.
3180 * @param[in] ctx Compile context
3181 * @param[in] node_p Parsed container node.
3182 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3183 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3184 * is enriched with the container-specific information.
3185 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3186 */
Radek Krejci19a96102018-11-15 13:38:09 +01003187static LY_ERR
3188lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3189{
3190 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3191 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3192 struct lysp_node *child_p;
3193 unsigned int u;
3194 LY_ERR ret = LY_SUCCESS;
3195
Radek Krejcife909632019-02-12 15:34:42 +01003196 if (cont_p->presence) {
3197 cont->flags |= LYS_PRESENCE;
3198 }
3199
Radek Krejci19a96102018-11-15 13:38:09 +01003200 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003201 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003202 }
3203
3204 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003205 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, options, u, lys_compile_action, 0, ret, done);
3206 // TODO COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, options, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003207
3208done:
3209 return ret;
3210}
3211
Radek Krejci33f72892019-02-21 10:36:58 +01003212/*
3213 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3214 * @param[in] ctx Compile context.
3215 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3216 * @param[in] type_p Parsed type to compile.
3217 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3218 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3219 * @return LY_ERR value.
3220 */
3221static LY_ERR
3222lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, int options, struct lysc_node_leaf *leaf)
3223{
3224 unsigned int u, v;
3225 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3226
3227 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, options, &leaf->type,
3228 leaf->units ? NULL : &leaf->units));
3229 if (leaf->nodetype == LYS_LEAFLIST) {
3230 if (llist->type->dflt && !llist->dflts && !llist->min) {
3231 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
3232 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3233 LY_ARRAY_INCREMENT(llist->dflts);
3234 }
3235 } else {
3236 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3237 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3238 }
3239 }
3240 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3241 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3242 ly_set_add(&ctx->unres, leaf, 0);
3243 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3244 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3245 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3246 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3247 ly_set_add(&ctx->unres, leaf, 0);
3248 }
3249 }
3250 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
3251 if (leaf->flags & LYS_SET_DFLT) {
3252 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "%s of type \"empty\" must not have a default value (%s).",
3253 leaf->nodetype == LYS_LEAFLIST ? "Leaf-list" : "Leaf", leaf->nodetype == LYS_LEAFLIST ? llist->dflts[0] : leaf->dflt);
3254 return LY_EVALID;
3255 }
3256 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3258 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3259 return LY_EVALID;
3260 }
3261 }
3262
3263 if (leaf->nodetype == LYS_LEAFLIST && (llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3264 /* configuration data values must be unique - so check the default values */
3265 LY_ARRAY_FOR(llist->dflts, u) {
3266 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3267 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3268 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3269 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3270 return LY_EVALID;
3271 }
3272 }
3273 }
3274 }
3275
3276 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3277
3278 return LY_SUCCESS;
3279}
3280
Radek Krejcia3045382018-11-22 14:30:31 +01003281/**
3282 * @brief Compile parsed leaf node information.
3283 * @param[in] ctx Compile context
3284 * @param[in] node_p Parsed leaf node.
3285 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3286 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3287 * is enriched with the leaf-specific information.
3288 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3289 */
Radek Krejci19a96102018-11-15 13:38:09 +01003290static LY_ERR
3291lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3292{
3293 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3294 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3295 unsigned int u;
3296 LY_ERR ret = LY_SUCCESS;
3297
Radek Krejci19a96102018-11-15 13:38:09 +01003298 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003299 if (leaf_p->units) {
3300 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3301 leaf->flags |= LYS_SET_UNITS;
3302 }
3303 if (leaf_p->dflt) {
3304 leaf->dflt = lydict_insert(ctx->ctx, leaf_p->dflt, 0);
Radek Krejci76b3e962018-12-14 17:01:25 +01003305 leaf->flags |= LYS_SET_DFLT;
3306 }
Radek Krejci43699232018-11-23 14:59:46 +01003307
Radek Krejci33f72892019-02-21 10:36:58 +01003308 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, options, leaf);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003309
Radek Krejci19a96102018-11-15 13:38:09 +01003310done:
3311 return ret;
3312}
3313
Radek Krejcia3045382018-11-22 14:30:31 +01003314/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003315 * @brief Compile parsed leaf-list node information.
3316 * @param[in] ctx Compile context
3317 * @param[in] node_p Parsed leaf-list node.
3318 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3319 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3320 * is enriched with the leaf-list-specific information.
3321 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3322 */
3323static LY_ERR
3324lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3325{
3326 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3327 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejci33f72892019-02-21 10:36:58 +01003328 unsigned int u;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003329 LY_ERR ret = LY_SUCCESS;
3330
Radek Krejci0e5d8382018-11-28 16:37:53 +01003331 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003332 if (llist_p->units) {
3333 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3334 llist->flags |= LYS_SET_UNITS;
3335 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003336
3337 if (llist_p->dflts) {
3338 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3339 LY_ARRAY_FOR(llist_p->dflts, u) {
3340 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3341 LY_ARRAY_INCREMENT(llist->dflts);
3342 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003343 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003344 }
3345
3346 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003347 if (llist->min) {
3348 llist->flags |= LYS_MAND_TRUE;
3349 }
Radek Krejcib7408632018-11-28 17:12:11 +01003350 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003351
Radek Krejci33f72892019-02-21 10:36:58 +01003352 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, options, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003353
3354done:
3355 return ret;
3356}
3357
3358/**
Radek Krejci7af64242019-02-18 13:07:53 +01003359 * @brief Compile information about list's uniques.
3360 * @param[in] ctx Compile context.
3361 * @param[in] context_module Module where the prefixes are going to be resolved.
3362 * @param[in] uniques Sized array list of unique statements.
3363 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3364 * @return LY_ERR value.
3365 */
3366static LY_ERR
3367lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3368{
3369 LY_ERR ret = LY_SUCCESS;
3370 struct lysc_node_leaf **key, ***unique;
3371 const char *keystr, *delim;
3372 size_t len;
3373 unsigned int v;
3374 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003375 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003376
3377 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3378 config = -1;
3379 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3380 keystr = uniques[v];
3381 while (keystr) {
3382 delim = strpbrk(keystr, " \t\n");
3383 if (delim) {
3384 len = delim - keystr;
3385 while (isspace(*delim)) {
3386 ++delim;
3387 }
3388 } else {
3389 len = strlen(keystr);
3390 }
3391
3392 /* unique node must be present */
3393 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003394 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3395 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003396 if (ret != LY_SUCCESS) {
3397 if (ret == LY_EDENIED) {
3398 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003399 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003400 len, keystr, lys_nodetype2str((*key)->nodetype));
3401 }
3402 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003403 } else if (flags) {
3404 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3405 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3406 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3407 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003408 }
3409
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003410
Radek Krejci7af64242019-02-18 13:07:53 +01003411 /* all referenced leafs must be of the same config type */
3412 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3413 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3414 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3415 return LY_EVALID;
3416 } else if ((*key)->flags & LYS_CONFIG_W) {
3417 config = 1;
3418 } else { /* LYS_CONFIG_R */
3419 config = 0;
3420 }
3421
3422 /* check status */
3423 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3424 (*key)->flags, (*key)->module, (*key)->name));
3425
3426 /* mark leaf as unique */
3427 (*key)->flags |= LYS_UNIQUE;
3428
3429 /* next unique value in line */
3430 keystr = delim;
3431 }
3432 /* next unique definition */
3433 }
3434
3435 return LY_SUCCESS;
3436}
3437
3438/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003439 * @brief Compile parsed list node information.
3440 * @param[in] ctx Compile context
3441 * @param[in] node_p Parsed list node.
3442 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3443 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3444 * is enriched with the list-specific information.
3445 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3446 */
3447static LY_ERR
3448lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3449{
3450 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3451 struct lysc_node_list *list = (struct lysc_node_list*)node;
3452 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003453 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003454 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003455 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003456 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003457 LY_ERR ret = LY_SUCCESS;
3458
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003459 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003460 if (list->min) {
3461 list->flags |= LYS_MAND_TRUE;
3462 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003463 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3464
3465 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003466 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003467 }
3468
3469 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
3470
3471 /* keys */
3472 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3474 return LY_EVALID;
3475 }
3476
3477 /* find all the keys (must be direct children) */
3478 keystr = list_p->key;
3479 while (keystr) {
3480 delim = strpbrk(keystr, " \t\n");
3481 if (delim) {
3482 len = delim - keystr;
3483 while (isspace(*delim)) {
3484 ++delim;
3485 }
3486 } else {
3487 len = strlen(keystr);
3488 }
3489
3490 /* key node must be present */
3491 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3492 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3493 if (!(*key)) {
3494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3495 "The list's key \"%.*s\" not found.", len, keystr);
3496 return LY_EVALID;
3497 }
3498 /* keys must be unique */
3499 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3500 if (*key == list->keys[u]) {
3501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3502 "Duplicated key identifier \"%.*s\".", len, keystr);
3503 return LY_EVALID;
3504 }
3505 }
3506 /* key must have the same config flag as the list itself */
3507 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3509 return LY_EVALID;
3510 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003511 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003512 /* YANG 1.0 denies key to be of empty type */
3513 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3515 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3516 return LY_EVALID;
3517 }
3518 } else {
3519 /* when and if-feature are illegal on list keys */
3520 if ((*key)->when) {
3521 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3522 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3523 return LY_EVALID;
3524 }
3525 if ((*key)->iffeatures) {
3526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3527 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3528 return LY_EVALID;
3529 }
3530 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003531
3532 /* check status */
3533 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3534 (*key)->flags, (*key)->module, (*key)->name));
3535
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003536 /* ignore default values of the key */
3537 if ((*key)->dflt) {
3538 lydict_remove(ctx->ctx, (*key)->dflt);
3539 (*key)->dflt = NULL;
3540 }
3541 /* mark leaf as key */
3542 (*key)->flags |= LYS_KEY;
3543
3544 /* next key value */
3545 keystr = delim;
3546 }
3547
3548 /* uniques */
3549 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003550 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003551 }
3552
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003553 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, options, u, lys_compile_action, 0, ret, done);
3554 // TODO COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, options, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003555
3556done:
3557 return ret;
3558}
3559
Radek Krejcib56c7502019-02-13 14:19:54 +01003560/**
3561 * @brief Do some checks and set the default choice's case.
3562 *
3563 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3564 *
3565 * @param[in] ctx Compile context.
3566 * @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,
3567 * not the reference to the imported module.
3568 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3569 * @return LY_ERR value.
3570 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003571static LY_ERR
3572lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3573{
3574 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3575 const char *prefix = NULL, *name;
3576 size_t prefix_len = 0;
3577
3578 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3579 name = strchr(dflt, ':');
3580 if (name) {
3581 prefix = dflt;
3582 prefix_len = name - prefix;
3583 ++name;
3584 } else {
3585 name = dflt;
3586 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003587 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003588 /* prefixed default case make sense only for the prefix of the schema itself */
3589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3590 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3591 prefix_len, prefix);
3592 return LY_EVALID;
3593 }
3594 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3595 if (!ch->dflt) {
3596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3597 "Default case \"%s\" not found.", dflt);
3598 return LY_EVALID;
3599 }
3600 /* no mandatory nodes directly under the default case */
3601 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003602 if (iter->parent != (struct lysc_node*)ch->dflt) {
3603 break;
3604 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003605 if (iter->flags & LYS_MAND_TRUE) {
3606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3607 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3608 return LY_EVALID;
3609 }
3610 }
Radek Krejci01342af2019-01-03 15:18:08 +01003611 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003612 return LY_SUCCESS;
3613}
3614
Radek Krejciccd20f12019-02-15 14:12:27 +01003615static LY_ERR
3616lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *devnodeid, const char *dflt, struct lysc_node_choice *ch)
3617{
3618 struct lys_module *mod;
3619 const char *prefix = NULL, *name;
3620 size_t prefix_len = 0;
3621 struct lysc_node_case *cs;
3622 struct lysc_node *node;
3623
3624 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3625 name = strchr(dflt, ':');
3626 if (name) {
3627 prefix = dflt;
3628 prefix_len = name - prefix;
3629 ++name;
3630 } else {
3631 name = dflt;
3632 }
3633 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3634 if (prefix) {
3635 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3636 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3637 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice. "
3638 "The prefix does not match any imported module of the deviation module.",
3639 devnodeid, dflt);
3640 return LY_EVALID;
3641 }
3642 } else {
3643 mod = ctx->mod;
3644 }
3645 /* get the default case */
3646 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3647 if (!cs) {
3648 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3649 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - the specified case does not exists.",
3650 devnodeid, dflt);
3651 return LY_EVALID;
3652 }
3653
3654 /* check that there is no mandatory node */
3655 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003656 if (node->parent != (struct lysc_node*)cs) {
3657 break;
3658 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003659 if (node->flags & LYS_MAND_TRUE) {
3660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3661 "Invalid deviation (%s) adding \"default\" property \"%s\" of choice - "
3662 "mandatory node \"%s\" under the default case.", devnodeid, dflt, node->name);
3663 return LY_EVALID;
3664 }
3665 }
3666
3667 /* set the default case in choice */
3668 ch->dflt = cs;
3669 cs->flags |= LYS_SET_DFLT;
3670
3671 return LY_SUCCESS;
3672}
3673
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003674/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003675 * @brief Compile parsed choice node information.
3676 * @param[in] ctx Compile context
3677 * @param[in] node_p Parsed choice node.
3678 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3679 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003680 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003681 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3682 */
3683static LY_ERR
3684lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3685{
3686 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3687 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3688 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003689 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003690 LY_ERR ret = LY_SUCCESS;
3691
Radek Krejci056d0a82018-12-06 16:57:25 +01003692 LY_LIST_FOR(ch_p->child, child_p) {
3693 if (child_p->nodetype == LYS_CASE) {
3694 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003695 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003696 }
3697 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003698 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003699 }
3700 }
3701
3702 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003703 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003704 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003705 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003706
Radek Krejci9800fb82018-12-13 14:26:23 +01003707 return ret;
3708}
3709
3710/**
3711 * @brief Compile parsed anydata or anyxml node information.
3712 * @param[in] ctx Compile context
3713 * @param[in] node_p Parsed anydata or anyxml node.
3714 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3715 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3716 * is enriched with the any-specific information.
3717 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3718 */
3719static LY_ERR
3720lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3721{
3722 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3723 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3724 unsigned int u;
3725 LY_ERR ret = LY_SUCCESS;
3726
3727 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3728
3729 if (any->flags & LYS_CONFIG_W) {
3730 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3731 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3732 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003733done:
3734 return ret;
3735}
3736
Radek Krejcib56c7502019-02-13 14:19:54 +01003737/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003738 * @brief Connect the node into the siblings list and check its name uniqueness.
3739 *
3740 * @param[in] ctx Compile context
3741 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3742 * the choice itself is expected instead of a specific case node.
3743 * @param[in] node Schema node to connect into the list.
Radek Krejci05b774b2019-02-25 13:26:18 +01003744 * @param[in] options Compile options to distinguish input/output when placing node into RPC/action.
Radek Krejci056d0a82018-12-06 16:57:25 +01003745 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3746 */
3747static LY_ERR
Radek Krejci05b774b2019-02-25 13:26:18 +01003748lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node, int options)
Radek Krejci056d0a82018-12-06 16:57:25 +01003749{
3750 struct lysc_node **children;
3751
3752 if (node->nodetype == LYS_CASE) {
3753 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3754 } else {
Radek Krejci05b774b2019-02-25 13:26:18 +01003755 children = lysc_node_children_p(parent, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003756 }
3757 if (children) {
3758 if (!(*children)) {
3759 /* first child */
3760 *children = node;
3761 } else if (*children != node) {
3762 /* by the condition in previous branch we cover the choice/case children
3763 * - the children list is shared by the choice and the the first case, in addition
3764 * the first child of each case must be referenced from the case node. So the node is
3765 * actually always already inserted in case it is the first children - so here such
3766 * a situation actually corresponds to the first branch */
3767 /* insert at the end of the parent's children list */
3768 (*children)->prev->next = node;
3769 node->prev = (*children)->prev;
3770 (*children)->prev = node;
3771
3772 /* check the name uniqueness */
3773 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3774 lysc_node_notifs(parent), node->name, node)) {
3775 return LY_EEXIST;
3776 }
3777 }
3778 }
3779 return LY_SUCCESS;
3780}
3781
Radek Krejci95710c92019-02-11 15:49:55 +01003782/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003783 * @brief Get the XPath context node for the given schema node.
3784 * @param[in] start The schema node where the XPath expression appears.
3785 * @return The context node to evaluate XPath expression in given schema node.
3786 * @return NULL in case the context node is the root node.
3787 */
3788static struct lysc_node *
3789lysc_xpath_context(struct lysc_node *start)
3790{
3791 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3792 start = start->parent);
3793 return start;
3794}
3795
3796/**
3797 * @brief Prepare the case structure in choice node for the new data node.
3798 *
3799 * 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
3800 * created in the choice when the first child was processed.
3801 *
3802 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003803 * @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,
3804 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003805 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3806 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3807 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3808 * @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,
3809 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003810 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003811static struct lysc_node_case*
3812lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3813{
3814 struct lysc_node *iter;
3815 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01003816 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003817 unsigned int u;
3818 LY_ERR ret;
3819
Radek Krejci95710c92019-02-11 15:49:55 +01003820#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003821 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003822 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003823 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3824 return NULL; \
3825 } \
3826 }
3827
Radek Krejci95710c92019-02-11 15:49:55 +01003828 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3829 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003830
3831 /* we have to add an implicit case node into the parent choice */
3832 cs = calloc(1, sizeof(struct lysc_node_case));
3833 DUP_STRING(ctx->ctx, child->name, cs->name);
3834 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003835 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003836 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3837 /* the case is already present since the child is not its first children */
3838 return (struct lysc_node_case*)ch->cases->prev;
3839 }
Radek Krejci95710c92019-02-11 15:49:55 +01003840 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003841
3842 /* explicit parent case is not present (this is its first child) */
3843 cs = calloc(1, sizeof(struct lysc_node_case));
3844 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3845 cs->flags = LYS_STATUS_MASK & node_p->flags;
3846 cs->sp = node_p;
3847
Radek Krejcib1b59152019-01-07 13:21:56 +01003848 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003849 LY_CHECK_RET(lys_compile_status(ctx, &cs->flags, ch->flags), NULL);
Radek Krejci00b874b2019-02-12 10:54:50 +01003850
3851 if (node_p->when) {
3852 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3853 ret = lys_compile_when(ctx, node_p->when, options, when);
3854 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003855 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003856 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003857 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003858 } else {
3859 LOGINT(ctx->ctx);
3860 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003861 }
3862 cs->module = ctx->mod;
3863 cs->prev = (struct lysc_node*)cs;
3864 cs->nodetype = LYS_CASE;
Radek Krejci05b774b2019-02-25 13:26:18 +01003865 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs, options);
Radek Krejci056d0a82018-12-06 16:57:25 +01003866 cs->parent = (struct lysc_node*)ch;
3867 cs->child = child;
3868
3869 return cs;
3870error:
3871 return NULL;
3872
3873#undef UNIQUE_CHECK
3874}
3875
Radek Krejcib56c7502019-02-13 14:19:54 +01003876/**
Radek Krejci93dcc392019-02-19 10:43:38 +01003877 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003878 *
3879 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01003880 * @param[in] node Target node where the config is supposed to be changed.
3881 * @param[in] config_flag Node's config flag to be applied to the @p node.
3882 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01003883 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3884 * 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 +01003885 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
3886 * @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 +01003887 * @return LY_ERR value.
3888 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003889static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01003890lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
3891 const char *nodeid, int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01003892{
3893 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01003894 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01003895
3896 if (config == (node->flags & LYS_CONFIG_MASK)) {
3897 /* nothing to do */
3898 return LY_SUCCESS;
3899 }
3900
3901 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003902 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01003903 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci93dcc392019-02-19 10:43:38 +01003905 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3906 refine_flag ? "refine" : "deviation", nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003907 return LY_EVALID;
3908 }
Radek Krejci93dcc392019-02-19 10:43:38 +01003909 node->flags |= LYS_SET_CONFIG;
3910 } else {
3911 if (node->flags & LYS_SET_CONFIG) {
3912 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
3913 /* setting config flags, but have node with explicit config true */
3914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3915 "Invalid %s of config in \"%s\" - configuration node cannot be child of any state data node.",
3916 refine_flag ? "refine" : "deviation", nodeid);
3917 return LY_EVALID;
3918 }
3919 /* do not change config on nodes where the config is explicitely set, this does not apply to
3920 * nodes, which are being changed explicitly (targets of refine or deviation) */
3921 return LY_SUCCESS;
3922 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003923 }
3924 node->flags &= ~LYS_CONFIG_MASK;
3925 node->flags |= config;
3926
3927 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003928 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci93dcc392019-02-19 10:43:38 +01003929 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, nodeid, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01003930 }
3931
Radek Krejci76b3e962018-12-14 17:01:25 +01003932 return LY_SUCCESS;
3933}
3934
Radek Krejcib56c7502019-02-13 14:19:54 +01003935/**
3936 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
3937 *
3938 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
3939 * the flag to such parents from a mandatory children.
3940 *
3941 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
3942 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
3943 * (mandatory children was removed).
3944 */
Radek Krejcife909632019-02-12 15:34:42 +01003945void
3946lys_compile_mandatory_parents(struct lysc_node *parent, int add)
3947{
3948 struct lysc_node *iter;
3949
3950 if (add) { /* set flag */
3951 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3952 parent = parent->parent) {
3953 parent->flags |= LYS_MAND_TRUE;
3954 }
3955 } else { /* unset flag */
3956 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003957 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01003958 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01003959 /* there is another mandatory node */
3960 return;
3961 }
3962 }
3963 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3964 parent->flags &= ~LYS_MAND_TRUE;
3965 }
3966 }
3967}
3968
Radek Krejci056d0a82018-12-06 16:57:25 +01003969/**
Radek Krejci3641f562019-02-13 15:38:40 +01003970 * @brief Internal sorting process for the lys_compile_augment_sort().
3971 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
3972 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
3973 * to be allocated to hold the complete list, its size is just incremented by adding another item.
3974 */
3975static void
3976lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
3977{
3978 unsigned int v;
3979 size_t len;
3980
3981 len = strlen(aug_p->nodeid);
3982 LY_ARRAY_FOR(result, v) {
3983 if (strlen(result[v]->nodeid) <= len) {
3984 continue;
3985 }
3986 if (v < LY_ARRAY_SIZE(result)) {
3987 /* move the rest of array */
3988 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
3989 break;
3990 }
3991 }
3992 result[v] = aug_p;
3993 LY_ARRAY_INCREMENT(result);
3994}
3995
3996/**
3997 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
3998 *
3999 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4000 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4001 *
4002 * @param[in] ctx Compile context.
4003 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4004 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4005 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4006 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4007 * @return LY_ERR value.
4008 */
4009LY_ERR
4010lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4011{
4012 struct lysp_augment **result = NULL;
4013 unsigned int u, v;
4014 size_t count = 0;
4015
4016 assert(augments);
4017
4018 /* get count of the augments in module and all its submodules */
4019 if (aug_p) {
4020 count += LY_ARRAY_SIZE(aug_p);
4021 }
4022 LY_ARRAY_FOR(inc_p, u) {
4023 if (inc_p[u].submodule->augments) {
4024 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4025 }
4026 }
4027
4028 if (!count) {
4029 *augments = NULL;
4030 return LY_SUCCESS;
4031 }
4032 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4033
4034 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4035 * together, so there can be even /z/y betwwen them. */
4036 LY_ARRAY_FOR(aug_p, u) {
4037 lys_compile_augment_sort_(&aug_p[u], result);
4038 }
4039 LY_ARRAY_FOR(inc_p, u) {
4040 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4041 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4042 }
4043 }
4044
4045 *augments = result;
4046 return LY_SUCCESS;
4047}
4048
4049/**
4050 * @brief Compile the parsed augment connecting it into its target.
4051 *
4052 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4053 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4054 * are already implemented and compiled.
4055 *
4056 * @param[in] ctx Compile context.
4057 * @param[in] aug_p Parsed augment to compile.
4058 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4059 * @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
4060 * children in case of the augmenting uses data.
4061 * @return LY_SUCCESS on success.
4062 * @return LY_EVALID on failure.
4063 */
4064LY_ERR
4065lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4066{
4067 LY_ERR ret = LY_SUCCESS;
4068 struct lysp_node *node_p, *case_node_p;
4069 struct lysc_node *target; /* target target of the augment */
4070 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004071 struct lysc_when **when, *when_shared;
4072 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004073 uint16_t flags = 0;
4074 unsigned int u;
Radek Krejci3641f562019-02-13 15:38:40 +01004075
Radek Krejci7af64242019-02-18 13:07:53 +01004076 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004077 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004078 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004079 if (ret != LY_SUCCESS) {
4080 if (ret == LY_EDENIED) {
4081 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4082 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4083 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4084 }
4085 return LY_EVALID;
4086 }
4087
4088 /* check for mandatory nodes
4089 * - new cases augmenting some choice can have mandatory nodes
4090 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4091 */
Radek Krejci733988a2019-02-15 15:12:44 +01004092 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004093 allow_mandatory = 1;
4094 }
4095
4096 when_shared = NULL;
4097 LY_LIST_FOR(aug_p->child, node_p) {
4098 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4099 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4100 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004101 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4102 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4104 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4105 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4106 return LY_EVALID;
4107 }
4108
4109 /* compile the children */
4110 if (node_p->nodetype != LYS_CASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004111 LY_CHECK_RET(lys_compile_node(ctx, node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004112 } else {
4113 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejci05b774b2019-02-25 13:26:18 +01004114 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options | flags, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004115 }
4116 }
4117
Radek Krejcife13da42019-02-15 14:51:01 +01004118 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4119 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004120 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004121 /* 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 +01004122 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 +01004123 } else if (target->nodetype == LYS_CHOICE) {
4124 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4125 node = ((struct lysc_node_choice*)target)->cases->prev;
4126 } else {
4127 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004128 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004129 }
4130
Radek Krejci733988a2019-02-15 15:12:44 +01004131 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004132 node->flags &= ~LYS_MAND_TRUE;
4133 lys_compile_mandatory_parents(target, 0);
4134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4135 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4136 aug_p->nodeid, node->name);
4137 return LY_EVALID;
4138 }
4139
4140 /* pass augment's when to all the children */
4141 if (aug_p->when) {
4142 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4143 if (!when_shared) {
4144 ret = lys_compile_when(ctx, aug_p->when, options, when);
4145 LY_CHECK_GOTO(ret, error);
4146 (*when)->context = lysc_xpath_context(target);
4147 when_shared = *when;
4148 } else {
4149 ++when_shared->refcount;
4150 (*when) = when_shared;
4151 }
4152 }
4153 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004154
4155 switch (target->nodetype) {
4156 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004157 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
4158 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004159 /* TODO notifications */
4160 break;
4161 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004162 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
4163 options | flags, u, lys_compile_action, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004164 /* TODO notifications */
4165 break;
4166 default:
4167 if (aug_p->actions) {
4168 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4169 "Invalid augment (%s) of %s node which is not allowed to contain RPC/action node \"%s\".",
4170 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
4171 return LY_EVALID;
4172 }
4173 if (aug_p->notifs) {
4174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4175 "Invalid augment (%s) of %s node which is not allowed to contain Notification node \"%s\".",
4176 aug_p->nodeid, lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
4177 return LY_EVALID;
4178 }
4179 }
Radek Krejci3641f562019-02-13 15:38:40 +01004180
4181error:
4182 return ret;
4183}
4184
4185/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004186 * @brief Apply refined or deviated mandatory flag to the target node.
4187 *
4188 * @param[in] ctx Compile context.
4189 * @param[in] node Target node where the mandatory property is supposed to be changed.
4190 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
4191 * @param[in] nodeid Schema nodeid used to identify target of refine/deviation (for logging).
4192 * @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 +01004193 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4194 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4195 * 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 +01004196 * @return LY_ERR value.
4197 */
4198static LY_ERR
4199lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, const char *nodeid, int refine_flag)
4200{
4201 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4203 "Invalid %s of mandatory in \"%s\" - %s cannot hold mandatory statement.",
4204 refine_flag ? "refine" : "deviation", nodeid, lys_nodetype2str(node->nodetype));
4205 return LY_EVALID;
4206 }
4207
4208 if (mandatory_flag & LYS_MAND_TRUE) {
4209 /* check if node has default value */
4210 if (node->nodetype & LYS_LEAF) {
4211 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004212 if (refine_flag) {
4213 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4214 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", nodeid);
4215 return LY_EVALID;
4216 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004217 } else {
4218 /* remove the default value taken from the leaf's type */
4219 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4220 ((struct lysc_node_leaf*)node)->dflt = NULL;
4221 }
4222 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004223 if (refine_flag) {
4224 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4225 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", nodeid);
4226 return LY_EVALID;
4227 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004228 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004229 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004230 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci551b12c2019-02-19 16:11:21 +01004231 "Invalid refine of mandatory in \"%s\" under the default case.", nodeid);
Radek Krejcif1421c22019-02-19 13:05:20 +01004232 return LY_EVALID;
4233 }
4234
4235 node->flags &= ~LYS_MAND_FALSE;
4236 node->flags |= LYS_MAND_TRUE;
4237 lys_compile_mandatory_parents(node->parent, 1);
4238 } else {
4239 /* make mandatory false */
4240 node->flags &= ~LYS_MAND_TRUE;
4241 node->flags |= LYS_MAND_FALSE;
4242 lys_compile_mandatory_parents(node->parent, 0);
4243 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4244 /* get the type's default value if any */
4245 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4246 }
4247 }
4248 return LY_SUCCESS;
4249}
4250
4251/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004252 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4253 * If present, also apply uses's modificators.
4254 *
4255 * @param[in] ctx Compile context
4256 * @param[in] uses_p Parsed uses schema node.
4257 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4258 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4259 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4260 * the compile context.
4261 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4262 */
4263static LY_ERR
4264lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
4265{
4266 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004267 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004268 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004269 struct lysc_node_container context_node_fake =
4270 {.nodetype = LYS_CONTAINER,
4271 .module = ctx->mod,
4272 .flags = parent ? parent->flags : 0,
4273 .child = NULL, .next = NULL,
4274 .prev = (struct lysc_node*)&context_node_fake};
Radek Krejcie86bf772018-12-14 11:39:53 +01004275 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004276 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004277 int found;
4278 const char *id, *name, *prefix;
4279 size_t prefix_len, name_len;
4280 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004281 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01004282 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01004283 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004284 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004285 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004286 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004287 struct lysp_augment **augments = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004288
4289 /* search for the grouping definition */
4290 found = 0;
4291 id = uses_p->name;
4292 lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
4293 if (prefix) {
4294 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4295 if (!mod) {
4296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4297 "Invalid prefix used for grouping reference (%s).", uses_p->name);
4298 return LY_EVALID;
4299 }
4300 } else {
4301 mod = ctx->mod_def;
4302 }
4303 if (mod == ctx->mod_def) {
4304 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
4305 grp = lysp_node_groupings(node_p);
4306 LY_ARRAY_FOR(grp, u) {
4307 if (!strcmp(grp[u].name, name)) {
4308 grp = &grp[u];
4309 found = 1;
4310 break;
4311 }
4312 }
4313 }
4314 }
4315 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004316 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004317 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004318 if (grp) {
4319 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4320 if (!strcmp(grp[u].name, name)) {
4321 grp = &grp[u];
4322 found = 1;
4323 }
4324 }
4325 }
4326 if (!found && mod->parsed->includes) {
4327 /* ... and all the submodules */
4328 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4329 grp = mod->parsed->includes[u].submodule->groupings;
4330 if (grp) {
4331 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4332 if (!strcmp(grp[v].name, name)) {
4333 grp = &grp[v];
4334 found = 1;
4335 }
4336 }
4337 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004338 }
4339 }
4340 }
4341 if (!found) {
4342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4343 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4344 return LY_EVALID;
4345 }
4346
4347 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4348 grp_stack_count = ctx->groupings.count;
4349 ly_set_add(&ctx->groupings, (void*)grp, 0);
4350 if (grp_stack_count == ctx->groupings.count) {
4351 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4353 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4354 return LY_EVALID;
4355 }
4356
4357 /* switch context's mod_def */
4358 mod_old = ctx->mod_def;
4359 ctx->mod_def = mod;
4360
4361 /* check status */
4362 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), error);
4363
Radek Krejcie86bf772018-12-14 11:39:53 +01004364 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004365 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
4366 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004367 child = parent ? lysc_node_children(parent, options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01004368
4369 /* some preparation for applying refines */
4370 if (grp->data == node_p) {
4371 /* remember the first child */
4372 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01004373 }
4374 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004375 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004376 LY_LIST_FOR(context_node_fake.child, child) {
4377 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004378
4379 /* pass uses's when to all the children */
4380 if (uses_p->when) {
4381 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, error);
4382 if (!when_shared) {
Radek Krejci3641f562019-02-13 15:38:40 +01004383 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, options, when), error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004384 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004385 when_shared = *when;
4386 } else {
4387 ++when_shared->refcount;
4388 (*when) = when_shared;
4389 }
4390 }
Radek Krejci01342af2019-01-03 15:18:08 +01004391 }
4392 if (context_node_fake.child) {
4393 child = context_node_fake.child->prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004394 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 +01004395 }
4396
Radek Krejci3641f562019-02-13 15:38:40 +01004397 /* sort and apply augments */
4398 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), error);
4399 LY_ARRAY_FOR(augments, u) {
4400 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], options, (struct lysc_node*)&context_node_fake), error);
4401 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004402
Radek Krejcif0089082019-01-07 16:42:01 +01004403 /* reload previous context's mod_def */
4404 ctx->mod_def = mod_old;
4405
Radek Krejci76b3e962018-12-14 17:01:25 +01004406 /* apply refine */
4407 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci7af64242019-02-18 13:07:53 +01004408 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 +01004409 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejci01342af2019-01-03 15:18:08 +01004410 error);
Radek Krejcif2271f12019-01-07 16:42:23 +01004411 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004412
4413 /* default value */
4414 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004415 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4417 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
4418 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
4419 goto error;
4420 }
4421 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4423 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
4424 rfn->nodeid, lys_nodetype2str(node->nodetype));
4425 goto error;
4426 }
4427 if (node->nodetype == LYS_LEAF) {
4428 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4429 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01004430 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004431 /* TODO check the default value according to type */
4432 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004433 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004434 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4435 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
4436 goto error;
4437 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004438 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
4439 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
4440 }
4441 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01004442 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004443 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, error);
4444 LY_ARRAY_FOR(rfn->dflts, u) {
4445 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
4446 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
4447 }
4448 /* TODO check the default values according to type */
4449 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004450 if (((struct lysc_node_choice*)node)->dflt) {
4451 /* unset LYS_SET_DFLT from the current default case */
4452 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4453 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004454 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), error);
4455 }
4456 }
4457
Radek Krejci12fb9142019-01-08 09:45:30 +01004458 /* description */
4459 if (rfn->dsc) {
4460 FREE_STRING(ctx->ctx, node->dsc);
4461 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4462 }
4463
4464 /* reference */
4465 if (rfn->ref) {
4466 FREE_STRING(ctx->ctx, node->ref);
4467 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4468 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004469
4470 /* config */
4471 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004472 if (!flags) {
4473 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, rfn->nodeid, 0, 1), error);
4474 } else {
4475 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4476 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4477 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004478 }
4479
4480 /* mandatory */
4481 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004482 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, rfn->nodeid, 1), error);
Radek Krejci76b3e962018-12-14 17:01:25 +01004483 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004484
4485 /* presence */
4486 if (rfn->presence) {
4487 if (node->nodetype != LYS_CONTAINER) {
4488 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4489 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4490 rfn->nodeid, lys_nodetype2str(node->nodetype));
4491 goto error;
4492 }
4493 node->flags |= LYS_PRESENCE;
4494 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004495
4496 /* must */
4497 if (rfn->musts) {
4498 switch (node->nodetype) {
4499 case LYS_LEAF:
4500 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, error);
4501 break;
4502 case LYS_LEAFLIST:
4503 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, error);
4504 break;
4505 case LYS_LIST:
4506 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, error);
4507 break;
4508 case LYS_CONTAINER:
4509 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, error);
4510 break;
4511 case LYS_ANYXML:
4512 case LYS_ANYDATA:
4513 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, error);
4514 break;
4515 default:
4516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4517 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4518 rfn->nodeid, lys_nodetype2str(node->nodetype));
4519 goto error;
4520 }
4521 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004522
4523 /* min/max-elements */
4524 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4525 switch (node->nodetype) {
4526 case LYS_LEAFLIST:
4527 if (rfn->flags & LYS_SET_MAX) {
4528 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4529 }
4530 if (rfn->flags & LYS_SET_MIN) {
4531 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004532 if (rfn->min) {
4533 node->flags |= LYS_MAND_TRUE;
4534 lys_compile_mandatory_parents(node->parent, 1);
4535 } else {
4536 node->flags &= ~LYS_MAND_TRUE;
4537 lys_compile_mandatory_parents(node->parent, 0);
4538 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004539 }
4540 break;
4541 case LYS_LIST:
4542 if (rfn->flags & LYS_SET_MAX) {
4543 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4544 }
4545 if (rfn->flags & LYS_SET_MIN) {
4546 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004547 if (rfn->min) {
4548 node->flags |= LYS_MAND_TRUE;
4549 lys_compile_mandatory_parents(node->parent, 1);
4550 } else {
4551 node->flags &= ~LYS_MAND_TRUE;
4552 lys_compile_mandatory_parents(node->parent, 0);
4553 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004554 }
4555 break;
4556 default:
4557 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4558 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4559 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
4560 goto error;
4561 }
4562 }
Radek Krejcif0089082019-01-07 16:42:01 +01004563
4564 /* if-feature */
4565 if (rfn->iffeatures) {
4566 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
4567 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
4568 }
Radek Krejci01342af2019-01-03 15:18:08 +01004569 }
4570 /* fix connection of the children nodes from fake context node back into the parent */
4571 if (context_node_fake.child) {
4572 context_node_fake.child->prev = child;
4573 }
4574 LY_LIST_FOR(context_node_fake.child, child) {
4575 child->parent = parent;
Radek Krejcie86bf772018-12-14 11:39:53 +01004576 }
4577
Radek Krejcif2271f12019-01-07 16:42:23 +01004578 /* do some additional checks of the changed nodes when all the refines are applied */
4579 for (u = 0; u < refined.count; ++u) {
4580 node = (struct lysc_node*)refined.objs[u];
4581 rfn = &uses_p->refines[u];
4582
4583 /* check possible conflict with default value (default added, mandatory left true) */
4584 if ((node->flags & LYS_MAND_TRUE) &&
4585 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4586 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4588 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
4589 goto error;
4590 }
4591
4592 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4593 if (node->nodetype == LYS_LIST) {
4594 min = ((struct lysc_node_list*)node)->min;
4595 max = ((struct lysc_node_list*)node)->max;
4596 } else {
4597 min = ((struct lysc_node_leaflist*)node)->min;
4598 max = ((struct lysc_node_leaflist*)node)->max;
4599 }
4600 if (min > max) {
4601 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4602 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4603 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
4604 goto error;
4605 }
4606 }
4607 }
4608
Radek Krejcie86bf772018-12-14 11:39:53 +01004609 ret = LY_SUCCESS;
4610error:
4611 /* reload previous context's mod_def */
4612 ctx->mod_def = mod_old;
4613 /* remove the grouping from the stack for circular groupings dependency check */
4614 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4615 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004616 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004617 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01004618
4619 return ret;
4620}
4621
Radek Krejcife909632019-02-12 15:34:42 +01004622
Radek Krejcie86bf772018-12-14 11:39:53 +01004623/**
Radek Krejcia3045382018-11-22 14:30:31 +01004624 * @brief Compile parsed schema node information.
4625 * @param[in] ctx Compile context
4626 * @param[in] node_p Parsed schema node.
4627 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4628 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4629 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4630 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004631 * @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).
4632 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004633 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4634 */
Radek Krejci19a96102018-11-15 13:38:09 +01004635static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01004636lys_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 +01004637{
4638 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004639 struct lysc_node *node;
4640 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004641 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004642 unsigned int u;
4643 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
4644
4645 switch (node_p->nodetype) {
4646 case LYS_CONTAINER:
4647 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4648 node_compile_spec = lys_compile_node_container;
4649 break;
4650 case LYS_LEAF:
4651 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4652 node_compile_spec = lys_compile_node_leaf;
4653 break;
4654 case LYS_LIST:
4655 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004656 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004657 break;
4658 case LYS_LEAFLIST:
4659 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004660 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004661 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004662 case LYS_CHOICE:
4663 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004664 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004665 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004666 case LYS_ANYXML:
4667 case LYS_ANYDATA:
4668 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004669 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004670 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004671 case LYS_USES:
4672 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004673 default:
4674 LOGINT(ctx->ctx);
4675 return LY_EINT;
4676 }
4677 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4678 node->nodetype = node_p->nodetype;
4679 node->module = ctx->mod;
4680 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004681 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004682
4683 /* config */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004684 if (options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
4685 /* ignore config statements inside RPC/action data */
4686 node->flags &= LYS_CONFIG_MASK;
4687 node->flags |= (options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
4688 } else if (options & LYSC_OPT_NOTIFICATION) {
4689 /* ignore config statements inside Notification data */
4690 node->flags &= LYS_CONFIG_MASK;
4691 node->flags |= LYS_CONFIG_R;
4692 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01004693 /* config not explicitely set, inherit it from parent */
4694 if (parent) {
4695 node->flags |= parent->flags & LYS_CONFIG_MASK;
4696 } else {
4697 /* default is config true */
4698 node->flags |= LYS_CONFIG_W;
4699 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004700 } else {
4701 /* config set explicitely */
4702 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01004703 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004704 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4706 "Configuration node cannot be child of any state data node.");
4707 goto error;
4708 }
Radek Krejci19a96102018-11-15 13:38:09 +01004709
Radek Krejcia6d57732018-11-29 13:40:37 +01004710 /* *list ordering */
4711 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4712 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
4713 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
4714 "RPC/action output parameters or notification content (%s).", ctx->path);
4715 node->flags &= ~LYS_ORDBY_MASK;
4716 node->flags |= LYS_ORDBY_SYSTEM;
4717 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4718 /* default ordering is system */
4719 node->flags |= LYS_ORDBY_SYSTEM;
4720 }
4721 }
4722
Radek Krejci19a96102018-11-15 13:38:09 +01004723 /* status - it is not inherited by specification, but it does not make sense to have
4724 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004725 if (!parent || parent->nodetype != LYS_CHOICE) {
4726 /* in case of choice/case's children, postpone the check to the moment we know if
4727 * 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 +01004728 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 +01004729 }
4730
4731 if (!(options & LYSC_OPT_FREE_SP)) {
4732 node->sp = node_p;
4733 }
4734 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004735 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4736 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004737 if (node_p->when) {
4738 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4739 ret = lys_compile_when(ctx, node_p->when, options, when);
4740 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004741 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004742 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004743 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004744 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
4745
4746 /* nodetype-specific part */
4747 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
4748
Radek Krejcife909632019-02-12 15:34:42 +01004749 /* inherit LYS_MAND_TRUE in parent containers */
4750 if (node->flags & LYS_MAND_TRUE) {
4751 lys_compile_mandatory_parents(parent, 1);
4752 }
4753
Radek Krejci19a96102018-11-15 13:38:09 +01004754 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004755 if (parent) {
4756 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004757 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
4758 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004759 if (uses_status) {
4760
4761 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004762 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004763 * it directly gets the same status flags as the choice;
4764 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004765 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004766 node->parent = (struct lysc_node*)cs;
4767 } else { /* other than choice */
4768 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004769 }
Radek Krejci05b774b2019-02-25 13:26:18 +01004770 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 +01004771 } else {
4772 /* top-level element */
4773 if (!ctx->mod->compiled->data) {
4774 ctx->mod->compiled->data = node;
4775 } else {
4776 /* insert at the end of the module's top-level nodes list */
4777 ctx->mod->compiled->data->prev->next = node;
4778 node->prev = ctx->mod->compiled->data->prev;
4779 ctx->mod->compiled->data->prev = node;
4780 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004781 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4782 ctx->mod->compiled->notifs, node->name, node)) {
4783 return LY_EVALID;
4784 }
Radek Krejci19a96102018-11-15 13:38:09 +01004785 }
4786
4787 return LY_SUCCESS;
4788
4789error:
4790 lysc_node_free(ctx->ctx, node);
4791 return ret;
4792}
4793
Radek Krejciccd20f12019-02-15 14:12:27 +01004794static void
4795lysc_disconnect(struct lysc_node *node)
4796{
Radek Krejci0a048dd2019-02-18 16:01:43 +01004797 struct lysc_node *parent, *child, *prev = NULL, *next;
4798 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01004799 int remove_cs = 0;
4800
4801 parent = node->parent;
4802
4803 /* parent's first child */
4804 if (!parent) {
4805 return;
4806 }
4807 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01004808 cs = (struct lysc_node_case*)node;
4809 } else if (parent->nodetype == LYS_CASE) {
4810 /* disconnecting some node in a case */
4811 cs = (struct lysc_node_case*)parent;
4812 parent = cs->parent;
4813 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
4814 if (child == node) {
4815 if (cs->child == child) {
4816 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
4817 /* case with a single child -> remove also the case */
4818 child->parent = NULL;
4819 remove_cs = 1;
4820 } else {
4821 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01004822 }
4823 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004824 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01004825 }
4826 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004827 if (!remove_cs) {
4828 cs = NULL;
4829 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004830 } else if (lysc_node_children(parent, node->flags) == node) {
4831 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004832 }
4833
4834 if (cs) {
4835 if (remove_cs) {
4836 /* cs has only one child which is being also removed */
4837 lysc_disconnect((struct lysc_node*)cs);
4838 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
4839 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01004840 if (((struct lysc_node_choice*)parent)->dflt == cs) {
4841 /* default case removed */
4842 ((struct lysc_node_choice*)parent)->dflt = NULL;
4843 }
4844 if (((struct lysc_node_choice*)parent)->cases == cs) {
4845 /* first case removed */
4846 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
4847 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01004848 if (cs->child) {
4849 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
4850 if (cs->child->prev->parent != (struct lysc_node*)cs) {
4851 prev = cs->child->prev;
4852 } /* else all the children are under a single case */
4853 LY_LIST_FOR_SAFE(cs->child, next, child) {
4854 if (child->parent != (struct lysc_node*)cs) {
4855 break;
4856 }
4857 lysc_node_free(node->module->ctx, child);
4858 }
4859 if (prev) {
4860 if (prev->next) {
4861 prev->next = child;
4862 }
4863 if (child) {
4864 child->prev = prev;
4865 } else {
4866 /* link from the first child under the cases */
4867 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
4868 }
4869 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004870 }
4871 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004872 }
4873
4874 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01004875 if (node->prev->next) {
4876 node->prev->next = node->next;
4877 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004878 if (node->next) {
4879 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01004880 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004881 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01004882 if (child) {
4883 child->prev = node->prev;
4884 }
4885 } else if (((struct lysc_node_choice*)parent)->cases) {
4886 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01004887 }
4888}
4889
4890LY_ERR
4891lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p, int options)
4892{
4893 LY_ERR ret = LY_EVALID;
4894 struct ly_set devs_p = {0};
4895 struct ly_set targets = {0};
4896 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01004897 struct lysc_node_list *list;
Radek Krejcif538ce52019-03-05 10:46:14 +01004898 struct lysc_action *actions;
Radek Krejciccd20f12019-02-15 14:12:27 +01004899 struct lysp_deviation *dev;
4900 struct lysp_deviate *d, **dp_new;
4901 struct lysp_deviate_add *d_add;
4902 struct lysp_deviate_del *d_del;
4903 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01004904 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01004905 struct lysc_deviation {
4906 const char *nodeid;
4907 struct lysc_node *target; /* target node of the deviation */
4908 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004909 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01004910 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
4911 } **devs = NULL;
4912 int i;
4913 size_t prefix_len, name_len;
4914 const char *prefix, *name, *nodeid;
4915 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01004916 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004917 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01004918
4919 /* get all deviations from the module and all its submodules ... */
4920 LY_ARRAY_FOR(mod_p->deviations, u) {
4921 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
4922 }
4923 LY_ARRAY_FOR(mod_p->includes, v) {
4924 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
4925 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
4926 }
4927 }
4928 if (!devs_p.count) {
4929 /* nothing to do */
4930 return LY_SUCCESS;
4931 }
4932
4933 /* ... and group them by the target node */
4934 devs = calloc(devs_p.count, sizeof *devs);
4935 for (u = 0; u < devs_p.count; ++u) {
4936 dev = devs_p.objs[u];
4937
4938 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004939 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
4940 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01004941 if (target->nodetype == LYS_ACTION) {
4942 /* move the target pointer to input/output to make them different from the action and
4943 * between them. Before the devs[] item is being processed, the target pointer must be fixed
4944 * back to the RPC/action node due to a better compatibility and decision code in this function.
4945 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
4946 if (flags & LYSC_OPT_RPC_INPUT) {
4947 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
4948 flags |= LYSC_OPT_INTERNAL;
4949 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
4950 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
4951 flags |= LYSC_OPT_INTERNAL;
4952 }
4953 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004954 /* insert into the set of targets with duplicity detection */
4955 i = ly_set_add(&targets, target, 0);
4956 if (!devs[i]) {
4957 /* new record */
4958 devs[i] = calloc(1, sizeof **devs);
4959 devs[i]->target = target;
4960 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004961 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01004962 }
4963 /* add deviates into the deviation's list of deviates */
4964 for (d = dev->deviates; d; d = d->next) {
4965 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
4966 *dp_new = d;
4967 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
4968 devs[i]->not_supported = 1;
4969 }
4970 }
4971 }
4972
4973 /* MACROS for deviates checking */
4974#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
4975 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
4976 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
4977 goto cleanup; \
4978 }
4979
4980#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
4981 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
4982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation (%s) of %s with too many (%u) %s properties.", \
4983 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
4984 goto cleanup; \
4985 }
4986
4987#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01004988 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01004989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
4990 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%s\").", \
4991 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
4992 goto cleanup; \
4993 }
4994
Radek Krejci551b12c2019-02-19 16:11:21 +01004995#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
4996 if (((TYPE)devs[u]->target)->MEMBER COND) { \
4997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
4998 "Invalid deviation (%s) adding \"%s\" property which already exists (with value \"%u\").", \
4999 devs[u]->nodeid, PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
5000 goto cleanup; \
5001 }
5002
Radek Krejciccd20f12019-02-15 14:12:27 +01005003#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5004 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
5005 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid, DEVTYPE, PROPERTY, VALUE); \
5006 goto cleanup; \
5007 }
5008
Radek Krejci551b12c2019-02-19 16:11:21 +01005009#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5010 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5012 "Invalid deviation (%s) replacing with \"%s\" property \"%u\" which is not present.", \
5013 devs[u]->nodeid, PROPERTY, d_rpl->MEMBER); \
5014 goto cleanup; \
5015 }
5016
Radek Krejciccd20f12019-02-15 14:12:27 +01005017#define DEV_DEL_MEMBER(TYPE, MEMBER_TRG, MEMBER_DEV, DELFUNC, PROPERTY) \
5018 DEV_CHECK_PRESENCE(TYPE, 0, MEMBER_TRG, "deleting", PROPERTY, d_del->MEMBER_DEV); \
5019 if (strcmp(((TYPE)devs[u]->target)->MEMBER_TRG, d_del->MEMBER_DEV)) { \
5020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5021 "Invalid deviation (%s) deleting \"%s\" property \"%s\" which does not match the target's property value \"%s\".", \
5022 devs[u]->nodeid, PROPERTY, d_del->MEMBER_DEV, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5023 goto cleanup; \
5024 } \
5025 DELFUNC(ctx->ctx, ((TYPE)devs[u]->target)->MEMBER_TRG); \
5026 ((TYPE)devs[u]->target)->MEMBER_TRG = NULL;
5027
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005028#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5029 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5030 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5031 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5032 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 +01005033 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005034 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005035 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5036 "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 +01005037 devs[u]->nodeid, PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005038 goto cleanup; \
5039 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005040 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5041 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5042 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5043 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5044 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005045 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005046 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5047 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5048 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005049 }
5050
5051 /* apply deviations */
5052 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcif538ce52019-03-05 10:46:14 +01005053 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5054 /* fix the target pointer in case of RPC's/action's input/output */
5055 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5056 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5057 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5058 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5059 }
5060 }
5061
Radek Krejciccd20f12019-02-15 14:12:27 +01005062 /* not-supported */
5063 if (devs[u]->not_supported) {
5064 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5065 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5066 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5067 }
Radek Krejcif538ce52019-03-05 10:46:14 +01005068 if (devs[u]->target->nodetype == LYS_ACTION) {
5069 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5070 /* remove RPC's/action's input */
5071 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5072 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
5073 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5074 /* remove RPC's/action's output */
5075 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5076 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
5077 } else {
5078 /* remove RPC/action */
5079 if (devs[u]->target->parent) {
5080 actions = (struct lysc_action*)lysc_node_actions(devs[u]->target->parent);
5081 } else {
5082 actions = devs[u]->target->module->compiled->rpcs;
5083 }
5084 LY_ARRAY_FOR(actions, x) {
5085 if (&actions[x] == (struct lysc_action*)devs[u]->target) {
5086 break;
5087 }
5088 }
5089 if (x < LY_ARRAY_SIZE(actions)) {
5090 lysc_action_free(ctx->ctx, &actions[x]);
5091 memmove(&actions[x], &actions[x + 1], (LY_ARRAY_SIZE(actions) - (x + 1)) * sizeof *actions);
5092 LY_ARRAY_DECREMENT(actions);
5093 }
5094 }
5095 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
5096 /* TODO Notification */
5097 } else {
5098 /* remove the target node */
5099 lysc_disconnect(devs[u]->target);
5100 lysc_node_free(ctx->ctx, devs[u]->target);
5101 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005102
5103 continue;
5104 }
5105
5106 /* list of deviates (not-supported is not present in the list) */
5107 LY_ARRAY_FOR(devs[u]->deviates, v) {
5108 d = devs[u]->deviates[v];
5109
5110 switch (d->mod) {
5111 case LYS_DEV_ADD:
5112 d_add = (struct lysp_deviate_add*)d;
5113 /* [units-stmt] */
5114 if (d_add->units) {
5115 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5116 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5117
5118 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5119 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5120 }
5121
5122 /* *must-stmt */
5123 if (d_add->musts) {
5124 switch (devs[u]->target->nodetype) {
5125 case LYS_CONTAINER:
5126 case LYS_LIST:
5127 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5128 options, x, lys_compile_must, ret, cleanup);
5129 break;
5130 case LYS_LEAF:
5131 case LYS_LEAFLIST:
5132 case LYS_ANYDATA:
5133 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5134 options, x, lys_compile_must, ret, cleanup);
5135 break;
5136 case LYS_NOTIF:
Radek Krejciccd20f12019-02-15 14:12:27 +01005137 /* TODO */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005138 break;
5139 case LYS_ACTION:
5140 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5141 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5142 options, x, lys_compile_must, ret, cleanup);
5143 break;
5144 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5145 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5146 options, x, lys_compile_must, ret, cleanup);
5147 break;
5148 }
5149 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005150 default:
5151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005152 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005153 goto cleanup;
5154 }
5155 }
5156
5157 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005158 if (d_add->uniques) {
5159 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5160 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5161 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005162
5163 /* *default-stmt */
5164 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005165 switch (devs[u]->target->nodetype) {
5166 case LYS_LEAF:
5167 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5168 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt);
5169 if (((struct lysc_node_leaf*)devs[u]->target)->dflt) {
5170 /* first, remove the default value taken from the type */
5171 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5172 ((struct lysc_node_leaf*)devs[u]->target)->dflt = NULL;
5173 }
5174 DUP_STRING(ctx->ctx, d_add->dflts[0], ((struct lysc_node_leaf*)devs[u]->target)->dflt);
Radek Krejci551b12c2019-02-19 16:11:21 +01005175 /* mark the new default values as leaf's own */
5176 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005177 break;
5178 case LYS_LEAFLIST:
5179 if (((struct lysc_node_leaflist*)devs[u]->target)->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
5180 /* first, remove the default value taken from the type */
5181 LY_ARRAY_FOR(((struct lysc_node_leaflist*)devs[u]->target)->dflts, x) {
5182 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5183 }
5184 LY_ARRAY_FREE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5185 ((struct lysc_node_leaflist*)devs[u]->target)->dflts = NULL;
5186 }
5187 /* add new default value(s) */
5188 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)devs[u]->target)->dflts,
5189 LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5190 for (x = y = LY_ARRAY_SIZE(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5191 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
5192 DUP_STRING(ctx->ctx, d_add->dflts[x - y], ((struct lysc_node_leaflist*)devs[u]->target)->dflts[x]);
5193 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)devs[u]->target)->dflts);
5194 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005195 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005196 devs[u]->target->flags |= LYS_SET_DFLT;
5197 break;
5198 case LYS_CHOICE:
5199 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5200 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5201 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5202 * to allow making the default case even the augmented case from the deviating module */
5203 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_add->dflts[0],
5204 (struct lysc_node_choice*)devs[u]->target)) {
5205 goto cleanup;
5206 }
5207 break;
5208 default:
5209 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005210 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005211 goto cleanup;
5212 }
5213 }
5214
5215 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005216 if (d_add->flags & LYS_CONFIG_MASK) {
5217 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5218 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5219 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5220 goto cleanup;
5221 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005222 if (devs[u]->flags) {
5223 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect (%s).",
5224 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", devs[u]->nodeid);
5225 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005226 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5227 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5228 "Invalid deviation (%s) adding \"config\" property which already exists (with value \"config %s\").",
5229 devs[u]->nodeid, devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
5230 goto cleanup;
5231 }
5232 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0, 0), cleanup);
5233 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005234
5235 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005236 if (d_add->flags & LYS_MAND_MASK) {
5237 if (devs[u]->target->flags & LYS_MAND_MASK) {
5238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5239 "Invalid deviation (%s) adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5240 devs[u]->nodeid, devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
5241 goto cleanup;
5242 }
5243 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, devs[u]->nodeid, 0), cleanup);
5244 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005245
5246 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005247 if (d_add->flags & LYS_SET_MIN) {
5248 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5249 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5250 /* change value */
5251 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5252 } else if (devs[u]->target->nodetype == LYS_LIST) {
5253 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5254 /* change value */
5255 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5256 } else {
5257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5258 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5259 goto cleanup;
5260 }
5261 if (d_add->min) {
5262 devs[u]->target->flags |= LYS_MAND_TRUE;
5263 }
5264 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005265
5266 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005267 if (d_add->flags & LYS_SET_MAX) {
5268 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5269 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5270 /* change value */
5271 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5272 } else if (devs[u]->target->nodetype == LYS_LIST) {
5273 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5274 /* change value */
5275 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5276 } else {
5277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5278 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5279 goto cleanup;
5280 }
5281 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005282
5283 break;
5284 case LYS_DEV_DELETE:
5285 d_del = (struct lysp_deviate_del*)d;
5286
5287 /* [units-stmt] */
5288 if (d_del->units) {
5289 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
5290 DEV_DEL_MEMBER(struct lysc_node_leaf*, units, units, lydict_remove, "units");
5291 }
5292
5293 /* *must-stmt */
5294 if (d_del->musts) {
5295 switch (devs[u]->target->nodetype) {
5296 case LYS_CONTAINER:
5297 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005298 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005299 break;
5300 case LYS_LEAF:
5301 case LYS_LEAFLIST:
5302 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005303 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005304 break;
5305 case LYS_NOTIF:
Radek Krejciccd20f12019-02-15 14:12:27 +01005306 /* TODO */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005307 break;
5308 case LYS_ACTION:
5309 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5310 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5311 break;
5312 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5313 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5314 break;
5315 }
5316 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005317 default:
5318 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005319 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005320 goto cleanup;
5321 }
5322 }
5323
5324 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005325 if (d_del->uniques) {
5326 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
5327 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
5328 LY_ARRAY_FOR(d_del->uniques, x) {
5329 LY_ARRAY_FOR(list->uniques, z) {
5330 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
5331 nodeid = strpbrk(name, " \t\n");
5332 if (nodeid) {
5333 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
5334 || list->uniques[z][y]->name[nodeid - name] != '\0') {
5335 break;
5336 }
5337 while (isspace(*nodeid)) {
5338 ++nodeid;
5339 }
5340 } else {
5341 if (strcmp(name, list->uniques[z][y]->name)) {
5342 break;
5343 }
5344 }
5345 }
5346 if (!name) {
5347 /* complete match - remove the unique */
5348 LY_ARRAY_DECREMENT(list->uniques);
5349 LY_ARRAY_FREE(list->uniques[z]);
5350 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
5351 --z;
5352 break;
5353 }
5354 }
5355 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
5356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5357 "Invalid deviation (%s) deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
5358 devs[u]->nodeid, d_del->uniques[x]);
5359 goto cleanup;
5360 }
5361 }
5362 if (!LY_ARRAY_SIZE(list->uniques)) {
5363 LY_ARRAY_FREE(list->uniques);
5364 list->uniques = NULL;
5365 }
5366 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005367
5368 /* *default-stmt */
5369 if (d_del->dflts) {
5370 switch (devs[u]->target->nodetype) {
5371 case LYS_LEAF:
5372 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5373 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5374 dflt, "deleting", "default", d_del->dflts[0]);
5375
5376 DEV_DEL_MEMBER(struct lysc_node_leaf*, dflt, dflts[0], lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005377 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005378 break;
5379 case LYS_LEAFLIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005380 DEV_DEL_ARRAY(struct lysc_node_leaflist*, dflts, dflts, , , , lydict_remove, "default");
Radek Krejci551b12c2019-02-19 16:11:21 +01005381 if (!((struct lysc_node_leaflist*)devs[u]->target)->dflts) {
5382 devs[u]->target->flags &= ~LYS_SET_DFLT;
5383 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005384 break;
5385 case LYS_CHOICE:
5386 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
5387 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
5388 nodeid = d_del->dflts[0];
5389 LY_CHECK_GOTO(lys_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
5390 if (prefix) {
5391 /* use module prefixes from the deviation module to match the module of the default case */
5392 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
5393 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5394 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5395 "The prefix does not match any imported module of the deviation module.",
5396 devs[u]->nodeid, d_del->dflts[0]);
5397 goto cleanup;
5398 }
5399 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
5400 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
5401 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice. "
5402 "The prefix does not match the default case's module.",
5403 devs[u]->nodeid, d_del->dflts[0]);
5404 goto cleanup;
5405 }
5406 }
5407 /* else {
5408 * strictly, the default prefix would point to the deviation module, but the value should actually
5409 * match the default string in the original module (usually unprefixed), so in this case we do not check
5410 * the module of the default case, just matching its name */
5411 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
5412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5413 "Invalid deviation (%s) deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
5414 devs[u]->nodeid, d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
5415 goto cleanup;
5416 }
5417 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
5418 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
5419 break;
5420 default:
5421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005422 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005423 goto cleanup;
5424 }
5425 }
5426
5427 break;
5428 case LYS_DEV_REPLACE:
5429 d_rpl = (struct lysp_deviate_rpl*)d;
5430
5431 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01005432 if (d_rpl->type) {
5433 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
5434 /* type is mandatory, so checking for its presence is not necessary */
5435 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
5436 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, options, (struct lysc_node_leaf*)devs[u]->target), cleanup);
5437 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005438
5439 /* [units-stmt] */
5440 if (d_rpl->units) {
5441 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
5442 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
5443 units, "replacing", "units", d_rpl->units);
5444
5445 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5446 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5447 }
5448
5449 /* [default-stmt] */
5450 if (d_rpl->dflt) {
5451 switch (devs[u]->target->nodetype) {
5452 case LYS_LEAF:
5453 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
5454 dflt, "replacing", "default", d_rpl->dflt);
5455
5456 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5457 DUP_STRING(ctx->ctx, d_rpl->dflt, ((struct lysc_node_leaf*)devs[u]->target)->dflt);
5458 break;
5459 case LYS_CHOICE:
5460 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
5461 if (lys_compile_deviation_set_choice_dflt(ctx, devs[u]->nodeid, d_rpl->dflt,
5462 (struct lysc_node_choice*)devs[u]->target)) {
5463 goto cleanup;
5464 }
5465 break;
5466 default:
5467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci7af64242019-02-18 13:07:53 +01005468 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005469 goto cleanup;
5470 }
5471 }
5472
5473 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005474 if (d_rpl->flags & LYS_CONFIG_MASK) {
5475 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
5476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5477 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
5478 goto cleanup;
5479 }
5480 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
5481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5482 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
5483 goto cleanup;
5484 }
5485 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0, 0), cleanup);
5486 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005487
5488 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005489 if (d_rpl->flags & LYS_MAND_MASK) {
5490 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
5491 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, devs[u]->nodeid,
5492 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
5493 goto cleanup;
5494 }
5495 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, devs[u]->nodeid, 0), cleanup);
5496 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005497
5498 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005499 if (d_rpl->flags & LYS_SET_MIN) {
5500 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5501 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5502 /* change value */
5503 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
5504 } else if (devs[u]->target->nodetype == LYS_LIST) {
5505 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5506 /* change value */
5507 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
5508 } else {
5509 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5510 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
5511 goto cleanup;
5512 }
5513 if (d_rpl->min) {
5514 devs[u]->target->flags |= LYS_MAND_TRUE;
5515 }
5516 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005517
5518 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005519 if (d_rpl->flags & LYS_SET_MAX) {
5520 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5521 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5522 /* change value */
5523 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5524 } else if (devs[u]->target->nodetype == LYS_LIST) {
5525 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5526 /* change value */
5527 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
5528 } else {
5529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, devs[u]->nodeid,
5530 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
5531 goto cleanup;
5532 }
5533 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005534
5535 break;
5536 default:
5537 LOGINT(ctx->ctx);
5538 goto cleanup;
5539 }
5540 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005541
Radek Krejci33f72892019-02-21 10:36:58 +01005542 /* final check when all deviations of a single target node are applied */
5543
Radek Krejci551b12c2019-02-19 16:11:21 +01005544 /* check min-max compatibility */
5545 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5546 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
5547 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
5548 } else if (devs[u]->target->nodetype == LYS_LIST) {
5549 min = ((struct lysc_node_list*)devs[u]->target)->min;
5550 max = ((struct lysc_node_list*)devs[u]->target)->max;
5551 } else {
5552 min = max = 0;
5553 }
5554 if (min > max) {
5555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
5556 "after deviation (%s): min value %u is bigger than max value %u.",
5557 devs[u]->nodeid, min, max);
5558 goto cleanup;
5559 }
5560
5561 /* check mandatory - default compatibility */
5562 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
5563 && (devs[u]->target->flags & LYS_SET_DFLT)
5564 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5565 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5566 "Invalid deviation (%s) combining default value and mandatory %s.",
5567 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype));
5568 goto cleanup;
5569 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
5570 && ((struct lysc_node_choice*)devs[u]->target)->dflt
5571 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5572 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5573 "Invalid deviation (%s) combining default case and mandatory choice.", devs[u]->nodeid);
5574 goto cleanup;
5575 }
5576 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
5577 /* mandatory node under a default case */
5578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5579 "Invalid deviation (%s) combining mandatory %s \"%s\" in a default choice's case \"%s\".",
5580 devs[u]->nodeid, lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
5581 goto cleanup;
5582 }
Radek Krejci33f72892019-02-21 10:36:58 +01005583
5584 /* TODO check default value(s) according to the type */
Radek Krejciccd20f12019-02-15 14:12:27 +01005585 }
5586
5587 ret = LY_SUCCESS;
5588
5589cleanup:
5590 for (u = 0; u < devs_p.count && devs[u]; ++u) {
5591 LY_ARRAY_FREE(devs[u]->deviates);
5592 free(devs[u]);
5593 }
5594 free(devs);
5595 ly_set_erase(&targets, NULL);
5596 ly_set_erase(&devs_p, NULL);
5597
5598 return ret;
5599}
5600
Radek Krejcib56c7502019-02-13 14:19:54 +01005601/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01005602 * @brief Compile the given YANG submodule into the main module.
5603 * @param[in] ctx Compile context
5604 * @param[in] inc Include structure from the main module defining the submodule.
5605 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
5606 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5607 */
5608LY_ERR
5609lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
5610{
5611 unsigned int u;
5612 LY_ERR ret = LY_SUCCESS;
5613 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005614 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005615 struct lysc_module *mainmod = ctx->mod->compiled;
5616
Radek Krejci0af46292019-01-11 16:02:31 +01005617 if (!mainmod->mod->off_features) {
5618 /* features are compiled directly into the compiled module structure,
5619 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
5620 * The features compilation is finished in the main module (lys_compile()). */
5621 ret = lys_feature_precompile(ctx->ctx, submod->features,
5622 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
5623 LY_CHECK_GOTO(ret, error);
5624 }
5625
Radek Krejcid05cbd92018-12-05 14:26:40 +01005626 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
5627
Radek Krejci8cce8532019-03-05 11:27:45 +01005628 /* TODO data nodes */
5629
5630 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
5631 // TODO COMPILE_ARRAY1_GOTO(&ctx, submod->notifs, mainmod->notifs, NULL, options, u, lys_compile_notif, 0, ret, error);
5632
Radek Krejcid05cbd92018-12-05 14:26:40 +01005633error:
5634 return ret;
5635}
5636
Radek Krejci19a96102018-11-15 13:38:09 +01005637LY_ERR
5638lys_compile(struct lys_module *mod, int options)
5639{
5640 struct lysc_ctx ctx = {0};
5641 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01005642 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01005643 struct lysp_module *sp;
5644 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01005645 struct lysp_augment **augments = NULL;
5646 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005647 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01005648 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01005649
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005650 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01005651
5652 if (!mod->implemented) {
5653 /* just imported modules are not compiled */
5654 return LY_SUCCESS;
5655 }
5656
Radek Krejci19a96102018-11-15 13:38:09 +01005657 sp = mod->parsed;
5658
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005659 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01005660 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01005661 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005662
5663 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005664 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
5665 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01005666
Radek Krejci19a96102018-11-15 13:38:09 +01005667 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01005668 LY_ARRAY_FOR(sp->includes, u) {
5669 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
5670 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5671 }
Radek Krejci0af46292019-01-11 16:02:31 +01005672 if (mod->off_features) {
5673 /* there is already precompiled array of features */
5674 mod_c->features = mod->off_features;
5675 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01005676 } else {
5677 /* features are compiled directly into the compiled module structure,
5678 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
5679 ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features);
5680 LY_CHECK_GOTO(ret, error);
5681 }
5682 /* finish feature compilation, not only for the main module, but also for the submodules.
5683 * Due to possible forward references, it must be done when all the features (including submodules)
5684 * are present. */
5685 LY_ARRAY_FOR(sp->features, u) {
5686 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
5687 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5688 }
5689 LY_ARRAY_FOR(sp->includes, v) {
5690 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
5691 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
5692 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
5693 }
5694 }
5695
Radek Krejcid05cbd92018-12-05 14:26:40 +01005696 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005697 if (sp->identities) {
5698 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
5699 }
5700
Radek Krejci95710c92019-02-11 15:49:55 +01005701 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01005702 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005703 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01005704 LY_CHECK_GOTO(ret, error);
5705 }
Radek Krejci95710c92019-02-11 15:49:55 +01005706
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005707 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, options, u, lys_compile_action, 0, ret, error);
5708 // TODO COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, options, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01005709
Radek Krejci95710c92019-02-11 15:49:55 +01005710 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01005711 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01005712 LY_CHECK_GOTO(ret, error);
5713 LY_ARRAY_FOR(augments, u) {
5714 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
5715 LY_CHECK_GOTO(ret, error);
5716 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005717
5718 /* deviations */
5719 ret = lys_compile_deviations(&ctx, sp, options);
5720 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005721
5722 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
5723
Radek Krejcia3045382018-11-22 14:30:31 +01005724 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005725 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
5726 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
5727 * 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 +01005728 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005729 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01005730 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5731 if (type->basetype == LY_TYPE_LEAFREF) {
5732 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01005733 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 +01005734 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01005735 } else if (type->basetype == LY_TYPE_UNION) {
5736 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5737 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5738 /* validate the path */
5739 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
5740 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
5741 LY_CHECK_GOTO(ret, error);
5742 }
5743 }
Radek Krejcia3045382018-11-22 14:30:31 +01005744 }
5745 }
5746 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005747 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01005748 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01005749 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
5750 if (type->basetype == LY_TYPE_LEAFREF) {
5751 /* store pointer to the real type */
5752 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
5753 typeiter->basetype == LY_TYPE_LEAFREF;
5754 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5755 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01005756 } else if (type->basetype == LY_TYPE_UNION) {
5757 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
5758 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
5759 /* store pointer to the real type */
5760 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
5761 typeiter->basetype == LY_TYPE_LEAFREF;
5762 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
5763 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
5764 }
5765 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01005766 }
5767 }
5768 }
Radek Krejcia3045382018-11-22 14:30:31 +01005769 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005770 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005771 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01005772
Radek Krejci19a96102018-11-15 13:38:09 +01005773 if (options & LYSC_OPT_FREE_SP) {
5774 lysp_module_free(mod->parsed);
5775 ((struct lys_module*)mod)->parsed = NULL;
5776 }
5777
Radek Krejci95710c92019-02-11 15:49:55 +01005778 if (!(options & LYSC_OPT_INTERNAL)) {
5779 /* remove flag of the modules implemented by dependency */
5780 for (u = 0; u < ctx.ctx->list.count; ++u) {
5781 m = ctx.ctx->list.objs[u];
5782 if (m->implemented == 2) {
5783 m->implemented = 1;
5784 }
5785 }
5786 }
5787
Radek Krejci19a96102018-11-15 13:38:09 +01005788 ((struct lys_module*)mod)->compiled = mod_c;
5789 return LY_SUCCESS;
5790
5791error:
Radek Krejci95710c92019-02-11 15:49:55 +01005792 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01005793 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005794 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005795 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01005796 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01005797 mod->compiled = NULL;
5798
5799 /* revert compilation of modules implemented by dependency */
5800 for (u = 0; u < ctx.ctx->list.count; ++u) {
5801 m = ctx.ctx->list.objs[u];
5802 if (m->implemented == 2) {
5803 /* revert features list to the precompiled state */
5804 lys_feature_precompile_revert(&ctx, m);
5805 /* mark module as imported-only / not-implemented */
5806 m->implemented = 0;
5807 /* free the compiled version of the module */
5808 lysc_module_free(m->compiled, NULL);
5809 m->compiled = NULL;
5810 }
5811 }
5812
Radek Krejci19a96102018-11-15 13:38:09 +01005813 return ret;
5814}