blob: bff9332d545ff92c5f384c006d0f95da4ecce5a4 [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) {
772 case LY_TYPE_BINARY: /* length */
773 if (valcopy) {
774 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
775 } else if (max) {
776 part->max_u64 = UINT64_C(18446744073709551615);
777 } else {
778 part->min_u64 = UINT64_C(0);
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(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100782 }
783 break;
784 case LY_TYPE_DEC64: /* range */
785 if (valcopy) {
786 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
787 max ? &part->max_64 : &part->min_64);
788 } else if (max) {
789 part->max_64 = INT64_C(9223372036854775807);
790 } else {
791 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
792 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100793 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100794 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100795 }
796 break;
797 case LY_TYPE_INT8: /* range */
798 if (valcopy) {
799 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
800 } else if (max) {
801 part->max_64 = INT64_C(127);
802 } else {
803 part->min_64 = INT64_C(-128);
804 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100805 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100806 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100807 }
808 break;
809 case LY_TYPE_INT16: /* range */
810 if (valcopy) {
811 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
812 } else if (max) {
813 part->max_64 = INT64_C(32767);
814 } else {
815 part->min_64 = INT64_C(-32768);
816 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100817 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100818 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100819 }
820 break;
821 case LY_TYPE_INT32: /* range */
822 if (valcopy) {
823 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
824 } else if (max) {
825 part->max_64 = INT64_C(2147483647);
826 } else {
827 part->min_64 = INT64_C(-2147483648);
828 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100829 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100830 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100831 }
832 break;
833 case LY_TYPE_INT64: /* range */
834 if (valcopy) {
835 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
836 max ? &part->max_64 : &part->min_64);
837 } else if (max) {
838 part->max_64 = INT64_C(9223372036854775807);
839 } else {
840 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
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(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100844 }
845 break;
846 case LY_TYPE_UINT8: /* range */
847 if (valcopy) {
848 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
849 } else if (max) {
850 part->max_u64 = UINT64_C(255);
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_UINT16: /* range */
859 if (valcopy) {
860 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
861 } else if (max) {
862 part->max_u64 = UINT64_C(65535);
863 } else {
864 part->min_u64 = UINT64_C(0);
865 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100866 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100867 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100868 }
869 break;
870 case LY_TYPE_UINT32: /* range */
871 if (valcopy) {
872 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
873 } else if (max) {
874 part->max_u64 = UINT64_C(4294967295);
875 } else {
876 part->min_u64 = UINT64_C(0);
877 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100878 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100879 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100880 }
881 break;
882 case LY_TYPE_UINT64: /* range */
883 if (valcopy) {
884 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
885 } else if (max) {
886 part->max_u64 = UINT64_C(18446744073709551615);
887 } else {
888 part->min_u64 = UINT64_C(0);
889 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100890 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100891 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100892 }
893 break;
894 case LY_TYPE_STRING: /* length */
895 if (valcopy) {
896 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
897 } else if (max) {
898 part->max_u64 = UINT64_C(18446744073709551615);
899 } else {
900 part->min_u64 = UINT64_C(0);
901 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100902 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100903 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100904 }
905 break;
906 default:
907 LOGINT(ctx->ctx);
908 ret = LY_EINT;
909 }
910
Radek Krejci5969f272018-11-23 10:03:58 +0100911finalize:
Radek Krejci19a96102018-11-15 13:38:09 +0100912 if (ret == LY_EDENIED) {
913 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
914 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
915 length_restr ? "length" : "range", valcopy ? valcopy : *value);
916 } else if (ret == LY_EVALID) {
917 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
918 "Invalid %s restriction - invalid value \"%s\".",
919 length_restr ? "length" : "range", valcopy ? valcopy : *value);
920 } else if (ret == LY_EEXIST) {
921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
922 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100923 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +0100924 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +0100925 } else if (!ret && value) {
926 *value = *value + len;
927 }
928 free(valcopy);
929 return ret;
930}
931
Radek Krejcia3045382018-11-22 14:30:31 +0100932/**
933 * @brief Compile the parsed range restriction.
934 * @param[in] ctx Compile context.
935 * @param[in] range_p Parsed range structure to compile.
936 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
937 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
938 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
939 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
940 * range restriction must be more restrictive than the base_range.
941 * @param[in,out] range Pointer to the created current range structure.
942 * @return LY_ERR value.
943 */
Radek Krejci19a96102018-11-15 13:38:09 +0100944static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100945lys_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 +0100946 struct lysc_range *base_range, struct lysc_range **range)
947{
948 LY_ERR ret = LY_EVALID;
949 const char *expr;
950 struct lysc_range_part *parts = NULL, *part;
951 int range_expected = 0, uns;
952 unsigned int parts_done = 0, u, v;
953
954 assert(range);
955 assert(range_p);
956
957 expr = range_p->arg;
958 while(1) {
959 if (isspace(*expr)) {
960 ++expr;
961 } else if (*expr == '\0') {
962 if (range_expected) {
963 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
964 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
965 length_restr ? "length" : "range", range_p->arg);
966 goto cleanup;
967 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
968 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
969 "Invalid %s restriction - unexpected end of the expression (%s).",
970 length_restr ? "length" : "range", range_p->arg);
971 goto cleanup;
972 }
973 parts_done++;
974 break;
975 } else if (!strncmp(expr, "min", 3)) {
976 if (parts) {
977 /* min cannot be used elsewhere than in the first part */
978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
979 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
980 expr - range_p->arg, range_p->arg);
981 goto cleanup;
982 }
983 expr += 3;
984
985 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +0100986 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 +0100987 part->max_64 = part->min_64;
988 } else if (*expr == '|') {
989 if (!parts || range_expected) {
990 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
991 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
992 goto cleanup;
993 }
994 expr++;
995 parts_done++;
996 /* process next part of the expression */
997 } else if (!strncmp(expr, "..", 2)) {
998 expr += 2;
999 while (isspace(*expr)) {
1000 expr++;
1001 }
1002 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1004 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1005 goto cleanup;
1006 }
1007 /* continue expecting the upper boundary */
1008 range_expected = 1;
1009 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1010 /* number */
1011 if (range_expected) {
1012 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001013 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 +01001014 range_expected = 0;
1015 } else {
1016 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1017 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 +01001018 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001019 part->max_64 = part->min_64;
1020 }
1021
1022 /* continue with possible another expression part */
1023 } else if (!strncmp(expr, "max", 3)) {
1024 expr += 3;
1025 while (isspace(*expr)) {
1026 expr++;
1027 }
1028 if (*expr != '\0') {
1029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1030 length_restr ? "length" : "range", expr);
1031 goto cleanup;
1032 }
1033 if (range_expected) {
1034 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001035 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 +01001036 range_expected = 0;
1037 } else {
1038 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1039 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 +01001040 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001041 part->min_64 = part->max_64;
1042 }
1043 } else {
1044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1045 length_restr ? "length" : "range", expr);
1046 goto cleanup;
1047 }
1048 }
1049
1050 /* check with the previous range/length restriction */
1051 if (base_range) {
1052 switch (basetype) {
1053 case LY_TYPE_BINARY:
1054 case LY_TYPE_UINT8:
1055 case LY_TYPE_UINT16:
1056 case LY_TYPE_UINT32:
1057 case LY_TYPE_UINT64:
1058 case LY_TYPE_STRING:
1059 uns = 1;
1060 break;
1061 case LY_TYPE_DEC64:
1062 case LY_TYPE_INT8:
1063 case LY_TYPE_INT16:
1064 case LY_TYPE_INT32:
1065 case LY_TYPE_INT64:
1066 uns = 0;
1067 break;
1068 default:
1069 LOGINT(ctx->ctx);
1070 ret = LY_EINT;
1071 goto cleanup;
1072 }
1073 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1074 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1075 goto baseerror;
1076 }
1077 /* current lower bound is not lower than the base */
1078 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1079 /* base has single value */
1080 if (base_range->parts[v].min_64 == parts[u].min_64) {
1081 /* both lower bounds are the same */
1082 if (parts[u].min_64 != parts[u].max_64) {
1083 /* current continues with a range */
1084 goto baseerror;
1085 } else {
1086 /* equal single values, move both forward */
1087 ++v;
1088 continue;
1089 }
1090 } else {
1091 /* base is single value lower than current range, so the
1092 * value from base range is removed in the current,
1093 * move only base and repeat checking */
1094 ++v;
1095 --u;
1096 continue;
1097 }
1098 } else {
1099 /* base is the range */
1100 if (parts[u].min_64 == parts[u].max_64) {
1101 /* current is a single value */
1102 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1103 /* current is behind the base range, so base range is omitted,
1104 * move the base and keep the current for further check */
1105 ++v;
1106 --u;
1107 } /* else it is within the base range, so move the current, but keep the base */
1108 continue;
1109 } else {
1110 /* both are ranges - check the higher bound, the lower was already checked */
1111 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1112 /* higher bound is higher than the current higher bound */
1113 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1114 /* but the current lower bound is also higher, so the base range is omitted,
1115 * continue with the same current, but move the base */
1116 --u;
1117 ++v;
1118 continue;
1119 }
1120 /* current range starts within the base range but end behind it */
1121 goto baseerror;
1122 } else {
1123 /* current range is smaller than the base,
1124 * move current, but stay with the base */
1125 continue;
1126 }
1127 }
1128 }
1129 }
1130 if (u != parts_done) {
1131baseerror:
1132 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1133 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1134 length_restr ? "length" : "range", range_p->arg);
1135 goto cleanup;
1136 }
1137 }
1138
1139 if (!(*range)) {
1140 *range = calloc(1, sizeof **range);
1141 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1142 }
1143
1144 if (range_p->eapptag) {
1145 lydict_remove(ctx->ctx, (*range)->eapptag);
1146 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1147 }
1148 if (range_p->emsg) {
1149 lydict_remove(ctx->ctx, (*range)->emsg);
1150 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1151 }
1152 /* extensions are taken only from the last range by the caller */
1153
1154 (*range)->parts = parts;
1155 parts = NULL;
1156 ret = LY_SUCCESS;
1157cleanup:
1158 /* TODO clean up */
1159 LY_ARRAY_FREE(parts);
1160
1161 return ret;
1162}
1163
1164/**
1165 * @brief Checks pattern syntax.
1166 *
Radek Krejcia3045382018-11-22 14:30:31 +01001167 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001168 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001169 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1170 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001171 */
1172static LY_ERR
1173lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1174{
1175 int idx, idx2, start, end, err_offset, count;
1176 char *perl_regex, *ptr;
1177 const char *err_msg, *orig_ptr;
1178 pcre *precomp;
1179#define URANGE_LEN 19
1180 char *ublock2urange[][2] = {
1181 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1182 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1183 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1184 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1185 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1186 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1187 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1188 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1189 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1190 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1191 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1192 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1193 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1194 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1195 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1196 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1197 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1198 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1199 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1200 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1201 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1202 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1203 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1204 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1205 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1206 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1207 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1208 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1209 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1210 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1211 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1212 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1213 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1214 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1215 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1216 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1217 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1218 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1219 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1220 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1221 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1222 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1223 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1224 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1225 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1226 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1227 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1228 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1229 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1230 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1231 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1232 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1233 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1234 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1235 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1236 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1237 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1238 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1239 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1240 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1241 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1242 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1243 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1244 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1245 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1246 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1247 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1248 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1249 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1250 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1251 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1252 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1253 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1254 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1255 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1256 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1257 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1258 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1259 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1260 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1261 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1262 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1263 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1264 {NULL, NULL}
1265 };
1266
1267 /* adjust the expression to a Perl equivalent
1268 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1269
1270 /* we need to replace all "$" with "\$", count them now */
1271 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1272
1273 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1274 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1275 perl_regex[0] = '\0';
1276
1277 ptr = perl_regex;
1278
1279 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1280 /* we will add line-end anchoring */
1281 ptr[0] = '(';
1282 ++ptr;
1283 }
1284
1285 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1286 if (orig_ptr[0] == '$') {
1287 ptr += sprintf(ptr, "\\$");
1288 } else if (orig_ptr[0] == '^') {
1289 ptr += sprintf(ptr, "\\^");
1290 } else {
1291 ptr[0] = orig_ptr[0];
1292 ++ptr;
1293 }
1294 }
1295
1296 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1297 ptr += sprintf(ptr, ")$");
1298 } else {
1299 ptr[0] = '\0';
1300 ++ptr;
1301 }
1302
1303 /* substitute Unicode Character Blocks with exact Character Ranges */
1304 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1305 start = ptr - perl_regex;
1306
1307 ptr = strchr(ptr, '}');
1308 if (!ptr) {
1309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1310 pattern, perl_regex + start + 2, "unterminated character property");
1311 free(perl_regex);
1312 return LY_EVALID;
1313 }
1314 end = (ptr - perl_regex) + 1;
1315
1316 /* need more space */
1317 if (end - start < URANGE_LEN) {
1318 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1319 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1320 }
1321
1322 /* find our range */
1323 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1324 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1325 break;
1326 }
1327 }
1328 if (!ublock2urange[idx][0]) {
1329 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1330 pattern, perl_regex + start + 5, "unknown block name");
1331 free(perl_regex);
1332 return LY_EVALID;
1333 }
1334
1335 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1336 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1337 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1338 ++count;
1339 }
1340 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1341 --count;
1342 }
1343 }
1344 if (count) {
1345 /* skip brackets */
1346 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1347 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1348 } else {
1349 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1350 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1351 }
1352 }
1353
1354 /* must return 0, already checked during parsing */
1355 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1356 &err_msg, &err_offset, NULL);
1357 if (!precomp) {
1358 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1359 free(perl_regex);
1360 return LY_EVALID;
1361 }
1362 free(perl_regex);
1363
1364 if (pcre_precomp) {
1365 *pcre_precomp = precomp;
1366 } else {
1367 free(precomp);
1368 }
1369
1370 return LY_SUCCESS;
1371
1372#undef URANGE_LEN
1373}
1374
Radek Krejcia3045382018-11-22 14:30:31 +01001375/**
1376 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1377 * @param[in] ctx Compile context.
1378 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1379 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1380 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1381 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1382 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1383 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1384 */
Radek Krejci19a96102018-11-15 13:38:09 +01001385static LY_ERR
1386lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1387 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1388{
1389 struct lysc_pattern **pattern;
1390 unsigned int u, v;
1391 const char *err_msg;
1392 LY_ERR ret = LY_SUCCESS;
1393
1394 /* first, copy the patterns from the base type */
1395 if (base_patterns) {
1396 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1397 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1398 }
1399
1400 LY_ARRAY_FOR(patterns_p, u) {
1401 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1402 *pattern = calloc(1, sizeof **pattern);
1403 ++(*pattern)->refcount;
1404
1405 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1406 LY_CHECK_RET(ret);
1407 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1408 if (err_msg) {
1409 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1410 }
1411
1412 if (patterns_p[u].arg[0] == 0x15) {
1413 (*pattern)->inverted = 1;
1414 }
1415 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1416 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
1417 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1418 options, v, lys_compile_ext, ret, done);
1419 }
1420done:
1421 return ret;
1422}
1423
Radek Krejcia3045382018-11-22 14:30:31 +01001424/**
1425 * @brief map of the possible restrictions combination for the specific built-in type.
1426 */
Radek Krejci19a96102018-11-15 13:38:09 +01001427static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1428 0 /* LY_TYPE_UNKNOWN */,
1429 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001430 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1431 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1432 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1433 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1434 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001435 LYS_SET_BIT /* LY_TYPE_BITS */,
1436 0 /* LY_TYPE_BOOL */,
1437 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1438 0 /* LY_TYPE_EMPTY */,
1439 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1440 LYS_SET_BASE /* LY_TYPE_IDENT */,
1441 LYS_SET_REQINST /* LY_TYPE_INST */,
1442 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001443 LYS_SET_TYPE /* LY_TYPE_UNION */,
1444 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001445 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001446 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001447 LYS_SET_RANGE /* LY_TYPE_INT64 */
1448};
1449
1450/**
1451 * @brief stringification of the YANG built-in data types
1452 */
1453const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1454 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1455 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001456};
1457
Radek Krejcia3045382018-11-22 14:30:31 +01001458/**
1459 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1460 * @param[in] ctx Compile context.
1461 * @param[in] enums_p Array of the parsed enum structures to compile.
1462 * @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.
1463 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1464 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1465 * @param[out] enums Newly created array of the compiled enums information for the current type.
1466 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1467 */
Radek Krejci19a96102018-11-15 13:38:09 +01001468static LY_ERR
1469lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1470 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1471{
1472 LY_ERR ret = LY_SUCCESS;
1473 unsigned int u, v, match;
1474 int32_t value = 0;
1475 uint32_t position = 0;
1476 struct lysc_type_enum_item *e, storage;
1477
1478 if (base_enums && ctx->mod->compiled->version < 2) {
1479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1480 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1481 return LY_EVALID;
1482 }
1483
1484 LY_ARRAY_FOR(enums_p, u) {
1485 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1486 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
1487 if (base_enums) {
1488 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1489 LY_ARRAY_FOR(base_enums, v) {
1490 if (!strcmp(e->name, base_enums[v].name)) {
1491 break;
1492 }
1493 }
1494 if (v == LY_ARRAY_SIZE(base_enums)) {
1495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1496 "Invalid %s - derived type adds new item \"%s\".",
1497 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1498 return LY_EVALID;
1499 }
1500 match = v;
1501 }
1502
1503 if (basetype == LY_TYPE_ENUM) {
1504 if (enums_p[u].flags & LYS_SET_VALUE) {
1505 e->value = (int32_t)enums_p[u].value;
1506 if (!u || e->value >= value) {
1507 value = e->value + 1;
1508 }
1509 /* check collision with other values */
1510 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1511 if (e->value == (*enums)[v].value) {
1512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1513 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1514 e->value, e->name, (*enums)[v].name);
1515 return LY_EVALID;
1516 }
1517 }
1518 } else if (base_enums) {
1519 /* inherit the assigned value */
1520 e->value = base_enums[match].value;
1521 if (!u || e->value >= value) {
1522 value = e->value + 1;
1523 }
1524 } else {
1525 /* assign value automatically */
1526 if (u && value == INT32_MIN) {
1527 /* counter overflow */
1528 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1529 "Invalid enumeration - it is not possible to auto-assign enum value for "
1530 "\"%s\" since the highest value is already 2147483647.", e->name);
1531 return LY_EVALID;
1532 }
1533 e->value = value++;
1534 }
1535 } else { /* LY_TYPE_BITS */
1536 if (enums_p[u].flags & LYS_SET_VALUE) {
1537 e->value = (int32_t)enums_p[u].value;
1538 if (!u || (uint32_t)e->value >= position) {
1539 position = (uint32_t)e->value + 1;
1540 }
1541 /* check collision with other values */
1542 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1543 if (e->value == (*enums)[v].value) {
1544 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1545 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1546 (uint32_t)e->value, e->name, (*enums)[v].name);
1547 return LY_EVALID;
1548 }
1549 }
1550 } else if (base_enums) {
1551 /* inherit the assigned value */
1552 e->value = base_enums[match].value;
1553 if (!u || (uint32_t)e->value >= position) {
1554 position = (uint32_t)e->value + 1;
1555 }
1556 } else {
1557 /* assign value automatically */
1558 if (u && position == 0) {
1559 /* counter overflow */
1560 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1561 "Invalid bits - it is not possible to auto-assign bit position for "
1562 "\"%s\" since the highest value is already 4294967295.", e->name);
1563 return LY_EVALID;
1564 }
1565 e->value = position++;
1566 }
1567 }
1568
1569 if (base_enums) {
1570 /* the assigned values must not change from the derived type */
1571 if (e->value != base_enums[match].value) {
1572 if (basetype == LY_TYPE_ENUM) {
1573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1574 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1575 e->name, base_enums[match].value, e->value);
1576 } else {
1577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1578 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1579 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1580 }
1581 return LY_EVALID;
1582 }
1583 }
1584
1585 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1586 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1587
1588 if (basetype == LY_TYPE_BITS) {
1589 /* keep bits ordered by position */
1590 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1591 if (v != u) {
1592 memcpy(&storage, e, sizeof *e);
1593 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1594 memcpy(&(*enums)[v], &storage, sizeof storage);
1595 }
1596 }
1597 }
1598
1599done:
1600 return ret;
1601}
1602
Radek Krejcia3045382018-11-22 14:30:31 +01001603#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1604 for ((NODE) = (NODE)->parent; \
1605 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1606 (NODE) = (NODE)->parent); \
1607 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1609 TERM; \
1610 }
1611
1612/**
1613 * @brief Validate the predicate(s) from the leafref path.
1614 * @param[in] ctx Compile context
1615 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1616 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
1617 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001618 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001619 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1620 */
1621static LY_ERR
1622lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001623 const struct lysc_node *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001624{
1625 LY_ERR ret = LY_EVALID;
1626 const struct lys_module *mod;
1627 const struct lysc_node *src_node, *dst_node;
1628 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1629 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
1630 unsigned int dest_parent_times;
1631 const char *start, *end, *pke_end;
1632 struct ly_set keys = {0};
1633 int i;
1634
1635 while (**predicate == '[') {
1636 start = (*predicate)++;
1637
1638 while (isspace(**predicate)) {
1639 ++(*predicate);
1640 }
1641 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1642 while (isspace(**predicate)) {
1643 ++(*predicate);
1644 }
1645 if (**predicate != '=') {
1646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1647 "Invalid leafref path predicate \"%.*s\" - missing \"=\".", *predicate - start + 1, *predicate);
1648 goto cleanup;
1649 }
1650 ++(*predicate);
1651 while (isspace(**predicate)) {
1652 ++(*predicate);
1653 }
1654
1655 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1657 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1658 goto cleanup;
1659 }
1660 --pke_end;
1661 while (isspace(*pke_end)) {
1662 --pke_end;
1663 }
1664 /* localize path-key-expr */
1665 pke_start = path_key_expr = *predicate;
1666 /* move after the current predicate */
1667 *predicate = end + 1;
1668
1669 /* source (must be leaf or leaf-list) */
1670 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001671 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001672 } else {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001673 mod = path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01001674 }
1675 src_node = lys_child(context_node, mod, src, src_len,
1676 mod->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST, LYS_GETNEXT_NOSTATECHECK);
1677 if (!src_node) {
1678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1679 "Invalid leafref path predicate \"%.*s\" - key node \"%.*s\" from module \"%s\" not found.",
1680 *predicate - start, start, src_len, src, mod->compiled->name);
1681 goto cleanup;
1682 }
1683 /* TODO - check the src_node is really a key of the context_node */
1684
1685 /* check that there is only one predicate for the */
1686 i = ly_set_add(&keys, (void*)src_node, 0);
1687 LY_CHECK_GOTO(i == -1, cleanup);
1688 if (keys.count != (unsigned int)i + 1) {
1689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1690 "Invalid leafref path predicate \"%.*s\" - multiple equality test for the key %s.",
1691 *predicate - start, start, src_node->name);
1692 goto cleanup;
1693 }
1694
1695 /* destination */
1696 dest_parent_times = 0;
1697 dst_node = context_node;
1698
1699 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
1700 if (strncmp(path_key_expr, "current()", 9)) {
1701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1702 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
1703 *predicate - start, start);
1704 goto cleanup;
1705 }
1706 path_key_expr += 9;
1707 while (isspace(*path_key_expr)) {
1708 ++path_key_expr;
1709 }
1710
1711 if (*path_key_expr != '/') {
1712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1713 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
1714 *predicate - start, start);
1715 goto cleanup;
1716 }
1717 ++path_key_expr;
1718 while (isspace(*path_key_expr)) {
1719 ++path_key_expr;
1720 }
1721
1722 /* rel-path-keyexpr:
1723 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
1724 while (!strncmp(path_key_expr, "..", 2)) {
1725 ++dest_parent_times;
1726 path_key_expr += 2;
1727 while (isspace(*path_key_expr)) {
1728 ++path_key_expr;
1729 }
1730 if (*path_key_expr != '/') {
1731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1732 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
1733 *predicate - start, start);
1734 goto cleanup;
1735 }
1736 ++path_key_expr;
1737 while (isspace(*path_key_expr)) {
1738 ++path_key_expr;
1739 }
1740
1741 /* path is supposed to be evaluated in data tree, so we have to skip
1742 * all schema nodes that cannot be instantiated in data tree */
1743 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
1744 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
1745 *predicate - start, start);
1746 }
1747 if (!dest_parent_times) {
1748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1749 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
1750 *predicate - start, start);
1751 goto cleanup;
1752 }
1753 if (path_key_expr == pke_end) {
1754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1755 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
1756 *predicate - start, start);
1757 goto cleanup;
1758 }
1759
1760 while(path_key_expr != pke_end) {
1761 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
1762 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1763 "Invalid node identifier in leafref path predicate - character %d (%.*s).",
1764 path_key_expr - start, *predicate - start, start);
1765 goto cleanup;
1766 }
1767
1768 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001769 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001770 } else {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001771 mod = path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01001772 }
1773 if (!mod) {
1774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1775 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
1776 *predicate - start, start, dst_len, dst);
1777 goto cleanup;
1778 }
1779
1780 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
1781 if (!dst_node) {
1782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1783 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
1784 *predicate - start, start, path_key_expr - pke_start, pke_start);
1785 goto cleanup;
1786 }
1787 }
1788 if (!(dst_node->nodetype & (dst_node->module->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
1789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1790 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s.",
1791 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
1792 goto cleanup;
1793 }
1794 }
1795
1796 ret = LY_SUCCESS;
1797cleanup:
1798 ly_set_erase(&keys, NULL);
1799 return ret;
1800}
1801
1802/**
1803 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
1804 *
1805 * path-arg = absolute-path / relative-path
1806 * absolute-path = 1*("/" (node-identifier *path-predicate))
1807 * relative-path = 1*(".." "/") descendant-path
1808 *
1809 * @param[in,out] path Path to parse.
1810 * @param[out] prefix Prefix of the token, NULL if there is not any.
1811 * @param[out] pref_len Length of the prefix, 0 if there is not any.
1812 * @param[out] name Name of the token.
1813 * @param[out] nam_len Length of the name.
1814 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
1815 * must not be changed between consecutive calls. -1 if the
1816 * path is absolute.
1817 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
1818 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
1819 */
1820static LY_ERR
1821lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
1822 int *parent_times, int *has_predicate)
1823{
1824 int par_times = 0;
1825
1826 assert(path && *path);
1827 assert(parent_times);
1828 assert(prefix);
1829 assert(prefix_len);
1830 assert(name);
1831 assert(name_len);
1832 assert(has_predicate);
1833
1834 *prefix = NULL;
1835 *prefix_len = 0;
1836 *name = NULL;
1837 *name_len = 0;
1838 *has_predicate = 0;
1839
1840 if (!*parent_times) {
1841 if (!strncmp(*path, "..", 2)) {
1842 *path += 2;
1843 ++par_times;
1844 while (!strncmp(*path, "/..", 3)) {
1845 *path += 3;
1846 ++par_times;
1847 }
1848 }
1849 if (par_times) {
1850 *parent_times = par_times;
1851 } else {
1852 *parent_times = -1;
1853 }
1854 }
1855
1856 if (**path != '/') {
1857 return LY_EINVAL;
1858 }
1859 /* skip '/' */
1860 ++(*path);
1861
1862 /* node-identifier ([prefix:]name) */
1863 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
1864
1865 if ((**path == '/' && (*path)[1]) || !**path) {
1866 /* path continues by another token or this is the last token */
1867 return LY_SUCCESS;
1868 } else if ((*path)[0] != '[') {
1869 /* unexpected character */
1870 return LY_EINVAL;
1871 } else {
1872 /* predicate starting with [ */
1873 *has_predicate = 1;
1874 return LY_SUCCESS;
1875 }
1876}
1877
1878/**
Radek Krejci58d171e2018-11-23 13:50:55 +01001879 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
1880 *
1881 * The set of features used for target must be a subset of features used for the leafref.
1882 * This is not a perfect, we should compare the truth tables but it could require too much resources
1883 * and RFC 7950 does not require it explicitely, so we simplify that.
1884 *
1885 * @param[in] refnode The leafref node.
1886 * @param[in] target Tha target node of the leafref.
1887 * @return LY_SUCCESS or LY_EVALID;
1888 */
1889static LY_ERR
1890lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
1891{
1892 LY_ERR ret = LY_EVALID;
1893 const struct lysc_node *iter;
1894 struct lysc_iffeature **iff;
1895 unsigned int u, v, count;
1896 struct ly_set features = {0};
1897
1898 for (iter = refnode; iter; iter = iter->parent) {
1899 iff = lysc_node_iff(iter);
1900 if (iff && *iff) {
1901 LY_ARRAY_FOR(*iff, u) {
1902 LY_ARRAY_FOR((*iff)[u].features, v) {
1903 LY_CHECK_GOTO(ly_set_add(&features, (*iff)[u].features[v], 0) == -1, cleanup);
1904 }
1905 }
1906 }
1907 }
1908
1909 /* we should have, in features set, a superset of features applicable to the target node.
1910 * So when adding features applicable to the target into the features set, we should not be
1911 * able to actually add any new feature, otherwise it is not a subset of features applicable
1912 * to the leafref itself. */
1913 count = features.count;
1914 for (iter = target; iter; iter = iter->parent) {
1915 iff = lysc_node_iff(iter);
1916 if (iff && *iff) {
1917 LY_ARRAY_FOR(*iff, u) {
1918 LY_ARRAY_FOR((*iff)[u].features, v) {
1919 if ((unsigned int)ly_set_add(&features, (*iff)[u].features[v], 0) >= count) {
1920 /* new feature was added (or LY_EMEM) */
1921 goto cleanup;
1922 }
1923 }
1924 }
1925 }
1926 }
1927 ret = LY_SUCCESS;
1928
1929cleanup:
1930 ly_set_erase(&features, NULL);
1931 return ret;
1932}
1933
1934/**
Radek Krejcia3045382018-11-22 14:30:31 +01001935 * @brief Validate the leafref path.
1936 * @param[in] ctx Compile context
1937 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01001938 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01001939 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1940 */
1941static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01001942lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01001943{
1944 const struct lysc_node *node = NULL, *parent = NULL;
1945 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001946 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01001947 const char *id, *prefix, *name;
1948 size_t prefix_len, name_len;
1949 int parent_times = 0, has_predicate;
1950 unsigned int iter, u;
1951 LY_ERR ret = LY_SUCCESS;
1952
1953 assert(ctx);
1954 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001955 assert(leafref);
1956
1957 /* 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 +01001958
1959 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001960 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01001961 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
1962 if (!iter) { /* first iteration */
1963 /* precess ".." in relative paths */
1964 if (parent_times > 0) {
1965 /* move from the context node */
1966 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
1967 /* path is supposed to be evaluated in data tree, so we have to skip
1968 * all schema nodes that cannot be instantiated in data tree */
1969 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001970 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001971 }
1972 }
1973 }
1974
1975 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001976 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001977 } else {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001978 mod = leafref->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01001979 }
1980 if (!mod) {
1981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001982 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
1983 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001984 return LY_EVALID;
1985 }
1986
1987 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
1988 if (!node) {
1989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001990 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001991 return LY_EVALID;
1992 }
1993 parent = node;
1994
1995 if (has_predicate) {
1996 /* we have predicate, so the current result must be list */
1997 if (node->nodetype != LYS_LIST) {
1998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1999 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002000 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002001 return LY_EVALID;
2002 }
2003
Radek Krejci412ddfa2018-11-23 11:44:11 +01002004 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, node, leafref->path_context), LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002005 }
2006
2007 ++iter;
2008 }
2009 if (ret) {
2010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002011 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002012 return LY_EVALID;
2013 }
2014
2015 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2017 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002018 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002019 return LY_EVALID;
2020 }
2021
2022 /* check status */
2023 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2024 return LY_EVALID;
2025 }
2026
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002027 /* check config */
2028 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2029 if (node->flags & LYS_CONFIG_R) {
2030 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2031 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2032 leafref->path);
2033 return LY_EVALID;
2034 }
2035 }
2036
Radek Krejci412ddfa2018-11-23 11:44:11 +01002037 /* store the target's type and check for circular chain of leafrefs */
2038 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2039 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2040 if (type == (struct lysc_type*)leafref) {
2041 /* circular chain detected */
2042 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2043 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2044 return LY_EVALID;
2045 }
2046 }
2047
Radek Krejci58d171e2018-11-23 13:50:55 +01002048 /* check if leafref and its target are under common if-features */
2049 if (lys_compile_leafref_features_validate(startnode, node)) {
2050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2051 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2052 leafref->path);
2053 return LY_EVALID;
2054 }
2055
Radek Krejcia3045382018-11-22 14:30:31 +01002056 return LY_SUCCESS;
2057}
2058
2059/**
2060 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2061 * @param[in] ctx Compile context.
2062 * @param[in] type_p Parsed type to compile.
2063 * @param[in] basetype Base YANG built-in type of the type to compile.
2064 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2065 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2066 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2067 * @param[out] type Newly created type structure with the filled information about the type.
2068 * @return LY_ERR value.
2069 */
Radek Krejci19a96102018-11-15 13:38:09 +01002070static LY_ERR
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002071lys_compile_type_(struct lysc_ctx *ctx, struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options,
2072 const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002073{
2074 LY_ERR ret = LY_SUCCESS;
2075 unsigned int u;
2076 struct lysc_type_bin *bin;
2077 struct lysc_type_num *num;
2078 struct lysc_type_str *str;
2079 struct lysc_type_bits *bits;
2080 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002081 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002082 struct lysc_type_identityref *idref;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002083
2084 switch (basetype) {
2085 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002086 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002087
2088 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002089 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002090 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002091 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2092 LY_CHECK_RET(ret);
2093 if (!tpdfname) {
2094 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2095 options, u, lys_compile_ext, ret, done);
2096 }
2097 }
2098
2099 if (tpdfname) {
2100 type_p->compiled = *type;
2101 *type = calloc(1, sizeof(struct lysc_type_bin));
2102 }
2103 break;
2104 case LY_TYPE_BITS:
2105 /* RFC 7950 9.7 - bits */
2106 bits = (struct lysc_type_bits*)(*type);
2107 if (type_p->bits) {
2108 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2109 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2110 (struct lysc_type_enum_item**)&bits->bits);
2111 LY_CHECK_RET(ret);
2112 }
2113
Radek Krejci555cb5b2018-11-16 14:54:33 +01002114 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002115 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002116 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002118 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002120 free(*type);
2121 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002122 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002123 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002124 }
2125
2126 if (tpdfname) {
2127 type_p->compiled = *type;
2128 *type = calloc(1, sizeof(struct lysc_type_bits));
2129 }
2130 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002131 case LY_TYPE_DEC64:
2132 dec = (struct lysc_type_dec*)(*type);
2133
2134 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002135 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002136 if (!type_p->fraction_digits) {
2137 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002139 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002140 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002141 free(*type);
2142 *type = NULL;
2143 }
2144 return LY_EVALID;
2145 }
2146 } else if (type_p->fraction_digits) {
2147 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002148 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2150 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002151 tpdfname);
2152 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2154 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002155 free(*type);
2156 *type = NULL;
2157 }
2158 return LY_EVALID;
2159 }
2160 dec->fraction_digits = type_p->fraction_digits;
2161
2162 /* RFC 7950 9.2.4 - range */
2163 if (type_p->range) {
2164 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2165 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2166 LY_CHECK_RET(ret);
2167 if (!tpdfname) {
2168 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2169 options, u, lys_compile_ext, ret, done);
2170 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002171 }
2172
2173 if (tpdfname) {
2174 type_p->compiled = *type;
2175 *type = calloc(1, sizeof(struct lysc_type_dec));
2176 }
2177 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002178 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002179 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002180
2181 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002182 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002183 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002184 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2185 LY_CHECK_RET(ret);
2186 if (!tpdfname) {
2187 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2188 options, u, lys_compile_ext, ret, done);
2189 }
2190 } else if (base && ((struct lysc_type_str*)base)->length) {
2191 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2192 }
2193
2194 /* RFC 7950 9.4.5 - pattern */
2195 if (type_p->patterns) {
2196 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2197 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2198 LY_CHECK_RET(ret);
2199 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2200 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2201 }
2202
2203 if (tpdfname) {
2204 type_p->compiled = *type;
2205 *type = calloc(1, sizeof(struct lysc_type_str));
2206 }
2207 break;
2208 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002209 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002210
2211 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002212 if (type_p->enums) {
2213 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2214 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2215 LY_CHECK_RET(ret);
2216 }
2217
Radek Krejci555cb5b2018-11-16 14:54:33 +01002218 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002219 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002220 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002222 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002223 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002224 free(*type);
2225 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002226 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002227 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002228 }
2229
2230 if (tpdfname) {
2231 type_p->compiled = *type;
2232 *type = calloc(1, sizeof(struct lysc_type_enum));
2233 }
2234 break;
2235 case LY_TYPE_INT8:
2236 case LY_TYPE_UINT8:
2237 case LY_TYPE_INT16:
2238 case LY_TYPE_UINT16:
2239 case LY_TYPE_INT32:
2240 case LY_TYPE_UINT32:
2241 case LY_TYPE_INT64:
2242 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002243 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002244
2245 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002246 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002247 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002248 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2249 LY_CHECK_RET(ret);
2250 if (!tpdfname) {
2251 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2252 options, u, lys_compile_ext, ret, done);
2253 }
2254 }
2255
2256 if (tpdfname) {
2257 type_p->compiled = *type;
2258 *type = calloc(1, sizeof(struct lysc_type_num));
2259 }
2260 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002261 case LY_TYPE_IDENT:
2262 idref = (struct lysc_type_identityref*)(*type);
2263
2264 /* RFC 7950 9.10.2 - base */
2265 if (type_p->bases) {
2266 if (base) {
2267 /* only the directly derived identityrefs can contain base specification */
2268 if (tpdfname) {
2269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2270 "Invalid base substatement for type \"%s\" not directly derived from identityref built-in type.",
2271 tpdfname);
2272 } else {
2273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2274 "Invalid base substatement for type not directly derived from identityref built-in type.");
2275 free(*type);
2276 *type = NULL;
2277 }
2278 return LY_EVALID;
2279 }
2280 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2281 LY_CHECK_RET(ret);
2282 }
2283
2284 if (!base && !type_p->flags) {
2285 /* type derived from identityref built-in type must contain at least one base */
2286 if (tpdfname) {
2287 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2288 } else {
2289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2290 free(*type);
2291 *type = NULL;
2292 }
2293 return LY_EVALID;
2294 }
2295
2296 if (tpdfname) {
2297 type_p->compiled = *type;
2298 *type = calloc(1, sizeof(struct lysc_type_identityref));
2299 }
2300 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002301 case LY_TYPE_LEAFREF:
2302 /* RFC 7950 9.9.3 - require-instance */
2303 if (type_p->flags & LYS_SET_REQINST) {
2304 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002305 } else if (base) {
2306 /* inherit */
2307 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002308 } else {
2309 /* default is true */
2310 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2311 }
2312 if (type_p->path) {
2313 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002314 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002315 } else if (base) {
2316 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002317 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002318 } else if (tpdfname) {
2319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2320 return LY_EVALID;
2321 } else {
2322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2323 free(*type);
2324 *type = NULL;
2325 return LY_EVALID;
2326 }
2327 if (tpdfname) {
2328 type_p->compiled = *type;
2329 *type = calloc(1, sizeof(struct lysc_type_leafref));
2330 }
2331 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002332 case LY_TYPE_INST:
2333 /* RFC 7950 9.9.3 - require-instance */
2334 if (type_p->flags & LYS_SET_REQINST) {
2335 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2336 } else {
2337 /* default is true */
2338 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2339 }
2340
2341 if (tpdfname) {
2342 type_p->compiled = *type;
2343 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2344 }
2345 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002346 case LY_TYPE_BOOL:
2347 case LY_TYPE_EMPTY:
2348 case LY_TYPE_UNKNOWN: /* just to complete switch */
2349 break;
2350 }
2351 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2352done:
2353 return ret;
2354}
2355
Radek Krejcia3045382018-11-22 14:30:31 +01002356/**
2357 * @brief Compile information about the leaf/leaf-list's type.
2358 * @param[in] ctx Compile context.
2359 * @param[in] leaf_p Parsed leaf with the type to compile.
2360 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2361 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
2362 * @return LY_ERR value.
2363 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002364static LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +01002365lys_compile_type(struct lysc_ctx *ctx, struct lysp_node_leaf *leaf_p, int options, struct lysc_type **type)
2366{
2367 LY_ERR ret = LY_SUCCESS;
2368 unsigned int u;
2369 struct lysp_type *type_p = &leaf_p->type;
2370 struct type_context {
2371 const struct lysp_tpdf *tpdf;
2372 struct lysp_node *node;
2373 struct lysp_module *mod;
2374 } *tctx, *tctx_prev = NULL;
2375 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002376 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002377 struct ly_set tpdf_chain = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01002378
2379 (*type) = NULL;
2380
2381 tctx = calloc(1, sizeof *tctx);
2382 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2383 for (ret = lysp_type_find(type_p->name, (struct lysp_node*)leaf_p, ctx->mod->parsed,
2384 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2385 ret == LY_SUCCESS;
2386 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2387 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2388 if (basetype) {
2389 break;
2390 }
2391
2392 /* check status */
2393 ret = lysc_check_status(ctx, leaf_p->flags, ctx->mod->parsed, leaf_p->name,
2394 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->mod->name);
2395 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2396
2397 if (tctx->tpdf->type.compiled) {
2398 /* it is not necessary to continue, the rest of the chain was already compiled */
2399 basetype = tctx->tpdf->type.compiled->basetype;
2400 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2401 tctx = NULL;
2402 break;
2403 }
2404
2405 /* store information for the following processing */
2406 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2407
2408 /* prepare next loop */
2409 tctx_prev = tctx;
2410 tctx = calloc(1, sizeof *tctx);
2411 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2412 }
2413 free(tctx);
2414
2415 /* allocate type according to the basetype */
2416 switch (basetype) {
2417 case LY_TYPE_BINARY:
2418 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002419 break;
2420 case LY_TYPE_BITS:
2421 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002422 break;
2423 case LY_TYPE_BOOL:
2424 case LY_TYPE_EMPTY:
2425 *type = calloc(1, sizeof(struct lysc_type));
2426 break;
2427 case LY_TYPE_DEC64:
2428 *type = calloc(1, sizeof(struct lysc_type_dec));
2429 break;
2430 case LY_TYPE_ENUM:
2431 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002432 break;
2433 case LY_TYPE_IDENT:
2434 *type = calloc(1, sizeof(struct lysc_type_identityref));
2435 break;
2436 case LY_TYPE_INST:
2437 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2438 break;
2439 case LY_TYPE_LEAFREF:
2440 *type = calloc(1, sizeof(struct lysc_type_leafref));
2441 break;
2442 case LY_TYPE_STRING:
2443 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002444 break;
2445 case LY_TYPE_UNION:
2446 *type = calloc(1, sizeof(struct lysc_type_union));
2447 break;
2448 case LY_TYPE_INT8:
2449 case LY_TYPE_UINT8:
2450 case LY_TYPE_INT16:
2451 case LY_TYPE_UINT16:
2452 case LY_TYPE_INT32:
2453 case LY_TYPE_UINT32:
2454 case LY_TYPE_INT64:
2455 case LY_TYPE_UINT64:
2456 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002457 break;
2458 case LY_TYPE_UNKNOWN:
2459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2460 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2461 ret = LY_EVALID;
2462 goto cleanup;
2463 }
2464 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
2465 if (~type_substmt_map[basetype] & leaf_p->type.flags) {
2466 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2467 ly_data_type2str[basetype]);
2468 free(*type);
2469 (*type) = NULL;
2470 ret = LY_EVALID;
2471 goto cleanup;
2472 }
2473
2474 /* get restrictions from the referred typedefs */
2475 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2476 tctx = (struct type_context*)tpdf_chain.objs[u];
2477 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2479 tctx->tpdf->name, ly_data_type2str[basetype]);
2480 ret = LY_EVALID;
2481 goto cleanup;
2482 } else if (tctx->tpdf->type.compiled) {
2483 base = tctx->tpdf->type.compiled;
2484 continue;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002485 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002486 /* no change, just use the type information from the base */
2487 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2488 ++base->refcount;
2489 continue;
2490 }
2491
2492 ++(*type)->refcount;
2493 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002494 prev_type = *type;
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002495 ret = lys_compile_type_(ctx, &((struct lysp_tpdf*)tctx->tpdf)->type,
2496 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2497 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002498 LY_CHECK_GOTO(ret, cleanup);
2499 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002500 }
2501
Radek Krejcic5c27e52018-11-15 14:38:11 +01002502 /* process the type definition in leaf */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002503 if (leaf_p->type.flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002504 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002505 (*type)->basetype = basetype;
2506 ++(*type)->refcount;
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002507 ret = lys_compile_type_(ctx, &leaf_p->type, ctx->mod, basetype, options, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002508 LY_CHECK_GOTO(ret, cleanup);
2509 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002510 /* no specific restriction in leaf's type definition, copy from the base */
2511 free(*type);
2512 (*type) = base;
2513 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002514 }
2515
2516 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
2517
2518cleanup:
2519 ly_set_erase(&tpdf_chain, free);
2520 return ret;
2521}
2522
2523static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent);
2524
Radek Krejcia3045382018-11-22 14:30:31 +01002525/**
2526 * @brief Compile parsed container node information.
2527 * @param[in] ctx Compile context
2528 * @param[in] node_p Parsed container node.
2529 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2530 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2531 * is enriched with the container-specific information.
2532 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2533 */
Radek Krejci19a96102018-11-15 13:38:09 +01002534static LY_ERR
2535lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2536{
2537 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
2538 struct lysc_node_container *cont = (struct lysc_node_container*)node;
2539 struct lysp_node *child_p;
2540 unsigned int u;
2541 LY_ERR ret = LY_SUCCESS;
2542
2543 COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done);
2544 COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done);
2545
2546 LY_LIST_FOR(cont_p->child, child_p) {
2547 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2548 }
2549
2550 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2551 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2552 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2553
2554done:
2555 return ret;
2556}
2557
Radek Krejcia3045382018-11-22 14:30:31 +01002558/**
2559 * @brief Compile parsed leaf node information.
2560 * @param[in] ctx Compile context
2561 * @param[in] node_p Parsed leaf node.
2562 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2563 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2564 * is enriched with the leaf-specific information.
2565 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2566 */
Radek Krejci19a96102018-11-15 13:38:09 +01002567static LY_ERR
2568lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2569{
2570 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2571 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2572 unsigned int u;
2573 LY_ERR ret = LY_SUCCESS;
2574
2575 COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done);
2576 COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002577 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
2578 ret = lys_compile_type(ctx, leaf_p, options, &leaf->type);
2579 LY_CHECK_GOTO(ret, done);
2580
Radek Krejcia3045382018-11-22 14:30:31 +01002581 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2582 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2583 ly_set_add(&ctx->unres, leaf, 0);
2584 }
2585
Radek Krejci19a96102018-11-15 13:38:09 +01002586 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2587 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
2588done:
2589 return ret;
2590}
2591
Radek Krejcia3045382018-11-22 14:30:31 +01002592/**
2593 * @brief Compile parsed schema node information.
2594 * @param[in] ctx Compile context
2595 * @param[in] node_p Parsed schema node.
2596 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2597 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
2598 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
2599 * the compile context.
2600 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2601 */
Radek Krejci19a96102018-11-15 13:38:09 +01002602static LY_ERR
2603lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
2604{
2605 LY_ERR ret = LY_EVALID;
2606 struct lysc_node *node, **children;
2607 unsigned int u;
2608 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
2609
2610 switch (node_p->nodetype) {
2611 case LYS_CONTAINER:
2612 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
2613 node_compile_spec = lys_compile_node_container;
2614 break;
2615 case LYS_LEAF:
2616 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
2617 node_compile_spec = lys_compile_node_leaf;
2618 break;
2619 case LYS_LIST:
2620 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
2621 break;
2622 case LYS_LEAFLIST:
2623 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
2624 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002625 case LYS_CHOICE:
2626 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
2627 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002628 case LYS_ANYXML:
2629 case LYS_ANYDATA:
2630 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
2631 break;
2632 default:
2633 LOGINT(ctx->ctx);
2634 return LY_EINT;
2635 }
2636 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
2637 node->nodetype = node_p->nodetype;
2638 node->module = ctx->mod;
2639 node->prev = node;
2640 node->flags = node_p->flags;
2641
2642 /* config */
2643 if (!(node->flags & LYS_CONFIG_MASK)) {
2644 /* config not explicitely set, inherit it from parent */
2645 if (parent) {
2646 node->flags |= parent->flags & LYS_CONFIG_MASK;
2647 } else {
2648 /* default is config true */
2649 node->flags |= LYS_CONFIG_W;
2650 }
2651 }
2652
2653 /* status - it is not inherited by specification, but it does not make sense to have
2654 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2655 if (!(node->flags & LYS_STATUS_MASK)) {
2656 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2657 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2658 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2659 node->flags |= parent->flags & LYS_STATUS_MASK;
2660 } else {
2661 node->flags |= LYS_STATUS_CURR;
2662 }
2663 } else if (parent) {
2664 /* check status compatibility with the parent */
2665 if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
2666 if (node->flags & LYS_STATUS_CURR) {
2667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2668 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2669 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2670 } else { /* LYS_STATUS_DEPRC */
2671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2672 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2673 }
2674 goto error;
2675 }
2676 }
2677
2678 if (!(options & LYSC_OPT_FREE_SP)) {
2679 node->sp = node_p;
2680 }
2681 DUP_STRING(ctx->ctx, node_p->name, node->name);
2682 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
2683
2684 /* nodetype-specific part */
2685 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
2686
2687 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01002688 if (parent) {
2689 if (parent->nodetype == LYS_CHOICE) {
2690 /* TODO exception for cases */
2691 } else if ((children = lysc_node_children(parent))) {
2692 if (!(*children)) {
2693 /* first child */
2694 *children = node;
2695 } else {
2696 /* insert at the end of the parent's children list */
2697 (*children)->prev->next = node;
2698 node->prev = (*children)->prev;
2699 (*children)->prev = node;
2700 }
Radek Krejci19a96102018-11-15 13:38:09 +01002701 }
2702 } else {
2703 /* top-level element */
2704 if (!ctx->mod->compiled->data) {
2705 ctx->mod->compiled->data = node;
2706 } else {
2707 /* insert at the end of the module's top-level nodes list */
2708 ctx->mod->compiled->data->prev->next = node;
2709 node->prev = ctx->mod->compiled->data->prev;
2710 ctx->mod->compiled->data->prev = node;
2711 }
2712 }
2713
2714 return LY_SUCCESS;
2715
2716error:
2717 lysc_node_free(ctx->ctx, node);
2718 return ret;
2719}
2720
Radek Krejcia3045382018-11-22 14:30:31 +01002721/**
2722 * @brief Compile the given YANG module.
2723 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
2724 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2725 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2726 */
Radek Krejci19a96102018-11-15 13:38:09 +01002727LY_ERR
2728lys_compile(struct lys_module *mod, int options)
2729{
2730 struct lysc_ctx ctx = {0};
2731 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002732 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01002733 struct lysp_module *sp;
2734 struct lysp_node *node_p;
2735 unsigned int u;
2736 LY_ERR ret;
2737
2738 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
2739 sp = mod->parsed;
2740
2741 if (sp->submodule) {
2742 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
2743 return LY_EINVAL;
2744 }
2745
2746 ctx.ctx = sp->ctx;
2747 ctx.mod = mod;
2748
2749 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
2750 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
2751 mod_c->ctx = sp->ctx;
2752 mod_c->implemented = sp->implemented;
2753 mod_c->latest_revision = sp->latest_revision;
2754 mod_c->version = sp->version;
2755
2756 DUP_STRING(sp->ctx, sp->name, mod_c->name);
2757 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
2758 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
2759 if (sp->revs) {
2760 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
2761 }
2762 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
2763 COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
2764 COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
2765 if (sp->identities) {
2766 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
2767 }
2768
2769 LY_LIST_FOR(sp->data, node_p) {
2770 ret = lys_compile_node(&ctx, node_p, options, NULL);
2771 LY_CHECK_GOTO(ret, error);
2772 }
2773 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
2774 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
2775
2776 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
2777
Radek Krejcia3045382018-11-22 14:30:31 +01002778 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002779 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
2780 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
2781 * 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 +01002782 for (u = 0; u < ctx.unres.count; ++u) {
2783 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) {
2784 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2785 if (type->basetype == LY_TYPE_LEAFREF) {
2786 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002787 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 +01002788 LY_CHECK_GOTO(ret, error);
2789 }
2790 }
2791 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01002792 for (u = 0; u < ctx.unres.count; ++u) {
2793 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) {
2794 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2795 if (type->basetype == LY_TYPE_LEAFREF) {
2796 /* store pointer to the real type */
2797 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
2798 typeiter->basetype == LY_TYPE_LEAFREF;
2799 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
2800 ((struct lysc_type_leafref*)type)->realtype = typeiter;
2801 }
2802 }
2803 }
Radek Krejcia3045382018-11-22 14:30:31 +01002804 ly_set_erase(&ctx.unres, NULL);
2805
Radek Krejci19a96102018-11-15 13:38:09 +01002806 if (options & LYSC_OPT_FREE_SP) {
2807 lysp_module_free(mod->parsed);
2808 ((struct lys_module*)mod)->parsed = NULL;
2809 }
2810
2811 ((struct lys_module*)mod)->compiled = mod_c;
2812 return LY_SUCCESS;
2813
2814error:
Radek Krejcia3045382018-11-22 14:30:31 +01002815 ly_set_erase(&ctx.unres, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01002816 lysc_module_free(mod_c, NULL);
2817 ((struct lys_module*)mod)->compiled = NULL;
2818 return ret;
2819}