blob: ff7a94f060158f1429a2a2806cfab1b089ca1e68 [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>
18#include <dirent.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <linux/limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#include "libyang.h"
29#include "context.h"
30#include "tree_schema_internal.h"
31#include "xpath.h"
32
33/**
34 * @brief Duplicate string into dictionary
35 * @param[in] CTX libyang context of the dictionary.
36 * @param[in] ORIG String to duplicate.
37 * @param[out] DUP Where to store the result.
38 */
39#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
40
41#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
42 if (ARRAY_P) { \
43 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
44 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
45 LY_ARRAY_INCREMENT(ARRAY_C); \
46 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER]); \
47 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
48 } \
49 }
50
51#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
52 if (MEMBER_P) { \
53 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
54 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
55 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
56 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
57 }
58
Radek Krejci19a96102018-11-15 13:38:09 +010059static struct lysc_ext_instance *
60lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
61{
62 /* TODO */
63 (void) ctx;
64 (void) orig;
65 return NULL;
66}
67
68static struct lysc_pattern*
69lysc_pattern_dup(struct lysc_pattern *orig)
70{
71 ++orig->refcount;
72 return orig;
73}
74
75static struct lysc_pattern**
76lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
77{
78 struct lysc_pattern **dup;
79 unsigned int u;
80
81 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
82 LY_ARRAY_FOR(orig, u) {
83 dup[u] = lysc_pattern_dup(orig[u]);
84 LY_ARRAY_INCREMENT(dup);
85 }
86 return dup;
87}
88
89struct lysc_range*
90lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
91{
92 struct lysc_range *dup;
93 LY_ERR ret;
94
95 dup = calloc(1, sizeof *dup);
96 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
97 if (orig->parts) {
98 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
99 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
100 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
101 }
102 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
103 DUP_STRING(ctx, orig->emsg, dup->emsg);
104 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
105
106 return dup;
107cleanup:
108 free(dup);
109 (void) ret; /* set but not used due to the return type */
110 return NULL;
111}
112
113struct iff_stack {
114 int size;
115 int index; /* first empty item */
116 uint8_t *stack;
117};
118
119static LY_ERR
120iff_stack_push(struct iff_stack *stack, uint8_t value)
121{
122 if (stack->index == stack->size) {
123 stack->size += 4;
124 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
125 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
126 }
127 stack->stack[stack->index++] = value;
128 return LY_SUCCESS;
129}
130
131static uint8_t
132iff_stack_pop(struct iff_stack *stack)
133{
134 stack->index--;
135 return stack->stack[stack->index];
136}
137
138static void
139iff_stack_clean(struct iff_stack *stack)
140{
141 stack->size = 0;
142 free(stack->stack);
143}
144
145static void
146iff_setop(uint8_t *list, uint8_t op, int pos)
147{
148 uint8_t *item;
149 uint8_t mask = 3;
150
151 assert(pos >= 0);
152 assert(op <= 3); /* max 2 bits */
153
154 item = &list[pos / 4];
155 mask = mask << 2 * (pos % 4);
156 *item = (*item) & ~mask;
157 *item = (*item) | (op << 2 * (pos % 4));
158}
159
160#define LYS_IFF_LP 0x04 /* ( */
161#define LYS_IFF_RP 0x08 /* ) */
162
163static struct lysc_feature *
164lysc_feature_find(struct lysc_module *mod, const char *name, size_t len)
165{
166 size_t i;
167 struct lysc_feature *f;
168
169 for (i = 0; i < len; ++i) {
170 if (name[i] == ':') {
171 /* we have a prefixed feature */
172 mod = lysc_module_find_prefix(mod, name, i);
173 LY_CHECK_RET(!mod, NULL);
174
175 name = &name[i + 1];
176 len = len - i - 1;
177 }
178 }
179
180 /* we have the correct module, get the feature */
181 LY_ARRAY_FOR(mod->features, i) {
182 f = &mod->features[i];
183 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
184 return f;
185 }
186 }
187
188 return NULL;
189}
190
191static LY_ERR
192lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
193{
194 const char *name;
195 unsigned int u;
196 const struct lys_module *mod;
197 struct lysp_ext *edef = NULL;
198
199 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
200 ext->insubstmt = ext_p->insubstmt;
201 ext->insubstmt_index = ext_p->insubstmt_index;
202
203 /* get module where the extension definition should be placed */
204 for (u = 0; ext_p->name[u] != ':'; ++u);
205 mod = lys_module_find_prefix(ctx->mod, ext_p->name, u);
206 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
207 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
208 LY_EVALID);
209 LY_CHECK_ERR_RET(!mod->parsed->extensions,
210 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
211 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
212 ext_p->name, mod->parsed->name),
213 LY_EVALID);
214 name = &ext_p->name[u + 1];
215 /* find the extension definition there */
216 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
217 if (!strcmp(name, mod->parsed->extensions[u].name)) {
218 edef = &mod->parsed->extensions[u];
219 break;
220 }
221 }
222 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
223 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
224 LY_EVALID);
225 /* TODO plugins */
226
227 return LY_SUCCESS;
228}
229
230static LY_ERR
231lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
232{
233 const char *c = *value;
234 int r, rc = EXIT_FAILURE;
235 int i, j, last_not, checkversion = 0;
236 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
237 uint8_t op;
238 struct iff_stack stack = {0, 0, NULL};
239 struct lysc_feature *f;
240
241 assert(c);
242
243 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
244 for (i = j = last_not = 0; c[i]; i++) {
245 if (c[i] == '(') {
246 j++;
247 checkversion = 1;
248 continue;
249 } else if (c[i] == ')') {
250 j--;
251 continue;
252 } else if (isspace(c[i])) {
253 checkversion = 1;
254 continue;
255 }
256
257 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
258 if (c[i + r] == '\0') {
259 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
260 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
261 return LY_EVALID;
262 } else if (!isspace(c[i + r])) {
263 /* feature name starting with the not/and/or */
264 last_not = 0;
265 f_size++;
266 } else if (c[i] == 'n') { /* not operation */
267 if (last_not) {
268 /* double not */
269 expr_size = expr_size - 2;
270 last_not = 0;
271 } else {
272 last_not = 1;
273 }
274 } else { /* and, or */
275 f_exp++;
276 /* not a not operation */
277 last_not = 0;
278 }
279 i += r;
280 } else {
281 f_size++;
282 last_not = 0;
283 }
284 expr_size++;
285
286 while (!isspace(c[i])) {
287 if (!c[i] || c[i] == ')') {
288 i--;
289 break;
290 }
291 i++;
292 }
293 }
294 if (j || f_exp != f_size) {
295 /* not matching count of ( and ) */
296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
297 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
298 return LY_EVALID;
299 }
300
301 if (checkversion || expr_size > 1) {
302 /* check that we have 1.1 module */
303 if (ctx->mod->compiled->version != LYS_VERSION_1_1) {
304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
305 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
306 return LY_EVALID;
307 }
308 }
309
310 /* allocate the memory */
311 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
312 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
313 stack.stack = malloc(expr_size * sizeof *stack.stack);
314 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
315
316 stack.size = expr_size;
317 f_size--; expr_size--; /* used as indexes from now */
318
319 for (i--; i >= 0; i--) {
320 if (c[i] == ')') {
321 /* push it on stack */
322 iff_stack_push(&stack, LYS_IFF_RP);
323 continue;
324 } else if (c[i] == '(') {
325 /* pop from the stack into result all operators until ) */
326 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
327 iff_setop(iff->expr, op, expr_size--);
328 }
329 continue;
330 } else if (isspace(c[i])) {
331 continue;
332 }
333
334 /* end of operator or operand -> find beginning and get what is it */
335 j = i + 1;
336 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
337 i--;
338 }
339 i++; /* go back by one step */
340
341 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
342 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
343 /* double not */
344 iff_stack_pop(&stack);
345 } else {
346 /* not has the highest priority, so do not pop from the stack
347 * as in case of AND and OR */
348 iff_stack_push(&stack, LYS_IFF_NOT);
349 }
350 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
351 /* as for OR - pop from the stack all operators with the same or higher
352 * priority and store them to the result, then push the AND to the stack */
353 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
354 op = iff_stack_pop(&stack);
355 iff_setop(iff->expr, op, expr_size--);
356 }
357 iff_stack_push(&stack, LYS_IFF_AND);
358 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
359 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
360 op = iff_stack_pop(&stack);
361 iff_setop(iff->expr, op, expr_size--);
362 }
363 iff_stack_push(&stack, LYS_IFF_OR);
364 } else {
365 /* feature name, length is j - i */
366
367 /* add it to the expression */
368 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
369
370 /* now get the link to the feature definition */
371 f = lysc_feature_find(ctx->mod->compiled, &c[i], j - i);
372 LY_CHECK_ERR_GOTO(!f,
373 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
374 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
375 rc = LY_EVALID,
376 error)
377 iff->features[f_size] = f;
378 LY_ARRAY_INCREMENT(iff->features);
379 f_size--;
380 }
381 }
382 while (stack.index) {
383 op = iff_stack_pop(&stack);
384 iff_setop(iff->expr, op, expr_size--);
385 }
386
387 if (++expr_size || ++f_size) {
388 /* not all expected operators and operands found */
389 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
390 "Invalid value \"%s\" of if-feature - processing error.", *value);
391 rc = LY_EINT;
392 } else {
393 rc = LY_SUCCESS;
394 }
395
396error:
397 /* cleanup */
398 iff_stack_clean(&stack);
399
400 return rc;
401}
402
403static LY_ERR
404lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when)
405{
406 unsigned int u;
407 LY_ERR ret = LY_SUCCESS;
408
409 when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
410 LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done);
411 COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done);
412
413done:
414 return ret;
415}
416
417static LY_ERR
418lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
419{
420 unsigned int u;
421 LY_ERR ret = LY_SUCCESS;
422
423 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
424 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
425
426 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
427 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
428 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
429
430done:
431 return ret;
432}
433
434static LY_ERR
435lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
436{
437 unsigned int u;
438 struct lys_module *mod = NULL;
439 struct lysc_module *comp;
440 LY_ERR ret = LY_SUCCESS;
441
442 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
443 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
444 imp->module = imp_p->module;
445
446 /* make sure that we have both versions (lysp_ and lysc_) of the imported module. To import groupings or
447 * typedefs, the lysp_ is needed. To augment or deviate imported module, we need the lysc_ structure */
448 if (!imp->module->parsed) {
449 comp = imp->module->compiled;
450 /* try to get filepath from the compiled version */
451 if (comp->filepath) {
452 mod = (struct lys_module*)lys_parse_path(ctx->ctx, comp->filepath,
453 !strcmp(&comp->filepath[strlen(comp->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
454 if (mod != imp->module) {
455 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
456 comp->filepath, comp->name);
457 mod = NULL;
458 }
459 }
460 if (!mod) {
461 if (lysp_load_module(ctx->ctx, comp->name, comp->revision, 0, 1, &mod)) {
462 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
463 comp->name, ctx->mod->compiled->name);
464 return LY_ENOTFOUND;
465 }
466 }
467 } else if (!imp->module->compiled) {
468 return lys_compile(imp->module, options);
469 }
470
471done:
472 return ret;
473}
474
475static LY_ERR
476lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *ident)
477{
478 unsigned int u;
479 LY_ERR ret = LY_SUCCESS;
480
481 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
482 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
483 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
484 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
485 ident->flags = ident_p->flags;
486
487done:
488 return ret;
489}
490
Radek Krejcia3045382018-11-22 14:30:31 +0100491/**
492 * @brief Find and process the referenced base identities from another identity or identityref
493 *
494 * For bases in identity se backlinks to them from the base identities. For identityref, store
495 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
496 * to distinguish these two use cases.
497 *
498 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
499 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
500 * @param[in] ident Referencing identity to work with.
501 * @param[in] bases Array of bases of identityref to fill in.
502 * @return LY_ERR value.
503 */
Radek Krejci19a96102018-11-15 13:38:09 +0100504static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100505lys_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 +0100506{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100507 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100508 const char *s, *name;
509 struct lysc_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100510 struct lysc_ident **idref;
511
512 assert(ident || bases);
513
514 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) {
515 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
516 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
517 return LY_EVALID;
518 }
519
520 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
521 s = strchr(bases_p[u], ':');
522 if (s) {
523 /* prefixed identity */
524 name = &s[1];
525 mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]);
526 } else {
527 name = bases_p[u];
528 mod = ctx->mod->compiled;
529 }
530 if (!mod) {
531 if (ident) {
532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
533 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
534 } else {
535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
536 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
537 }
538 return LY_EVALID;
539 }
540 idref = NULL;
541 if (mod->identities) {
542 for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) {
543 if (!strcmp(name, mod->identities[v].name)) {
544 if (ident) {
545 /* we have match! store the backlink */
546 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
547 *idref = ident;
548 } else {
549 /* we have match! store the found identity */
550 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
551 *idref = &mod->identities[v];
552 }
553 break;
554 }
555 }
556 }
557 if (!idref || !(*idref)) {
558 if (ident) {
559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
560 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
561 } else {
562 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
563 "Unable to find base (%s) of identityref.", bases_p[u]);
564 }
565 return LY_EVALID;
566 }
567 }
568 return LY_SUCCESS;
569}
570
Radek Krejcia3045382018-11-22 14:30:31 +0100571/**
572 * @brief For the given array of identities, set the backlinks from all their base identities.
573 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
574 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
575 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
576 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
577 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100578static LY_ERR
579lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
580{
581 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100582
583 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
584 if (!idents_p[i].bases) {
585 continue;
586 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100587 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100588 }
589 return LY_SUCCESS;
590}
591
Radek Krejcia3045382018-11-22 14:30:31 +0100592/**
593 * @brief Create compiled feature structure.
594 * @param[in] ctx Compile context.
595 * @param[in] feature_p Parsed feature definition to compile.
596 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
597 * @param[in,out] feature Compiled feature structure to fill.
598 * @return LY_ERR value.
599 */
Radek Krejci19a96102018-11-15 13:38:09 +0100600static LY_ERR
601lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature)
602{
603 unsigned int u, v;
604 LY_ERR ret = LY_SUCCESS;
605 struct lysc_feature **df;
606
607 DUP_STRING(ctx->ctx, feature_p->name, feature->name);
608 feature->flags = feature_p->flags;
609
610 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
611 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
612 if (feature->iffeatures) {
613 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
614 if (feature->iffeatures[u].features) {
615 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
616 /* add itself into the dependants list */
617 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
618 *df = feature;
619 }
620 /* TODO check for circular dependency */
621 }
622 }
623 }
624done:
625 return ret;
626}
627
Radek Krejcia3045382018-11-22 14:30:31 +0100628/**
629 * @brief Validate and normalize numeric value from a range definition.
630 * @param[in] ctx Compile context.
631 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
632 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
633 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
634 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
635 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
636 * @param[in] value String value of the range boundary.
637 * @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.
638 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
639 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
640 */
Radek Krejci19a96102018-11-15 13:38:09 +0100641static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100642range_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 +0100643{
Radek Krejci6cba4292018-11-15 17:33:29 +0100644 size_t fraction = 0, size;
645
Radek Krejci19a96102018-11-15 13:38:09 +0100646 *len = 0;
647
648 assert(value);
649 /* parse value */
650 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
651 return LY_EVALID;
652 }
653
654 if ((value[*len] == '-') || (value[*len] == '+')) {
655 ++(*len);
656 }
657
658 while (isdigit(value[*len])) {
659 ++(*len);
660 }
661
662 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100663 if (basetype == LY_TYPE_DEC64) {
664 goto decimal;
665 } else {
666 *valcopy = strndup(value, *len);
667 return LY_SUCCESS;
668 }
Radek Krejci19a96102018-11-15 13:38:09 +0100669 }
670 fraction = *len;
671
672 ++(*len);
673 while (isdigit(value[*len])) {
674 ++(*len);
675 }
676
Radek Krejci6cba4292018-11-15 17:33:29 +0100677 if (basetype == LY_TYPE_DEC64) {
678decimal:
679 assert(frdigits);
680 if (*len - 1 - fraction > frdigits) {
681 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
682 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
683 *len, value, frdigits);
684 return LY_EINVAL;
685 }
686 if (fraction) {
687 size = (*len) + (frdigits - ((*len) - 1 - fraction));
688 } else {
689 size = (*len) + frdigits + 1;
690 }
691 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100692 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
693
Radek Krejci6cba4292018-11-15 17:33:29 +0100694 (*valcopy)[size - 1] = '\0';
695 if (fraction) {
696 memcpy(&(*valcopy)[0], &value[0], fraction);
697 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
698 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
699 } else {
700 memcpy(&(*valcopy)[0], &value[0], *len);
701 memset(&(*valcopy)[*len], '0', frdigits);
702 }
Radek Krejci19a96102018-11-15 13:38:09 +0100703 }
704 return LY_SUCCESS;
705}
706
Radek Krejcia3045382018-11-22 14:30:31 +0100707/**
708 * @brief Check that values in range are in ascendant order.
709 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +0100710 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
711 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +0100712 * @param[in] value Current value to check.
713 * @param[in] prev_value The last seen value.
714 * @return LY_SUCCESS or LY_EEXIST for invalid order.
715 */
Radek Krejci19a96102018-11-15 13:38:09 +0100716static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +0100717range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +0100718{
719 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +0100720 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100721 return LY_EEXIST;
722 }
723 } else {
Radek Krejci5969f272018-11-23 10:03:58 +0100724 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100725 return LY_EEXIST;
726 }
727 }
728 return LY_SUCCESS;
729}
730
Radek Krejcia3045382018-11-22 14:30:31 +0100731/**
732 * @brief Set min/max value of the range part.
733 * @param[in] ctx Compile context.
734 * @param[in] part Range part structure to fill.
735 * @param[in] max Flag to distinguish if storing min or max value.
736 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
737 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
738 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
739 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
740 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +0100741 * @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 +0100742 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
743 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
744 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
745 * frdigits value), LY_EMEM.
746 */
Radek Krejci19a96102018-11-15 13:38:09 +0100747static LY_ERR
748range_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 +0100749 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100750{
751 LY_ERR ret = LY_SUCCESS;
752 char *valcopy = NULL;
753 size_t len;
754
755 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100756 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +0100757 LY_CHECK_GOTO(ret, finalize);
758 }
759 if (!valcopy && base_range) {
760 if (max) {
761 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
762 } else {
763 part->min_64 = base_range->parts[0].min_64;
764 }
765 if (!first) {
766 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
767 }
768 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +0100769 }
770
771 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100772 case LY_TYPE_INT8: /* range */
773 if (valcopy) {
774 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
775 } else if (max) {
776 part->max_64 = INT64_C(127);
777 } else {
778 part->min_64 = INT64_C(-128);
779 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100780 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100781 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100782 }
783 break;
784 case LY_TYPE_INT16: /* range */
785 if (valcopy) {
786 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
787 } else if (max) {
788 part->max_64 = INT64_C(32767);
789 } else {
790 part->min_64 = INT64_C(-32768);
791 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100792 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100793 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100794 }
795 break;
796 case LY_TYPE_INT32: /* range */
797 if (valcopy) {
798 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
799 } else if (max) {
800 part->max_64 = INT64_C(2147483647);
801 } else {
802 part->min_64 = INT64_C(-2147483648);
803 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100804 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100805 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100806 }
807 break;
808 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +0100809 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100810 if (valcopy) {
811 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
812 max ? &part->max_64 : &part->min_64);
813 } else if (max) {
814 part->max_64 = INT64_C(9223372036854775807);
815 } else {
816 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
817 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100818 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100819 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100820 }
821 break;
822 case LY_TYPE_UINT8: /* range */
823 if (valcopy) {
824 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
825 } else if (max) {
826 part->max_u64 = UINT64_C(255);
827 } else {
828 part->min_u64 = UINT64_C(0);
829 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100830 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100831 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100832 }
833 break;
834 case LY_TYPE_UINT16: /* range */
835 if (valcopy) {
836 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
837 } else if (max) {
838 part->max_u64 = UINT64_C(65535);
839 } else {
840 part->min_u64 = UINT64_C(0);
841 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100842 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100843 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100844 }
845 break;
846 case LY_TYPE_UINT32: /* range */
847 if (valcopy) {
848 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
849 } else if (max) {
850 part->max_u64 = UINT64_C(4294967295);
851 } else {
852 part->min_u64 = UINT64_C(0);
853 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100854 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100855 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100856 }
857 break;
858 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100859 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +0100860 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +0100861 if (valcopy) {
862 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
863 } else if (max) {
864 part->max_u64 = UINT64_C(18446744073709551615);
865 } else {
866 part->min_u64 = UINT64_C(0);
867 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100868 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100869 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100870 }
871 break;
872 default:
873 LOGINT(ctx->ctx);
874 ret = LY_EINT;
875 }
876
Radek Krejci5969f272018-11-23 10:03:58 +0100877finalize:
Radek Krejci19a96102018-11-15 13:38:09 +0100878 if (ret == LY_EDENIED) {
879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
880 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
881 length_restr ? "length" : "range", valcopy ? valcopy : *value);
882 } else if (ret == LY_EVALID) {
883 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
884 "Invalid %s restriction - invalid value \"%s\".",
885 length_restr ? "length" : "range", valcopy ? valcopy : *value);
886 } else if (ret == LY_EEXIST) {
887 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
888 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100889 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +0100890 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +0100891 } else if (!ret && value) {
892 *value = *value + len;
893 }
894 free(valcopy);
895 return ret;
896}
897
Radek Krejcia3045382018-11-22 14:30:31 +0100898/**
899 * @brief Compile the parsed range restriction.
900 * @param[in] ctx Compile context.
901 * @param[in] range_p Parsed range structure to compile.
902 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
903 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
904 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
905 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
906 * range restriction must be more restrictive than the base_range.
907 * @param[in,out] range Pointer to the created current range structure.
908 * @return LY_ERR value.
909 */
Radek Krejci19a96102018-11-15 13:38:09 +0100910static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100911lys_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 +0100912 struct lysc_range *base_range, struct lysc_range **range)
913{
914 LY_ERR ret = LY_EVALID;
915 const char *expr;
916 struct lysc_range_part *parts = NULL, *part;
917 int range_expected = 0, uns;
918 unsigned int parts_done = 0, u, v;
919
920 assert(range);
921 assert(range_p);
922
923 expr = range_p->arg;
924 while(1) {
925 if (isspace(*expr)) {
926 ++expr;
927 } else if (*expr == '\0') {
928 if (range_expected) {
929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
930 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
931 length_restr ? "length" : "range", range_p->arg);
932 goto cleanup;
933 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
935 "Invalid %s restriction - unexpected end of the expression (%s).",
936 length_restr ? "length" : "range", range_p->arg);
937 goto cleanup;
938 }
939 parts_done++;
940 break;
941 } else if (!strncmp(expr, "min", 3)) {
942 if (parts) {
943 /* min cannot be used elsewhere than in the first part */
944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
945 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
946 expr - range_p->arg, range_p->arg);
947 goto cleanup;
948 }
949 expr += 3;
950
951 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +0100952 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 +0100953 part->max_64 = part->min_64;
954 } else if (*expr == '|') {
955 if (!parts || range_expected) {
956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
957 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
958 goto cleanup;
959 }
960 expr++;
961 parts_done++;
962 /* process next part of the expression */
963 } else if (!strncmp(expr, "..", 2)) {
964 expr += 2;
965 while (isspace(*expr)) {
966 expr++;
967 }
968 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
969 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
970 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
971 goto cleanup;
972 }
973 /* continue expecting the upper boundary */
974 range_expected = 1;
975 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
976 /* number */
977 if (range_expected) {
978 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100979 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 +0100980 range_expected = 0;
981 } else {
982 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
983 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 +0100984 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100985 part->max_64 = part->min_64;
986 }
987
988 /* continue with possible another expression part */
989 } else if (!strncmp(expr, "max", 3)) {
990 expr += 3;
991 while (isspace(*expr)) {
992 expr++;
993 }
994 if (*expr != '\0') {
995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
996 length_restr ? "length" : "range", expr);
997 goto cleanup;
998 }
999 if (range_expected) {
1000 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001001 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 +01001002 range_expected = 0;
1003 } else {
1004 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1005 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 +01001006 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001007 part->min_64 = part->max_64;
1008 }
1009 } else {
1010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1011 length_restr ? "length" : "range", expr);
1012 goto cleanup;
1013 }
1014 }
1015
1016 /* check with the previous range/length restriction */
1017 if (base_range) {
1018 switch (basetype) {
1019 case LY_TYPE_BINARY:
1020 case LY_TYPE_UINT8:
1021 case LY_TYPE_UINT16:
1022 case LY_TYPE_UINT32:
1023 case LY_TYPE_UINT64:
1024 case LY_TYPE_STRING:
1025 uns = 1;
1026 break;
1027 case LY_TYPE_DEC64:
1028 case LY_TYPE_INT8:
1029 case LY_TYPE_INT16:
1030 case LY_TYPE_INT32:
1031 case LY_TYPE_INT64:
1032 uns = 0;
1033 break;
1034 default:
1035 LOGINT(ctx->ctx);
1036 ret = LY_EINT;
1037 goto cleanup;
1038 }
1039 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1040 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1041 goto baseerror;
1042 }
1043 /* current lower bound is not lower than the base */
1044 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1045 /* base has single value */
1046 if (base_range->parts[v].min_64 == parts[u].min_64) {
1047 /* both lower bounds are the same */
1048 if (parts[u].min_64 != parts[u].max_64) {
1049 /* current continues with a range */
1050 goto baseerror;
1051 } else {
1052 /* equal single values, move both forward */
1053 ++v;
1054 continue;
1055 }
1056 } else {
1057 /* base is single value lower than current range, so the
1058 * value from base range is removed in the current,
1059 * move only base and repeat checking */
1060 ++v;
1061 --u;
1062 continue;
1063 }
1064 } else {
1065 /* base is the range */
1066 if (parts[u].min_64 == parts[u].max_64) {
1067 /* current is a single value */
1068 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1069 /* current is behind the base range, so base range is omitted,
1070 * move the base and keep the current for further check */
1071 ++v;
1072 --u;
1073 } /* else it is within the base range, so move the current, but keep the base */
1074 continue;
1075 } else {
1076 /* both are ranges - check the higher bound, the lower was already checked */
1077 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1078 /* higher bound is higher than the current higher bound */
1079 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1080 /* but the current lower bound is also higher, so the base range is omitted,
1081 * continue with the same current, but move the base */
1082 --u;
1083 ++v;
1084 continue;
1085 }
1086 /* current range starts within the base range but end behind it */
1087 goto baseerror;
1088 } else {
1089 /* current range is smaller than the base,
1090 * move current, but stay with the base */
1091 continue;
1092 }
1093 }
1094 }
1095 }
1096 if (u != parts_done) {
1097baseerror:
1098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1099 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1100 length_restr ? "length" : "range", range_p->arg);
1101 goto cleanup;
1102 }
1103 }
1104
1105 if (!(*range)) {
1106 *range = calloc(1, sizeof **range);
1107 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1108 }
1109
1110 if (range_p->eapptag) {
1111 lydict_remove(ctx->ctx, (*range)->eapptag);
1112 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1113 }
1114 if (range_p->emsg) {
1115 lydict_remove(ctx->ctx, (*range)->emsg);
1116 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1117 }
1118 /* extensions are taken only from the last range by the caller */
1119
1120 (*range)->parts = parts;
1121 parts = NULL;
1122 ret = LY_SUCCESS;
1123cleanup:
1124 /* TODO clean up */
1125 LY_ARRAY_FREE(parts);
1126
1127 return ret;
1128}
1129
1130/**
1131 * @brief Checks pattern syntax.
1132 *
Radek Krejcia3045382018-11-22 14:30:31 +01001133 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001134 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001135 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1136 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001137 */
1138static LY_ERR
1139lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1140{
1141 int idx, idx2, start, end, err_offset, count;
1142 char *perl_regex, *ptr;
1143 const char *err_msg, *orig_ptr;
1144 pcre *precomp;
1145#define URANGE_LEN 19
1146 char *ublock2urange[][2] = {
1147 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1148 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1149 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1150 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1151 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1152 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1153 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1154 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1155 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1156 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1157 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1158 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1159 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1160 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1161 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1162 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1163 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1164 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1165 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1166 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1167 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1168 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1169 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1170 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1171 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1172 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1173 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1174 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1175 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1176 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1177 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1178 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1179 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1180 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1181 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1182 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1183 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1184 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1185 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1186 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1187 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1188 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1189 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1190 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1191 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1192 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1193 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1194 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1195 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1196 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1197 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1198 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1199 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1200 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1201 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1202 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1203 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1204 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1205 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1206 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1207 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1208 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1209 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1210 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1211 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1212 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1213 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1214 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1215 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1216 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1217 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1218 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1219 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1220 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1221 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1222 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1223 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1224 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1225 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1226 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1227 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1228 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1229 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1230 {NULL, NULL}
1231 };
1232
1233 /* adjust the expression to a Perl equivalent
1234 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1235
1236 /* we need to replace all "$" with "\$", count them now */
1237 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1238
1239 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1240 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1241 perl_regex[0] = '\0';
1242
1243 ptr = perl_regex;
1244
1245 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1246 /* we will add line-end anchoring */
1247 ptr[0] = '(';
1248 ++ptr;
1249 }
1250
1251 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1252 if (orig_ptr[0] == '$') {
1253 ptr += sprintf(ptr, "\\$");
1254 } else if (orig_ptr[0] == '^') {
1255 ptr += sprintf(ptr, "\\^");
1256 } else {
1257 ptr[0] = orig_ptr[0];
1258 ++ptr;
1259 }
1260 }
1261
1262 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1263 ptr += sprintf(ptr, ")$");
1264 } else {
1265 ptr[0] = '\0';
1266 ++ptr;
1267 }
1268
1269 /* substitute Unicode Character Blocks with exact Character Ranges */
1270 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1271 start = ptr - perl_regex;
1272
1273 ptr = strchr(ptr, '}');
1274 if (!ptr) {
1275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1276 pattern, perl_regex + start + 2, "unterminated character property");
1277 free(perl_regex);
1278 return LY_EVALID;
1279 }
1280 end = (ptr - perl_regex) + 1;
1281
1282 /* need more space */
1283 if (end - start < URANGE_LEN) {
1284 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1285 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1286 }
1287
1288 /* find our range */
1289 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1290 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1291 break;
1292 }
1293 }
1294 if (!ublock2urange[idx][0]) {
1295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1296 pattern, perl_regex + start + 5, "unknown block name");
1297 free(perl_regex);
1298 return LY_EVALID;
1299 }
1300
1301 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1302 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1303 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1304 ++count;
1305 }
1306 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1307 --count;
1308 }
1309 }
1310 if (count) {
1311 /* skip brackets */
1312 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1313 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1314 } else {
1315 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1316 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1317 }
1318 }
1319
1320 /* must return 0, already checked during parsing */
1321 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1322 &err_msg, &err_offset, NULL);
1323 if (!precomp) {
1324 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1325 free(perl_regex);
1326 return LY_EVALID;
1327 }
1328 free(perl_regex);
1329
1330 if (pcre_precomp) {
1331 *pcre_precomp = precomp;
1332 } else {
1333 free(precomp);
1334 }
1335
1336 return LY_SUCCESS;
1337
1338#undef URANGE_LEN
1339}
1340
Radek Krejcia3045382018-11-22 14:30:31 +01001341/**
1342 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1343 * @param[in] ctx Compile context.
1344 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1345 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1346 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1347 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1348 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1349 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1350 */
Radek Krejci19a96102018-11-15 13:38:09 +01001351static LY_ERR
1352lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1353 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1354{
1355 struct lysc_pattern **pattern;
1356 unsigned int u, v;
1357 const char *err_msg;
1358 LY_ERR ret = LY_SUCCESS;
1359
1360 /* first, copy the patterns from the base type */
1361 if (base_patterns) {
1362 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1363 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1364 }
1365
1366 LY_ARRAY_FOR(patterns_p, u) {
1367 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1368 *pattern = calloc(1, sizeof **pattern);
1369 ++(*pattern)->refcount;
1370
1371 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1372 LY_CHECK_RET(ret);
1373 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1374 if (err_msg) {
1375 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1376 }
1377
1378 if (patterns_p[u].arg[0] == 0x15) {
1379 (*pattern)->inverted = 1;
1380 }
1381 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1382 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
1383 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1384 options, v, lys_compile_ext, ret, done);
1385 }
1386done:
1387 return ret;
1388}
1389
Radek Krejcia3045382018-11-22 14:30:31 +01001390/**
1391 * @brief map of the possible restrictions combination for the specific built-in type.
1392 */
Radek Krejci19a96102018-11-15 13:38:09 +01001393static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1394 0 /* LY_TYPE_UNKNOWN */,
1395 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001396 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1397 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1398 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1399 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1400 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001401 LYS_SET_BIT /* LY_TYPE_BITS */,
1402 0 /* LY_TYPE_BOOL */,
1403 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1404 0 /* LY_TYPE_EMPTY */,
1405 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1406 LYS_SET_BASE /* LY_TYPE_IDENT */,
1407 LYS_SET_REQINST /* LY_TYPE_INST */,
1408 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001409 LYS_SET_TYPE /* LY_TYPE_UNION */,
1410 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001411 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001412 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001413 LYS_SET_RANGE /* LY_TYPE_INT64 */
1414};
1415
1416/**
1417 * @brief stringification of the YANG built-in data types
1418 */
1419const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1420 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1421 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001422};
1423
Radek Krejcia3045382018-11-22 14:30:31 +01001424/**
1425 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1426 * @param[in] ctx Compile context.
1427 * @param[in] enums_p Array of the parsed enum structures to compile.
1428 * @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.
1429 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1430 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1431 * @param[out] enums Newly created array of the compiled enums information for the current type.
1432 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1433 */
Radek Krejci19a96102018-11-15 13:38:09 +01001434static LY_ERR
1435lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1436 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1437{
1438 LY_ERR ret = LY_SUCCESS;
1439 unsigned int u, v, match;
1440 int32_t value = 0;
1441 uint32_t position = 0;
1442 struct lysc_type_enum_item *e, storage;
1443
1444 if (base_enums && ctx->mod->compiled->version < 2) {
1445 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1446 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1447 return LY_EVALID;
1448 }
1449
1450 LY_ARRAY_FOR(enums_p, u) {
1451 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1452 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
1453 if (base_enums) {
1454 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1455 LY_ARRAY_FOR(base_enums, v) {
1456 if (!strcmp(e->name, base_enums[v].name)) {
1457 break;
1458 }
1459 }
1460 if (v == LY_ARRAY_SIZE(base_enums)) {
1461 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1462 "Invalid %s - derived type adds new item \"%s\".",
1463 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1464 return LY_EVALID;
1465 }
1466 match = v;
1467 }
1468
1469 if (basetype == LY_TYPE_ENUM) {
1470 if (enums_p[u].flags & LYS_SET_VALUE) {
1471 e->value = (int32_t)enums_p[u].value;
1472 if (!u || e->value >= value) {
1473 value = e->value + 1;
1474 }
1475 /* check collision with other values */
1476 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1477 if (e->value == (*enums)[v].value) {
1478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1479 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1480 e->value, e->name, (*enums)[v].name);
1481 return LY_EVALID;
1482 }
1483 }
1484 } else if (base_enums) {
1485 /* inherit the assigned value */
1486 e->value = base_enums[match].value;
1487 if (!u || e->value >= value) {
1488 value = e->value + 1;
1489 }
1490 } else {
1491 /* assign value automatically */
1492 if (u && value == INT32_MIN) {
1493 /* counter overflow */
1494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1495 "Invalid enumeration - it is not possible to auto-assign enum value for "
1496 "\"%s\" since the highest value is already 2147483647.", e->name);
1497 return LY_EVALID;
1498 }
1499 e->value = value++;
1500 }
1501 } else { /* LY_TYPE_BITS */
1502 if (enums_p[u].flags & LYS_SET_VALUE) {
1503 e->value = (int32_t)enums_p[u].value;
1504 if (!u || (uint32_t)e->value >= position) {
1505 position = (uint32_t)e->value + 1;
1506 }
1507 /* check collision with other values */
1508 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1509 if (e->value == (*enums)[v].value) {
1510 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1511 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1512 (uint32_t)e->value, e->name, (*enums)[v].name);
1513 return LY_EVALID;
1514 }
1515 }
1516 } else if (base_enums) {
1517 /* inherit the assigned value */
1518 e->value = base_enums[match].value;
1519 if (!u || (uint32_t)e->value >= position) {
1520 position = (uint32_t)e->value + 1;
1521 }
1522 } else {
1523 /* assign value automatically */
1524 if (u && position == 0) {
1525 /* counter overflow */
1526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1527 "Invalid bits - it is not possible to auto-assign bit position for "
1528 "\"%s\" since the highest value is already 4294967295.", e->name);
1529 return LY_EVALID;
1530 }
1531 e->value = position++;
1532 }
1533 }
1534
1535 if (base_enums) {
1536 /* the assigned values must not change from the derived type */
1537 if (e->value != base_enums[match].value) {
1538 if (basetype == LY_TYPE_ENUM) {
1539 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1540 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1541 e->name, base_enums[match].value, e->value);
1542 } else {
1543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1544 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1545 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1546 }
1547 return LY_EVALID;
1548 }
1549 }
1550
1551 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1552 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1553
1554 if (basetype == LY_TYPE_BITS) {
1555 /* keep bits ordered by position */
1556 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1557 if (v != u) {
1558 memcpy(&storage, e, sizeof *e);
1559 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1560 memcpy(&(*enums)[v], &storage, sizeof storage);
1561 }
1562 }
1563 }
1564
1565done:
1566 return ret;
1567}
1568
Radek Krejcia3045382018-11-22 14:30:31 +01001569#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1570 for ((NODE) = (NODE)->parent; \
1571 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1572 (NODE) = (NODE)->parent); \
1573 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1574 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1575 TERM; \
1576 }
1577
1578/**
1579 * @brief Validate the predicate(s) from the leafref path.
1580 * @param[in] ctx Compile context
1581 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1582 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
1583 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001584 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001585 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1586 */
1587static LY_ERR
1588lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001589 const struct lysc_node *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001590{
1591 LY_ERR ret = LY_EVALID;
1592 const struct lys_module *mod;
1593 const struct lysc_node *src_node, *dst_node;
1594 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1595 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
1596 unsigned int dest_parent_times;
1597 const char *start, *end, *pke_end;
1598 struct ly_set keys = {0};
1599 int i;
1600
1601 while (**predicate == '[') {
1602 start = (*predicate)++;
1603
1604 while (isspace(**predicate)) {
1605 ++(*predicate);
1606 }
1607 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1608 while (isspace(**predicate)) {
1609 ++(*predicate);
1610 }
1611 if (**predicate != '=') {
1612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1613 "Invalid leafref path predicate \"%.*s\" - missing \"=\".", *predicate - start + 1, *predicate);
1614 goto cleanup;
1615 }
1616 ++(*predicate);
1617 while (isspace(**predicate)) {
1618 ++(*predicate);
1619 }
1620
1621 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1623 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1624 goto cleanup;
1625 }
1626 --pke_end;
1627 while (isspace(*pke_end)) {
1628 --pke_end;
1629 }
1630 /* localize path-key-expr */
1631 pke_start = path_key_expr = *predicate;
1632 /* move after the current predicate */
1633 *predicate = end + 1;
1634
1635 /* source (must be leaf or leaf-list) */
1636 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001637 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001638 } else {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001639 mod = path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01001640 }
1641 src_node = lys_child(context_node, mod, src, src_len,
1642 mod->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST, LYS_GETNEXT_NOSTATECHECK);
1643 if (!src_node) {
1644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1645 "Invalid leafref path predicate \"%.*s\" - key node \"%.*s\" from module \"%s\" not found.",
1646 *predicate - start, start, src_len, src, mod->compiled->name);
1647 goto cleanup;
1648 }
1649 /* TODO - check the src_node is really a key of the context_node */
1650
1651 /* check that there is only one predicate for the */
1652 i = ly_set_add(&keys, (void*)src_node, 0);
1653 LY_CHECK_GOTO(i == -1, cleanup);
1654 if (keys.count != (unsigned int)i + 1) {
1655 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1656 "Invalid leafref path predicate \"%.*s\" - multiple equality test for the key %s.",
1657 *predicate - start, start, src_node->name);
1658 goto cleanup;
1659 }
1660
1661 /* destination */
1662 dest_parent_times = 0;
1663 dst_node = context_node;
1664
1665 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
1666 if (strncmp(path_key_expr, "current()", 9)) {
1667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1668 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
1669 *predicate - start, start);
1670 goto cleanup;
1671 }
1672 path_key_expr += 9;
1673 while (isspace(*path_key_expr)) {
1674 ++path_key_expr;
1675 }
1676
1677 if (*path_key_expr != '/') {
1678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1679 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
1680 *predicate - start, start);
1681 goto cleanup;
1682 }
1683 ++path_key_expr;
1684 while (isspace(*path_key_expr)) {
1685 ++path_key_expr;
1686 }
1687
1688 /* rel-path-keyexpr:
1689 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
1690 while (!strncmp(path_key_expr, "..", 2)) {
1691 ++dest_parent_times;
1692 path_key_expr += 2;
1693 while (isspace(*path_key_expr)) {
1694 ++path_key_expr;
1695 }
1696 if (*path_key_expr != '/') {
1697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1698 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
1699 *predicate - start, start);
1700 goto cleanup;
1701 }
1702 ++path_key_expr;
1703 while (isspace(*path_key_expr)) {
1704 ++path_key_expr;
1705 }
1706
1707 /* path is supposed to be evaluated in data tree, so we have to skip
1708 * all schema nodes that cannot be instantiated in data tree */
1709 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
1710 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
1711 *predicate - start, start);
1712 }
1713 if (!dest_parent_times) {
1714 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1715 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
1716 *predicate - start, start);
1717 goto cleanup;
1718 }
1719 if (path_key_expr == pke_end) {
1720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1721 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
1722 *predicate - start, start);
1723 goto cleanup;
1724 }
1725
1726 while(path_key_expr != pke_end) {
1727 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
1728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1729 "Invalid node identifier in leafref path predicate - character %d (%.*s).",
1730 path_key_expr - start, *predicate - start, start);
1731 goto cleanup;
1732 }
1733
1734 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001735 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001736 } else {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001737 mod = path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01001738 }
1739 if (!mod) {
1740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1741 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
1742 *predicate - start, start, dst_len, dst);
1743 goto cleanup;
1744 }
1745
1746 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
1747 if (!dst_node) {
1748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1749 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
1750 *predicate - start, start, path_key_expr - pke_start, pke_start);
1751 goto cleanup;
1752 }
1753 }
1754 if (!(dst_node->nodetype & (dst_node->module->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
1755 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1756 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s.",
1757 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
1758 goto cleanup;
1759 }
1760 }
1761
1762 ret = LY_SUCCESS;
1763cleanup:
1764 ly_set_erase(&keys, NULL);
1765 return ret;
1766}
1767
1768/**
1769 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
1770 *
1771 * path-arg = absolute-path / relative-path
1772 * absolute-path = 1*("/" (node-identifier *path-predicate))
1773 * relative-path = 1*(".." "/") descendant-path
1774 *
1775 * @param[in,out] path Path to parse.
1776 * @param[out] prefix Prefix of the token, NULL if there is not any.
1777 * @param[out] pref_len Length of the prefix, 0 if there is not any.
1778 * @param[out] name Name of the token.
1779 * @param[out] nam_len Length of the name.
1780 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
1781 * must not be changed between consecutive calls. -1 if the
1782 * path is absolute.
1783 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
1784 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
1785 */
1786static LY_ERR
1787lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
1788 int *parent_times, int *has_predicate)
1789{
1790 int par_times = 0;
1791
1792 assert(path && *path);
1793 assert(parent_times);
1794 assert(prefix);
1795 assert(prefix_len);
1796 assert(name);
1797 assert(name_len);
1798 assert(has_predicate);
1799
1800 *prefix = NULL;
1801 *prefix_len = 0;
1802 *name = NULL;
1803 *name_len = 0;
1804 *has_predicate = 0;
1805
1806 if (!*parent_times) {
1807 if (!strncmp(*path, "..", 2)) {
1808 *path += 2;
1809 ++par_times;
1810 while (!strncmp(*path, "/..", 3)) {
1811 *path += 3;
1812 ++par_times;
1813 }
1814 }
1815 if (par_times) {
1816 *parent_times = par_times;
1817 } else {
1818 *parent_times = -1;
1819 }
1820 }
1821
1822 if (**path != '/') {
1823 return LY_EINVAL;
1824 }
1825 /* skip '/' */
1826 ++(*path);
1827
1828 /* node-identifier ([prefix:]name) */
1829 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
1830
1831 if ((**path == '/' && (*path)[1]) || !**path) {
1832 /* path continues by another token or this is the last token */
1833 return LY_SUCCESS;
1834 } else if ((*path)[0] != '[') {
1835 /* unexpected character */
1836 return LY_EINVAL;
1837 } else {
1838 /* predicate starting with [ */
1839 *has_predicate = 1;
1840 return LY_SUCCESS;
1841 }
1842}
1843
1844/**
Radek Krejci58d171e2018-11-23 13:50:55 +01001845 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
1846 *
1847 * The set of features used for target must be a subset of features used for the leafref.
1848 * This is not a perfect, we should compare the truth tables but it could require too much resources
1849 * and RFC 7950 does not require it explicitely, so we simplify that.
1850 *
1851 * @param[in] refnode The leafref node.
1852 * @param[in] target Tha target node of the leafref.
1853 * @return LY_SUCCESS or LY_EVALID;
1854 */
1855static LY_ERR
1856lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
1857{
1858 LY_ERR ret = LY_EVALID;
1859 const struct lysc_node *iter;
1860 struct lysc_iffeature **iff;
1861 unsigned int u, v, count;
1862 struct ly_set features = {0};
1863
1864 for (iter = refnode; iter; iter = iter->parent) {
1865 iff = lysc_node_iff(iter);
1866 if (iff && *iff) {
1867 LY_ARRAY_FOR(*iff, u) {
1868 LY_ARRAY_FOR((*iff)[u].features, v) {
1869 LY_CHECK_GOTO(ly_set_add(&features, (*iff)[u].features[v], 0) == -1, cleanup);
1870 }
1871 }
1872 }
1873 }
1874
1875 /* we should have, in features set, a superset of features applicable to the target node.
1876 * So when adding features applicable to the target into the features set, we should not be
1877 * able to actually add any new feature, otherwise it is not a subset of features applicable
1878 * to the leafref itself. */
1879 count = features.count;
1880 for (iter = target; iter; iter = iter->parent) {
1881 iff = lysc_node_iff(iter);
1882 if (iff && *iff) {
1883 LY_ARRAY_FOR(*iff, u) {
1884 LY_ARRAY_FOR((*iff)[u].features, v) {
1885 if ((unsigned int)ly_set_add(&features, (*iff)[u].features[v], 0) >= count) {
1886 /* new feature was added (or LY_EMEM) */
1887 goto cleanup;
1888 }
1889 }
1890 }
1891 }
1892 }
1893 ret = LY_SUCCESS;
1894
1895cleanup:
1896 ly_set_erase(&features, NULL);
1897 return ret;
1898}
1899
1900/**
Radek Krejcia3045382018-11-22 14:30:31 +01001901 * @brief Validate the leafref path.
1902 * @param[in] ctx Compile context
1903 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01001904 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01001905 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1906 */
1907static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01001908lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01001909{
1910 const struct lysc_node *node = NULL, *parent = NULL;
1911 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001912 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01001913 const char *id, *prefix, *name;
1914 size_t prefix_len, name_len;
1915 int parent_times = 0, has_predicate;
1916 unsigned int iter, u;
1917 LY_ERR ret = LY_SUCCESS;
1918
1919 assert(ctx);
1920 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001921 assert(leafref);
1922
1923 /* 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 +01001924
1925 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001926 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01001927 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
1928 if (!iter) { /* first iteration */
1929 /* precess ".." in relative paths */
1930 if (parent_times > 0) {
1931 /* move from the context node */
1932 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
1933 /* path is supposed to be evaluated in data tree, so we have to skip
1934 * all schema nodes that cannot be instantiated in data tree */
1935 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001936 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001937 }
1938 }
1939 }
1940
1941 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001942 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001943 } else {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001944 mod = leafref->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01001945 }
1946 if (!mod) {
1947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001948 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
1949 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001950 return LY_EVALID;
1951 }
1952
1953 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
1954 if (!node) {
1955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001956 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001957 return LY_EVALID;
1958 }
1959 parent = node;
1960
1961 if (has_predicate) {
1962 /* we have predicate, so the current result must be list */
1963 if (node->nodetype != LYS_LIST) {
1964 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1965 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001966 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001967 return LY_EVALID;
1968 }
1969
Radek Krejci412ddfa2018-11-23 11:44:11 +01001970 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, node, leafref->path_context), LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01001971 }
1972
1973 ++iter;
1974 }
1975 if (ret) {
1976 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001977 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001978 return LY_EVALID;
1979 }
1980
1981 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1983 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001984 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001985 return LY_EVALID;
1986 }
1987
1988 /* check status */
1989 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
1990 return LY_EVALID;
1991 }
1992
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001993 /* check config */
1994 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
1995 if (node->flags & LYS_CONFIG_R) {
1996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1997 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
1998 leafref->path);
1999 return LY_EVALID;
2000 }
2001 }
2002
Radek Krejci412ddfa2018-11-23 11:44:11 +01002003 /* store the target's type and check for circular chain of leafrefs */
2004 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2005 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2006 if (type == (struct lysc_type*)leafref) {
2007 /* circular chain detected */
2008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2009 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2010 return LY_EVALID;
2011 }
2012 }
2013
Radek Krejci58d171e2018-11-23 13:50:55 +01002014 /* check if leafref and its target are under common if-features */
2015 if (lys_compile_leafref_features_validate(startnode, node)) {
2016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2017 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2018 leafref->path);
2019 return LY_EVALID;
2020 }
2021
Radek Krejcia3045382018-11-22 14:30:31 +01002022 return LY_SUCCESS;
2023}
2024
2025/**
2026 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2027 * @param[in] ctx Compile context.
2028 * @param[in] type_p Parsed type to compile.
2029 * @param[in] basetype Base YANG built-in type of the type to compile.
2030 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2031 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2032 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2033 * @param[out] type Newly created type structure with the filled information about the type.
2034 * @return LY_ERR value.
2035 */
Radek Krejci19a96102018-11-15 13:38:09 +01002036static LY_ERR
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002037lys_compile_type_(struct lysc_ctx *ctx, struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options,
2038 const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002039{
2040 LY_ERR ret = LY_SUCCESS;
2041 unsigned int u;
2042 struct lysc_type_bin *bin;
2043 struct lysc_type_num *num;
2044 struct lysc_type_str *str;
2045 struct lysc_type_bits *bits;
2046 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002047 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002048 struct lysc_type_identityref *idref;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002049
2050 switch (basetype) {
2051 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002052 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002053
2054 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002055 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002056 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002057 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2058 LY_CHECK_RET(ret);
2059 if (!tpdfname) {
2060 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2061 options, u, lys_compile_ext, ret, done);
2062 }
2063 }
2064
2065 if (tpdfname) {
2066 type_p->compiled = *type;
2067 *type = calloc(1, sizeof(struct lysc_type_bin));
2068 }
2069 break;
2070 case LY_TYPE_BITS:
2071 /* RFC 7950 9.7 - bits */
2072 bits = (struct lysc_type_bits*)(*type);
2073 if (type_p->bits) {
2074 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2075 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2076 (struct lysc_type_enum_item**)&bits->bits);
2077 LY_CHECK_RET(ret);
2078 }
2079
Radek Krejci555cb5b2018-11-16 14:54:33 +01002080 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002081 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002082 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002084 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002086 free(*type);
2087 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002088 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002089 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002090 }
2091
2092 if (tpdfname) {
2093 type_p->compiled = *type;
2094 *type = calloc(1, sizeof(struct lysc_type_bits));
2095 }
2096 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002097 case LY_TYPE_DEC64:
2098 dec = (struct lysc_type_dec*)(*type);
2099
2100 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002101 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002102 if (!type_p->fraction_digits) {
2103 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002104 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002105 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002106 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002107 free(*type);
2108 *type = NULL;
2109 }
2110 return LY_EVALID;
2111 }
2112 } else if (type_p->fraction_digits) {
2113 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002114 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2116 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002117 tpdfname);
2118 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2120 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002121 free(*type);
2122 *type = NULL;
2123 }
2124 return LY_EVALID;
2125 }
2126 dec->fraction_digits = type_p->fraction_digits;
2127
2128 /* RFC 7950 9.2.4 - range */
2129 if (type_p->range) {
2130 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2131 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2132 LY_CHECK_RET(ret);
2133 if (!tpdfname) {
2134 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2135 options, u, lys_compile_ext, ret, done);
2136 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002137 }
2138
2139 if (tpdfname) {
2140 type_p->compiled = *type;
2141 *type = calloc(1, sizeof(struct lysc_type_dec));
2142 }
2143 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002144 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002145 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002146
2147 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002148 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002149 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002150 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2151 LY_CHECK_RET(ret);
2152 if (!tpdfname) {
2153 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2154 options, u, lys_compile_ext, ret, done);
2155 }
2156 } else if (base && ((struct lysc_type_str*)base)->length) {
2157 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2158 }
2159
2160 /* RFC 7950 9.4.5 - pattern */
2161 if (type_p->patterns) {
2162 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2163 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2164 LY_CHECK_RET(ret);
2165 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2166 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2167 }
2168
2169 if (tpdfname) {
2170 type_p->compiled = *type;
2171 *type = calloc(1, sizeof(struct lysc_type_str));
2172 }
2173 break;
2174 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002175 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002176
2177 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002178 if (type_p->enums) {
2179 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2180 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2181 LY_CHECK_RET(ret);
2182 }
2183
Radek Krejci555cb5b2018-11-16 14:54:33 +01002184 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002185 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002186 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002187 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002188 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002189 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002190 free(*type);
2191 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002192 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002193 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002194 }
2195
2196 if (tpdfname) {
2197 type_p->compiled = *type;
2198 *type = calloc(1, sizeof(struct lysc_type_enum));
2199 }
2200 break;
2201 case LY_TYPE_INT8:
2202 case LY_TYPE_UINT8:
2203 case LY_TYPE_INT16:
2204 case LY_TYPE_UINT16:
2205 case LY_TYPE_INT32:
2206 case LY_TYPE_UINT32:
2207 case LY_TYPE_INT64:
2208 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002209 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002210
2211 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002212 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002213 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002214 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2215 LY_CHECK_RET(ret);
2216 if (!tpdfname) {
2217 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2218 options, u, lys_compile_ext, ret, done);
2219 }
2220 }
2221
2222 if (tpdfname) {
2223 type_p->compiled = *type;
2224 *type = calloc(1, sizeof(struct lysc_type_num));
2225 }
2226 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002227 case LY_TYPE_IDENT:
2228 idref = (struct lysc_type_identityref*)(*type);
2229
2230 /* RFC 7950 9.10.2 - base */
2231 if (type_p->bases) {
2232 if (base) {
2233 /* only the directly derived identityrefs can contain base specification */
2234 if (tpdfname) {
2235 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2236 "Invalid base substatement for type \"%s\" not directly derived from identityref built-in type.",
2237 tpdfname);
2238 } else {
2239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2240 "Invalid base substatement for type not directly derived from identityref built-in type.");
2241 free(*type);
2242 *type = NULL;
2243 }
2244 return LY_EVALID;
2245 }
2246 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2247 LY_CHECK_RET(ret);
2248 }
2249
2250 if (!base && !type_p->flags) {
2251 /* type derived from identityref built-in type must contain at least one base */
2252 if (tpdfname) {
2253 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2254 } else {
2255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2256 free(*type);
2257 *type = NULL;
2258 }
2259 return LY_EVALID;
2260 }
2261
2262 if (tpdfname) {
2263 type_p->compiled = *type;
2264 *type = calloc(1, sizeof(struct lysc_type_identityref));
2265 }
2266 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002267 case LY_TYPE_LEAFREF:
2268 /* RFC 7950 9.9.3 - require-instance */
2269 if (type_p->flags & LYS_SET_REQINST) {
2270 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002271 } else if (base) {
2272 /* inherit */
2273 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002274 } else {
2275 /* default is true */
2276 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2277 }
2278 if (type_p->path) {
2279 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002280 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002281 } else if (base) {
2282 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002283 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002284 } else if (tpdfname) {
2285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2286 return LY_EVALID;
2287 } else {
2288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2289 free(*type);
2290 *type = NULL;
2291 return LY_EVALID;
2292 }
2293 if (tpdfname) {
2294 type_p->compiled = *type;
2295 *type = calloc(1, sizeof(struct lysc_type_leafref));
2296 }
2297 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002298 case LY_TYPE_INST:
2299 /* RFC 7950 9.9.3 - require-instance */
2300 if (type_p->flags & LYS_SET_REQINST) {
2301 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2302 } else {
2303 /* default is true */
2304 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2305 }
2306
2307 if (tpdfname) {
2308 type_p->compiled = *type;
2309 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2310 }
2311 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002312 case LY_TYPE_BOOL:
2313 case LY_TYPE_EMPTY:
2314 case LY_TYPE_UNKNOWN: /* just to complete switch */
2315 break;
2316 }
2317 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2318done:
2319 return ret;
2320}
2321
Radek Krejcia3045382018-11-22 14:30:31 +01002322/**
2323 * @brief Compile information about the leaf/leaf-list's type.
2324 * @param[in] ctx Compile context.
2325 * @param[in] leaf_p Parsed leaf with the type to compile.
2326 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2327 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
2328 * @return LY_ERR value.
2329 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002330static LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +01002331lys_compile_type(struct lysc_ctx *ctx, struct lysp_node_leaf *leaf_p, int options, struct lysc_type **type)
2332{
2333 LY_ERR ret = LY_SUCCESS;
2334 unsigned int u;
2335 struct lysp_type *type_p = &leaf_p->type;
2336 struct type_context {
2337 const struct lysp_tpdf *tpdf;
2338 struct lysp_node *node;
2339 struct lysp_module *mod;
2340 } *tctx, *tctx_prev = NULL;
2341 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002342 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002343 struct ly_set tpdf_chain = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01002344
2345 (*type) = NULL;
2346
2347 tctx = calloc(1, sizeof *tctx);
2348 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2349 for (ret = lysp_type_find(type_p->name, (struct lysp_node*)leaf_p, ctx->mod->parsed,
2350 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2351 ret == LY_SUCCESS;
2352 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2353 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2354 if (basetype) {
2355 break;
2356 }
2357
2358 /* check status */
2359 ret = lysc_check_status(ctx, leaf_p->flags, ctx->mod->parsed, leaf_p->name,
2360 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->mod->name);
2361 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2362
2363 if (tctx->tpdf->type.compiled) {
2364 /* it is not necessary to continue, the rest of the chain was already compiled */
2365 basetype = tctx->tpdf->type.compiled->basetype;
2366 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2367 tctx = NULL;
2368 break;
2369 }
2370
2371 /* store information for the following processing */
2372 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2373
2374 /* prepare next loop */
2375 tctx_prev = tctx;
2376 tctx = calloc(1, sizeof *tctx);
2377 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2378 }
2379 free(tctx);
2380
2381 /* allocate type according to the basetype */
2382 switch (basetype) {
2383 case LY_TYPE_BINARY:
2384 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002385 break;
2386 case LY_TYPE_BITS:
2387 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002388 break;
2389 case LY_TYPE_BOOL:
2390 case LY_TYPE_EMPTY:
2391 *type = calloc(1, sizeof(struct lysc_type));
2392 break;
2393 case LY_TYPE_DEC64:
2394 *type = calloc(1, sizeof(struct lysc_type_dec));
2395 break;
2396 case LY_TYPE_ENUM:
2397 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002398 break;
2399 case LY_TYPE_IDENT:
2400 *type = calloc(1, sizeof(struct lysc_type_identityref));
2401 break;
2402 case LY_TYPE_INST:
2403 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2404 break;
2405 case LY_TYPE_LEAFREF:
2406 *type = calloc(1, sizeof(struct lysc_type_leafref));
2407 break;
2408 case LY_TYPE_STRING:
2409 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002410 break;
2411 case LY_TYPE_UNION:
2412 *type = calloc(1, sizeof(struct lysc_type_union));
2413 break;
2414 case LY_TYPE_INT8:
2415 case LY_TYPE_UINT8:
2416 case LY_TYPE_INT16:
2417 case LY_TYPE_UINT16:
2418 case LY_TYPE_INT32:
2419 case LY_TYPE_UINT32:
2420 case LY_TYPE_INT64:
2421 case LY_TYPE_UINT64:
2422 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002423 break;
2424 case LY_TYPE_UNKNOWN:
2425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2426 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2427 ret = LY_EVALID;
2428 goto cleanup;
2429 }
2430 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
2431 if (~type_substmt_map[basetype] & leaf_p->type.flags) {
2432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2433 ly_data_type2str[basetype]);
2434 free(*type);
2435 (*type) = NULL;
2436 ret = LY_EVALID;
2437 goto cleanup;
2438 }
2439
2440 /* get restrictions from the referred typedefs */
2441 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2442 tctx = (struct type_context*)tpdf_chain.objs[u];
2443 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2445 tctx->tpdf->name, ly_data_type2str[basetype]);
2446 ret = LY_EVALID;
2447 goto cleanup;
2448 } else if (tctx->tpdf->type.compiled) {
2449 base = tctx->tpdf->type.compiled;
2450 continue;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002451 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002452 /* no change, just use the type information from the base */
2453 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2454 ++base->refcount;
2455 continue;
2456 }
2457
2458 ++(*type)->refcount;
2459 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002460 prev_type = *type;
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002461 ret = lys_compile_type_(ctx, &((struct lysp_tpdf*)tctx->tpdf)->type,
2462 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2463 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002464 LY_CHECK_GOTO(ret, cleanup);
2465 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002466 }
2467
Radek Krejcic5c27e52018-11-15 14:38:11 +01002468 /* process the type definition in leaf */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002469 if (leaf_p->type.flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002470 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002471 (*type)->basetype = basetype;
2472 ++(*type)->refcount;
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002473 ret = lys_compile_type_(ctx, &leaf_p->type, ctx->mod, basetype, options, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002474 LY_CHECK_GOTO(ret, cleanup);
2475 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002476 /* no specific restriction in leaf's type definition, copy from the base */
2477 free(*type);
2478 (*type) = base;
2479 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002480 }
2481
2482 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
2483
2484cleanup:
2485 ly_set_erase(&tpdf_chain, free);
2486 return ret;
2487}
2488
2489static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent);
2490
Radek Krejcia3045382018-11-22 14:30:31 +01002491/**
2492 * @brief Compile parsed container node information.
2493 * @param[in] ctx Compile context
2494 * @param[in] node_p Parsed container node.
2495 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2496 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2497 * is enriched with the container-specific information.
2498 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2499 */
Radek Krejci19a96102018-11-15 13:38:09 +01002500static LY_ERR
2501lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2502{
2503 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
2504 struct lysc_node_container *cont = (struct lysc_node_container*)node;
2505 struct lysp_node *child_p;
2506 unsigned int u;
2507 LY_ERR ret = LY_SUCCESS;
2508
2509 COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done);
2510 COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done);
2511
2512 LY_LIST_FOR(cont_p->child, child_p) {
2513 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2514 }
2515
2516 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2517 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2518 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2519
2520done:
2521 return ret;
2522}
2523
Radek Krejcia3045382018-11-22 14:30:31 +01002524/**
2525 * @brief Compile parsed leaf node information.
2526 * @param[in] ctx Compile context
2527 * @param[in] node_p Parsed leaf node.
2528 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2529 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2530 * is enriched with the leaf-specific information.
2531 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2532 */
Radek Krejci19a96102018-11-15 13:38:09 +01002533static LY_ERR
2534lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2535{
2536 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2537 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2538 unsigned int u;
2539 LY_ERR ret = LY_SUCCESS;
2540
2541 COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done);
2542 COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002543 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
2544 ret = lys_compile_type(ctx, leaf_p, options, &leaf->type);
2545 LY_CHECK_GOTO(ret, done);
2546
Radek Krejcia3045382018-11-22 14:30:31 +01002547 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2548 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2549 ly_set_add(&ctx->unres, leaf, 0);
2550 }
2551
Radek Krejci19a96102018-11-15 13:38:09 +01002552 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2553 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
2554done:
2555 return ret;
2556}
2557
Radek Krejcia3045382018-11-22 14:30:31 +01002558/**
2559 * @brief Compile parsed schema node information.
2560 * @param[in] ctx Compile context
2561 * @param[in] node_p Parsed schema node.
2562 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2563 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
2564 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
2565 * the compile context.
2566 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2567 */
Radek Krejci19a96102018-11-15 13:38:09 +01002568static LY_ERR
2569lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
2570{
2571 LY_ERR ret = LY_EVALID;
2572 struct lysc_node *node, **children;
2573 unsigned int u;
2574 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
2575
2576 switch (node_p->nodetype) {
2577 case LYS_CONTAINER:
2578 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
2579 node_compile_spec = lys_compile_node_container;
2580 break;
2581 case LYS_LEAF:
2582 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
2583 node_compile_spec = lys_compile_node_leaf;
2584 break;
2585 case LYS_LIST:
2586 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
2587 break;
2588 case LYS_LEAFLIST:
2589 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
2590 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002591 case LYS_CHOICE:
2592 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
2593 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002594 case LYS_ANYXML:
2595 case LYS_ANYDATA:
2596 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
2597 break;
2598 default:
2599 LOGINT(ctx->ctx);
2600 return LY_EINT;
2601 }
2602 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
2603 node->nodetype = node_p->nodetype;
2604 node->module = ctx->mod;
2605 node->prev = node;
2606 node->flags = node_p->flags;
2607
2608 /* config */
2609 if (!(node->flags & LYS_CONFIG_MASK)) {
2610 /* config not explicitely set, inherit it from parent */
2611 if (parent) {
2612 node->flags |= parent->flags & LYS_CONFIG_MASK;
2613 } else {
2614 /* default is config true */
2615 node->flags |= LYS_CONFIG_W;
2616 }
2617 }
2618
2619 /* status - it is not inherited by specification, but it does not make sense to have
2620 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2621 if (!(node->flags & LYS_STATUS_MASK)) {
2622 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2623 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2624 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2625 node->flags |= parent->flags & LYS_STATUS_MASK;
2626 } else {
2627 node->flags |= LYS_STATUS_CURR;
2628 }
2629 } else if (parent) {
2630 /* check status compatibility with the parent */
2631 if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
2632 if (node->flags & LYS_STATUS_CURR) {
2633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2634 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2635 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2636 } else { /* LYS_STATUS_DEPRC */
2637 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2638 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2639 }
2640 goto error;
2641 }
2642 }
2643
2644 if (!(options & LYSC_OPT_FREE_SP)) {
2645 node->sp = node_p;
2646 }
2647 DUP_STRING(ctx->ctx, node_p->name, node->name);
2648 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
2649
2650 /* nodetype-specific part */
2651 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
2652
2653 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01002654 if (parent) {
2655 if (parent->nodetype == LYS_CHOICE) {
2656 /* TODO exception for cases */
2657 } else if ((children = lysc_node_children(parent))) {
2658 if (!(*children)) {
2659 /* first child */
2660 *children = node;
2661 } else {
2662 /* insert at the end of the parent's children list */
2663 (*children)->prev->next = node;
2664 node->prev = (*children)->prev;
2665 (*children)->prev = node;
2666 }
Radek Krejci19a96102018-11-15 13:38:09 +01002667 }
2668 } else {
2669 /* top-level element */
2670 if (!ctx->mod->compiled->data) {
2671 ctx->mod->compiled->data = node;
2672 } else {
2673 /* insert at the end of the module's top-level nodes list */
2674 ctx->mod->compiled->data->prev->next = node;
2675 node->prev = ctx->mod->compiled->data->prev;
2676 ctx->mod->compiled->data->prev = node;
2677 }
2678 }
2679
2680 return LY_SUCCESS;
2681
2682error:
2683 lysc_node_free(ctx->ctx, node);
2684 return ret;
2685}
2686
Radek Krejcia3045382018-11-22 14:30:31 +01002687/**
2688 * @brief Compile the given YANG module.
2689 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
2690 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2691 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2692 */
Radek Krejci19a96102018-11-15 13:38:09 +01002693LY_ERR
2694lys_compile(struct lys_module *mod, int options)
2695{
2696 struct lysc_ctx ctx = {0};
2697 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002698 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01002699 struct lysp_module *sp;
2700 struct lysp_node *node_p;
2701 unsigned int u;
2702 LY_ERR ret;
2703
2704 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
2705 sp = mod->parsed;
2706
2707 if (sp->submodule) {
2708 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
2709 return LY_EINVAL;
2710 }
2711
2712 ctx.ctx = sp->ctx;
2713 ctx.mod = mod;
2714
2715 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
2716 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
2717 mod_c->ctx = sp->ctx;
2718 mod_c->implemented = sp->implemented;
2719 mod_c->latest_revision = sp->latest_revision;
2720 mod_c->version = sp->version;
2721
2722 DUP_STRING(sp->ctx, sp->name, mod_c->name);
2723 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
2724 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
2725 if (sp->revs) {
2726 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
2727 }
2728 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
2729 COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
2730 COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
2731 if (sp->identities) {
2732 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
2733 }
2734
2735 LY_LIST_FOR(sp->data, node_p) {
2736 ret = lys_compile_node(&ctx, node_p, options, NULL);
2737 LY_CHECK_GOTO(ret, error);
2738 }
2739 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
2740 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
2741
2742 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
2743
Radek Krejcia3045382018-11-22 14:30:31 +01002744 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002745 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
2746 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
2747 * 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 +01002748 for (u = 0; u < ctx.unres.count; ++u) {
2749 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) {
2750 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2751 if (type->basetype == LY_TYPE_LEAFREF) {
2752 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002753 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 +01002754 LY_CHECK_GOTO(ret, error);
2755 }
2756 }
2757 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01002758 for (u = 0; u < ctx.unres.count; ++u) {
2759 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) {
2760 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2761 if (type->basetype == LY_TYPE_LEAFREF) {
2762 /* store pointer to the real type */
2763 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
2764 typeiter->basetype == LY_TYPE_LEAFREF;
2765 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
2766 ((struct lysc_type_leafref*)type)->realtype = typeiter;
2767 }
2768 }
2769 }
Radek Krejcia3045382018-11-22 14:30:31 +01002770 ly_set_erase(&ctx.unres, NULL);
2771
Radek Krejci19a96102018-11-15 13:38:09 +01002772 if (options & LYSC_OPT_FREE_SP) {
2773 lysp_module_free(mod->parsed);
2774 ((struct lys_module*)mod)->parsed = NULL;
2775 }
2776
2777 ((struct lys_module*)mod)->compiled = mod_c;
2778 return LY_SUCCESS;
2779
2780error:
Radek Krejcia3045382018-11-22 14:30:31 +01002781 ly_set_erase(&ctx.unres, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01002782 lysc_module_free(mod_c, NULL);
2783 ((struct lys_module*)mod)->compiled = NULL;
2784 return ret;
2785}