blob: 17921d7bea6683a021d98f5f5d5bf39f7064ed93 [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
44#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, 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, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
55#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
56 if (MEMBER_P) { \
57 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
58 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
59 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
60 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
61 }
62
Radek Krejci00b874b2019-02-12 10:54:50 +010063#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, OPTIONS, FUNC, RET, GOTO) \
64 if (MEMBER_P) { \
65 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
66 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
67 LY_ARRAY_INCREMENT(ARRAY_C); \
68 RET = FUNC(CTX, MEMBER_P, OPTIONS, &(ARRAY_C)[__array_offset]); \
69 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
70 }
71
Radek Krejcid05cbd92018-12-05 14:26:40 +010072#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
73 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010074 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
75 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010076 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
77 return LY_EVALID; \
78 } \
79 } \
80 }
81
Radek Krejci19a96102018-11-15 13:38:09 +010082static struct lysc_ext_instance *
83lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
84{
85 /* TODO */
86 (void) ctx;
87 (void) orig;
88 return NULL;
89}
90
Radek Krejcib56c7502019-02-13 14:19:54 +010091/**
92 * @brief Duplicate the compiled pattern structure.
93 *
94 * Instead of duplicating memory, the reference counter in the @p orig is increased.
95 *
96 * @param[in] orig The pattern structure to duplicate.
97 * @return The duplicated structure to use.
98 */
Radek Krejci19a96102018-11-15 13:38:09 +010099static struct lysc_pattern*
100lysc_pattern_dup(struct lysc_pattern *orig)
101{
102 ++orig->refcount;
103 return orig;
104}
105
Radek Krejcib56c7502019-02-13 14:19:54 +0100106/**
107 * @brief Duplicate the array of compiled patterns.
108 *
109 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
110 *
111 * @param[in] ctx Libyang context for logging.
112 * @param[in] orig The patterns sized array to duplicate.
113 * @return New sized array as a copy of @p orig.
114 * @return NULL in case of memory allocation error.
115 */
Radek Krejci19a96102018-11-15 13:38:09 +0100116static struct lysc_pattern**
117lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
118{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100119 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100120 unsigned int u;
121
Radek Krejcib56c7502019-02-13 14:19:54 +0100122 assert(orig);
123
Radek Krejci19a96102018-11-15 13:38:09 +0100124 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
125 LY_ARRAY_FOR(orig, u) {
126 dup[u] = lysc_pattern_dup(orig[u]);
127 LY_ARRAY_INCREMENT(dup);
128 }
129 return dup;
130}
131
Radek Krejcib56c7502019-02-13 14:19:54 +0100132/**
133 * @brief Duplicate compiled range structure.
134 *
135 * @param[in] ctx Libyang context for logging.
136 * @param[in] orig The range structure to be duplicated.
137 * @return New compiled range structure as a copy of @p orig.
138 * @return NULL in case of memory allocation error.
139 */
Radek Krejci19a96102018-11-15 13:38:09 +0100140struct lysc_range*
141lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
142{
143 struct lysc_range *dup;
144 LY_ERR ret;
145
Radek Krejcib56c7502019-02-13 14:19:54 +0100146 assert(orig);
147
Radek Krejci19a96102018-11-15 13:38:09 +0100148 dup = calloc(1, sizeof *dup);
149 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
150 if (orig->parts) {
151 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
152 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
153 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
154 }
155 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
156 DUP_STRING(ctx, orig->emsg, dup->emsg);
157 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
158
159 return dup;
160cleanup:
161 free(dup);
162 (void) ret; /* set but not used due to the return type */
163 return NULL;
164}
165
Radek Krejcib56c7502019-02-13 14:19:54 +0100166/**
167 * @brief Stack for processing if-feature expressions.
168 */
Radek Krejci19a96102018-11-15 13:38:09 +0100169struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100170 int size; /**< number of items in the stack */
171 int index; /**< first empty item */
172 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100173};
174
Radek Krejcib56c7502019-02-13 14:19:54 +0100175/**
176 * @brief Add @ref ifftokens into the stack.
177 * @param[in] stack The if-feature stack to use.
178 * @param[in] value One of the @ref ifftokens to store in the stack.
179 * @return LY_EMEM in case of memory allocation error
180 * @return LY_ESUCCESS if the value successfully stored.
181 */
Radek Krejci19a96102018-11-15 13:38:09 +0100182static LY_ERR
183iff_stack_push(struct iff_stack *stack, uint8_t value)
184{
185 if (stack->index == stack->size) {
186 stack->size += 4;
187 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
188 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
189 }
190 stack->stack[stack->index++] = value;
191 return LY_SUCCESS;
192}
193
Radek Krejcib56c7502019-02-13 14:19:54 +0100194/**
195 * @brief Get (and remove) the last item form the stack.
196 * @param[in] stack The if-feature stack to use.
197 * @return The value from the top of the stack.
198 */
Radek Krejci19a96102018-11-15 13:38:09 +0100199static uint8_t
200iff_stack_pop(struct iff_stack *stack)
201{
Radek Krejcib56c7502019-02-13 14:19:54 +0100202 assert(stack && stack->index);
203
Radek Krejci19a96102018-11-15 13:38:09 +0100204 stack->index--;
205 return stack->stack[stack->index];
206}
207
Radek Krejcib56c7502019-02-13 14:19:54 +0100208/**
209 * @brief Clean up the stack.
210 * @param[in] stack The if-feature stack to use.
211 */
Radek Krejci19a96102018-11-15 13:38:09 +0100212static void
213iff_stack_clean(struct iff_stack *stack)
214{
215 stack->size = 0;
216 free(stack->stack);
217}
218
Radek Krejcib56c7502019-02-13 14:19:54 +0100219/**
220 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
221 * (libyang format of the if-feature expression).
222 * @param[in,out] list The 2bits array to modify.
223 * @param[in] op The operand (@ref ifftokens) to store.
224 * @param[in] pos Position (0-based) where to store the given @p op.
225 */
Radek Krejci19a96102018-11-15 13:38:09 +0100226static void
227iff_setop(uint8_t *list, uint8_t op, int pos)
228{
229 uint8_t *item;
230 uint8_t mask = 3;
231
232 assert(pos >= 0);
233 assert(op <= 3); /* max 2 bits */
234
235 item = &list[pos / 4];
236 mask = mask << 2 * (pos % 4);
237 *item = (*item) & ~mask;
238 *item = (*item) | (op << 2 * (pos % 4));
239}
240
Radek Krejcib56c7502019-02-13 14:19:54 +0100241#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
242#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100243
Radek Krejci0af46292019-01-11 16:02:31 +0100244/**
245 * @brief Find a feature of the given name and referenced in the given module.
246 *
247 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
248 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
249 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
250 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
251 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
252 * assigned till that time will be still valid.
253 *
254 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
255 * @param[in] name Name of the feature including possible prefix.
256 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
257 * @return Pointer to the feature structure if found, NULL otherwise.
258 */
Radek Krejci19a96102018-11-15 13:38:09 +0100259static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100260lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100261{
262 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100263 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100264
265 for (i = 0; i < len; ++i) {
266 if (name[i] == ':') {
267 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100268 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100269 LY_CHECK_RET(!mod, NULL);
270
271 name = &name[i + 1];
272 len = len - i - 1;
273 }
274 }
275
276 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100277 if (mod->implemented) {
278 /* module is implemented so there is already the compiled schema */
279 flist = mod->compiled->features;
280 } else {
281 flist = mod->off_features;
282 }
283 LY_ARRAY_FOR(flist, i) {
284 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100285 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
286 return f;
287 }
288 }
289
290 return NULL;
291}
292
293static LY_ERR
294lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
295{
296 const char *name;
297 unsigned int u;
298 const struct lys_module *mod;
299 struct lysp_ext *edef = NULL;
300
301 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
302 ext->insubstmt = ext_p->insubstmt;
303 ext->insubstmt_index = ext_p->insubstmt_index;
304
305 /* get module where the extension definition should be placed */
306 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100307 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100308 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
309 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
310 LY_EVALID);
311 LY_CHECK_ERR_RET(!mod->parsed->extensions,
312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
313 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100314 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100315 LY_EVALID);
316 name = &ext_p->name[u + 1];
317 /* find the extension definition there */
318 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
319 if (!strcmp(name, mod->parsed->extensions[u].name)) {
320 edef = &mod->parsed->extensions[u];
321 break;
322 }
323 }
324 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
325 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
326 LY_EVALID);
327 /* TODO plugins */
328
329 return LY_SUCCESS;
330}
331
Radek Krejcib56c7502019-02-13 14:19:54 +0100332/**
333 * @brief Compile information from the if-feature statement
334 * @param[in] ctx Compile context.
335 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
336 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
337 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
338 * @return LY_ERR value.
339 */
Radek Krejci19a96102018-11-15 13:38:09 +0100340static LY_ERR
341lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
342{
343 const char *c = *value;
344 int r, rc = EXIT_FAILURE;
345 int i, j, last_not, checkversion = 0;
346 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
347 uint8_t op;
348 struct iff_stack stack = {0, 0, NULL};
349 struct lysc_feature *f;
350
351 assert(c);
352
353 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
354 for (i = j = last_not = 0; c[i]; i++) {
355 if (c[i] == '(') {
356 j++;
357 checkversion = 1;
358 continue;
359 } else if (c[i] == ')') {
360 j--;
361 continue;
362 } else if (isspace(c[i])) {
363 checkversion = 1;
364 continue;
365 }
366
367 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
368 if (c[i + r] == '\0') {
369 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
370 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
371 return LY_EVALID;
372 } else if (!isspace(c[i + r])) {
373 /* feature name starting with the not/and/or */
374 last_not = 0;
375 f_size++;
376 } else if (c[i] == 'n') { /* not operation */
377 if (last_not) {
378 /* double not */
379 expr_size = expr_size - 2;
380 last_not = 0;
381 } else {
382 last_not = 1;
383 }
384 } else { /* and, or */
385 f_exp++;
386 /* not a not operation */
387 last_not = 0;
388 }
389 i += r;
390 } else {
391 f_size++;
392 last_not = 0;
393 }
394 expr_size++;
395
396 while (!isspace(c[i])) {
397 if (!c[i] || c[i] == ')') {
398 i--;
399 break;
400 }
401 i++;
402 }
403 }
404 if (j || f_exp != f_size) {
405 /* not matching count of ( and ) */
406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
407 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
408 return LY_EVALID;
409 }
410
411 if (checkversion || expr_size > 1) {
412 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100413 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100414 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
415 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
416 return LY_EVALID;
417 }
418 }
419
420 /* allocate the memory */
421 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
422 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
423 stack.stack = malloc(expr_size * sizeof *stack.stack);
424 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
425
426 stack.size = expr_size;
427 f_size--; expr_size--; /* used as indexes from now */
428
429 for (i--; i >= 0; i--) {
430 if (c[i] == ')') {
431 /* push it on stack */
432 iff_stack_push(&stack, LYS_IFF_RP);
433 continue;
434 } else if (c[i] == '(') {
435 /* pop from the stack into result all operators until ) */
436 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
437 iff_setop(iff->expr, op, expr_size--);
438 }
439 continue;
440 } else if (isspace(c[i])) {
441 continue;
442 }
443
444 /* end of operator or operand -> find beginning and get what is it */
445 j = i + 1;
446 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
447 i--;
448 }
449 i++; /* go back by one step */
450
451 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
452 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
453 /* double not */
454 iff_stack_pop(&stack);
455 } else {
456 /* not has the highest priority, so do not pop from the stack
457 * as in case of AND and OR */
458 iff_stack_push(&stack, LYS_IFF_NOT);
459 }
460 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
461 /* as for OR - pop from the stack all operators with the same or higher
462 * priority and store them to the result, then push the AND to the stack */
463 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
464 op = iff_stack_pop(&stack);
465 iff_setop(iff->expr, op, expr_size--);
466 }
467 iff_stack_push(&stack, LYS_IFF_AND);
468 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
469 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
470 op = iff_stack_pop(&stack);
471 iff_setop(iff->expr, op, expr_size--);
472 }
473 iff_stack_push(&stack, LYS_IFF_OR);
474 } else {
475 /* feature name, length is j - i */
476
477 /* add it to the expression */
478 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
479
480 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100481 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
482 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
483 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
484 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100485 iff->features[f_size] = f;
486 LY_ARRAY_INCREMENT(iff->features);
487 f_size--;
488 }
489 }
490 while (stack.index) {
491 op = iff_stack_pop(&stack);
492 iff_setop(iff->expr, op, expr_size--);
493 }
494
495 if (++expr_size || ++f_size) {
496 /* not all expected operators and operands found */
497 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
498 "Invalid value \"%s\" of if-feature - processing error.", *value);
499 rc = LY_EINT;
500 } else {
501 rc = LY_SUCCESS;
502 }
503
504error:
505 /* cleanup */
506 iff_stack_clean(&stack);
507
508 return rc;
509}
510
Radek Krejcib56c7502019-02-13 14:19:54 +0100511/**
512 * @brief Compile information from the when statement
513 * @param[in] ctx Compile context.
514 * @param[in] when_p The parsed when statement structure.
515 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
516 * @param[out] when Pointer where to store pointer to the created compiled when structure.
517 * @return LY_ERR value.
518 */
Radek Krejci19a96102018-11-15 13:38:09 +0100519static LY_ERR
Radek Krejci00b874b2019-02-12 10:54:50 +0100520lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100521{
522 unsigned int u;
523 LY_ERR ret = LY_SUCCESS;
524
Radek Krejci00b874b2019-02-12 10:54:50 +0100525 *when = calloc(1, sizeof **when);
526 (*when)->refcount = 1;
527 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
528 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
529 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
530 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
531 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, options, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100532
533done:
534 return ret;
535}
536
Radek Krejcib56c7502019-02-13 14:19:54 +0100537/**
538 * @brief Compile information from the must statement
539 * @param[in] ctx Compile context.
540 * @param[in] must_p The parsed must statement structure.
541 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
542 * @param[in,out] must Prepared (empty) compiled must structure to fill.
543 * @return LY_ERR value.
544 */
Radek Krejci19a96102018-11-15 13:38:09 +0100545static LY_ERR
546lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
547{
548 unsigned int u;
549 LY_ERR ret = LY_SUCCESS;
550
551 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
552 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
553
554 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
555 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100556 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
557 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100558 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
559
560done:
561 return ret;
562}
563
Radek Krejcib56c7502019-02-13 14:19:54 +0100564/**
565 * @brief Compile information from the import statement
566 * @param[in] ctx Compile context.
567 * @param[in] imp_p The parsed import statement structure.
568 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
569 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
570 * @return LY_ERR value.
571 */
Radek Krejci19a96102018-11-15 13:38:09 +0100572static LY_ERR
573lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
574{
575 unsigned int u;
576 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100577 LY_ERR ret = LY_SUCCESS;
578
579 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
580 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
581 imp->module = imp_p->module;
582
Radek Krejci7f2a5362018-11-28 13:05:37 +0100583 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
584 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100585 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100586 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100587 /* try to use filepath if present */
588 if (imp->module->filepath) {
589 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
590 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100591 if (mod != imp->module) {
592 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100593 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100594 mod = NULL;
595 }
596 }
597 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100598 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100599 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 +0100600 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100601 return LY_ENOTFOUND;
602 }
603 }
Radek Krejci19a96102018-11-15 13:38:09 +0100604 }
605
606done:
607 return ret;
608}
609
Radek Krejcib56c7502019-02-13 14:19:54 +0100610/**
611 * @brief Compile information from the identity statement
612 *
613 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
614 *
615 * @param[in] ctx Compile context.
616 * @param[in] ident_p The parsed identity statement structure.
617 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
618 * @param[in] idents List of so far compiled identities to check the name uniqueness.
619 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
620 * @return LY_ERR value.
621 */
Radek Krejci19a96102018-11-15 13:38:09 +0100622static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100623lys_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 +0100624{
625 unsigned int u;
626 LY_ERR ret = LY_SUCCESS;
627
Radek Krejcid05cbd92018-12-05 14:26:40 +0100628 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100629 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100630 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
631 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
Radek Krejci19a96102018-11-15 13:38:09 +0100632 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
633 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
634 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
635 ident->flags = ident_p->flags;
636
637done:
638 return ret;
639}
640
Radek Krejcib56c7502019-02-13 14:19:54 +0100641/**
642 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
643 *
644 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
645 *
646 * @param[in] ctx Compile context for logging.
647 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
648 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
649 * @return LY_SUCCESS if everything is ok.
650 * @return LY_EVALID if the identity is derived from itself.
651 */
Radek Krejci38222632019-02-12 16:55:05 +0100652static LY_ERR
653lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
654{
655 LY_ERR ret = LY_EVALID;
656 unsigned int u, v;
657 struct ly_set recursion = {0};
658 struct lysc_ident *drv;
659
660 if (!derived) {
661 return LY_SUCCESS;
662 }
663
664 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
665 if (ident == derived[u]) {
666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
667 "Identity \"%s\" is indirectly derived from itself.", ident->name);
668 goto cleanup;
669 }
670 ly_set_add(&recursion, derived[u], 0);
671 }
672
673 for (v = 0; v < recursion.count; ++v) {
674 drv = recursion.objs[v];
675 if (!drv->derived) {
676 continue;
677 }
678 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
679 if (ident == drv->derived[u]) {
680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
681 "Identity \"%s\" is indirectly derived from itself.", ident->name);
682 goto cleanup;
683 }
684 ly_set_add(&recursion, drv->derived[u], 0);
685 }
686 }
687 ret = LY_SUCCESS;
688
689cleanup:
690 ly_set_erase(&recursion, NULL);
691 return ret;
692}
693
Radek Krejcia3045382018-11-22 14:30:31 +0100694/**
695 * @brief Find and process the referenced base identities from another identity or identityref
696 *
697 * For bases in identity se backlinks to them from the base identities. For identityref, store
698 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
699 * to distinguish these two use cases.
700 *
701 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
702 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
703 * @param[in] ident Referencing identity to work with.
704 * @param[in] bases Array of bases of identityref to fill in.
705 * @return LY_ERR value.
706 */
Radek Krejci19a96102018-11-15 13:38:09 +0100707static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100708lys_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 +0100709{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100710 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100711 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100712 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100713 struct lysc_ident **idref;
714
715 assert(ident || bases);
716
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100717 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
719 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
720 return LY_EVALID;
721 }
722
723 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
724 s = strchr(bases_p[u], ':');
725 if (s) {
726 /* prefixed identity */
727 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100728 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100729 } else {
730 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100731 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100732 }
733 if (!mod) {
734 if (ident) {
735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
736 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
737 } else {
738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
739 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
740 }
741 return LY_EVALID;
742 }
743 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100744 if (mod->compiled && mod->compiled->identities) {
745 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
746 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100747 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100748 if (ident == &mod->compiled->identities[v]) {
749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
750 "Identity \"%s\" is derived from itself.", ident->name);
751 return LY_EVALID;
752 }
753 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100754 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100755 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100756 *idref = ident;
757 } else {
758 /* we have match! store the found identity */
759 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100760 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100761 }
762 break;
763 }
764 }
765 }
766 if (!idref || !(*idref)) {
767 if (ident) {
768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
769 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
770 } else {
771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
772 "Unable to find base (%s) of identityref.", bases_p[u]);
773 }
774 return LY_EVALID;
775 }
776 }
777 return LY_SUCCESS;
778}
779
Radek Krejcia3045382018-11-22 14:30:31 +0100780/**
781 * @brief For the given array of identities, set the backlinks from all their base identities.
782 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
783 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
784 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
785 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
786 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100787static LY_ERR
788lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
789{
790 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100791
792 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
793 if (!idents_p[i].bases) {
794 continue;
795 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100796 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100797 }
798 return LY_SUCCESS;
799}
800
Radek Krejci0af46292019-01-11 16:02:31 +0100801LY_ERR
802lys_feature_precompile(struct ly_ctx *ctx, struct lysp_feature *features_p, struct lysc_feature **features)
803{
804 unsigned int offset = 0, u;
805 struct lysc_ctx context = {0};
806
807 assert(ctx);
808 context.ctx = ctx;
809
810 if (!features_p) {
811 return LY_SUCCESS;
812 }
813 if (*features) {
814 offset = LY_ARRAY_SIZE(*features);
815 }
816
817 LY_ARRAY_CREATE_RET(ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
818 LY_ARRAY_FOR(features_p, u) {
819 LY_ARRAY_INCREMENT(*features);
820 COMPILE_CHECK_UNIQUENESS(&context, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
821 DUP_STRING(ctx, features_p[u].name, (*features)[offset + u].name);
822 DUP_STRING(ctx, features_p[u].dsc, (*features)[offset + u].dsc);
823 DUP_STRING(ctx, features_p[u].ref, (*features)[offset + u].ref);
824 (*features)[offset + u].flags = features_p[u].flags;
825 }
826
827 return LY_SUCCESS;
828}
829
Radek Krejcia3045382018-11-22 14:30:31 +0100830/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100831 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100832 *
833 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
834 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100835 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100836 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
837 * being currently processed).
838 * @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 +0100839 * @return LY_SUCCESS if everything is ok.
840 * @return LY_EVALID if the feature references indirectly itself.
841 */
842static LY_ERR
843lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
844{
845 LY_ERR ret = LY_EVALID;
846 unsigned int u, v;
847 struct ly_set recursion = {0};
848 struct lysc_feature *drv;
849
850 if (!depfeatures) {
851 return LY_SUCCESS;
852 }
853
854 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
855 if (feature == depfeatures[u]) {
856 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
857 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
858 goto cleanup;
859 }
860 ly_set_add(&recursion, depfeatures[u], 0);
861 }
862
863 for (v = 0; v < recursion.count; ++v) {
864 drv = recursion.objs[v];
865 if (!drv->depfeatures) {
866 continue;
867 }
868 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
869 if (feature == drv->depfeatures[u]) {
870 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
871 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
872 goto cleanup;
873 }
874 ly_set_add(&recursion, drv->depfeatures[u], 0);
875 }
876 }
877 ret = LY_SUCCESS;
878
879cleanup:
880 ly_set_erase(&recursion, NULL);
881 return ret;
882}
883
884/**
Radek Krejci0af46292019-01-11 16:02:31 +0100885 * @brief Create pre-compiled features array.
886 *
887 * See lys_feature_precompile() for more details.
888 *
Radek Krejcia3045382018-11-22 14:30:31 +0100889 * @param[in] ctx Compile context.
890 * @param[in] feature_p Parsed feature definition to compile.
891 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejci0af46292019-01-11 16:02:31 +0100892 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +0100893 * @return LY_ERR value.
894 */
Radek Krejci19a96102018-11-15 13:38:09 +0100895static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100896lys_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 +0100897{
Radek Krejci0af46292019-01-11 16:02:31 +0100898 unsigned int u, v, x;
899 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +0100900 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +0100901
Radek Krejci0af46292019-01-11 16:02:31 +0100902 /* find the preprecompiled feature */
903 LY_ARRAY_FOR(features, x) {
904 if (strcmp(features[x].name, feature_p->name)) {
905 continue;
906 }
907 feature = &features[x];
Radek Krejci19a96102018-11-15 13:38:09 +0100908
Radek Krejci0af46292019-01-11 16:02:31 +0100909 /* finish compilation started in lys_feature_precompile() */
910 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
911 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
912 if (feature->iffeatures) {
913 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
914 if (feature->iffeatures[u].features) {
915 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +0100916 /* check for circular dependency - direct reference first,... */
917 if (feature == feature->iffeatures[u].features[v]) {
918 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
919 "Feature \"%s\" is referenced from itself.", feature->name);
920 return LY_EVALID;
921 }
922 /* ... and indirect circular reference */
923 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
924
Radek Krejci0af46292019-01-11 16:02:31 +0100925 /* add itself into the dependants list */
926 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
927 *df = feature;
928 }
Radek Krejci19a96102018-11-15 13:38:09 +0100929 }
Radek Krejci19a96102018-11-15 13:38:09 +0100930 }
931 }
Radek Krejci0af46292019-01-11 16:02:31 +0100932 done:
933 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +0100934 }
Radek Krejci0af46292019-01-11 16:02:31 +0100935
936 LOGINT(ctx->ctx);
937 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +0100938}
939
Radek Krejcib56c7502019-02-13 14:19:54 +0100940/**
941 * @brief Revert compiled list of features back to the precompiled state.
942 *
943 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
944 * The features are supposed to be stored again as off_features in ::lys_module structure.
945 *
946 * @param[in] ctx Compilation context.
947 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
948 * and supposed to hold the off_features list.
949 */
Radek Krejci95710c92019-02-11 15:49:55 +0100950static void
951lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
952{
953 unsigned int u, v;
954
955 /* keep the off_features list until the complete lys_module is freed */
956 mod->off_features = mod->compiled->features;
957 mod->compiled->features = NULL;
958
959 /* in the off_features list, remove all the parts (from finished compiling process)
960 * which may points into the data being freed here */
961 LY_ARRAY_FOR(mod->off_features, u) {
962 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
963 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
964 }
965 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
966 mod->off_features[u].iffeatures = NULL;
967
968 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
969 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
970 }
971 LY_ARRAY_FREE(mod->off_features[u].exts);
972 mod->off_features[u].exts = NULL;
973 }
974}
975
Radek Krejcia3045382018-11-22 14:30:31 +0100976/**
977 * @brief Validate and normalize numeric value from a range definition.
978 * @param[in] ctx Compile context.
979 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
980 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
981 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
982 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
983 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
984 * @param[in] value String value of the range boundary.
985 * @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.
986 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
987 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
988 */
Radek Krejci19a96102018-11-15 13:38:09 +0100989static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100990range_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 +0100991{
Radek Krejci6cba4292018-11-15 17:33:29 +0100992 size_t fraction = 0, size;
993
Radek Krejci19a96102018-11-15 13:38:09 +0100994 *len = 0;
995
996 assert(value);
997 /* parse value */
998 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
999 return LY_EVALID;
1000 }
1001
1002 if ((value[*len] == '-') || (value[*len] == '+')) {
1003 ++(*len);
1004 }
1005
1006 while (isdigit(value[*len])) {
1007 ++(*len);
1008 }
1009
1010 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001011 if (basetype == LY_TYPE_DEC64) {
1012 goto decimal;
1013 } else {
1014 *valcopy = strndup(value, *len);
1015 return LY_SUCCESS;
1016 }
Radek Krejci19a96102018-11-15 13:38:09 +01001017 }
1018 fraction = *len;
1019
1020 ++(*len);
1021 while (isdigit(value[*len])) {
1022 ++(*len);
1023 }
1024
Radek Krejci6cba4292018-11-15 17:33:29 +01001025 if (basetype == LY_TYPE_DEC64) {
1026decimal:
1027 assert(frdigits);
1028 if (*len - 1 - fraction > frdigits) {
1029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1030 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1031 *len, value, frdigits);
1032 return LY_EINVAL;
1033 }
1034 if (fraction) {
1035 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1036 } else {
1037 size = (*len) + frdigits + 1;
1038 }
1039 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001040 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1041
Radek Krejci6cba4292018-11-15 17:33:29 +01001042 (*valcopy)[size - 1] = '\0';
1043 if (fraction) {
1044 memcpy(&(*valcopy)[0], &value[0], fraction);
1045 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1046 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1047 } else {
1048 memcpy(&(*valcopy)[0], &value[0], *len);
1049 memset(&(*valcopy)[*len], '0', frdigits);
1050 }
Radek Krejci19a96102018-11-15 13:38:09 +01001051 }
1052 return LY_SUCCESS;
1053}
1054
Radek Krejcia3045382018-11-22 14:30:31 +01001055/**
1056 * @brief Check that values in range are in ascendant order.
1057 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001058 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1059 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001060 * @param[in] value Current value to check.
1061 * @param[in] prev_value The last seen value.
1062 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1063 */
Radek Krejci19a96102018-11-15 13:38:09 +01001064static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001065range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001066{
1067 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001068 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001069 return LY_EEXIST;
1070 }
1071 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001072 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001073 return LY_EEXIST;
1074 }
1075 }
1076 return LY_SUCCESS;
1077}
1078
Radek Krejcia3045382018-11-22 14:30:31 +01001079/**
1080 * @brief Set min/max value of the range part.
1081 * @param[in] ctx Compile context.
1082 * @param[in] part Range part structure to fill.
1083 * @param[in] max Flag to distinguish if storing min or max value.
1084 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1085 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1086 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1087 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1088 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001089 * @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 +01001090 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1091 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1092 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1093 * frdigits value), LY_EMEM.
1094 */
Radek Krejci19a96102018-11-15 13:38:09 +01001095static LY_ERR
1096range_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 +01001097 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001098{
1099 LY_ERR ret = LY_SUCCESS;
1100 char *valcopy = NULL;
1101 size_t len;
1102
1103 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001104 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001105 LY_CHECK_GOTO(ret, finalize);
1106 }
1107 if (!valcopy && base_range) {
1108 if (max) {
1109 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1110 } else {
1111 part->min_64 = base_range->parts[0].min_64;
1112 }
1113 if (!first) {
1114 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1115 }
1116 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001117 }
1118
1119 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001120 case LY_TYPE_INT8: /* range */
1121 if (valcopy) {
1122 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
1123 } else if (max) {
1124 part->max_64 = INT64_C(127);
1125 } else {
1126 part->min_64 = INT64_C(-128);
1127 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001128 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001129 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001130 }
1131 break;
1132 case LY_TYPE_INT16: /* range */
1133 if (valcopy) {
1134 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
1135 } else if (max) {
1136 part->max_64 = INT64_C(32767);
1137 } else {
1138 part->min_64 = INT64_C(-32768);
1139 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001140 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001141 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001142 }
1143 break;
1144 case LY_TYPE_INT32: /* range */
1145 if (valcopy) {
1146 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
1147 } else if (max) {
1148 part->max_64 = INT64_C(2147483647);
1149 } else {
1150 part->min_64 = INT64_C(-2147483648);
1151 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001152 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001153 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001154 }
1155 break;
1156 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001157 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001158 if (valcopy) {
1159 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
1160 max ? &part->max_64 : &part->min_64);
1161 } else if (max) {
1162 part->max_64 = INT64_C(9223372036854775807);
1163 } else {
1164 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1165 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001166 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001167 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001168 }
1169 break;
1170 case LY_TYPE_UINT8: /* range */
1171 if (valcopy) {
1172 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
1173 } else if (max) {
1174 part->max_u64 = UINT64_C(255);
1175 } else {
1176 part->min_u64 = UINT64_C(0);
1177 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001178 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001179 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001180 }
1181 break;
1182 case LY_TYPE_UINT16: /* range */
1183 if (valcopy) {
1184 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
1185 } else if (max) {
1186 part->max_u64 = UINT64_C(65535);
1187 } else {
1188 part->min_u64 = UINT64_C(0);
1189 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001190 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001191 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001192 }
1193 break;
1194 case LY_TYPE_UINT32: /* range */
1195 if (valcopy) {
1196 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
1197 } else if (max) {
1198 part->max_u64 = UINT64_C(4294967295);
1199 } else {
1200 part->min_u64 = UINT64_C(0);
1201 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001202 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001203 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001204 }
1205 break;
1206 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001207 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001208 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001209 if (valcopy) {
1210 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
1211 } else if (max) {
1212 part->max_u64 = UINT64_C(18446744073709551615);
1213 } else {
1214 part->min_u64 = UINT64_C(0);
1215 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001216 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001217 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001218 }
1219 break;
1220 default:
1221 LOGINT(ctx->ctx);
1222 ret = LY_EINT;
1223 }
1224
Radek Krejci5969f272018-11-23 10:03:58 +01001225finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001226 if (ret == LY_EDENIED) {
1227 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1228 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1229 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1230 } else if (ret == LY_EVALID) {
1231 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1232 "Invalid %s restriction - invalid value \"%s\".",
1233 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1234 } else if (ret == LY_EEXIST) {
1235 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1236 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001237 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001238 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001239 } else if (!ret && value) {
1240 *value = *value + len;
1241 }
1242 free(valcopy);
1243 return ret;
1244}
1245
Radek Krejcia3045382018-11-22 14:30:31 +01001246/**
1247 * @brief Compile the parsed range restriction.
1248 * @param[in] ctx Compile context.
1249 * @param[in] range_p Parsed range structure to compile.
1250 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1251 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1252 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1253 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1254 * range restriction must be more restrictive than the base_range.
1255 * @param[in,out] range Pointer to the created current range structure.
1256 * @return LY_ERR value.
1257 */
Radek Krejci19a96102018-11-15 13:38:09 +01001258static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001259lys_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 +01001260 struct lysc_range *base_range, struct lysc_range **range)
1261{
1262 LY_ERR ret = LY_EVALID;
1263 const char *expr;
1264 struct lysc_range_part *parts = NULL, *part;
1265 int range_expected = 0, uns;
1266 unsigned int parts_done = 0, u, v;
1267
1268 assert(range);
1269 assert(range_p);
1270
1271 expr = range_p->arg;
1272 while(1) {
1273 if (isspace(*expr)) {
1274 ++expr;
1275 } else if (*expr == '\0') {
1276 if (range_expected) {
1277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1278 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1279 length_restr ? "length" : "range", range_p->arg);
1280 goto cleanup;
1281 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1282 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1283 "Invalid %s restriction - unexpected end of the expression (%s).",
1284 length_restr ? "length" : "range", range_p->arg);
1285 goto cleanup;
1286 }
1287 parts_done++;
1288 break;
1289 } else if (!strncmp(expr, "min", 3)) {
1290 if (parts) {
1291 /* min cannot be used elsewhere than in the first part */
1292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1293 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1294 expr - range_p->arg, range_p->arg);
1295 goto cleanup;
1296 }
1297 expr += 3;
1298
1299 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001300 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 +01001301 part->max_64 = part->min_64;
1302 } else if (*expr == '|') {
1303 if (!parts || range_expected) {
1304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1305 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1306 goto cleanup;
1307 }
1308 expr++;
1309 parts_done++;
1310 /* process next part of the expression */
1311 } else if (!strncmp(expr, "..", 2)) {
1312 expr += 2;
1313 while (isspace(*expr)) {
1314 expr++;
1315 }
1316 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1318 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1319 goto cleanup;
1320 }
1321 /* continue expecting the upper boundary */
1322 range_expected = 1;
1323 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1324 /* number */
1325 if (range_expected) {
1326 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001327 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 +01001328 range_expected = 0;
1329 } else {
1330 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1331 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 +01001332 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001333 part->max_64 = part->min_64;
1334 }
1335
1336 /* continue with possible another expression part */
1337 } else if (!strncmp(expr, "max", 3)) {
1338 expr += 3;
1339 while (isspace(*expr)) {
1340 expr++;
1341 }
1342 if (*expr != '\0') {
1343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1344 length_restr ? "length" : "range", expr);
1345 goto cleanup;
1346 }
1347 if (range_expected) {
1348 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001349 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 +01001350 range_expected = 0;
1351 } else {
1352 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1353 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 +01001354 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001355 part->min_64 = part->max_64;
1356 }
1357 } else {
1358 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1359 length_restr ? "length" : "range", expr);
1360 goto cleanup;
1361 }
1362 }
1363
1364 /* check with the previous range/length restriction */
1365 if (base_range) {
1366 switch (basetype) {
1367 case LY_TYPE_BINARY:
1368 case LY_TYPE_UINT8:
1369 case LY_TYPE_UINT16:
1370 case LY_TYPE_UINT32:
1371 case LY_TYPE_UINT64:
1372 case LY_TYPE_STRING:
1373 uns = 1;
1374 break;
1375 case LY_TYPE_DEC64:
1376 case LY_TYPE_INT8:
1377 case LY_TYPE_INT16:
1378 case LY_TYPE_INT32:
1379 case LY_TYPE_INT64:
1380 uns = 0;
1381 break;
1382 default:
1383 LOGINT(ctx->ctx);
1384 ret = LY_EINT;
1385 goto cleanup;
1386 }
1387 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1388 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1389 goto baseerror;
1390 }
1391 /* current lower bound is not lower than the base */
1392 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1393 /* base has single value */
1394 if (base_range->parts[v].min_64 == parts[u].min_64) {
1395 /* both lower bounds are the same */
1396 if (parts[u].min_64 != parts[u].max_64) {
1397 /* current continues with a range */
1398 goto baseerror;
1399 } else {
1400 /* equal single values, move both forward */
1401 ++v;
1402 continue;
1403 }
1404 } else {
1405 /* base is single value lower than current range, so the
1406 * value from base range is removed in the current,
1407 * move only base and repeat checking */
1408 ++v;
1409 --u;
1410 continue;
1411 }
1412 } else {
1413 /* base is the range */
1414 if (parts[u].min_64 == parts[u].max_64) {
1415 /* current is a single value */
1416 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1417 /* current is behind the base range, so base range is omitted,
1418 * move the base and keep the current for further check */
1419 ++v;
1420 --u;
1421 } /* else it is within the base range, so move the current, but keep the base */
1422 continue;
1423 } else {
1424 /* both are ranges - check the higher bound, the lower was already checked */
1425 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1426 /* higher bound is higher than the current higher bound */
1427 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1428 /* but the current lower bound is also higher, so the base range is omitted,
1429 * continue with the same current, but move the base */
1430 --u;
1431 ++v;
1432 continue;
1433 }
1434 /* current range starts within the base range but end behind it */
1435 goto baseerror;
1436 } else {
1437 /* current range is smaller than the base,
1438 * move current, but stay with the base */
1439 continue;
1440 }
1441 }
1442 }
1443 }
1444 if (u != parts_done) {
1445baseerror:
1446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1447 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1448 length_restr ? "length" : "range", range_p->arg);
1449 goto cleanup;
1450 }
1451 }
1452
1453 if (!(*range)) {
1454 *range = calloc(1, sizeof **range);
1455 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1456 }
1457
Radek Krejcic8b31002019-01-08 10:24:45 +01001458 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001459 if (range_p->eapptag) {
1460 lydict_remove(ctx->ctx, (*range)->eapptag);
1461 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1462 }
1463 if (range_p->emsg) {
1464 lydict_remove(ctx->ctx, (*range)->emsg);
1465 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1466 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001467 if (range_p->dsc) {
1468 lydict_remove(ctx->ctx, (*range)->dsc);
1469 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1470 }
1471 if (range_p->ref) {
1472 lydict_remove(ctx->ctx, (*range)->ref);
1473 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1474 }
Radek Krejci19a96102018-11-15 13:38:09 +01001475 /* extensions are taken only from the last range by the caller */
1476
1477 (*range)->parts = parts;
1478 parts = NULL;
1479 ret = LY_SUCCESS;
1480cleanup:
1481 /* TODO clean up */
1482 LY_ARRAY_FREE(parts);
1483
1484 return ret;
1485}
1486
1487/**
1488 * @brief Checks pattern syntax.
1489 *
Radek Krejcia3045382018-11-22 14:30:31 +01001490 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001491 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001492 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1493 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001494 */
1495static LY_ERR
1496lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1497{
1498 int idx, idx2, start, end, err_offset, count;
1499 char *perl_regex, *ptr;
1500 const char *err_msg, *orig_ptr;
1501 pcre *precomp;
1502#define URANGE_LEN 19
1503 char *ublock2urange[][2] = {
1504 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1505 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1506 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1507 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1508 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1509 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1510 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1511 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1512 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1513 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1514 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1515 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1516 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1517 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1518 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1519 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1520 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1521 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1522 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1523 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1524 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1525 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1526 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1527 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1528 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1529 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1530 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1531 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1532 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1533 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1534 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1535 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1536 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1537 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1538 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1539 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1540 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1541 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1542 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1543 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1544 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1545 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1546 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1547 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1548 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1549 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1550 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1551 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1552 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1553 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1554 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1555 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1556 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1557 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1558 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1559 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1560 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1561 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1562 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1563 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1564 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1565 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1566 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1567 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1568 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1569 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1570 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1571 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1572 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1573 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1574 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1575 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1576 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1577 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1578 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1579 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1580 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1581 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1582 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1583 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1584 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1585 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1586 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1587 {NULL, NULL}
1588 };
1589
1590 /* adjust the expression to a Perl equivalent
1591 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1592
1593 /* we need to replace all "$" with "\$", count them now */
1594 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1595
1596 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1597 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1598 perl_regex[0] = '\0';
1599
1600 ptr = perl_regex;
1601
1602 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1603 /* we will add line-end anchoring */
1604 ptr[0] = '(';
1605 ++ptr;
1606 }
1607
1608 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1609 if (orig_ptr[0] == '$') {
1610 ptr += sprintf(ptr, "\\$");
1611 } else if (orig_ptr[0] == '^') {
1612 ptr += sprintf(ptr, "\\^");
1613 } else {
1614 ptr[0] = orig_ptr[0];
1615 ++ptr;
1616 }
1617 }
1618
1619 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1620 ptr += sprintf(ptr, ")$");
1621 } else {
1622 ptr[0] = '\0';
1623 ++ptr;
1624 }
1625
1626 /* substitute Unicode Character Blocks with exact Character Ranges */
1627 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1628 start = ptr - perl_regex;
1629
1630 ptr = strchr(ptr, '}');
1631 if (!ptr) {
1632 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1633 pattern, perl_regex + start + 2, "unterminated character property");
1634 free(perl_regex);
1635 return LY_EVALID;
1636 }
1637 end = (ptr - perl_regex) + 1;
1638
1639 /* need more space */
1640 if (end - start < URANGE_LEN) {
1641 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1642 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1643 }
1644
1645 /* find our range */
1646 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1647 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1648 break;
1649 }
1650 }
1651 if (!ublock2urange[idx][0]) {
1652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1653 pattern, perl_regex + start + 5, "unknown block name");
1654 free(perl_regex);
1655 return LY_EVALID;
1656 }
1657
1658 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1659 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1660 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1661 ++count;
1662 }
1663 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1664 --count;
1665 }
1666 }
1667 if (count) {
1668 /* skip brackets */
1669 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1670 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1671 } else {
1672 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1673 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1674 }
1675 }
1676
1677 /* must return 0, already checked during parsing */
1678 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1679 &err_msg, &err_offset, NULL);
1680 if (!precomp) {
1681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1682 free(perl_regex);
1683 return LY_EVALID;
1684 }
1685 free(perl_regex);
1686
1687 if (pcre_precomp) {
1688 *pcre_precomp = precomp;
1689 } else {
1690 free(precomp);
1691 }
1692
1693 return LY_SUCCESS;
1694
1695#undef URANGE_LEN
1696}
1697
Radek Krejcia3045382018-11-22 14:30:31 +01001698/**
1699 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1700 * @param[in] ctx Compile context.
1701 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1702 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1703 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1704 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1705 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1706 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1707 */
Radek Krejci19a96102018-11-15 13:38:09 +01001708static LY_ERR
1709lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1710 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1711{
1712 struct lysc_pattern **pattern;
1713 unsigned int u, v;
1714 const char *err_msg;
1715 LY_ERR ret = LY_SUCCESS;
1716
1717 /* first, copy the patterns from the base type */
1718 if (base_patterns) {
1719 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1720 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1721 }
1722
1723 LY_ARRAY_FOR(patterns_p, u) {
1724 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1725 *pattern = calloc(1, sizeof **pattern);
1726 ++(*pattern)->refcount;
1727
1728 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1729 LY_CHECK_RET(ret);
1730 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1731 if (err_msg) {
1732 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1733 }
1734
1735 if (patterns_p[u].arg[0] == 0x15) {
1736 (*pattern)->inverted = 1;
1737 }
1738 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1739 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001740 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1741 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001742 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1743 options, v, lys_compile_ext, ret, done);
1744 }
1745done:
1746 return ret;
1747}
1748
Radek Krejcia3045382018-11-22 14:30:31 +01001749/**
1750 * @brief map of the possible restrictions combination for the specific built-in type.
1751 */
Radek Krejci19a96102018-11-15 13:38:09 +01001752static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1753 0 /* LY_TYPE_UNKNOWN */,
1754 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001755 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1756 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1757 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1758 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1759 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001760 LYS_SET_BIT /* LY_TYPE_BITS */,
1761 0 /* LY_TYPE_BOOL */,
1762 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1763 0 /* LY_TYPE_EMPTY */,
1764 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1765 LYS_SET_BASE /* LY_TYPE_IDENT */,
1766 LYS_SET_REQINST /* LY_TYPE_INST */,
1767 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001768 LYS_SET_TYPE /* LY_TYPE_UNION */,
1769 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001770 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001771 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001772 LYS_SET_RANGE /* LY_TYPE_INT64 */
1773};
1774
1775/**
1776 * @brief stringification of the YANG built-in data types
1777 */
1778const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1779 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1780 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001781};
1782
Radek Krejcia3045382018-11-22 14:30:31 +01001783/**
1784 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1785 * @param[in] ctx Compile context.
1786 * @param[in] enums_p Array of the parsed enum structures to compile.
1787 * @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.
1788 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1789 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1790 * @param[out] enums Newly created array of the compiled enums information for the current type.
1791 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1792 */
Radek Krejci19a96102018-11-15 13:38:09 +01001793static LY_ERR
1794lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1795 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1796{
1797 LY_ERR ret = LY_SUCCESS;
1798 unsigned int u, v, match;
1799 int32_t value = 0;
1800 uint32_t position = 0;
1801 struct lysc_type_enum_item *e, storage;
1802
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001803 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001804 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1805 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1806 return LY_EVALID;
1807 }
1808
1809 LY_ARRAY_FOR(enums_p, u) {
1810 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1811 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001812 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1813 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001814 if (base_enums) {
1815 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1816 LY_ARRAY_FOR(base_enums, v) {
1817 if (!strcmp(e->name, base_enums[v].name)) {
1818 break;
1819 }
1820 }
1821 if (v == LY_ARRAY_SIZE(base_enums)) {
1822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1823 "Invalid %s - derived type adds new item \"%s\".",
1824 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1825 return LY_EVALID;
1826 }
1827 match = v;
1828 }
1829
1830 if (basetype == LY_TYPE_ENUM) {
1831 if (enums_p[u].flags & LYS_SET_VALUE) {
1832 e->value = (int32_t)enums_p[u].value;
1833 if (!u || e->value >= value) {
1834 value = e->value + 1;
1835 }
1836 /* check collision with other values */
1837 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1838 if (e->value == (*enums)[v].value) {
1839 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1840 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1841 e->value, e->name, (*enums)[v].name);
1842 return LY_EVALID;
1843 }
1844 }
1845 } else if (base_enums) {
1846 /* inherit the assigned value */
1847 e->value = base_enums[match].value;
1848 if (!u || e->value >= value) {
1849 value = e->value + 1;
1850 }
1851 } else {
1852 /* assign value automatically */
1853 if (u && value == INT32_MIN) {
1854 /* counter overflow */
1855 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1856 "Invalid enumeration - it is not possible to auto-assign enum value for "
1857 "\"%s\" since the highest value is already 2147483647.", e->name);
1858 return LY_EVALID;
1859 }
1860 e->value = value++;
1861 }
1862 } else { /* LY_TYPE_BITS */
1863 if (enums_p[u].flags & LYS_SET_VALUE) {
1864 e->value = (int32_t)enums_p[u].value;
1865 if (!u || (uint32_t)e->value >= position) {
1866 position = (uint32_t)e->value + 1;
1867 }
1868 /* check collision with other values */
1869 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1870 if (e->value == (*enums)[v].value) {
1871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1872 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1873 (uint32_t)e->value, e->name, (*enums)[v].name);
1874 return LY_EVALID;
1875 }
1876 }
1877 } else if (base_enums) {
1878 /* inherit the assigned value */
1879 e->value = base_enums[match].value;
1880 if (!u || (uint32_t)e->value >= position) {
1881 position = (uint32_t)e->value + 1;
1882 }
1883 } else {
1884 /* assign value automatically */
1885 if (u && position == 0) {
1886 /* counter overflow */
1887 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1888 "Invalid bits - it is not possible to auto-assign bit position for "
1889 "\"%s\" since the highest value is already 4294967295.", e->name);
1890 return LY_EVALID;
1891 }
1892 e->value = position++;
1893 }
1894 }
1895
1896 if (base_enums) {
1897 /* the assigned values must not change from the derived type */
1898 if (e->value != base_enums[match].value) {
1899 if (basetype == LY_TYPE_ENUM) {
1900 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1901 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1902 e->name, base_enums[match].value, e->value);
1903 } else {
1904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1905 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1906 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1907 }
1908 return LY_EVALID;
1909 }
1910 }
1911
1912 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1913 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1914
1915 if (basetype == LY_TYPE_BITS) {
1916 /* keep bits ordered by position */
1917 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1918 if (v != u) {
1919 memcpy(&storage, e, sizeof *e);
1920 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1921 memcpy(&(*enums)[v], &storage, sizeof storage);
1922 }
1923 }
1924 }
1925
1926done:
1927 return ret;
1928}
1929
Radek Krejcia3045382018-11-22 14:30:31 +01001930#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1931 for ((NODE) = (NODE)->parent; \
1932 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1933 (NODE) = (NODE)->parent); \
1934 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1936 TERM; \
1937 }
1938
1939/**
1940 * @brief Validate the predicate(s) from the leafref path.
1941 * @param[in] ctx Compile context
1942 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1943 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001944 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001945 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001946 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001947 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1948 */
1949static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001950lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001951 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001952{
1953 LY_ERR ret = LY_EVALID;
1954 const struct lys_module *mod;
1955 const struct lysc_node *src_node, *dst_node;
1956 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1957 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001958 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001959 const char *start, *end, *pke_end;
1960 struct ly_set keys = {0};
1961 int i;
1962
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001963 assert(path_context);
1964
Radek Krejcia3045382018-11-22 14:30:31 +01001965 while (**predicate == '[') {
1966 start = (*predicate)++;
1967
1968 while (isspace(**predicate)) {
1969 ++(*predicate);
1970 }
1971 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1972 while (isspace(**predicate)) {
1973 ++(*predicate);
1974 }
1975 if (**predicate != '=') {
1976 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001977 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1978 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001979 goto cleanup;
1980 }
1981 ++(*predicate);
1982 while (isspace(**predicate)) {
1983 ++(*predicate);
1984 }
1985
1986 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1988 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1989 goto cleanup;
1990 }
1991 --pke_end;
1992 while (isspace(*pke_end)) {
1993 --pke_end;
1994 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001995 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01001996 /* localize path-key-expr */
1997 pke_start = path_key_expr = *predicate;
1998 /* move after the current predicate */
1999 *predicate = end + 1;
2000
2001 /* source (must be leaf or leaf-list) */
2002 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002003 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002004 if (!mod) {
2005 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2006 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002007 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002008 goto cleanup;
2009 }
Radek Krejcia3045382018-11-22 14:30:31 +01002010 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002011 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002012 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002013 src_node = NULL;
2014 if (context_node->keys) {
2015 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2016 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2017 src_node = (const struct lysc_node*)context_node->keys[u];
2018 break;
2019 }
2020 }
2021 }
Radek Krejcia3045382018-11-22 14:30:31 +01002022 if (!src_node) {
2023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002024 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002025 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002026 goto cleanup;
2027 }
Radek Krejcia3045382018-11-22 14:30:31 +01002028
2029 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002030 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002031 i = ly_set_add(&keys, (void*)src_node, 0);
2032 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002033 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002034 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002035 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002036 *predicate - start, start, src_node->name);
2037 goto cleanup;
2038 }
2039
2040 /* destination */
2041 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002042 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002043
2044 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
2045 if (strncmp(path_key_expr, "current()", 9)) {
2046 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2047 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2048 *predicate - start, start);
2049 goto cleanup;
2050 }
2051 path_key_expr += 9;
2052 while (isspace(*path_key_expr)) {
2053 ++path_key_expr;
2054 }
2055
2056 if (*path_key_expr != '/') {
2057 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2058 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2059 *predicate - start, start);
2060 goto cleanup;
2061 }
2062 ++path_key_expr;
2063 while (isspace(*path_key_expr)) {
2064 ++path_key_expr;
2065 }
2066
2067 /* rel-path-keyexpr:
2068 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2069 while (!strncmp(path_key_expr, "..", 2)) {
2070 ++dest_parent_times;
2071 path_key_expr += 2;
2072 while (isspace(*path_key_expr)) {
2073 ++path_key_expr;
2074 }
2075 if (*path_key_expr != '/') {
2076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2077 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2078 *predicate - start, start);
2079 goto cleanup;
2080 }
2081 ++path_key_expr;
2082 while (isspace(*path_key_expr)) {
2083 ++path_key_expr;
2084 }
2085
2086 /* path is supposed to be evaluated in data tree, so we have to skip
2087 * all schema nodes that cannot be instantiated in data tree */
2088 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2089 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2090 *predicate - start, start);
2091 }
2092 if (!dest_parent_times) {
2093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2094 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2095 *predicate - start, start);
2096 goto cleanup;
2097 }
2098 if (path_key_expr == pke_end) {
2099 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2100 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2101 *predicate - start, start);
2102 goto cleanup;
2103 }
2104
2105 while(path_key_expr != pke_end) {
2106 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
2107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002108 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2109 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002110 goto cleanup;
2111 }
2112
2113 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002114 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002115 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002116 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002117 }
2118 if (!mod) {
2119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2120 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
2121 *predicate - start, start, dst_len, dst);
2122 goto cleanup;
2123 }
2124
2125 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2126 if (!dst_node) {
2127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2128 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
2129 *predicate - start, start, path_key_expr - pke_start, pke_start);
2130 goto cleanup;
2131 }
2132 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002133 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 +01002134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002135 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002136 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2137 goto cleanup;
2138 }
2139 }
2140
2141 ret = LY_SUCCESS;
2142cleanup:
2143 ly_set_erase(&keys, NULL);
2144 return ret;
2145}
2146
2147/**
2148 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2149 *
2150 * path-arg = absolute-path / relative-path
2151 * absolute-path = 1*("/" (node-identifier *path-predicate))
2152 * relative-path = 1*(".." "/") descendant-path
2153 *
2154 * @param[in,out] path Path to parse.
2155 * @param[out] prefix Prefix of the token, NULL if there is not any.
2156 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2157 * @param[out] name Name of the token.
2158 * @param[out] nam_len Length of the name.
2159 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2160 * must not be changed between consecutive calls. -1 if the
2161 * path is absolute.
2162 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2163 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2164 */
2165static LY_ERR
2166lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2167 int *parent_times, int *has_predicate)
2168{
2169 int par_times = 0;
2170
2171 assert(path && *path);
2172 assert(parent_times);
2173 assert(prefix);
2174 assert(prefix_len);
2175 assert(name);
2176 assert(name_len);
2177 assert(has_predicate);
2178
2179 *prefix = NULL;
2180 *prefix_len = 0;
2181 *name = NULL;
2182 *name_len = 0;
2183 *has_predicate = 0;
2184
2185 if (!*parent_times) {
2186 if (!strncmp(*path, "..", 2)) {
2187 *path += 2;
2188 ++par_times;
2189 while (!strncmp(*path, "/..", 3)) {
2190 *path += 3;
2191 ++par_times;
2192 }
2193 }
2194 if (par_times) {
2195 *parent_times = par_times;
2196 } else {
2197 *parent_times = -1;
2198 }
2199 }
2200
2201 if (**path != '/') {
2202 return LY_EINVAL;
2203 }
2204 /* skip '/' */
2205 ++(*path);
2206
2207 /* node-identifier ([prefix:]name) */
2208 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
2209
2210 if ((**path == '/' && (*path)[1]) || !**path) {
2211 /* path continues by another token or this is the last token */
2212 return LY_SUCCESS;
2213 } else if ((*path)[0] != '[') {
2214 /* unexpected character */
2215 return LY_EINVAL;
2216 } else {
2217 /* predicate starting with [ */
2218 *has_predicate = 1;
2219 return LY_SUCCESS;
2220 }
2221}
2222
2223/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002224 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2225 *
2226 * The set of features used for target must be a subset of features used for the leafref.
2227 * This is not a perfect, we should compare the truth tables but it could require too much resources
2228 * and RFC 7950 does not require it explicitely, so we simplify that.
2229 *
2230 * @param[in] refnode The leafref node.
2231 * @param[in] target Tha target node of the leafref.
2232 * @return LY_SUCCESS or LY_EVALID;
2233 */
2234static LY_ERR
2235lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2236{
2237 LY_ERR ret = LY_EVALID;
2238 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002239 unsigned int u, v, count;
2240 struct ly_set features = {0};
2241
2242 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002243 if (iter->iffeatures) {
2244 LY_ARRAY_FOR(iter->iffeatures, u) {
2245 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2246 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002247 }
2248 }
2249 }
2250 }
2251
2252 /* we should have, in features set, a superset of features applicable to the target node.
2253 * So when adding features applicable to the target into the features set, we should not be
2254 * able to actually add any new feature, otherwise it is not a subset of features applicable
2255 * to the leafref itself. */
2256 count = features.count;
2257 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002258 if (iter->iffeatures) {
2259 LY_ARRAY_FOR(iter->iffeatures, u) {
2260 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2261 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002262 /* new feature was added (or LY_EMEM) */
2263 goto cleanup;
2264 }
2265 }
2266 }
2267 }
2268 }
2269 ret = LY_SUCCESS;
2270
2271cleanup:
2272 ly_set_erase(&features, NULL);
2273 return ret;
2274}
2275
2276/**
Radek Krejcia3045382018-11-22 14:30:31 +01002277 * @brief Validate the leafref path.
2278 * @param[in] ctx Compile context
2279 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002280 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002281 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2282 */
2283static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002284lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002285{
2286 const struct lysc_node *node = NULL, *parent = NULL;
2287 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002288 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002289 const char *id, *prefix, *name;
2290 size_t prefix_len, name_len;
2291 int parent_times = 0, has_predicate;
2292 unsigned int iter, u;
2293 LY_ERR ret = LY_SUCCESS;
2294
2295 assert(ctx);
2296 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002297 assert(leafref);
2298
2299 /* TODO leafref targets may be not implemented, in such a case we actually could make (we did it in libyang1) such a models implemented */
Radek Krejcia3045382018-11-22 14:30:31 +01002300
2301 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002302 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002303 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2304 if (!iter) { /* first iteration */
2305 /* precess ".." in relative paths */
2306 if (parent_times > 0) {
2307 /* move from the context node */
2308 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2309 /* path is supposed to be evaluated in data tree, so we have to skip
2310 * all schema nodes that cannot be instantiated in data tree */
2311 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002312 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002313 }
2314 }
2315 }
2316
2317 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002318 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002319 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002320 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002321 }
2322 if (!mod) {
2323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002324 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2325 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002326 return LY_EVALID;
2327 }
2328
2329 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2330 if (!node) {
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 \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002333 return LY_EVALID;
2334 }
2335 parent = node;
2336
2337 if (has_predicate) {
2338 /* we have predicate, so the current result must be list */
2339 if (node->nodetype != LYS_LIST) {
2340 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2341 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002342 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002343 return LY_EVALID;
2344 }
2345
Radek Krejcibade82a2018-12-05 10:13:48 +01002346 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2347 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002348 }
2349
2350 ++iter;
2351 }
2352 if (ret) {
2353 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002354 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002355 return LY_EVALID;
2356 }
2357
2358 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2359 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2360 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002361 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002362 return LY_EVALID;
2363 }
2364
2365 /* check status */
2366 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2367 return LY_EVALID;
2368 }
2369
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002370 /* check config */
2371 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2372 if (node->flags & LYS_CONFIG_R) {
2373 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2374 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2375 leafref->path);
2376 return LY_EVALID;
2377 }
2378 }
2379
Radek Krejci412ddfa2018-11-23 11:44:11 +01002380 /* store the target's type and check for circular chain of leafrefs */
2381 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2382 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2383 if (type == (struct lysc_type*)leafref) {
2384 /* circular chain detected */
2385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2386 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2387 return LY_EVALID;
2388 }
2389 }
2390
Radek Krejci58d171e2018-11-23 13:50:55 +01002391 /* check if leafref and its target are under common if-features */
2392 if (lys_compile_leafref_features_validate(startnode, node)) {
2393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2394 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2395 leafref->path);
2396 return LY_EVALID;
2397 }
2398
Radek Krejcia3045382018-11-22 14:30:31 +01002399 return LY_SUCCESS;
2400}
2401
Radek Krejcicdfecd92018-11-26 11:27:32 +01002402static 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 +01002403 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002404/**
2405 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2406 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002407 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2408 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2409 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2410 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002411 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002412 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002413 * @param[in] basetype Base YANG built-in type of the type to compile.
2414 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2415 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2416 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2417 * @param[out] type Newly created type structure with the filled information about the type.
2418 * @return LY_ERR value.
2419 */
Radek Krejci19a96102018-11-15 13:38:09 +01002420static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002421lys_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,
2422 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2423 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002424{
2425 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002426 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002427 struct lysc_type_bin *bin;
2428 struct lysc_type_num *num;
2429 struct lysc_type_str *str;
2430 struct lysc_type_bits *bits;
2431 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002432 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002433 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002434 struct lysc_type_union *un, *un_aux;
2435 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002436
2437 switch (basetype) {
2438 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002439 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002440
2441 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002442 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002443 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002444 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2445 LY_CHECK_RET(ret);
2446 if (!tpdfname) {
2447 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2448 options, u, lys_compile_ext, ret, done);
2449 }
2450 }
2451
2452 if (tpdfname) {
2453 type_p->compiled = *type;
2454 *type = calloc(1, sizeof(struct lysc_type_bin));
2455 }
2456 break;
2457 case LY_TYPE_BITS:
2458 /* RFC 7950 9.7 - bits */
2459 bits = (struct lysc_type_bits*)(*type);
2460 if (type_p->bits) {
2461 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2462 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2463 (struct lysc_type_enum_item**)&bits->bits);
2464 LY_CHECK_RET(ret);
2465 }
2466
Radek Krejci555cb5b2018-11-16 14:54:33 +01002467 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002468 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002469 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002470 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002471 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002472 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002473 free(*type);
2474 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002475 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002476 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002477 }
2478
2479 if (tpdfname) {
2480 type_p->compiled = *type;
2481 *type = calloc(1, sizeof(struct lysc_type_bits));
2482 }
2483 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002484 case LY_TYPE_DEC64:
2485 dec = (struct lysc_type_dec*)(*type);
2486
2487 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002488 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002489 if (!type_p->fraction_digits) {
2490 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002491 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002492 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002494 free(*type);
2495 *type = NULL;
2496 }
2497 return LY_EVALID;
2498 }
2499 } else if (type_p->fraction_digits) {
2500 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002501 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2503 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002504 tpdfname);
2505 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002506 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2507 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002508 free(*type);
2509 *type = NULL;
2510 }
2511 return LY_EVALID;
2512 }
2513 dec->fraction_digits = type_p->fraction_digits;
2514
2515 /* RFC 7950 9.2.4 - range */
2516 if (type_p->range) {
2517 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2518 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2519 LY_CHECK_RET(ret);
2520 if (!tpdfname) {
2521 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2522 options, u, lys_compile_ext, ret, done);
2523 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002524 }
2525
2526 if (tpdfname) {
2527 type_p->compiled = *type;
2528 *type = calloc(1, sizeof(struct lysc_type_dec));
2529 }
2530 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002531 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002532 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002533
2534 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002535 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002536 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002537 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2538 LY_CHECK_RET(ret);
2539 if (!tpdfname) {
2540 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2541 options, u, lys_compile_ext, ret, done);
2542 }
2543 } else if (base && ((struct lysc_type_str*)base)->length) {
2544 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2545 }
2546
2547 /* RFC 7950 9.4.5 - pattern */
2548 if (type_p->patterns) {
2549 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2550 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2551 LY_CHECK_RET(ret);
2552 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2553 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2554 }
2555
2556 if (tpdfname) {
2557 type_p->compiled = *type;
2558 *type = calloc(1, sizeof(struct lysc_type_str));
2559 }
2560 break;
2561 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002562 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002563
2564 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002565 if (type_p->enums) {
2566 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2567 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2568 LY_CHECK_RET(ret);
2569 }
2570
Radek Krejci555cb5b2018-11-16 14:54:33 +01002571 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002572 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002573 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002574 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002575 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002577 free(*type);
2578 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002579 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002580 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002581 }
2582
2583 if (tpdfname) {
2584 type_p->compiled = *type;
2585 *type = calloc(1, sizeof(struct lysc_type_enum));
2586 }
2587 break;
2588 case LY_TYPE_INT8:
2589 case LY_TYPE_UINT8:
2590 case LY_TYPE_INT16:
2591 case LY_TYPE_UINT16:
2592 case LY_TYPE_INT32:
2593 case LY_TYPE_UINT32:
2594 case LY_TYPE_INT64:
2595 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002596 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002597
2598 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002599 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002600 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002601 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2602 LY_CHECK_RET(ret);
2603 if (!tpdfname) {
2604 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2605 options, u, lys_compile_ext, ret, done);
2606 }
2607 }
2608
2609 if (tpdfname) {
2610 type_p->compiled = *type;
2611 *type = calloc(1, sizeof(struct lysc_type_num));
2612 }
2613 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002614 case LY_TYPE_IDENT:
2615 idref = (struct lysc_type_identityref*)(*type);
2616
2617 /* RFC 7950 9.10.2 - base */
2618 if (type_p->bases) {
2619 if (base) {
2620 /* only the directly derived identityrefs can contain base specification */
2621 if (tpdfname) {
2622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002623 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002624 tpdfname);
2625 } else {
2626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002627 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002628 free(*type);
2629 *type = NULL;
2630 }
2631 return LY_EVALID;
2632 }
2633 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2634 LY_CHECK_RET(ret);
2635 }
2636
2637 if (!base && !type_p->flags) {
2638 /* type derived from identityref built-in type must contain at least one base */
2639 if (tpdfname) {
2640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2641 } else {
2642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2643 free(*type);
2644 *type = NULL;
2645 }
2646 return LY_EVALID;
2647 }
2648
2649 if (tpdfname) {
2650 type_p->compiled = *type;
2651 *type = calloc(1, sizeof(struct lysc_type_identityref));
2652 }
2653 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002654 case LY_TYPE_LEAFREF:
2655 /* RFC 7950 9.9.3 - require-instance */
2656 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002657 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002658 if (tpdfname) {
2659 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2660 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2661 } else {
2662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2663 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2664 free(*type);
2665 *type = NULL;
2666 }
2667 return LY_EVALID;
2668 }
Radek Krejcia3045382018-11-22 14:30:31 +01002669 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002670 } else if (base) {
2671 /* inherit */
2672 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002673 } else {
2674 /* default is true */
2675 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2676 }
2677 if (type_p->path) {
2678 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002679 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002680 } else if (base) {
2681 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002682 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002683 } else if (tpdfname) {
2684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2685 return LY_EVALID;
2686 } else {
2687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2688 free(*type);
2689 *type = NULL;
2690 return LY_EVALID;
2691 }
2692 if (tpdfname) {
2693 type_p->compiled = *type;
2694 *type = calloc(1, sizeof(struct lysc_type_leafref));
2695 }
2696 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002697 case LY_TYPE_INST:
2698 /* RFC 7950 9.9.3 - require-instance */
2699 if (type_p->flags & LYS_SET_REQINST) {
2700 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2701 } else {
2702 /* default is true */
2703 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2704 }
2705
2706 if (tpdfname) {
2707 type_p->compiled = *type;
2708 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2709 }
2710 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002711 case LY_TYPE_UNION:
2712 un = (struct lysc_type_union*)(*type);
2713
2714 /* RFC 7950 7.4 - type */
2715 if (type_p->types) {
2716 if (base) {
2717 /* only the directly derived union can contain types specification */
2718 if (tpdfname) {
2719 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2720 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2721 tpdfname);
2722 } else {
2723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2724 "Invalid type substatement for the type not directly derived from union built-in type.");
2725 free(*type);
2726 *type = NULL;
2727 }
2728 return LY_EVALID;
2729 }
2730 /* compile the type */
2731 additional = 0;
2732 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2733 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002734 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 +01002735 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2736 /* add space for additional types from the union subtype */
2737 un_aux = (struct lysc_type_union *)un->types[u + additional];
2738 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)));
2739 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2740 un->types = (void*)((uint32_t*)(p) + 1);
2741
2742 /* copy subtypes of the subtype union */
2743 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2744 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2745 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2746 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2747 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2748 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2749 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2750 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2751 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2752 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2753 /* TODO extensions */
2754
2755 } else {
2756 un->types[u + additional] = un_aux->types[v];
2757 ++un_aux->types[v]->refcount;
2758 }
2759 ++additional;
2760 LY_ARRAY_INCREMENT(un->types);
2761 }
2762 /* compensate u increment in main loop */
2763 --additional;
2764
2765 /* free the replaced union subtype */
2766 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2767 } else {
2768 LY_ARRAY_INCREMENT(un->types);
2769 }
2770 LY_CHECK_RET(ret);
2771 }
2772 }
2773
2774 if (!base && !type_p->flags) {
2775 /* type derived from union built-in type must contain at least one type */
2776 if (tpdfname) {
2777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2778 } else {
2779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2780 free(*type);
2781 *type = NULL;
2782 }
2783 return LY_EVALID;
2784 }
2785
2786 if (tpdfname) {
2787 type_p->compiled = *type;
2788 *type = calloc(1, sizeof(struct lysc_type_union));
2789 }
2790 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002791 case LY_TYPE_BOOL:
2792 case LY_TYPE_EMPTY:
2793 case LY_TYPE_UNKNOWN: /* just to complete switch */
2794 break;
2795 }
2796 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2797done:
2798 return ret;
2799}
2800
Radek Krejcia3045382018-11-22 14:30:31 +01002801/**
2802 * @brief Compile information about the leaf/leaf-list's type.
2803 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002804 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2805 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2806 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2807 * @param[in] context_name Name of the context node or referencing typedef for logging.
2808 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002809 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2810 * @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 +01002811 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002812 * @return LY_ERR value.
2813 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002814static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002815lys_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 +01002816 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002817{
2818 LY_ERR ret = LY_SUCCESS;
2819 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002820 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002821 struct type_context {
2822 const struct lysp_tpdf *tpdf;
2823 struct lysp_node *node;
2824 struct lysp_module *mod;
2825 } *tctx, *tctx_prev = NULL;
2826 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002827 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002828 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002829 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002830
2831 (*type) = NULL;
2832
2833 tctx = calloc(1, sizeof *tctx);
2834 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002835 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002836 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2837 ret == LY_SUCCESS;
2838 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2839 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2840 if (basetype) {
2841 break;
2842 }
2843
2844 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002845 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2846 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002847 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2848
Radek Krejcicdfecd92018-11-26 11:27:32 +01002849 if (units && !*units) {
2850 /* inherit units */
2851 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2852 }
Radek Krejci01342af2019-01-03 15:18:08 +01002853 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002854 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002855 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002856 }
Radek Krejci01342af2019-01-03 15:18:08 +01002857 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002858 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2859 break;
2860 }
2861
Radek Krejci19a96102018-11-15 13:38:09 +01002862 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002863 /* it is not necessary to continue, the rest of the chain was already compiled,
2864 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002865 basetype = tctx->tpdf->type.compiled->basetype;
2866 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002867 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002868 dummyloops = 1;
2869 goto preparenext;
2870 } else {
2871 tctx = NULL;
2872 break;
2873 }
Radek Krejci19a96102018-11-15 13:38:09 +01002874 }
2875
2876 /* store information for the following processing */
2877 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2878
Radek Krejcicdfecd92018-11-26 11:27:32 +01002879preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002880 /* prepare next loop */
2881 tctx_prev = tctx;
2882 tctx = calloc(1, sizeof *tctx);
2883 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2884 }
2885 free(tctx);
2886
2887 /* allocate type according to the basetype */
2888 switch (basetype) {
2889 case LY_TYPE_BINARY:
2890 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002891 break;
2892 case LY_TYPE_BITS:
2893 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002894 break;
2895 case LY_TYPE_BOOL:
2896 case LY_TYPE_EMPTY:
2897 *type = calloc(1, sizeof(struct lysc_type));
2898 break;
2899 case LY_TYPE_DEC64:
2900 *type = calloc(1, sizeof(struct lysc_type_dec));
2901 break;
2902 case LY_TYPE_ENUM:
2903 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002904 break;
2905 case LY_TYPE_IDENT:
2906 *type = calloc(1, sizeof(struct lysc_type_identityref));
2907 break;
2908 case LY_TYPE_INST:
2909 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2910 break;
2911 case LY_TYPE_LEAFREF:
2912 *type = calloc(1, sizeof(struct lysc_type_leafref));
2913 break;
2914 case LY_TYPE_STRING:
2915 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002916 break;
2917 case LY_TYPE_UNION:
2918 *type = calloc(1, sizeof(struct lysc_type_union));
2919 break;
2920 case LY_TYPE_INT8:
2921 case LY_TYPE_UINT8:
2922 case LY_TYPE_INT16:
2923 case LY_TYPE_UINT16:
2924 case LY_TYPE_INT32:
2925 case LY_TYPE_UINT32:
2926 case LY_TYPE_INT64:
2927 case LY_TYPE_UINT64:
2928 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002929 break;
2930 case LY_TYPE_UNKNOWN:
2931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2932 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2933 ret = LY_EVALID;
2934 goto cleanup;
2935 }
2936 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002937 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002938 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2939 ly_data_type2str[basetype]);
2940 free(*type);
2941 (*type) = NULL;
2942 ret = LY_EVALID;
2943 goto cleanup;
2944 }
2945
2946 /* get restrictions from the referred typedefs */
2947 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2948 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002949 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002950 base = tctx->tpdf->type.compiled;
2951 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002952 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002953 /* no change, just use the type information from the base */
2954 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2955 ++base->refcount;
2956 continue;
2957 }
2958
2959 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002960 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2961 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2962 tctx->tpdf->name, ly_data_type2str[basetype]);
2963 ret = LY_EVALID;
2964 goto cleanup;
2965 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2966 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2967 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2968 tctx->tpdf->name, tctx->tpdf->dflt);
2969 ret = LY_EVALID;
2970 goto cleanup;
2971 }
2972
Radek Krejci19a96102018-11-15 13:38:09 +01002973 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002974 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002975 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 +01002976 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2977 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002978 LY_CHECK_GOTO(ret, cleanup);
2979 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002980 }
2981
Radek Krejcic5c27e52018-11-15 14:38:11 +01002982 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002983 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002984 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002985 (*type)->basetype = basetype;
2986 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01002987 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 +01002988 LY_CHECK_GOTO(ret, cleanup);
2989 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002990 /* no specific restriction in leaf's type definition, copy from the base */
2991 free(*type);
2992 (*type) = base;
2993 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002994 }
Radek Krejci01342af2019-01-03 15:18:08 +01002995 if (!(*type)->dflt) {
2996 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
2997 }
Radek Krejci19a96102018-11-15 13:38:09 +01002998
2999 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
3000
3001cleanup:
3002 ly_set_erase(&tpdf_chain, free);
3003 return ret;
3004}
3005
Radek Krejcib1b59152019-01-07 13:21:56 +01003006static 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 +01003007
Radek Krejcia3045382018-11-22 14:30:31 +01003008/**
3009 * @brief Compile parsed container node information.
3010 * @param[in] ctx Compile context
3011 * @param[in] node_p Parsed container node.
3012 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3013 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3014 * is enriched with the container-specific information.
3015 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3016 */
Radek Krejci19a96102018-11-15 13:38:09 +01003017static LY_ERR
3018lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3019{
3020 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3021 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3022 struct lysp_node *child_p;
3023 unsigned int u;
3024 LY_ERR ret = LY_SUCCESS;
3025
Radek Krejcife909632019-02-12 15:34:42 +01003026 if (cont_p->presence) {
3027 cont->flags |= LYS_PRESENCE;
3028 }
3029
Radek Krejci19a96102018-11-15 13:38:09 +01003030 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003031 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003032 }
3033
3034 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
3035 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
3036 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
3037
3038done:
3039 return ret;
3040}
3041
Radek Krejcia3045382018-11-22 14:30:31 +01003042/**
3043 * @brief Compile parsed leaf node information.
3044 * @param[in] ctx Compile context
3045 * @param[in] node_p Parsed leaf node.
3046 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3047 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3048 * is enriched with the leaf-specific information.
3049 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3050 */
Radek Krejci19a96102018-11-15 13:38:09 +01003051static LY_ERR
3052lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3053{
3054 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3055 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3056 unsigned int u;
3057 LY_ERR ret = LY_SUCCESS;
3058
Radek Krejci19a96102018-11-15 13:38:09 +01003059 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003060 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
3061 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
Radek Krejci76b3e962018-12-14 17:01:25 +01003062 if (leaf->dflt) {
3063 leaf->flags |= LYS_SET_DFLT;
3064 }
Radek Krejci43699232018-11-23 14:59:46 +01003065
Radek Krejcie86bf772018-12-14 11:39:53 +01003066 ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod_def->parsed, node_p->name, &leaf_p->type, options, &leaf->type,
Radek Krejci01342af2019-01-03 15:18:08 +01003067 leaf->units ? NULL : &leaf->units);
Radek Krejci19a96102018-11-15 13:38:09 +01003068 LY_CHECK_GOTO(ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01003069 if (!leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
3070 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
3071 }
Radek Krejcia3045382018-11-22 14:30:31 +01003072 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3073 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3074 ly_set_add(&ctx->unres, leaf, 0);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003075 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3076 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3077 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3078 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3079 ly_set_add(&ctx->unres, leaf, 0);
3080 }
3081 }
Radek Krejci43699232018-11-23 14:59:46 +01003082 } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) {
3083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3084 "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt);
3085 return LY_EVALID;
Radek Krejcia3045382018-11-22 14:30:31 +01003086 }
3087
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003088 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3089
Radek Krejci19a96102018-11-15 13:38:09 +01003090done:
3091 return ret;
3092}
3093
Radek Krejcia3045382018-11-22 14:30:31 +01003094/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003095 * @brief Compile parsed leaf-list node information.
3096 * @param[in] ctx Compile context
3097 * @param[in] node_p Parsed leaf-list node.
3098 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3099 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3100 * is enriched with the leaf-list-specific information.
3101 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3102 */
3103static LY_ERR
3104lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3105{
3106 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3107 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
3108 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003109 LY_ERR ret = LY_SUCCESS;
3110
Radek Krejci0e5d8382018-11-28 16:37:53 +01003111 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
3112 DUP_STRING(ctx->ctx, llist_p->units, llist->units);
3113
3114 if (llist_p->dflts) {
3115 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3116 LY_ARRAY_FOR(llist_p->dflts, u) {
3117 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
3118 LY_ARRAY_INCREMENT(llist->dflts);
3119 }
3120 }
3121
3122 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003123 if (llist->min) {
3124 llist->flags |= LYS_MAND_TRUE;
3125 }
Radek Krejcib7408632018-11-28 17:12:11 +01003126 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003127
Radek Krejcie86bf772018-12-14 11:39:53 +01003128 ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod_def->parsed, node_p->name, &llist_p->type, options, &llist->type,
Radek Krejci01342af2019-01-03 15:18:08 +01003129 llist->units ? NULL : &llist->units);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003130 LY_CHECK_GOTO(ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01003131 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003132 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01003133 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003134 LY_ARRAY_INCREMENT(llist->dflts);
3135 }
3136
3137 if (llist->type->basetype == LY_TYPE_LEAFREF) {
3138 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3139 ly_set_add(&ctx->unres, llist, 0);
3140 } else if (llist->type->basetype == LY_TYPE_UNION) {
3141 LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) {
3142 if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3143 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3144 ly_set_add(&ctx->unres, llist, 0);
3145 }
3146 }
Radek Krejci6bb080b2018-11-28 17:23:41 +01003147 } else if (llist->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003148 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci6bb080b2018-11-28 17:23:41 +01003149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3150 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3151 return LY_EVALID;
3152 } else if (llist_p->dflts) {
3153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3154 "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]);
3155 return LY_EVALID;
3156 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003157 }
3158
3159 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3160 /* configuration data values must be unique - so check the default values */
3161 LY_ARRAY_FOR(llist->dflts, u) {
3162 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3163 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
3164 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3165 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
3166 return LY_EVALID;
3167 }
3168 }
3169 }
3170 }
3171
3172 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
3173
3174done:
3175 return ret;
3176}
3177
3178/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003179 * @brief Compile parsed list node information.
3180 * @param[in] ctx Compile context
3181 * @param[in] node_p Parsed list 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 list-specific information.
3185 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3186 */
3187static LY_ERR
3188lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3189{
3190 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3191 struct lysc_node_list *list = (struct lysc_node_list*)node;
3192 struct lysp_node *child_p;
3193 struct lysc_node_leaf **key, ***unique;
3194 size_t len;
3195 unsigned int u, v;
3196 const char *keystr, *delim;
3197 int config;
3198 LY_ERR ret = LY_SUCCESS;
3199
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003200 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003201 if (list->min) {
3202 list->flags |= LYS_MAND_TRUE;
3203 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003204 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3205
3206 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003207 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003208 }
3209
3210 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
3211
3212 /* keys */
3213 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3214 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3215 return LY_EVALID;
3216 }
3217
3218 /* find all the keys (must be direct children) */
3219 keystr = list_p->key;
3220 while (keystr) {
3221 delim = strpbrk(keystr, " \t\n");
3222 if (delim) {
3223 len = delim - keystr;
3224 while (isspace(*delim)) {
3225 ++delim;
3226 }
3227 } else {
3228 len = strlen(keystr);
3229 }
3230
3231 /* key node must be present */
3232 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3233 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3234 if (!(*key)) {
3235 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3236 "The list's key \"%.*s\" not found.", len, keystr);
3237 return LY_EVALID;
3238 }
3239 /* keys must be unique */
3240 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3241 if (*key == list->keys[u]) {
3242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3243 "Duplicated key identifier \"%.*s\".", len, keystr);
3244 return LY_EVALID;
3245 }
3246 }
3247 /* key must have the same config flag as the list itself */
3248 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3249 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3250 return LY_EVALID;
3251 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003252 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003253 /* YANG 1.0 denies key to be of empty type */
3254 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3256 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
3257 return LY_EVALID;
3258 }
3259 } else {
3260 /* when and if-feature are illegal on list keys */
3261 if ((*key)->when) {
3262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3263 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
3264 return LY_EVALID;
3265 }
3266 if ((*key)->iffeatures) {
3267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3268 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
3269 return LY_EVALID;
3270 }
3271 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003272
3273 /* check status */
3274 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3275 (*key)->flags, (*key)->module, (*key)->name));
3276
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003277 /* ignore default values of the key */
3278 if ((*key)->dflt) {
3279 lydict_remove(ctx->ctx, (*key)->dflt);
3280 (*key)->dflt = NULL;
3281 }
3282 /* mark leaf as key */
3283 (*key)->flags |= LYS_KEY;
3284
3285 /* next key value */
3286 keystr = delim;
3287 }
3288
3289 /* uniques */
3290 if (list_p->uniques) {
3291 for (v = 0; v < LY_ARRAY_SIZE(list_p->uniques); ++v) {
3292 config = -1;
3293 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3294 keystr = list_p->uniques[v];
3295 while (keystr) {
3296 delim = strpbrk(keystr, " \t\n");
3297 if (delim) {
3298 len = delim - keystr;
3299 while (isspace(*delim)) {
3300 ++delim;
3301 }
3302 } else {
3303 len = strlen(keystr);
3304 }
3305
3306 /* unique node must be present */
3307 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci95710c92019-02-11 15:49:55 +01003308 ret = lys_resolve_schema_nodeid(ctx, keystr, len, node, LYS_LEAF, 0, (const struct lysc_node**)key);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003309 if (ret != LY_SUCCESS) {
3310 if (ret == LY_EDENIED) {
3311 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3312 "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.",
3313 len, keystr, lys_nodetype2str((*key)->nodetype));
3314 }
3315 return LY_EVALID;
3316 }
3317
3318 /* all referenced leafs must be of the same config type */
3319 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3320 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3321 "Unique statement \"%s\" refers to leafs with different config type.", list_p->uniques[v]);
3322 return LY_EVALID;
3323 } else if ((*key)->flags & LYS_CONFIG_W) {
3324 config = 1;
3325 } else { /* LYS_CONFIG_R */
3326 config = 0;
3327 }
3328
Radek Krejci76b3e962018-12-14 17:01:25 +01003329 /* check status */
3330 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3331 (*key)->flags, (*key)->module, (*key)->name));
3332
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003333 /* mark leaf as unique */
3334 (*key)->flags |= LYS_UNIQUE;
3335
3336 /* next unique value in line */
3337 keystr = delim;
3338 }
3339 /* next unique definition */
3340 }
3341 }
3342
3343 //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done);
3344 //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done);
3345
3346done:
3347 return ret;
3348}
3349
Radek Krejcib56c7502019-02-13 14:19:54 +01003350/**
3351 * @brief Do some checks and set the default choice's case.
3352 *
3353 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3354 *
3355 * @param[in] ctx Compile context.
3356 * @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,
3357 * not the reference to the imported module.
3358 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3359 * @return LY_ERR value.
3360 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003361static LY_ERR
3362lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3363{
3364 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3365 const char *prefix = NULL, *name;
3366 size_t prefix_len = 0;
3367
3368 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3369 name = strchr(dflt, ':');
3370 if (name) {
3371 prefix = dflt;
3372 prefix_len = name - prefix;
3373 ++name;
3374 } else {
3375 name = dflt;
3376 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003377 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003378 /* prefixed default case make sense only for the prefix of the schema itself */
3379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3380 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3381 prefix_len, prefix);
3382 return LY_EVALID;
3383 }
3384 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3385 if (!ch->dflt) {
3386 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3387 "Default case \"%s\" not found.", dflt);
3388 return LY_EVALID;
3389 }
3390 /* no mandatory nodes directly under the default case */
3391 LY_LIST_FOR(ch->dflt->child, iter) {
3392 if (iter->flags & LYS_MAND_TRUE) {
3393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3394 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3395 return LY_EVALID;
3396 }
3397 }
Radek Krejci01342af2019-01-03 15:18:08 +01003398 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003399 return LY_SUCCESS;
3400}
3401
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003402/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003403 * @brief Compile parsed choice node information.
3404 * @param[in] ctx Compile context
3405 * @param[in] node_p Parsed choice node.
3406 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3407 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003408 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003409 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3410 */
3411static LY_ERR
3412lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3413{
3414 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3415 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3416 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003417 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003418 LY_ERR ret = LY_SUCCESS;
3419
Radek Krejci056d0a82018-12-06 16:57:25 +01003420 LY_LIST_FOR(ch_p->child, child_p) {
3421 if (child_p->nodetype == LYS_CASE) {
3422 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003423 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003424 }
3425 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003426 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003427 }
3428 }
3429
3430 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003431 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003432 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003433 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003434
Radek Krejci9800fb82018-12-13 14:26:23 +01003435 return ret;
3436}
3437
3438/**
3439 * @brief Compile parsed anydata or anyxml node information.
3440 * @param[in] ctx Compile context
3441 * @param[in] node_p Parsed anydata or anyxml 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 any-specific information.
3445 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3446 */
3447static LY_ERR
3448lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3449{
3450 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3451 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3452 unsigned int u;
3453 LY_ERR ret = LY_SUCCESS;
3454
3455 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3456
3457 if (any->flags & LYS_CONFIG_W) {
3458 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3459 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3460 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003461done:
3462 return ret;
3463}
3464
Radek Krejcib56c7502019-02-13 14:19:54 +01003465/**
3466 * @brief Compile status information of the given node.
3467 *
3468 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3469 * has the status correctly set during the compilation.
3470 *
3471 * @param[in] ctx Compile context
3472 * @param[in,out] node Compiled node which status is supposed to be resolved. If the status was set explicitely on the node, it is already set in the
3473 * flags value and we just check the compatibility with the parent's status value.
3474 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3475 * @return LY_ERR value.
3476 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003477static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01003478lys_compile_status(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t parent_flags)
Radek Krejci056d0a82018-12-06 16:57:25 +01003479{
Radek Krejci056d0a82018-12-06 16:57:25 +01003480 /* status - it is not inherited by specification, but it does not make sense to have
3481 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3482 if (!(node->flags & LYS_STATUS_MASK)) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003483 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3484 if ((parent_flags & 0x3) != 0x3) {
3485 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3486 * combination of bits (0x3) which marks the uses_status value */
3487 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3488 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3489 }
3490 node->flags |= parent_flags & LYS_STATUS_MASK;
Radek Krejci056d0a82018-12-06 16:57:25 +01003491 } else {
3492 node->flags |= LYS_STATUS_CURR;
3493 }
Radek Krejcib1b59152019-01-07 13:21:56 +01003494 } else if (parent_flags & LYS_STATUS_MASK) {
Radek Krejcib56c7502019-02-13 14:19:54 +01003495 /* check status compatibility with the parent */
3496 if ((parent_flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
3497 if (node->flags & LYS_STATUS_CURR) {
3498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3499 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3500 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3501 } else { /* LYS_STATUS_DEPRC */
3502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3503 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3504 }
3505 return LY_EVALID;
3506 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003507 }
3508 return LY_SUCCESS;
3509}
3510
Radek Krejcib56c7502019-02-13 14:19:54 +01003511/**
3512 * @brief Check uniqness of the node/action/notification name.
3513 *
3514 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3515 * structures, but they share the namespace so we need to check their name collisions.
3516 *
3517 * @param[in] ctx Compile context.
3518 * @param[in] children List (linked list) of data nodes to go through.
3519 * @param[in] actions List (sized array) of actions or RPCs to go through.
3520 * @param[in] notifs List (sized array) of Notifications to go through.
3521 * @param[in] name Name of the item to find in the given lists.
3522 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3523 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3524 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3525 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003526static LY_ERR
3527lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3528 const struct lysc_action *actions, const struct lysc_notif *notifs,
3529 const char *name, void *exclude)
3530{
3531 const struct lysc_node *iter;
3532 unsigned int u;
3533
3534 LY_LIST_FOR(children, iter) {
Radek Krejci95710c92019-02-11 15:49:55 +01003535 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003536 goto error;
3537 }
3538 }
3539 LY_ARRAY_FOR(actions, u) {
Radek Krejci95710c92019-02-11 15:49:55 +01003540 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003541 goto error;
3542 }
3543 }
3544 LY_ARRAY_FOR(notifs, u) {
Radek Krejci95710c92019-02-11 15:49:55 +01003545 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003546 goto error;
3547 }
3548 }
3549 return LY_SUCCESS;
3550error:
3551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition");
3552 return LY_EEXIST;
3553}
3554
3555/**
3556 * @brief Connect the node into the siblings list and check its name uniqueness.
3557 *
3558 * @param[in] ctx Compile context
3559 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3560 * the choice itself is expected instead of a specific case node.
3561 * @param[in] node Schema node to connect into the list.
3562 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3563 */
3564static LY_ERR
3565lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3566{
3567 struct lysc_node **children;
3568
3569 if (node->nodetype == LYS_CASE) {
3570 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3571 } else {
3572 children = lysc_node_children_p(parent);
3573 }
3574 if (children) {
3575 if (!(*children)) {
3576 /* first child */
3577 *children = node;
3578 } else if (*children != node) {
3579 /* by the condition in previous branch we cover the choice/case children
3580 * - the children list is shared by the choice and the the first case, in addition
3581 * the first child of each case must be referenced from the case node. So the node is
3582 * actually always already inserted in case it is the first children - so here such
3583 * a situation actually corresponds to the first branch */
3584 /* insert at the end of the parent's children list */
3585 (*children)->prev->next = node;
3586 node->prev = (*children)->prev;
3587 (*children)->prev = node;
3588
3589 /* check the name uniqueness */
3590 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3591 lysc_node_notifs(parent), node->name, node)) {
3592 return LY_EEXIST;
3593 }
3594 }
3595 }
3596 return LY_SUCCESS;
3597}
3598
Radek Krejci95710c92019-02-11 15:49:55 +01003599/**
Radek Krejcib56c7502019-02-13 14:19:54 +01003600 * @brief Get the XPath context node for the given schema node.
3601 * @param[in] start The schema node where the XPath expression appears.
3602 * @return The context node to evaluate XPath expression in given schema node.
3603 * @return NULL in case the context node is the root node.
3604 */
3605static struct lysc_node *
3606lysc_xpath_context(struct lysc_node *start)
3607{
3608 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3609 start = start->parent);
3610 return start;
3611}
3612
3613/**
3614 * @brief Prepare the case structure in choice node for the new data node.
3615 *
3616 * 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
3617 * created in the choice when the first child was processed.
3618 *
3619 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01003620 * @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,
3621 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01003622 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3623 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3624 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3625 * @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,
3626 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01003627 */
Radek Krejci056d0a82018-12-06 16:57:25 +01003628static struct lysc_node_case*
3629lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3630{
3631 struct lysc_node *iter;
3632 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01003633 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01003634 unsigned int u;
3635 LY_ERR ret;
3636
Radek Krejci95710c92019-02-11 15:49:55 +01003637#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01003638 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01003639 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01003640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3641 return NULL; \
3642 } \
3643 }
3644
Radek Krejci95710c92019-02-11 15:49:55 +01003645 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
3646 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003647
3648 /* we have to add an implicit case node into the parent choice */
3649 cs = calloc(1, sizeof(struct lysc_node_case));
3650 DUP_STRING(ctx->ctx, child->name, cs->name);
3651 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01003652 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003653 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3654 /* the case is already present since the child is not its first children */
3655 return (struct lysc_node_case*)ch->cases->prev;
3656 }
Radek Krejci95710c92019-02-11 15:49:55 +01003657 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01003658
3659 /* explicit parent case is not present (this is its first child) */
3660 cs = calloc(1, sizeof(struct lysc_node_case));
3661 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3662 cs->flags = LYS_STATUS_MASK & node_p->flags;
3663 cs->sp = node_p;
3664
Radek Krejcib1b59152019-01-07 13:21:56 +01003665 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
3666 LY_CHECK_RET(lys_compile_status(ctx, (struct lysc_node*)cs, ch->flags), NULL);
Radek Krejci00b874b2019-02-12 10:54:50 +01003667
3668 if (node_p->when) {
3669 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
3670 ret = lys_compile_when(ctx, node_p->when, options, when);
3671 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003672 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003673 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003674 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01003675 } else {
3676 LOGINT(ctx->ctx);
3677 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01003678 }
3679 cs->module = ctx->mod;
3680 cs->prev = (struct lysc_node*)cs;
3681 cs->nodetype = LYS_CASE;
3682 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
3683 cs->parent = (struct lysc_node*)ch;
3684 cs->child = child;
3685
3686 return cs;
3687error:
3688 return NULL;
3689
3690#undef UNIQUE_CHECK
3691}
3692
Radek Krejcib56c7502019-02-13 14:19:54 +01003693/**
3694 * @brief Apply refined config to the refine's target node.
3695 *
3696 * @param[in] ctx Compile context.
3697 * @param[in] node Refine's target node.
3698 * @param[in] rfn Parsed refine information.
3699 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
3700 * done only on the node for which the refine was created. The function applies also recursively to apply the config change
3701 * to the complete subtree and the test is not needed for the subnodes.
3702 * @return LY_ERR value.
3703 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003704static LY_ERR
3705lys_compile_refine_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_refine *rfn, int inheriting)
3706{
3707 struct lysc_node *child;
3708 uint16_t config = rfn->flags & LYS_CONFIG_MASK;
3709
3710 if (config == (node->flags & LYS_CONFIG_MASK)) {
3711 /* nothing to do */
3712 return LY_SUCCESS;
3713 }
3714
3715 if (!inheriting) {
3716 /* explicit refine */
3717 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3718 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcib1b59152019-01-07 13:21:56 +01003719 "Invalid refine of config in \"%s\" - configuration node cannot be child of any state data node.",
3720 rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003721 return LY_EVALID;
3722 }
3723 }
3724 node->flags &= ~LYS_CONFIG_MASK;
3725 node->flags |= config;
3726
3727 /* inherit the change into the children */
3728 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node), child) {
3729 LY_CHECK_RET(lys_compile_refine_config(ctx, child, rfn, 1));
3730 }
3731
3732 /* TODO actions and notifications */
3733
3734 return LY_SUCCESS;
3735}
3736
Radek Krejcib56c7502019-02-13 14:19:54 +01003737/**
3738 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
3739 *
3740 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
3741 * the flag to such parents from a mandatory children.
3742 *
3743 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
3744 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
3745 * (mandatory children was removed).
3746 */
Radek Krejcife909632019-02-12 15:34:42 +01003747void
3748lys_compile_mandatory_parents(struct lysc_node *parent, int add)
3749{
3750 struct lysc_node *iter;
3751
3752 if (add) { /* set flag */
3753 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3754 parent = parent->parent) {
3755 parent->flags |= LYS_MAND_TRUE;
3756 }
3757 } else { /* unset flag */
3758 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
3759 for (iter = (struct lysc_node*)lysc_node_children(parent); iter; iter = iter->next) {
3760 if (iter->flags && LYS_MAND_TRUE) {
3761 /* there is another mandatory node */
3762 return;
3763 }
3764 }
3765 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3766 parent->flags &= ~LYS_MAND_TRUE;
3767 }
3768 }
3769}
3770
Radek Krejci056d0a82018-12-06 16:57:25 +01003771/**
Radek Krejcie86bf772018-12-14 11:39:53 +01003772 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3773 * If present, also apply uses's modificators.
3774 *
3775 * @param[in] ctx Compile context
3776 * @param[in] uses_p Parsed uses schema node.
3777 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3778 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3779 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3780 * the compile context.
3781 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3782 */
3783static LY_ERR
3784lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
3785{
3786 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01003787 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01003788 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01003789 struct lysc_node_container context_node_fake =
3790 {.nodetype = LYS_CONTAINER,
3791 .module = ctx->mod,
3792 .flags = parent ? parent->flags : 0,
3793 .child = NULL, .next = NULL,
3794 .prev = (struct lysc_node*)&context_node_fake};
Radek Krejcie86bf772018-12-14 11:39:53 +01003795 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01003796 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01003797 int found;
3798 const char *id, *name, *prefix;
3799 size_t prefix_len, name_len;
3800 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01003801 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01003802 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01003803 uint32_t min, max;
3804 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01003805 struct lysc_when **when, *when_shared;
Radek Krejcie86bf772018-12-14 11:39:53 +01003806
3807 /* search for the grouping definition */
3808 found = 0;
3809 id = uses_p->name;
3810 lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3811 if (prefix) {
3812 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
3813 if (!mod) {
3814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3815 "Invalid prefix used for grouping reference (%s).", uses_p->name);
3816 return LY_EVALID;
3817 }
3818 } else {
3819 mod = ctx->mod_def;
3820 }
3821 if (mod == ctx->mod_def) {
3822 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
3823 grp = lysp_node_groupings(node_p);
3824 LY_ARRAY_FOR(grp, u) {
3825 if (!strcmp(grp[u].name, name)) {
3826 grp = &grp[u];
3827 found = 1;
3828 break;
3829 }
3830 }
3831 }
3832 }
3833 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003834 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01003835 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01003836 if (grp) {
3837 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
3838 if (!strcmp(grp[u].name, name)) {
3839 grp = &grp[u];
3840 found = 1;
3841 }
3842 }
3843 }
3844 if (!found && mod->parsed->includes) {
3845 /* ... and all the submodules */
3846 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
3847 grp = mod->parsed->includes[u].submodule->groupings;
3848 if (grp) {
3849 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
3850 if (!strcmp(grp[v].name, name)) {
3851 grp = &grp[v];
3852 found = 1;
3853 }
3854 }
3855 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003856 }
3857 }
3858 }
3859 if (!found) {
3860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3861 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3862 return LY_EVALID;
3863 }
3864
3865 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3866 grp_stack_count = ctx->groupings.count;
3867 ly_set_add(&ctx->groupings, (void*)grp, 0);
3868 if (grp_stack_count == ctx->groupings.count) {
3869 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
3870 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3871 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3872 return LY_EVALID;
3873 }
3874
3875 /* switch context's mod_def */
3876 mod_old = ctx->mod_def;
3877 ctx->mod_def = mod;
3878
3879 /* check status */
3880 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), error);
3881
Radek Krejcie86bf772018-12-14 11:39:53 +01003882 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003883 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
3884 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), error);
Radek Krejci01342af2019-01-03 15:18:08 +01003885 child = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev;
Radek Krejci00b874b2019-02-12 10:54:50 +01003886
3887 /* some preparation for applying refines */
3888 if (grp->data == node_p) {
3889 /* remember the first child */
3890 context_node_fake.child = child;
Radek Krejci01342af2019-01-03 15:18:08 +01003891 }
3892 }
Radek Krejci00b874b2019-02-12 10:54:50 +01003893 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01003894 LY_LIST_FOR(context_node_fake.child, child) {
3895 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01003896
3897 /* pass uses's when to all the children */
3898 if (uses_p->when) {
3899 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, error);
3900 if (!when_shared) {
3901 ret = lys_compile_when(ctx, uses_p->when, options, when);
3902 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01003903 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01003904 when_shared = *when;
3905 } else {
3906 ++when_shared->refcount;
3907 (*when) = when_shared;
3908 }
3909 }
Radek Krejci01342af2019-01-03 15:18:08 +01003910 }
3911 if (context_node_fake.child) {
3912 child = context_node_fake.child->prev;
3913 context_node_fake.child->prev = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01003914 }
3915
Radek Krejci12fb9142019-01-08 09:45:30 +01003916 /* TODO: apply augment */
3917
Radek Krejcif0089082019-01-07 16:42:01 +01003918 /* reload previous context's mod_def */
3919 ctx->mod_def = mod_old;
3920
Radek Krejci76b3e962018-12-14 17:01:25 +01003921 /* apply refine */
3922 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci95710c92019-02-11 15:49:55 +01003923 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, 0, 0, (const struct lysc_node**)&node),
Radek Krejci01342af2019-01-03 15:18:08 +01003924 error);
Radek Krejcif2271f12019-01-07 16:42:23 +01003925 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01003926
3927 /* default value */
3928 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01003929 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003930 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3931 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
3932 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
3933 goto error;
3934 }
3935 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
3936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3937 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
3938 rfn->nodeid, lys_nodetype2str(node->nodetype));
3939 goto error;
3940 }
3941 if (node->nodetype == LYS_LEAF) {
3942 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
3943 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01003944 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003945 /* TODO check the default value according to type */
3946 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003947 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01003948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3949 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
3950 goto error;
3951 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003952 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
3953 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
3954 }
3955 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01003956 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01003957 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, error);
3958 LY_ARRAY_FOR(rfn->dflts, u) {
3959 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
3960 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
3961 }
3962 /* TODO check the default values according to type */
3963 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01003964 if (((struct lysc_node_choice*)node)->dflt) {
3965 /* unset LYS_SET_DFLT from the current default case */
3966 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
3967 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003968 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), error);
3969 }
3970 }
3971
Radek Krejci12fb9142019-01-08 09:45:30 +01003972 /* description */
3973 if (rfn->dsc) {
3974 FREE_STRING(ctx->ctx, node->dsc);
3975 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
3976 }
3977
3978 /* reference */
3979 if (rfn->ref) {
3980 FREE_STRING(ctx->ctx, node->ref);
3981 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
3982 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003983
3984 /* config */
3985 if (rfn->flags & LYS_CONFIG_MASK) {
3986 LY_CHECK_GOTO(lys_compile_refine_config(ctx, node, rfn, 0), error);
3987 }
3988
3989 /* mandatory */
3990 if (rfn->flags & LYS_MAND_MASK) {
3991 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
3992 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3993 "Invalid refine of mandatory in \"%s\" - %s cannot hold mandatory statement.",
3994 rfn->nodeid, lys_nodetype2str(node->nodetype));
3995 goto error;
3996 }
3997 /* in compiled flags, only the LYS_MAND_TRUE is present */
3998 if (rfn->flags & LYS_MAND_TRUE) {
3999 /* check if node has default value */
4000 if (node->nodetype & LYS_LEAF) {
4001 if (node->flags & LYS_SET_DFLT) {
4002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci01342af2019-01-03 15:18:08 +01004003 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01004004 goto error;
4005 } else {
4006 /* remove the default value taken from the leaf's type */
4007 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
4008 ((struct lysc_node_leaf*)node)->dflt = NULL;
4009 }
4010 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
4011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci01342af2019-01-03 15:18:08 +01004012 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01004013 goto error;
4014 }
4015 if (node->parent && (node->parent->flags & LYS_SET_DFLT)) {
4016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4017 "Invalid refine of mandatory in \"%s\" - %s under the default case.",
4018 rfn->nodeid, lys_nodetype2str(node->nodetype));
4019 goto error;
4020 }
4021
4022 node->flags |= LYS_MAND_TRUE;
Radek Krejcife909632019-02-12 15:34:42 +01004023 lys_compile_mandatory_parents(node->parent, 1);
Radek Krejci76b3e962018-12-14 17:01:25 +01004024 } else {
Radek Krejci01342af2019-01-03 15:18:08 +01004025 /* make mandatory false */
Radek Krejci76b3e962018-12-14 17:01:25 +01004026 node->flags &= ~LYS_MAND_TRUE;
Radek Krejcife909632019-02-12 15:34:42 +01004027 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejci01342af2019-01-03 15:18:08 +01004028 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
4029 /* get the type's default value if any */
4030 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
4031 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004032 }
4033 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004034
4035 /* presence */
4036 if (rfn->presence) {
4037 if (node->nodetype != LYS_CONTAINER) {
4038 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4039 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
4040 rfn->nodeid, lys_nodetype2str(node->nodetype));
4041 goto error;
4042 }
4043 node->flags |= LYS_PRESENCE;
4044 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004045
4046 /* must */
4047 if (rfn->musts) {
4048 switch (node->nodetype) {
4049 case LYS_LEAF:
4050 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, error);
4051 break;
4052 case LYS_LEAFLIST:
4053 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, error);
4054 break;
4055 case LYS_LIST:
4056 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, error);
4057 break;
4058 case LYS_CONTAINER:
4059 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, error);
4060 break;
4061 case LYS_ANYXML:
4062 case LYS_ANYDATA:
4063 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, error);
4064 break;
4065 default:
4066 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4067 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
4068 rfn->nodeid, lys_nodetype2str(node->nodetype));
4069 goto error;
4070 }
4071 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004072
4073 /* min/max-elements */
4074 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4075 switch (node->nodetype) {
4076 case LYS_LEAFLIST:
4077 if (rfn->flags & LYS_SET_MAX) {
4078 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4079 }
4080 if (rfn->flags & LYS_SET_MIN) {
4081 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004082 if (rfn->min) {
4083 node->flags |= LYS_MAND_TRUE;
4084 lys_compile_mandatory_parents(node->parent, 1);
4085 } else {
4086 node->flags &= ~LYS_MAND_TRUE;
4087 lys_compile_mandatory_parents(node->parent, 0);
4088 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004089 }
4090 break;
4091 case LYS_LIST:
4092 if (rfn->flags & LYS_SET_MAX) {
4093 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4094 }
4095 if (rfn->flags & LYS_SET_MIN) {
4096 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004097 if (rfn->min) {
4098 node->flags |= LYS_MAND_TRUE;
4099 lys_compile_mandatory_parents(node->parent, 1);
4100 } else {
4101 node->flags &= ~LYS_MAND_TRUE;
4102 lys_compile_mandatory_parents(node->parent, 0);
4103 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004104 }
4105 break;
4106 default:
4107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4108 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
4109 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
4110 goto error;
4111 }
4112 }
Radek Krejcif0089082019-01-07 16:42:01 +01004113
4114 /* if-feature */
4115 if (rfn->iffeatures) {
4116 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
4117 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
4118 }
Radek Krejci01342af2019-01-03 15:18:08 +01004119 }
4120 /* fix connection of the children nodes from fake context node back into the parent */
4121 if (context_node_fake.child) {
4122 context_node_fake.child->prev = child;
4123 }
4124 LY_LIST_FOR(context_node_fake.child, child) {
4125 child->parent = parent;
Radek Krejcie86bf772018-12-14 11:39:53 +01004126 }
4127
Radek Krejcif2271f12019-01-07 16:42:23 +01004128 /* do some additional checks of the changed nodes when all the refines are applied */
4129 for (u = 0; u < refined.count; ++u) {
4130 node = (struct lysc_node*)refined.objs[u];
4131 rfn = &uses_p->refines[u];
4132
4133 /* check possible conflict with default value (default added, mandatory left true) */
4134 if ((node->flags & LYS_MAND_TRUE) &&
4135 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
4136 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
4137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4138 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
4139 goto error;
4140 }
4141
4142 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4143 if (node->nodetype == LYS_LIST) {
4144 min = ((struct lysc_node_list*)node)->min;
4145 max = ((struct lysc_node_list*)node)->max;
4146 } else {
4147 min = ((struct lysc_node_leaflist*)node)->min;
4148 max = ((struct lysc_node_leaflist*)node)->max;
4149 }
4150 if (min > max) {
4151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4152 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
4153 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
4154 goto error;
4155 }
4156 }
4157 }
4158
Radek Krejcie86bf772018-12-14 11:39:53 +01004159 ret = LY_SUCCESS;
4160error:
4161 /* reload previous context's mod_def */
4162 ctx->mod_def = mod_old;
4163 /* remove the grouping from the stack for circular groupings dependency check */
4164 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4165 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01004166 ly_set_erase(&refined, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004167
4168 return ret;
4169}
4170
Radek Krejcife909632019-02-12 15:34:42 +01004171
Radek Krejcie86bf772018-12-14 11:39:53 +01004172/**
Radek Krejcia3045382018-11-22 14:30:31 +01004173 * @brief Compile parsed schema node information.
4174 * @param[in] ctx Compile context
4175 * @param[in] node_p Parsed schema node.
4176 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4177 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
4178 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4179 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01004180 * @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).
4181 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01004182 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4183 */
Radek Krejci19a96102018-11-15 13:38:09 +01004184static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01004185lys_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 +01004186{
4187 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01004188 struct lysc_node *node;
4189 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01004190 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01004191 unsigned int u;
4192 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
4193
4194 switch (node_p->nodetype) {
4195 case LYS_CONTAINER:
4196 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
4197 node_compile_spec = lys_compile_node_container;
4198 break;
4199 case LYS_LEAF:
4200 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
4201 node_compile_spec = lys_compile_node_leaf;
4202 break;
4203 case LYS_LIST:
4204 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004205 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01004206 break;
4207 case LYS_LEAFLIST:
4208 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01004209 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01004210 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004211 case LYS_CHOICE:
4212 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01004213 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01004214 break;
Radek Krejci19a96102018-11-15 13:38:09 +01004215 case LYS_ANYXML:
4216 case LYS_ANYDATA:
4217 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01004218 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01004219 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01004220 case LYS_USES:
4221 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01004222 default:
4223 LOGINT(ctx->ctx);
4224 return LY_EINT;
4225 }
4226 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4227 node->nodetype = node_p->nodetype;
4228 node->module = ctx->mod;
4229 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01004230 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01004231
4232 /* config */
4233 if (!(node->flags & LYS_CONFIG_MASK)) {
4234 /* config not explicitely set, inherit it from parent */
4235 if (parent) {
4236 node->flags |= parent->flags & LYS_CONFIG_MASK;
4237 } else {
4238 /* default is config true */
4239 node->flags |= LYS_CONFIG_W;
4240 }
4241 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004242 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
4243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4244 "Configuration node cannot be child of any state data node.");
4245 goto error;
4246 }
Radek Krejci19a96102018-11-15 13:38:09 +01004247
Radek Krejcia6d57732018-11-29 13:40:37 +01004248 /* *list ordering */
4249 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
4250 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
4251 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
4252 "RPC/action output parameters or notification content (%s).", ctx->path);
4253 node->flags &= ~LYS_ORDBY_MASK;
4254 node->flags |= LYS_ORDBY_SYSTEM;
4255 } else if (!(node->flags & LYS_ORDBY_MASK)) {
4256 /* default ordering is system */
4257 node->flags |= LYS_ORDBY_SYSTEM;
4258 }
4259 }
4260
Radek Krejci19a96102018-11-15 13:38:09 +01004261 /* status - it is not inherited by specification, but it does not make sense to have
4262 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01004263 if (!parent || parent->nodetype != LYS_CHOICE) {
4264 /* in case of choice/case's children, postpone the check to the moment we know if
4265 * the parent is choice (parent here) or some case (so we have to get its flags to check) */
Radek Krejcib1b59152019-01-07 13:21:56 +01004266 LY_CHECK_GOTO(lys_compile_status(ctx, node, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
Radek Krejci19a96102018-11-15 13:38:09 +01004267 }
4268
4269 if (!(options & LYSC_OPT_FREE_SP)) {
4270 node->sp = node_p;
4271 }
4272 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01004273 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
4274 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01004275 if (node_p->when) {
4276 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4277 ret = lys_compile_when(ctx, node_p->when, options, when);
4278 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004279 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01004280 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004281 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004282 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
4283
4284 /* nodetype-specific part */
4285 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
4286
Radek Krejcife909632019-02-12 15:34:42 +01004287 /* inherit LYS_MAND_TRUE in parent containers */
4288 if (node->flags & LYS_MAND_TRUE) {
4289 lys_compile_mandatory_parents(parent, 1);
4290 }
4291
Radek Krejci19a96102018-11-15 13:38:09 +01004292 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01004293 if (parent) {
4294 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004295 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
4296 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01004297 if (uses_status) {
4298
4299 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004300 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01004301 * it directly gets the same status flags as the choice;
4302 * uses_status cannot be applied here since uses cannot be child statement of choice */
4303 LY_CHECK_GOTO(lys_compile_status(ctx, node, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01004304 node->parent = (struct lysc_node*)cs;
4305 } else { /* other than choice */
4306 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01004307 }
Radek Krejci95710c92019-02-11 15:49:55 +01004308 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci19a96102018-11-15 13:38:09 +01004309 } else {
4310 /* top-level element */
4311 if (!ctx->mod->compiled->data) {
4312 ctx->mod->compiled->data = node;
4313 } else {
4314 /* insert at the end of the module's top-level nodes list */
4315 ctx->mod->compiled->data->prev->next = node;
4316 node->prev = ctx->mod->compiled->data->prev;
4317 ctx->mod->compiled->data->prev = node;
4318 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004319 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
4320 ctx->mod->compiled->notifs, node->name, node)) {
4321 return LY_EVALID;
4322 }
Radek Krejci19a96102018-11-15 13:38:09 +01004323 }
4324
4325 return LY_SUCCESS;
4326
4327error:
4328 lysc_node_free(ctx->ctx, node);
4329 return ret;
4330}
4331
Radek Krejcib56c7502019-02-13 14:19:54 +01004332/**
4333 * @brief Internal sorting process for the lys_compile_augment_sort().
4334 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4335 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4336 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4337 */
Radek Krejcif12a1f02019-02-11 16:42:08 +01004338static void
4339lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4340{
4341 unsigned int v;
4342 size_t len;
4343
4344 len = strlen(aug_p->nodeid);
4345 LY_ARRAY_FOR(result, v) {
4346 if (strlen(result[v]->nodeid) <= len) {
4347 continue;
4348 }
4349 if (v < LY_ARRAY_SIZE(result)) {
4350 /* move the rest of array */
4351 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4352 break;
4353 }
4354 }
4355 result[v] = aug_p;
4356 LY_ARRAY_INCREMENT(result);
4357}
4358
Radek Krejcib56c7502019-02-13 14:19:54 +01004359/**
4360 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4361 *
4362 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4363 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4364 *
4365 * @param[in] ctx Compile context.
4366 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4367 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4368 * @return LY_ERR value.
4369 */
Radek Krejci95710c92019-02-11 15:49:55 +01004370LY_ERR
Radek Krejcif12a1f02019-02-11 16:42:08 +01004371lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_module *mod_p, struct lysp_augment ***augments)
Radek Krejci95710c92019-02-11 15:49:55 +01004372{
4373 struct lysp_augment **result = NULL;
4374 unsigned int u, v;
Radek Krejcif12a1f02019-02-11 16:42:08 +01004375 size_t count = 0;
Radek Krejci95710c92019-02-11 15:49:55 +01004376
Radek Krejcif12a1f02019-02-11 16:42:08 +01004377 assert(mod_p);
Radek Krejci95710c92019-02-11 15:49:55 +01004378 assert(augments);
4379
Radek Krejcif12a1f02019-02-11 16:42:08 +01004380 /* get count of the augments in module and all its submodules */
4381 if (mod_p->augments) {
4382 count += LY_ARRAY_SIZE(mod_p->augments);
4383 }
4384 LY_ARRAY_FOR(mod_p->includes, u) {
4385 if (mod_p->includes[u].submodule->augments) {
4386 count += LY_ARRAY_SIZE(mod_p->includes[u].submodule->augments);
4387 }
4388 }
4389
4390 if (!count) {
Radek Krejci95710c92019-02-11 15:49:55 +01004391 *augments = NULL;
4392 return LY_SUCCESS;
4393 }
Radek Krejcif12a1f02019-02-11 16:42:08 +01004394 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
Radek Krejci95710c92019-02-11 15:49:55 +01004395
4396 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4397 * together, so there can be even /z/y betwwen them. */
Radek Krejcif12a1f02019-02-11 16:42:08 +01004398 LY_ARRAY_FOR(mod_p->augments, u) {
4399 lys_compile_augment_sort_(&mod_p->augments[u], result);
4400 }
4401 LY_ARRAY_FOR(mod_p->includes, u) {
4402 LY_ARRAY_FOR(mod_p->includes[u].submodule->augments, v) {
4403 lys_compile_augment_sort_(&mod_p->includes[u].submodule->augments[v], result);
Radek Krejci95710c92019-02-11 15:49:55 +01004404 }
Radek Krejci95710c92019-02-11 15:49:55 +01004405 }
4406
4407 *augments = result;
4408 return LY_SUCCESS;
4409}
4410
4411/**
4412 * @brief Compile the parsed augment connecting it into its target.
4413 *
4414 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4415 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4416 * are already implemented and compiled.
4417 *
4418 * @param[in] ctx Compile context.
4419 * @param[in] aug_p Parsed augment to compile.
4420 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4421 * @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
4422 * children in case of the augmenting uses data.
4423 * @return LY_SUCCESS on success.
4424 * @return LY_EVALID on failure.
4425 */
4426LY_ERR
4427lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, int options, const struct lysc_node *parent)
4428{
4429 LY_ERR ret = LY_SUCCESS;
4430 struct lysp_node *node_p, *case_node_p;
4431 struct lysc_node *target; /* target target of the augment */
Radek Krejci00b874b2019-02-12 10:54:50 +01004432 struct lysc_node *node;
4433 struct lysc_node_case *next_case;
4434 struct lysc_when **when, *when_shared;
Radek Krejcife909632019-02-12 15:34:42 +01004435 int allow_mandatory = 0;
Radek Krejci95710c92019-02-11 15:49:55 +01004436
4437 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent,
4438 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
4439 1, (const struct lysc_node**)&target);
4440 if (ret != LY_SUCCESS) {
4441 if (ret == LY_EDENIED) {
4442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4443 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4444 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4445 }
4446 return LY_EVALID;
4447 }
4448
Radek Krejci00b874b2019-02-12 10:54:50 +01004449 /* check for mandatory nodes
4450 * - new cases augmenting some choice can have mandatory nodes
4451 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4452 */
Radek Krejcife909632019-02-12 15:34:42 +01004453 if (aug_p->when || target->nodetype == LYS_CHOICE) {
4454 allow_mandatory = 1;
4455 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004456
4457 when_shared = NULL;
Radek Krejci95710c92019-02-11 15:49:55 +01004458 LY_LIST_FOR(aug_p->child, node_p) {
4459 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4460 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4461 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
4462 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES))) {
4463 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4464 "Invalid augment (%s) of %s node which is not allowed to contain %s node \"%s\".",
4465 aug_p->nodeid, lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
4466 return LY_EVALID;
4467 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004468
Radek Krejci95710c92019-02-11 15:49:55 +01004469 /* compile the children */
4470 if (node_p->nodetype != LYS_CASE) {
4471 LY_CHECK_RET(lys_compile_node(ctx, node_p, options, target, 0));
4472 } else {
4473 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
4474 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options, target, 0));
4475 }
4476 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004477
4478 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children */
4479 if (target->nodetype == LYS_CASE) {
4480 /* the compiled node is the last child of the target (but it is a case, so we have to be careful) */
4481 next_case = target->next ? (struct lysc_node_case*)target->next : ((struct lysc_node_choice*)target->parent)->cases;
4482 for (node = (struct lysc_node*)lysc_node_children(target); node->next && node->next != next_case->child; node = node->next);
4483 } else if (target->nodetype == LYS_CHOICE) {
4484 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4485 node = ((struct lysc_node_choice*)target)->cases->prev;
4486 } else {
4487 /* the compiled node is the last child of the target */
4488 node = lysc_node_children(target)->prev;
4489 }
4490
Radek Krejcife909632019-02-12 15:34:42 +01004491 if (!allow_mandatory && (node->flags & LYS_MAND_TRUE)) {
4492 node->flags &= ~LYS_MAND_TRUE;
4493 lys_compile_mandatory_parents(target, 0);
4494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4495 "Invalid augment (%s) adding mandatory node \"%s\" without making it conditional via when statement.",
4496 aug_p->nodeid, node->name);
4497 return LY_EVALID;
4498 }
4499
Radek Krejci00b874b2019-02-12 10:54:50 +01004500 /* pass augment's when to all the children */
4501 if (aug_p->when) {
4502 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4503 if (!when_shared) {
4504 ret = lys_compile_when(ctx, aug_p->when, options, when);
4505 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004506 (*when)->context = lysc_xpath_context(target);
Radek Krejci00b874b2019-02-12 10:54:50 +01004507 when_shared = *when;
4508 } else {
4509 ++when_shared->refcount;
4510 (*when) = when_shared;
4511 }
4512 }
Radek Krejci95710c92019-02-11 15:49:55 +01004513 }
4514 /* TODO actions, notifications */
4515
Radek Krejci00b874b2019-02-12 10:54:50 +01004516error:
Radek Krejci95710c92019-02-11 15:49:55 +01004517 return ret;
4518}
4519
Radek Krejcia3045382018-11-22 14:30:31 +01004520/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01004521 * @brief Compile the given YANG submodule into the main module.
4522 * @param[in] ctx Compile context
4523 * @param[in] inc Include structure from the main module defining the submodule.
4524 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
4525 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4526 */
4527LY_ERR
4528lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
4529{
4530 unsigned int u;
4531 LY_ERR ret = LY_SUCCESS;
4532 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004533 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01004534 struct lysc_module *mainmod = ctx->mod->compiled;
4535
Radek Krejci0af46292019-01-11 16:02:31 +01004536 if (!mainmod->mod->off_features) {
4537 /* features are compiled directly into the compiled module structure,
4538 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
4539 * The features compilation is finished in the main module (lys_compile()). */
4540 ret = lys_feature_precompile(ctx->ctx, submod->features,
4541 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
4542 LY_CHECK_GOTO(ret, error);
4543 }
4544
Radek Krejcid05cbd92018-12-05 14:26:40 +01004545 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
4546
4547error:
4548 return ret;
4549}
4550
Radek Krejci19a96102018-11-15 13:38:09 +01004551LY_ERR
4552lys_compile(struct lys_module *mod, int options)
4553{
4554 struct lysc_ctx ctx = {0};
4555 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01004556 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01004557 struct lysp_module *sp;
4558 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01004559 struct lysp_augment **augments = NULL;
4560 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01004561 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01004562 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01004563
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004564 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01004565
4566 if (!mod->implemented) {
4567 /* just imported modules are not compiled */
4568 return LY_SUCCESS;
4569 }
4570
Radek Krejci19a96102018-11-15 13:38:09 +01004571 sp = mod->parsed;
4572
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004573 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01004574 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01004575 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01004576
4577 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004578 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
4579 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01004580
Radek Krejci19a96102018-11-15 13:38:09 +01004581 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01004582 LY_ARRAY_FOR(sp->includes, u) {
4583 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
4584 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
4585 }
Radek Krejci0af46292019-01-11 16:02:31 +01004586 if (mod->off_features) {
4587 /* there is already precompiled array of features */
4588 mod_c->features = mod->off_features;
4589 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01004590 } else {
4591 /* features are compiled directly into the compiled module structure,
4592 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
4593 ret = lys_feature_precompile(ctx.ctx, sp->features, &mod_c->features);
4594 LY_CHECK_GOTO(ret, error);
4595 }
4596 /* finish feature compilation, not only for the main module, but also for the submodules.
4597 * Due to possible forward references, it must be done when all the features (including submodules)
4598 * are present. */
4599 LY_ARRAY_FOR(sp->features, u) {
4600 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], options, mod_c->features);
4601 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
4602 }
4603 LY_ARRAY_FOR(sp->includes, v) {
4604 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
4605 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], options, mod_c->features);
4606 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
4607 }
4608 }
4609
Radek Krejcid05cbd92018-12-05 14:26:40 +01004610 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01004611 if (sp->identities) {
4612 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
4613 }
4614
Radek Krejci95710c92019-02-11 15:49:55 +01004615 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01004616 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004617 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01004618 LY_CHECK_GOTO(ret, error);
4619 }
Radek Krejci95710c92019-02-11 15:49:55 +01004620
4621 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcif12a1f02019-02-11 16:42:08 +01004622 ret = lys_compile_augment_sort(&ctx, sp, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01004623 LY_CHECK_GOTO(ret, error);
4624 LY_ARRAY_FOR(augments, u) {
4625 ret = lys_compile_augment(&ctx, augments[u], options, NULL);
4626 LY_CHECK_GOTO(ret, error);
4627 }
Radek Krejci19a96102018-11-15 13:38:09 +01004628 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
4629 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
4630
4631 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
4632
Radek Krejcia3045382018-11-22 14:30:31 +01004633 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01004634 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
4635 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
4636 * 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 +01004637 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01004638 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01004639 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
4640 if (type->basetype == LY_TYPE_LEAFREF) {
4641 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01004642 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 +01004643 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01004644 } else if (type->basetype == LY_TYPE_UNION) {
4645 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
4646 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
4647 /* validate the path */
4648 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
4649 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
4650 LY_CHECK_GOTO(ret, error);
4651 }
4652 }
Radek Krejcia3045382018-11-22 14:30:31 +01004653 }
4654 }
4655 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01004656 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01004657 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01004658 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
4659 if (type->basetype == LY_TYPE_LEAFREF) {
4660 /* store pointer to the real type */
4661 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
4662 typeiter->basetype == LY_TYPE_LEAFREF;
4663 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
4664 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01004665 } else if (type->basetype == LY_TYPE_UNION) {
4666 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
4667 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
4668 /* store pointer to the real type */
4669 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
4670 typeiter->basetype == LY_TYPE_LEAFREF;
4671 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
4672 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
4673 }
4674 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01004675 }
4676 }
4677 }
Radek Krejcia3045382018-11-22 14:30:31 +01004678 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004679 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01004680 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01004681
Radek Krejci19a96102018-11-15 13:38:09 +01004682 if (options & LYSC_OPT_FREE_SP) {
4683 lysp_module_free(mod->parsed);
4684 ((struct lys_module*)mod)->parsed = NULL;
4685 }
4686
Radek Krejci95710c92019-02-11 15:49:55 +01004687 if (!(options & LYSC_OPT_INTERNAL)) {
4688 /* remove flag of the modules implemented by dependency */
4689 for (u = 0; u < ctx.ctx->list.count; ++u) {
4690 m = ctx.ctx->list.objs[u];
4691 if (m->implemented == 2) {
4692 m->implemented = 1;
4693 }
4694 }
4695 }
4696
Radek Krejci19a96102018-11-15 13:38:09 +01004697 ((struct lys_module*)mod)->compiled = mod_c;
4698 return LY_SUCCESS;
4699
4700error:
Radek Krejci95710c92019-02-11 15:49:55 +01004701 lys_feature_precompile_revert(&ctx, mod);
Radek Krejcia3045382018-11-22 14:30:31 +01004702 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01004703 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01004704 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01004705 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01004706 mod->compiled = NULL;
4707
4708 /* revert compilation of modules implemented by dependency */
4709 for (u = 0; u < ctx.ctx->list.count; ++u) {
4710 m = ctx.ctx->list.objs[u];
4711 if (m->implemented == 2) {
4712 /* revert features list to the precompiled state */
4713 lys_feature_precompile_revert(&ctx, m);
4714 /* mark module as imported-only / not-implemented */
4715 m->implemented = 0;
4716 /* free the compiled version of the module */
4717 lysc_module_free(m->compiled, NULL);
4718 m->compiled = NULL;
4719 }
4720 }
4721
Radek Krejci19a96102018-11-15 13:38:09 +01004722 return ret;
4723}