blob: c25a07cf8d97eb44bf76e9488c8fd7ee8ab5ff8f [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <ctype.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <stdio.h>
Radek Krejci19a96102018-11-15 13:38:09 +010019
20#include "libyang.h"
21#include "context.h"
22#include "tree_schema_internal.h"
23#include "xpath.h"
24
25/**
26 * @brief Duplicate string into dictionary
27 * @param[in] CTX libyang context of the dictionary.
28 * @param[in] ORIG String to duplicate.
29 * @param[out] DUP Where to store the result.
30 */
31#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
32
33#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
34 if (ARRAY_P) { \
35 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
36 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
37 LY_ARRAY_INCREMENT(ARRAY_C); \
38 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER]); \
39 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
40 } \
41 }
42
43#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
44 if (MEMBER_P) { \
45 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
46 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
47 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
48 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
49 }
50
Radek Krejci19a96102018-11-15 13:38:09 +010051static struct lysc_ext_instance *
52lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
53{
54 /* TODO */
55 (void) ctx;
56 (void) orig;
57 return NULL;
58}
59
60static struct lysc_pattern*
61lysc_pattern_dup(struct lysc_pattern *orig)
62{
63 ++orig->refcount;
64 return orig;
65}
66
67static struct lysc_pattern**
68lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
69{
70 struct lysc_pattern **dup;
71 unsigned int u;
72
73 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
74 LY_ARRAY_FOR(orig, u) {
75 dup[u] = lysc_pattern_dup(orig[u]);
76 LY_ARRAY_INCREMENT(dup);
77 }
78 return dup;
79}
80
81struct lysc_range*
82lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
83{
84 struct lysc_range *dup;
85 LY_ERR ret;
86
87 dup = calloc(1, sizeof *dup);
88 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
89 if (orig->parts) {
90 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
91 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
92 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
93 }
94 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
95 DUP_STRING(ctx, orig->emsg, dup->emsg);
96 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
97
98 return dup;
99cleanup:
100 free(dup);
101 (void) ret; /* set but not used due to the return type */
102 return NULL;
103}
104
105struct iff_stack {
106 int size;
107 int index; /* first empty item */
108 uint8_t *stack;
109};
110
111static LY_ERR
112iff_stack_push(struct iff_stack *stack, uint8_t value)
113{
114 if (stack->index == stack->size) {
115 stack->size += 4;
116 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
117 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
118 }
119 stack->stack[stack->index++] = value;
120 return LY_SUCCESS;
121}
122
123static uint8_t
124iff_stack_pop(struct iff_stack *stack)
125{
126 stack->index--;
127 return stack->stack[stack->index];
128}
129
130static void
131iff_stack_clean(struct iff_stack *stack)
132{
133 stack->size = 0;
134 free(stack->stack);
135}
136
137static void
138iff_setop(uint8_t *list, uint8_t op, int pos)
139{
140 uint8_t *item;
141 uint8_t mask = 3;
142
143 assert(pos >= 0);
144 assert(op <= 3); /* max 2 bits */
145
146 item = &list[pos / 4];
147 mask = mask << 2 * (pos % 4);
148 *item = (*item) & ~mask;
149 *item = (*item) | (op << 2 * (pos % 4));
150}
151
152#define LYS_IFF_LP 0x04 /* ( */
153#define LYS_IFF_RP 0x08 /* ) */
154
155static struct lysc_feature *
156lysc_feature_find(struct lysc_module *mod, const char *name, size_t len)
157{
158 size_t i;
159 struct lysc_feature *f;
160
161 for (i = 0; i < len; ++i) {
162 if (name[i] == ':') {
163 /* we have a prefixed feature */
164 mod = lysc_module_find_prefix(mod, name, i);
165 LY_CHECK_RET(!mod, NULL);
166
167 name = &name[i + 1];
168 len = len - i - 1;
169 }
170 }
171
172 /* we have the correct module, get the feature */
173 LY_ARRAY_FOR(mod->features, i) {
174 f = &mod->features[i];
175 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
176 return f;
177 }
178 }
179
180 return NULL;
181}
182
183static LY_ERR
184lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
185{
186 const char *name;
187 unsigned int u;
188 const struct lys_module *mod;
189 struct lysp_ext *edef = NULL;
190
191 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
192 ext->insubstmt = ext_p->insubstmt;
193 ext->insubstmt_index = ext_p->insubstmt_index;
194
195 /* get module where the extension definition should be placed */
196 for (u = 0; ext_p->name[u] != ':'; ++u);
197 mod = lys_module_find_prefix(ctx->mod, ext_p->name, u);
198 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
199 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
200 LY_EVALID);
201 LY_CHECK_ERR_RET(!mod->parsed->extensions,
202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
203 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
204 ext_p->name, mod->parsed->name),
205 LY_EVALID);
206 name = &ext_p->name[u + 1];
207 /* find the extension definition there */
208 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
209 if (!strcmp(name, mod->parsed->extensions[u].name)) {
210 edef = &mod->parsed->extensions[u];
211 break;
212 }
213 }
214 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
215 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
216 LY_EVALID);
217 /* TODO plugins */
218
219 return LY_SUCCESS;
220}
221
222static LY_ERR
223lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
224{
225 const char *c = *value;
226 int r, rc = EXIT_FAILURE;
227 int i, j, last_not, checkversion = 0;
228 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
229 uint8_t op;
230 struct iff_stack stack = {0, 0, NULL};
231 struct lysc_feature *f;
232
233 assert(c);
234
235 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
236 for (i = j = last_not = 0; c[i]; i++) {
237 if (c[i] == '(') {
238 j++;
239 checkversion = 1;
240 continue;
241 } else if (c[i] == ')') {
242 j--;
243 continue;
244 } else if (isspace(c[i])) {
245 checkversion = 1;
246 continue;
247 }
248
249 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
250 if (c[i + r] == '\0') {
251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
252 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
253 return LY_EVALID;
254 } else if (!isspace(c[i + r])) {
255 /* feature name starting with the not/and/or */
256 last_not = 0;
257 f_size++;
258 } else if (c[i] == 'n') { /* not operation */
259 if (last_not) {
260 /* double not */
261 expr_size = expr_size - 2;
262 last_not = 0;
263 } else {
264 last_not = 1;
265 }
266 } else { /* and, or */
267 f_exp++;
268 /* not a not operation */
269 last_not = 0;
270 }
271 i += r;
272 } else {
273 f_size++;
274 last_not = 0;
275 }
276 expr_size++;
277
278 while (!isspace(c[i])) {
279 if (!c[i] || c[i] == ')') {
280 i--;
281 break;
282 }
283 i++;
284 }
285 }
286 if (j || f_exp != f_size) {
287 /* not matching count of ( and ) */
288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
289 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
290 return LY_EVALID;
291 }
292
293 if (checkversion || expr_size > 1) {
294 /* check that we have 1.1 module */
295 if (ctx->mod->compiled->version != LYS_VERSION_1_1) {
296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
297 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
298 return LY_EVALID;
299 }
300 }
301
302 /* allocate the memory */
303 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
304 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
305 stack.stack = malloc(expr_size * sizeof *stack.stack);
306 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
307
308 stack.size = expr_size;
309 f_size--; expr_size--; /* used as indexes from now */
310
311 for (i--; i >= 0; i--) {
312 if (c[i] == ')') {
313 /* push it on stack */
314 iff_stack_push(&stack, LYS_IFF_RP);
315 continue;
316 } else if (c[i] == '(') {
317 /* pop from the stack into result all operators until ) */
318 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
319 iff_setop(iff->expr, op, expr_size--);
320 }
321 continue;
322 } else if (isspace(c[i])) {
323 continue;
324 }
325
326 /* end of operator or operand -> find beginning and get what is it */
327 j = i + 1;
328 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
329 i--;
330 }
331 i++; /* go back by one step */
332
333 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
334 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
335 /* double not */
336 iff_stack_pop(&stack);
337 } else {
338 /* not has the highest priority, so do not pop from the stack
339 * as in case of AND and OR */
340 iff_stack_push(&stack, LYS_IFF_NOT);
341 }
342 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
343 /* as for OR - pop from the stack all operators with the same or higher
344 * priority and store them to the result, then push the AND to the stack */
345 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
346 op = iff_stack_pop(&stack);
347 iff_setop(iff->expr, op, expr_size--);
348 }
349 iff_stack_push(&stack, LYS_IFF_AND);
350 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
351 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
352 op = iff_stack_pop(&stack);
353 iff_setop(iff->expr, op, expr_size--);
354 }
355 iff_stack_push(&stack, LYS_IFF_OR);
356 } else {
357 /* feature name, length is j - i */
358
359 /* add it to the expression */
360 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
361
362 /* now get the link to the feature definition */
363 f = lysc_feature_find(ctx->mod->compiled, &c[i], j - i);
364 LY_CHECK_ERR_GOTO(!f,
365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
366 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
367 rc = LY_EVALID,
368 error)
369 iff->features[f_size] = f;
370 LY_ARRAY_INCREMENT(iff->features);
371 f_size--;
372 }
373 }
374 while (stack.index) {
375 op = iff_stack_pop(&stack);
376 iff_setop(iff->expr, op, expr_size--);
377 }
378
379 if (++expr_size || ++f_size) {
380 /* not all expected operators and operands found */
381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
382 "Invalid value \"%s\" of if-feature - processing error.", *value);
383 rc = LY_EINT;
384 } else {
385 rc = LY_SUCCESS;
386 }
387
388error:
389 /* cleanup */
390 iff_stack_clean(&stack);
391
392 return rc;
393}
394
395static LY_ERR
396lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when)
397{
398 unsigned int u;
399 LY_ERR ret = LY_SUCCESS;
400
401 when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
402 LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done);
403 COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done);
404
405done:
406 return ret;
407}
408
409static LY_ERR
410lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
411{
412 unsigned int u;
413 LY_ERR ret = LY_SUCCESS;
414
415 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
416 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
417
418 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
419 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
420 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
421
422done:
423 return ret;
424}
425
426static LY_ERR
427lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
428{
429 unsigned int u;
430 struct lys_module *mod = NULL;
431 struct lysc_module *comp;
432 LY_ERR ret = LY_SUCCESS;
433
434 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
435 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
436 imp->module = imp_p->module;
437
438 /* make sure that we have both versions (lysp_ and lysc_) of the imported module. To import groupings or
439 * typedefs, the lysp_ is needed. To augment or deviate imported module, we need the lysc_ structure */
440 if (!imp->module->parsed) {
441 comp = imp->module->compiled;
442 /* try to get filepath from the compiled version */
443 if (comp->filepath) {
444 mod = (struct lys_module*)lys_parse_path(ctx->ctx, comp->filepath,
445 !strcmp(&comp->filepath[strlen(comp->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
446 if (mod != imp->module) {
447 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
448 comp->filepath, comp->name);
449 mod = NULL;
450 }
451 }
452 if (!mod) {
453 if (lysp_load_module(ctx->ctx, comp->name, comp->revision, 0, 1, &mod)) {
454 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
455 comp->name, ctx->mod->compiled->name);
456 return LY_ENOTFOUND;
457 }
458 }
459 } else if (!imp->module->compiled) {
460 return lys_compile(imp->module, options);
461 }
462
463done:
464 return ret;
465}
466
467static LY_ERR
468lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *ident)
469{
470 unsigned int u;
471 LY_ERR ret = LY_SUCCESS;
472
473 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
474 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
475 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
476 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
477 ident->flags = ident_p->flags;
478
479done:
480 return ret;
481}
482
Radek Krejcia3045382018-11-22 14:30:31 +0100483/**
484 * @brief Find and process the referenced base identities from another identity or identityref
485 *
486 * For bases in identity se backlinks to them from the base identities. For identityref, store
487 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
488 * to distinguish these two use cases.
489 *
490 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
491 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
492 * @param[in] ident Referencing identity to work with.
493 * @param[in] bases Array of bases of identityref to fill in.
494 * @return LY_ERR value.
495 */
Radek Krejci19a96102018-11-15 13:38:09 +0100496static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100497lys_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 +0100498{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100499 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100500 const char *s, *name;
501 struct lysc_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100502 struct lysc_ident **idref;
503
504 assert(ident || bases);
505
506 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) {
507 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
508 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
509 return LY_EVALID;
510 }
511
512 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
513 s = strchr(bases_p[u], ':');
514 if (s) {
515 /* prefixed identity */
516 name = &s[1];
517 mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]);
518 } else {
519 name = bases_p[u];
520 mod = ctx->mod->compiled;
521 }
522 if (!mod) {
523 if (ident) {
524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
525 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
526 } else {
527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
528 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
529 }
530 return LY_EVALID;
531 }
532 idref = NULL;
533 if (mod->identities) {
534 for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) {
535 if (!strcmp(name, mod->identities[v].name)) {
536 if (ident) {
537 /* we have match! store the backlink */
538 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
539 *idref = ident;
540 } else {
541 /* we have match! store the found identity */
542 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
543 *idref = &mod->identities[v];
544 }
545 break;
546 }
547 }
548 }
549 if (!idref || !(*idref)) {
550 if (ident) {
551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
552 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
553 } else {
554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
555 "Unable to find base (%s) of identityref.", bases_p[u]);
556 }
557 return LY_EVALID;
558 }
559 }
560 return LY_SUCCESS;
561}
562
Radek Krejcia3045382018-11-22 14:30:31 +0100563/**
564 * @brief For the given array of identities, set the backlinks from all their base identities.
565 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
566 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
567 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
568 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
569 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100570static LY_ERR
571lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
572{
573 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100574
575 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
576 if (!idents_p[i].bases) {
577 continue;
578 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100579 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100580 }
581 return LY_SUCCESS;
582}
583
Radek Krejcia3045382018-11-22 14:30:31 +0100584/**
585 * @brief Create compiled feature structure.
586 * @param[in] ctx Compile context.
587 * @param[in] feature_p Parsed feature definition to compile.
588 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
589 * @param[in,out] feature Compiled feature structure to fill.
590 * @return LY_ERR value.
591 */
Radek Krejci19a96102018-11-15 13:38:09 +0100592static LY_ERR
593lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature)
594{
595 unsigned int u, v;
596 LY_ERR ret = LY_SUCCESS;
597 struct lysc_feature **df;
598
599 DUP_STRING(ctx->ctx, feature_p->name, feature->name);
600 feature->flags = feature_p->flags;
601
602 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
603 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
604 if (feature->iffeatures) {
605 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
606 if (feature->iffeatures[u].features) {
607 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
608 /* add itself into the dependants list */
609 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
610 *df = feature;
611 }
612 /* TODO check for circular dependency */
613 }
614 }
615 }
616done:
617 return ret;
618}
619
Radek Krejcia3045382018-11-22 14:30:31 +0100620/**
621 * @brief Validate and normalize numeric value from a range definition.
622 * @param[in] ctx Compile context.
623 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
624 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
625 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
626 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
627 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
628 * @param[in] value String value of the range boundary.
629 * @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.
630 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
631 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
632 */
Radek Krejci19a96102018-11-15 13:38:09 +0100633static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100634range_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 +0100635{
Radek Krejci6cba4292018-11-15 17:33:29 +0100636 size_t fraction = 0, size;
637
Radek Krejci19a96102018-11-15 13:38:09 +0100638 *len = 0;
639
640 assert(value);
641 /* parse value */
642 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
643 return LY_EVALID;
644 }
645
646 if ((value[*len] == '-') || (value[*len] == '+')) {
647 ++(*len);
648 }
649
650 while (isdigit(value[*len])) {
651 ++(*len);
652 }
653
654 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100655 if (basetype == LY_TYPE_DEC64) {
656 goto decimal;
657 } else {
658 *valcopy = strndup(value, *len);
659 return LY_SUCCESS;
660 }
Radek Krejci19a96102018-11-15 13:38:09 +0100661 }
662 fraction = *len;
663
664 ++(*len);
665 while (isdigit(value[*len])) {
666 ++(*len);
667 }
668
Radek Krejci6cba4292018-11-15 17:33:29 +0100669 if (basetype == LY_TYPE_DEC64) {
670decimal:
671 assert(frdigits);
672 if (*len - 1 - fraction > frdigits) {
673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
674 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
675 *len, value, frdigits);
676 return LY_EINVAL;
677 }
678 if (fraction) {
679 size = (*len) + (frdigits - ((*len) - 1 - fraction));
680 } else {
681 size = (*len) + frdigits + 1;
682 }
683 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100684 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
685
Radek Krejci6cba4292018-11-15 17:33:29 +0100686 (*valcopy)[size - 1] = '\0';
687 if (fraction) {
688 memcpy(&(*valcopy)[0], &value[0], fraction);
689 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
690 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
691 } else {
692 memcpy(&(*valcopy)[0], &value[0], *len);
693 memset(&(*valcopy)[*len], '0', frdigits);
694 }
Radek Krejci19a96102018-11-15 13:38:09 +0100695 }
696 return LY_SUCCESS;
697}
698
Radek Krejcia3045382018-11-22 14:30:31 +0100699/**
700 * @brief Check that values in range are in ascendant order.
701 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +0100702 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
703 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +0100704 * @param[in] value Current value to check.
705 * @param[in] prev_value The last seen value.
706 * @return LY_SUCCESS or LY_EEXIST for invalid order.
707 */
Radek Krejci19a96102018-11-15 13:38:09 +0100708static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +0100709range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +0100710{
711 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +0100712 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100713 return LY_EEXIST;
714 }
715 } else {
Radek Krejci5969f272018-11-23 10:03:58 +0100716 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100717 return LY_EEXIST;
718 }
719 }
720 return LY_SUCCESS;
721}
722
Radek Krejcia3045382018-11-22 14:30:31 +0100723/**
724 * @brief Set min/max value of the range part.
725 * @param[in] ctx Compile context.
726 * @param[in] part Range part structure to fill.
727 * @param[in] max Flag to distinguish if storing min or max value.
728 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
729 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
730 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
731 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
732 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +0100733 * @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 +0100734 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
735 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
736 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
737 * frdigits value), LY_EMEM.
738 */
Radek Krejci19a96102018-11-15 13:38:09 +0100739static LY_ERR
740range_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 +0100741 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100742{
743 LY_ERR ret = LY_SUCCESS;
744 char *valcopy = NULL;
745 size_t len;
746
747 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100748 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +0100749 LY_CHECK_GOTO(ret, finalize);
750 }
751 if (!valcopy && base_range) {
752 if (max) {
753 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
754 } else {
755 part->min_64 = base_range->parts[0].min_64;
756 }
757 if (!first) {
758 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
759 }
760 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +0100761 }
762
763 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100764 case LY_TYPE_INT8: /* range */
765 if (valcopy) {
766 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
767 } else if (max) {
768 part->max_64 = INT64_C(127);
769 } else {
770 part->min_64 = INT64_C(-128);
771 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100772 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100773 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100774 }
775 break;
776 case LY_TYPE_INT16: /* range */
777 if (valcopy) {
778 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
779 } else if (max) {
780 part->max_64 = INT64_C(32767);
781 } else {
782 part->min_64 = INT64_C(-32768);
783 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100784 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100785 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100786 }
787 break;
788 case LY_TYPE_INT32: /* range */
789 if (valcopy) {
790 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
791 } else if (max) {
792 part->max_64 = INT64_C(2147483647);
793 } else {
794 part->min_64 = INT64_C(-2147483648);
795 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100796 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100797 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100798 }
799 break;
800 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +0100801 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100802 if (valcopy) {
803 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
804 max ? &part->max_64 : &part->min_64);
805 } else if (max) {
806 part->max_64 = INT64_C(9223372036854775807);
807 } else {
808 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
809 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100810 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100811 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100812 }
813 break;
814 case LY_TYPE_UINT8: /* range */
815 if (valcopy) {
816 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
817 } else if (max) {
818 part->max_u64 = UINT64_C(255);
819 } else {
820 part->min_u64 = UINT64_C(0);
821 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100822 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100823 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100824 }
825 break;
826 case LY_TYPE_UINT16: /* range */
827 if (valcopy) {
828 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
829 } else if (max) {
830 part->max_u64 = UINT64_C(65535);
831 } else {
832 part->min_u64 = UINT64_C(0);
833 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100834 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100835 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100836 }
837 break;
838 case LY_TYPE_UINT32: /* range */
839 if (valcopy) {
840 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
841 } else if (max) {
842 part->max_u64 = UINT64_C(4294967295);
843 } else {
844 part->min_u64 = UINT64_C(0);
845 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100846 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100847 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100848 }
849 break;
850 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100851 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +0100852 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +0100853 if (valcopy) {
854 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
855 } else if (max) {
856 part->max_u64 = UINT64_C(18446744073709551615);
857 } else {
858 part->min_u64 = UINT64_C(0);
859 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100860 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100861 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100862 }
863 break;
864 default:
865 LOGINT(ctx->ctx);
866 ret = LY_EINT;
867 }
868
Radek Krejci5969f272018-11-23 10:03:58 +0100869finalize:
Radek Krejci19a96102018-11-15 13:38:09 +0100870 if (ret == LY_EDENIED) {
871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
872 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
873 length_restr ? "length" : "range", valcopy ? valcopy : *value);
874 } else if (ret == LY_EVALID) {
875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
876 "Invalid %s restriction - invalid value \"%s\".",
877 length_restr ? "length" : "range", valcopy ? valcopy : *value);
878 } else if (ret == LY_EEXIST) {
879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
880 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100881 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +0100882 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +0100883 } else if (!ret && value) {
884 *value = *value + len;
885 }
886 free(valcopy);
887 return ret;
888}
889
Radek Krejcia3045382018-11-22 14:30:31 +0100890/**
891 * @brief Compile the parsed range restriction.
892 * @param[in] ctx Compile context.
893 * @param[in] range_p Parsed range structure to compile.
894 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
895 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
896 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
897 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
898 * range restriction must be more restrictive than the base_range.
899 * @param[in,out] range Pointer to the created current range structure.
900 * @return LY_ERR value.
901 */
Radek Krejci19a96102018-11-15 13:38:09 +0100902static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100903lys_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 +0100904 struct lysc_range *base_range, struct lysc_range **range)
905{
906 LY_ERR ret = LY_EVALID;
907 const char *expr;
908 struct lysc_range_part *parts = NULL, *part;
909 int range_expected = 0, uns;
910 unsigned int parts_done = 0, u, v;
911
912 assert(range);
913 assert(range_p);
914
915 expr = range_p->arg;
916 while(1) {
917 if (isspace(*expr)) {
918 ++expr;
919 } else if (*expr == '\0') {
920 if (range_expected) {
921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
922 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
923 length_restr ? "length" : "range", range_p->arg);
924 goto cleanup;
925 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
927 "Invalid %s restriction - unexpected end of the expression (%s).",
928 length_restr ? "length" : "range", range_p->arg);
929 goto cleanup;
930 }
931 parts_done++;
932 break;
933 } else if (!strncmp(expr, "min", 3)) {
934 if (parts) {
935 /* min cannot be used elsewhere than in the first part */
936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
937 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
938 expr - range_p->arg, range_p->arg);
939 goto cleanup;
940 }
941 expr += 3;
942
943 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +0100944 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 +0100945 part->max_64 = part->min_64;
946 } else if (*expr == '|') {
947 if (!parts || range_expected) {
948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
949 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
950 goto cleanup;
951 }
952 expr++;
953 parts_done++;
954 /* process next part of the expression */
955 } else if (!strncmp(expr, "..", 2)) {
956 expr += 2;
957 while (isspace(*expr)) {
958 expr++;
959 }
960 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
961 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
962 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
963 goto cleanup;
964 }
965 /* continue expecting the upper boundary */
966 range_expected = 1;
967 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
968 /* number */
969 if (range_expected) {
970 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100971 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 +0100972 range_expected = 0;
973 } else {
974 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
975 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 +0100976 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100977 part->max_64 = part->min_64;
978 }
979
980 /* continue with possible another expression part */
981 } else if (!strncmp(expr, "max", 3)) {
982 expr += 3;
983 while (isspace(*expr)) {
984 expr++;
985 }
986 if (*expr != '\0') {
987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
988 length_restr ? "length" : "range", expr);
989 goto cleanup;
990 }
991 if (range_expected) {
992 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100993 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 +0100994 range_expected = 0;
995 } else {
996 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
997 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 +0100998 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100999 part->min_64 = part->max_64;
1000 }
1001 } else {
1002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1003 length_restr ? "length" : "range", expr);
1004 goto cleanup;
1005 }
1006 }
1007
1008 /* check with the previous range/length restriction */
1009 if (base_range) {
1010 switch (basetype) {
1011 case LY_TYPE_BINARY:
1012 case LY_TYPE_UINT8:
1013 case LY_TYPE_UINT16:
1014 case LY_TYPE_UINT32:
1015 case LY_TYPE_UINT64:
1016 case LY_TYPE_STRING:
1017 uns = 1;
1018 break;
1019 case LY_TYPE_DEC64:
1020 case LY_TYPE_INT8:
1021 case LY_TYPE_INT16:
1022 case LY_TYPE_INT32:
1023 case LY_TYPE_INT64:
1024 uns = 0;
1025 break;
1026 default:
1027 LOGINT(ctx->ctx);
1028 ret = LY_EINT;
1029 goto cleanup;
1030 }
1031 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1032 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1033 goto baseerror;
1034 }
1035 /* current lower bound is not lower than the base */
1036 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1037 /* base has single value */
1038 if (base_range->parts[v].min_64 == parts[u].min_64) {
1039 /* both lower bounds are the same */
1040 if (parts[u].min_64 != parts[u].max_64) {
1041 /* current continues with a range */
1042 goto baseerror;
1043 } else {
1044 /* equal single values, move both forward */
1045 ++v;
1046 continue;
1047 }
1048 } else {
1049 /* base is single value lower than current range, so the
1050 * value from base range is removed in the current,
1051 * move only base and repeat checking */
1052 ++v;
1053 --u;
1054 continue;
1055 }
1056 } else {
1057 /* base is the range */
1058 if (parts[u].min_64 == parts[u].max_64) {
1059 /* current is a single value */
1060 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1061 /* current is behind the base range, so base range is omitted,
1062 * move the base and keep the current for further check */
1063 ++v;
1064 --u;
1065 } /* else it is within the base range, so move the current, but keep the base */
1066 continue;
1067 } else {
1068 /* both are ranges - check the higher bound, the lower was already checked */
1069 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1070 /* higher bound is higher than the current higher bound */
1071 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1072 /* but the current lower bound is also higher, so the base range is omitted,
1073 * continue with the same current, but move the base */
1074 --u;
1075 ++v;
1076 continue;
1077 }
1078 /* current range starts within the base range but end behind it */
1079 goto baseerror;
1080 } else {
1081 /* current range is smaller than the base,
1082 * move current, but stay with the base */
1083 continue;
1084 }
1085 }
1086 }
1087 }
1088 if (u != parts_done) {
1089baseerror:
1090 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1091 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1092 length_restr ? "length" : "range", range_p->arg);
1093 goto cleanup;
1094 }
1095 }
1096
1097 if (!(*range)) {
1098 *range = calloc(1, sizeof **range);
1099 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1100 }
1101
1102 if (range_p->eapptag) {
1103 lydict_remove(ctx->ctx, (*range)->eapptag);
1104 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1105 }
1106 if (range_p->emsg) {
1107 lydict_remove(ctx->ctx, (*range)->emsg);
1108 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1109 }
1110 /* extensions are taken only from the last range by the caller */
1111
1112 (*range)->parts = parts;
1113 parts = NULL;
1114 ret = LY_SUCCESS;
1115cleanup:
1116 /* TODO clean up */
1117 LY_ARRAY_FREE(parts);
1118
1119 return ret;
1120}
1121
1122/**
1123 * @brief Checks pattern syntax.
1124 *
Radek Krejcia3045382018-11-22 14:30:31 +01001125 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001126 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001127 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1128 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001129 */
1130static LY_ERR
1131lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1132{
1133 int idx, idx2, start, end, err_offset, count;
1134 char *perl_regex, *ptr;
1135 const char *err_msg, *orig_ptr;
1136 pcre *precomp;
1137#define URANGE_LEN 19
1138 char *ublock2urange[][2] = {
1139 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1140 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1141 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1142 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1143 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1144 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1145 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1146 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1147 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1148 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1149 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1150 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1151 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1152 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1153 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1154 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1155 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1156 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1157 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1158 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1159 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1160 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1161 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1162 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1163 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1164 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1165 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1166 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1167 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1168 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1169 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1170 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1171 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1172 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1173 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1174 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1175 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1176 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1177 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1178 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1179 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1180 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1181 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1182 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1183 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1184 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1185 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1186 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1187 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1188 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1189 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1190 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1191 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1192 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1193 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1194 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1195 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1196 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1197 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1198 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1199 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1200 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1201 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1202 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1203 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1204 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1205 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1206 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1207 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1208 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1209 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1210 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1211 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1212 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1213 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1214 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1215 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1216 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1217 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1218 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1219 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1220 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1221 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1222 {NULL, NULL}
1223 };
1224
1225 /* adjust the expression to a Perl equivalent
1226 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1227
1228 /* we need to replace all "$" with "\$", count them now */
1229 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1230
1231 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1232 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1233 perl_regex[0] = '\0';
1234
1235 ptr = perl_regex;
1236
1237 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1238 /* we will add line-end anchoring */
1239 ptr[0] = '(';
1240 ++ptr;
1241 }
1242
1243 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1244 if (orig_ptr[0] == '$') {
1245 ptr += sprintf(ptr, "\\$");
1246 } else if (orig_ptr[0] == '^') {
1247 ptr += sprintf(ptr, "\\^");
1248 } else {
1249 ptr[0] = orig_ptr[0];
1250 ++ptr;
1251 }
1252 }
1253
1254 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1255 ptr += sprintf(ptr, ")$");
1256 } else {
1257 ptr[0] = '\0';
1258 ++ptr;
1259 }
1260
1261 /* substitute Unicode Character Blocks with exact Character Ranges */
1262 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1263 start = ptr - perl_regex;
1264
1265 ptr = strchr(ptr, '}');
1266 if (!ptr) {
1267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1268 pattern, perl_regex + start + 2, "unterminated character property");
1269 free(perl_regex);
1270 return LY_EVALID;
1271 }
1272 end = (ptr - perl_regex) + 1;
1273
1274 /* need more space */
1275 if (end - start < URANGE_LEN) {
1276 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1277 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1278 }
1279
1280 /* find our range */
1281 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1282 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1283 break;
1284 }
1285 }
1286 if (!ublock2urange[idx][0]) {
1287 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1288 pattern, perl_regex + start + 5, "unknown block name");
1289 free(perl_regex);
1290 return LY_EVALID;
1291 }
1292
1293 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1294 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1295 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1296 ++count;
1297 }
1298 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1299 --count;
1300 }
1301 }
1302 if (count) {
1303 /* skip brackets */
1304 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1305 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1306 } else {
1307 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1308 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1309 }
1310 }
1311
1312 /* must return 0, already checked during parsing */
1313 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1314 &err_msg, &err_offset, NULL);
1315 if (!precomp) {
1316 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1317 free(perl_regex);
1318 return LY_EVALID;
1319 }
1320 free(perl_regex);
1321
1322 if (pcre_precomp) {
1323 *pcre_precomp = precomp;
1324 } else {
1325 free(precomp);
1326 }
1327
1328 return LY_SUCCESS;
1329
1330#undef URANGE_LEN
1331}
1332
Radek Krejcia3045382018-11-22 14:30:31 +01001333/**
1334 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1335 * @param[in] ctx Compile context.
1336 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1337 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1338 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1339 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1340 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1341 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1342 */
Radek Krejci19a96102018-11-15 13:38:09 +01001343static LY_ERR
1344lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1345 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1346{
1347 struct lysc_pattern **pattern;
1348 unsigned int u, v;
1349 const char *err_msg;
1350 LY_ERR ret = LY_SUCCESS;
1351
1352 /* first, copy the patterns from the base type */
1353 if (base_patterns) {
1354 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1355 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1356 }
1357
1358 LY_ARRAY_FOR(patterns_p, u) {
1359 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1360 *pattern = calloc(1, sizeof **pattern);
1361 ++(*pattern)->refcount;
1362
1363 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1364 LY_CHECK_RET(ret);
1365 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1366 if (err_msg) {
1367 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1368 }
1369
1370 if (patterns_p[u].arg[0] == 0x15) {
1371 (*pattern)->inverted = 1;
1372 }
1373 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1374 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
1375 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1376 options, v, lys_compile_ext, ret, done);
1377 }
1378done:
1379 return ret;
1380}
1381
Radek Krejcia3045382018-11-22 14:30:31 +01001382/**
1383 * @brief map of the possible restrictions combination for the specific built-in type.
1384 */
Radek Krejci19a96102018-11-15 13:38:09 +01001385static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1386 0 /* LY_TYPE_UNKNOWN */,
1387 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001388 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1389 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1390 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1391 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1392 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001393 LYS_SET_BIT /* LY_TYPE_BITS */,
1394 0 /* LY_TYPE_BOOL */,
1395 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1396 0 /* LY_TYPE_EMPTY */,
1397 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1398 LYS_SET_BASE /* LY_TYPE_IDENT */,
1399 LYS_SET_REQINST /* LY_TYPE_INST */,
1400 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001401 LYS_SET_TYPE /* LY_TYPE_UNION */,
1402 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001403 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001404 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001405 LYS_SET_RANGE /* LY_TYPE_INT64 */
1406};
1407
1408/**
1409 * @brief stringification of the YANG built-in data types
1410 */
1411const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1412 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1413 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001414};
1415
Radek Krejcia3045382018-11-22 14:30:31 +01001416/**
1417 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1418 * @param[in] ctx Compile context.
1419 * @param[in] enums_p Array of the parsed enum structures to compile.
1420 * @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.
1421 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1422 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1423 * @param[out] enums Newly created array of the compiled enums information for the current type.
1424 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1425 */
Radek Krejci19a96102018-11-15 13:38:09 +01001426static LY_ERR
1427lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1428 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1429{
1430 LY_ERR ret = LY_SUCCESS;
1431 unsigned int u, v, match;
1432 int32_t value = 0;
1433 uint32_t position = 0;
1434 struct lysc_type_enum_item *e, storage;
1435
1436 if (base_enums && ctx->mod->compiled->version < 2) {
1437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1438 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1439 return LY_EVALID;
1440 }
1441
1442 LY_ARRAY_FOR(enums_p, u) {
1443 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1444 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
1445 if (base_enums) {
1446 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1447 LY_ARRAY_FOR(base_enums, v) {
1448 if (!strcmp(e->name, base_enums[v].name)) {
1449 break;
1450 }
1451 }
1452 if (v == LY_ARRAY_SIZE(base_enums)) {
1453 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1454 "Invalid %s - derived type adds new item \"%s\".",
1455 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1456 return LY_EVALID;
1457 }
1458 match = v;
1459 }
1460
1461 if (basetype == LY_TYPE_ENUM) {
1462 if (enums_p[u].flags & LYS_SET_VALUE) {
1463 e->value = (int32_t)enums_p[u].value;
1464 if (!u || e->value >= value) {
1465 value = e->value + 1;
1466 }
1467 /* check collision with other values */
1468 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1469 if (e->value == (*enums)[v].value) {
1470 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1471 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1472 e->value, e->name, (*enums)[v].name);
1473 return LY_EVALID;
1474 }
1475 }
1476 } else if (base_enums) {
1477 /* inherit the assigned value */
1478 e->value = base_enums[match].value;
1479 if (!u || e->value >= value) {
1480 value = e->value + 1;
1481 }
1482 } else {
1483 /* assign value automatically */
1484 if (u && value == INT32_MIN) {
1485 /* counter overflow */
1486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1487 "Invalid enumeration - it is not possible to auto-assign enum value for "
1488 "\"%s\" since the highest value is already 2147483647.", e->name);
1489 return LY_EVALID;
1490 }
1491 e->value = value++;
1492 }
1493 } else { /* LY_TYPE_BITS */
1494 if (enums_p[u].flags & LYS_SET_VALUE) {
1495 e->value = (int32_t)enums_p[u].value;
1496 if (!u || (uint32_t)e->value >= position) {
1497 position = (uint32_t)e->value + 1;
1498 }
1499 /* check collision with other values */
1500 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1501 if (e->value == (*enums)[v].value) {
1502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1503 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1504 (uint32_t)e->value, e->name, (*enums)[v].name);
1505 return LY_EVALID;
1506 }
1507 }
1508 } else if (base_enums) {
1509 /* inherit the assigned value */
1510 e->value = base_enums[match].value;
1511 if (!u || (uint32_t)e->value >= position) {
1512 position = (uint32_t)e->value + 1;
1513 }
1514 } else {
1515 /* assign value automatically */
1516 if (u && position == 0) {
1517 /* counter overflow */
1518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1519 "Invalid bits - it is not possible to auto-assign bit position for "
1520 "\"%s\" since the highest value is already 4294967295.", e->name);
1521 return LY_EVALID;
1522 }
1523 e->value = position++;
1524 }
1525 }
1526
1527 if (base_enums) {
1528 /* the assigned values must not change from the derived type */
1529 if (e->value != base_enums[match].value) {
1530 if (basetype == LY_TYPE_ENUM) {
1531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1532 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1533 e->name, base_enums[match].value, e->value);
1534 } else {
1535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1536 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1537 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1538 }
1539 return LY_EVALID;
1540 }
1541 }
1542
1543 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1544 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1545
1546 if (basetype == LY_TYPE_BITS) {
1547 /* keep bits ordered by position */
1548 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1549 if (v != u) {
1550 memcpy(&storage, e, sizeof *e);
1551 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1552 memcpy(&(*enums)[v], &storage, sizeof storage);
1553 }
1554 }
1555 }
1556
1557done:
1558 return ret;
1559}
1560
Radek Krejcia3045382018-11-22 14:30:31 +01001561#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1562 for ((NODE) = (NODE)->parent; \
1563 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1564 (NODE) = (NODE)->parent); \
1565 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1567 TERM; \
1568 }
1569
1570/**
1571 * @brief Validate the predicate(s) from the leafref path.
1572 * @param[in] ctx Compile context
1573 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1574 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001575 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001576 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001577 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001578 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1579 */
1580static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001581lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001582 const struct lysc_node *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001583{
1584 LY_ERR ret = LY_EVALID;
1585 const struct lys_module *mod;
1586 const struct lysc_node *src_node, *dst_node;
1587 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1588 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
1589 unsigned int dest_parent_times;
1590 const char *start, *end, *pke_end;
1591 struct ly_set keys = {0};
1592 int i;
1593
1594 while (**predicate == '[') {
1595 start = (*predicate)++;
1596
1597 while (isspace(**predicate)) {
1598 ++(*predicate);
1599 }
1600 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1601 while (isspace(**predicate)) {
1602 ++(*predicate);
1603 }
1604 if (**predicate != '=') {
1605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1606 "Invalid leafref path predicate \"%.*s\" - missing \"=\".", *predicate - start + 1, *predicate);
1607 goto cleanup;
1608 }
1609 ++(*predicate);
1610 while (isspace(**predicate)) {
1611 ++(*predicate);
1612 }
1613
1614 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1616 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1617 goto cleanup;
1618 }
1619 --pke_end;
1620 while (isspace(*pke_end)) {
1621 --pke_end;
1622 }
1623 /* localize path-key-expr */
1624 pke_start = path_key_expr = *predicate;
1625 /* move after the current predicate */
1626 *predicate = end + 1;
1627
1628 /* source (must be leaf or leaf-list) */
1629 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001630 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001631 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001632 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001633 }
1634 src_node = lys_child(context_node, mod, src, src_len,
1635 mod->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST, LYS_GETNEXT_NOSTATECHECK);
1636 if (!src_node) {
1637 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1638 "Invalid leafref path predicate \"%.*s\" - key node \"%.*s\" from module \"%s\" not found.",
1639 *predicate - start, start, src_len, src, mod->compiled->name);
1640 goto cleanup;
1641 }
1642 /* TODO - check the src_node is really a key of the context_node */
1643
1644 /* check that there is only one predicate for the */
1645 i = ly_set_add(&keys, (void*)src_node, 0);
1646 LY_CHECK_GOTO(i == -1, cleanup);
1647 if (keys.count != (unsigned int)i + 1) {
1648 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1649 "Invalid leafref path predicate \"%.*s\" - multiple equality test for the key %s.",
1650 *predicate - start, start, src_node->name);
1651 goto cleanup;
1652 }
1653
1654 /* destination */
1655 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01001656 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01001657
1658 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
1659 if (strncmp(path_key_expr, "current()", 9)) {
1660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1661 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
1662 *predicate - start, start);
1663 goto cleanup;
1664 }
1665 path_key_expr += 9;
1666 while (isspace(*path_key_expr)) {
1667 ++path_key_expr;
1668 }
1669
1670 if (*path_key_expr != '/') {
1671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1672 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
1673 *predicate - start, start);
1674 goto cleanup;
1675 }
1676 ++path_key_expr;
1677 while (isspace(*path_key_expr)) {
1678 ++path_key_expr;
1679 }
1680
1681 /* rel-path-keyexpr:
1682 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
1683 while (!strncmp(path_key_expr, "..", 2)) {
1684 ++dest_parent_times;
1685 path_key_expr += 2;
1686 while (isspace(*path_key_expr)) {
1687 ++path_key_expr;
1688 }
1689 if (*path_key_expr != '/') {
1690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1691 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
1692 *predicate - start, start);
1693 goto cleanup;
1694 }
1695 ++path_key_expr;
1696 while (isspace(*path_key_expr)) {
1697 ++path_key_expr;
1698 }
1699
1700 /* path is supposed to be evaluated in data tree, so we have to skip
1701 * all schema nodes that cannot be instantiated in data tree */
1702 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
1703 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
1704 *predicate - start, start);
1705 }
1706 if (!dest_parent_times) {
1707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1708 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
1709 *predicate - start, start);
1710 goto cleanup;
1711 }
1712 if (path_key_expr == pke_end) {
1713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1714 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
1715 *predicate - start, start);
1716 goto cleanup;
1717 }
1718
1719 while(path_key_expr != pke_end) {
1720 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
1721 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1722 "Invalid node identifier in leafref path predicate - character %d (%.*s).",
1723 path_key_expr - start, *predicate - start, start);
1724 goto cleanup;
1725 }
1726
1727 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001728 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001729 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001730 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001731 }
1732 if (!mod) {
1733 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1734 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
1735 *predicate - start, start, dst_len, dst);
1736 goto cleanup;
1737 }
1738
1739 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
1740 if (!dst_node) {
1741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1742 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
1743 *predicate - start, start, path_key_expr - pke_start, pke_start);
1744 goto cleanup;
1745 }
1746 }
1747 if (!(dst_node->nodetype & (dst_node->module->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
1748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1749 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s.",
1750 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
1751 goto cleanup;
1752 }
1753 }
1754
1755 ret = LY_SUCCESS;
1756cleanup:
1757 ly_set_erase(&keys, NULL);
1758 return ret;
1759}
1760
1761/**
1762 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
1763 *
1764 * path-arg = absolute-path / relative-path
1765 * absolute-path = 1*("/" (node-identifier *path-predicate))
1766 * relative-path = 1*(".." "/") descendant-path
1767 *
1768 * @param[in,out] path Path to parse.
1769 * @param[out] prefix Prefix of the token, NULL if there is not any.
1770 * @param[out] pref_len Length of the prefix, 0 if there is not any.
1771 * @param[out] name Name of the token.
1772 * @param[out] nam_len Length of the name.
1773 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
1774 * must not be changed between consecutive calls. -1 if the
1775 * path is absolute.
1776 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
1777 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
1778 */
1779static LY_ERR
1780lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
1781 int *parent_times, int *has_predicate)
1782{
1783 int par_times = 0;
1784
1785 assert(path && *path);
1786 assert(parent_times);
1787 assert(prefix);
1788 assert(prefix_len);
1789 assert(name);
1790 assert(name_len);
1791 assert(has_predicate);
1792
1793 *prefix = NULL;
1794 *prefix_len = 0;
1795 *name = NULL;
1796 *name_len = 0;
1797 *has_predicate = 0;
1798
1799 if (!*parent_times) {
1800 if (!strncmp(*path, "..", 2)) {
1801 *path += 2;
1802 ++par_times;
1803 while (!strncmp(*path, "/..", 3)) {
1804 *path += 3;
1805 ++par_times;
1806 }
1807 }
1808 if (par_times) {
1809 *parent_times = par_times;
1810 } else {
1811 *parent_times = -1;
1812 }
1813 }
1814
1815 if (**path != '/') {
1816 return LY_EINVAL;
1817 }
1818 /* skip '/' */
1819 ++(*path);
1820
1821 /* node-identifier ([prefix:]name) */
1822 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
1823
1824 if ((**path == '/' && (*path)[1]) || !**path) {
1825 /* path continues by another token or this is the last token */
1826 return LY_SUCCESS;
1827 } else if ((*path)[0] != '[') {
1828 /* unexpected character */
1829 return LY_EINVAL;
1830 } else {
1831 /* predicate starting with [ */
1832 *has_predicate = 1;
1833 return LY_SUCCESS;
1834 }
1835}
1836
1837/**
Radek Krejci58d171e2018-11-23 13:50:55 +01001838 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
1839 *
1840 * The set of features used for target must be a subset of features used for the leafref.
1841 * This is not a perfect, we should compare the truth tables but it could require too much resources
1842 * and RFC 7950 does not require it explicitely, so we simplify that.
1843 *
1844 * @param[in] refnode The leafref node.
1845 * @param[in] target Tha target node of the leafref.
1846 * @return LY_SUCCESS or LY_EVALID;
1847 */
1848static LY_ERR
1849lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
1850{
1851 LY_ERR ret = LY_EVALID;
1852 const struct lysc_node *iter;
1853 struct lysc_iffeature **iff;
1854 unsigned int u, v, count;
1855 struct ly_set features = {0};
1856
1857 for (iter = refnode; iter; iter = iter->parent) {
1858 iff = lysc_node_iff(iter);
1859 if (iff && *iff) {
1860 LY_ARRAY_FOR(*iff, u) {
1861 LY_ARRAY_FOR((*iff)[u].features, v) {
1862 LY_CHECK_GOTO(ly_set_add(&features, (*iff)[u].features[v], 0) == -1, cleanup);
1863 }
1864 }
1865 }
1866 }
1867
1868 /* we should have, in features set, a superset of features applicable to the target node.
1869 * So when adding features applicable to the target into the features set, we should not be
1870 * able to actually add any new feature, otherwise it is not a subset of features applicable
1871 * to the leafref itself. */
1872 count = features.count;
1873 for (iter = target; iter; iter = iter->parent) {
1874 iff = lysc_node_iff(iter);
1875 if (iff && *iff) {
1876 LY_ARRAY_FOR(*iff, u) {
1877 LY_ARRAY_FOR((*iff)[u].features, v) {
1878 if ((unsigned int)ly_set_add(&features, (*iff)[u].features[v], 0) >= count) {
1879 /* new feature was added (or LY_EMEM) */
1880 goto cleanup;
1881 }
1882 }
1883 }
1884 }
1885 }
1886 ret = LY_SUCCESS;
1887
1888cleanup:
1889 ly_set_erase(&features, NULL);
1890 return ret;
1891}
1892
1893/**
Radek Krejcia3045382018-11-22 14:30:31 +01001894 * @brief Validate the leafref path.
1895 * @param[in] ctx Compile context
1896 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01001897 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01001898 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1899 */
1900static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01001901lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01001902{
1903 const struct lysc_node *node = NULL, *parent = NULL;
1904 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001905 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01001906 const char *id, *prefix, *name;
1907 size_t prefix_len, name_len;
1908 int parent_times = 0, has_predicate;
1909 unsigned int iter, u;
1910 LY_ERR ret = LY_SUCCESS;
1911
1912 assert(ctx);
1913 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001914 assert(leafref);
1915
1916 /* 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 +01001917
1918 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001919 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01001920 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
1921 if (!iter) { /* first iteration */
1922 /* precess ".." in relative paths */
1923 if (parent_times > 0) {
1924 /* move from the context node */
1925 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
1926 /* path is supposed to be evaluated in data tree, so we have to skip
1927 * all schema nodes that cannot be instantiated in data tree */
1928 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001929 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001930 }
1931 }
1932 }
1933
1934 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001935 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001936 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001937 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001938 }
1939 if (!mod) {
1940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001941 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
1942 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001943 return LY_EVALID;
1944 }
1945
1946 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
1947 if (!node) {
1948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001949 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001950 return LY_EVALID;
1951 }
1952 parent = node;
1953
1954 if (has_predicate) {
1955 /* we have predicate, so the current result must be list */
1956 if (node->nodetype != LYS_LIST) {
1957 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1958 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001959 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001960 return LY_EVALID;
1961 }
1962
Radek Krejci4ce68932018-11-28 12:53:36 +01001963 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, node, leafref->path_context), LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01001964 }
1965
1966 ++iter;
1967 }
1968 if (ret) {
1969 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001970 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001971 return LY_EVALID;
1972 }
1973
1974 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1975 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1976 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001977 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001978 return LY_EVALID;
1979 }
1980
1981 /* check status */
1982 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
1983 return LY_EVALID;
1984 }
1985
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001986 /* check config */
1987 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
1988 if (node->flags & LYS_CONFIG_R) {
1989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1990 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
1991 leafref->path);
1992 return LY_EVALID;
1993 }
1994 }
1995
Radek Krejci412ddfa2018-11-23 11:44:11 +01001996 /* store the target's type and check for circular chain of leafrefs */
1997 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
1998 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
1999 if (type == (struct lysc_type*)leafref) {
2000 /* circular chain detected */
2001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2002 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2003 return LY_EVALID;
2004 }
2005 }
2006
Radek Krejci58d171e2018-11-23 13:50:55 +01002007 /* check if leafref and its target are under common if-features */
2008 if (lys_compile_leafref_features_validate(startnode, node)) {
2009 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2010 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2011 leafref->path);
2012 return LY_EVALID;
2013 }
2014
Radek Krejcia3045382018-11-22 14:30:31 +01002015 return LY_SUCCESS;
2016}
2017
Radek Krejcicdfecd92018-11-26 11:27:32 +01002018static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
2019 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt);
Radek Krejcia3045382018-11-22 14:30:31 +01002020/**
2021 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2022 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002023 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2024 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2025 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2026 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002027 * @param[in] type_p Parsed type to compile.
2028 * @param[in] basetype Base YANG built-in type of the type to compile.
2029 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2030 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2031 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2032 * @param[out] type Newly created type structure with the filled information about the type.
2033 * @return LY_ERR value.
2034 */
Radek Krejci19a96102018-11-15 13:38:09 +01002035static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002036lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
2037 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2038 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002039{
2040 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002041 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002042 struct lysc_type_bin *bin;
2043 struct lysc_type_num *num;
2044 struct lysc_type_str *str;
2045 struct lysc_type_bits *bits;
2046 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002047 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002048 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002049 struct lysc_type_union *un, *un_aux;
2050 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002051
2052 switch (basetype) {
2053 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002054 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002055
2056 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002057 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002058 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002059 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2060 LY_CHECK_RET(ret);
2061 if (!tpdfname) {
2062 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2063 options, u, lys_compile_ext, ret, done);
2064 }
2065 }
2066
2067 if (tpdfname) {
2068 type_p->compiled = *type;
2069 *type = calloc(1, sizeof(struct lysc_type_bin));
2070 }
2071 break;
2072 case LY_TYPE_BITS:
2073 /* RFC 7950 9.7 - bits */
2074 bits = (struct lysc_type_bits*)(*type);
2075 if (type_p->bits) {
2076 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2077 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2078 (struct lysc_type_enum_item**)&bits->bits);
2079 LY_CHECK_RET(ret);
2080 }
2081
Radek Krejci555cb5b2018-11-16 14:54:33 +01002082 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002083 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002084 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002086 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002087 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002088 free(*type);
2089 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002090 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002091 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002092 }
2093
2094 if (tpdfname) {
2095 type_p->compiled = *type;
2096 *type = calloc(1, sizeof(struct lysc_type_bits));
2097 }
2098 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002099 case LY_TYPE_DEC64:
2100 dec = (struct lysc_type_dec*)(*type);
2101
2102 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002103 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002104 if (!type_p->fraction_digits) {
2105 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002106 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002107 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002108 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002109 free(*type);
2110 *type = NULL;
2111 }
2112 return LY_EVALID;
2113 }
2114 } else if (type_p->fraction_digits) {
2115 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002116 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2118 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002119 tpdfname);
2120 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002121 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2122 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002123 free(*type);
2124 *type = NULL;
2125 }
2126 return LY_EVALID;
2127 }
2128 dec->fraction_digits = type_p->fraction_digits;
2129
2130 /* RFC 7950 9.2.4 - range */
2131 if (type_p->range) {
2132 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2133 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2134 LY_CHECK_RET(ret);
2135 if (!tpdfname) {
2136 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2137 options, u, lys_compile_ext, ret, done);
2138 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002139 }
2140
2141 if (tpdfname) {
2142 type_p->compiled = *type;
2143 *type = calloc(1, sizeof(struct lysc_type_dec));
2144 }
2145 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002146 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002147 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002148
2149 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002150 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002151 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002152 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2153 LY_CHECK_RET(ret);
2154 if (!tpdfname) {
2155 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2156 options, u, lys_compile_ext, ret, done);
2157 }
2158 } else if (base && ((struct lysc_type_str*)base)->length) {
2159 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2160 }
2161
2162 /* RFC 7950 9.4.5 - pattern */
2163 if (type_p->patterns) {
2164 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2165 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2166 LY_CHECK_RET(ret);
2167 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2168 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2169 }
2170
2171 if (tpdfname) {
2172 type_p->compiled = *type;
2173 *type = calloc(1, sizeof(struct lysc_type_str));
2174 }
2175 break;
2176 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002177 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002178
2179 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002180 if (type_p->enums) {
2181 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2182 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2183 LY_CHECK_RET(ret);
2184 }
2185
Radek Krejci555cb5b2018-11-16 14:54:33 +01002186 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002187 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002188 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002189 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002190 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002192 free(*type);
2193 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002194 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002195 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002196 }
2197
2198 if (tpdfname) {
2199 type_p->compiled = *type;
2200 *type = calloc(1, sizeof(struct lysc_type_enum));
2201 }
2202 break;
2203 case LY_TYPE_INT8:
2204 case LY_TYPE_UINT8:
2205 case LY_TYPE_INT16:
2206 case LY_TYPE_UINT16:
2207 case LY_TYPE_INT32:
2208 case LY_TYPE_UINT32:
2209 case LY_TYPE_INT64:
2210 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002211 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002212
2213 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002214 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002215 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002216 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2217 LY_CHECK_RET(ret);
2218 if (!tpdfname) {
2219 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2220 options, u, lys_compile_ext, ret, done);
2221 }
2222 }
2223
2224 if (tpdfname) {
2225 type_p->compiled = *type;
2226 *type = calloc(1, sizeof(struct lysc_type_num));
2227 }
2228 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002229 case LY_TYPE_IDENT:
2230 idref = (struct lysc_type_identityref*)(*type);
2231
2232 /* RFC 7950 9.10.2 - base */
2233 if (type_p->bases) {
2234 if (base) {
2235 /* only the directly derived identityrefs can contain base specification */
2236 if (tpdfname) {
2237 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002238 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002239 tpdfname);
2240 } else {
2241 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002242 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002243 free(*type);
2244 *type = NULL;
2245 }
2246 return LY_EVALID;
2247 }
2248 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2249 LY_CHECK_RET(ret);
2250 }
2251
2252 if (!base && !type_p->flags) {
2253 /* type derived from identityref built-in type must contain at least one base */
2254 if (tpdfname) {
2255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2256 } else {
2257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2258 free(*type);
2259 *type = NULL;
2260 }
2261 return LY_EVALID;
2262 }
2263
2264 if (tpdfname) {
2265 type_p->compiled = *type;
2266 *type = calloc(1, sizeof(struct lysc_type_identityref));
2267 }
2268 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002269 case LY_TYPE_LEAFREF:
2270 /* RFC 7950 9.9.3 - require-instance */
2271 if (type_p->flags & LYS_SET_REQINST) {
2272 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002273 } else if (base) {
2274 /* inherit */
2275 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002276 } else {
2277 /* default is true */
2278 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2279 }
2280 if (type_p->path) {
2281 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002282 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002283 } else if (base) {
2284 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002285 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002286 } else if (tpdfname) {
2287 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2288 return LY_EVALID;
2289 } else {
2290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2291 free(*type);
2292 *type = NULL;
2293 return LY_EVALID;
2294 }
2295 if (tpdfname) {
2296 type_p->compiled = *type;
2297 *type = calloc(1, sizeof(struct lysc_type_leafref));
2298 }
2299 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002300 case LY_TYPE_INST:
2301 /* RFC 7950 9.9.3 - require-instance */
2302 if (type_p->flags & LYS_SET_REQINST) {
2303 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2304 } else {
2305 /* default is true */
2306 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2307 }
2308
2309 if (tpdfname) {
2310 type_p->compiled = *type;
2311 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2312 }
2313 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002314 case LY_TYPE_UNION:
2315 un = (struct lysc_type_union*)(*type);
2316
2317 /* RFC 7950 7.4 - type */
2318 if (type_p->types) {
2319 if (base) {
2320 /* only the directly derived union can contain types specification */
2321 if (tpdfname) {
2322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2323 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2324 tpdfname);
2325 } else {
2326 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2327 "Invalid type substatement for the type not directly derived from union built-in type.");
2328 free(*type);
2329 *type = NULL;
2330 }
2331 return LY_EVALID;
2332 }
2333 /* compile the type */
2334 additional = 0;
2335 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2336 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
2337 ret = lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], options, &un->types[u + additional], NULL, NULL);
2338 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2339 /* add space for additional types from the union subtype */
2340 un_aux = (struct lysc_type_union *)un->types[u + additional];
2341 p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types)));
2342 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2343 un->types = (void*)((uint32_t*)(p) + 1);
2344
2345 /* copy subtypes of the subtype union */
2346 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2347 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2348 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2349 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2350 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2351 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2352 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2353 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2354 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2355 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2356 /* TODO extensions */
2357
2358 } else {
2359 un->types[u + additional] = un_aux->types[v];
2360 ++un_aux->types[v]->refcount;
2361 }
2362 ++additional;
2363 LY_ARRAY_INCREMENT(un->types);
2364 }
2365 /* compensate u increment in main loop */
2366 --additional;
2367
2368 /* free the replaced union subtype */
2369 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2370 } else {
2371 LY_ARRAY_INCREMENT(un->types);
2372 }
2373 LY_CHECK_RET(ret);
2374 }
2375 }
2376
2377 if (!base && !type_p->flags) {
2378 /* type derived from union built-in type must contain at least one type */
2379 if (tpdfname) {
2380 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2381 } else {
2382 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2383 free(*type);
2384 *type = NULL;
2385 }
2386 return LY_EVALID;
2387 }
2388
2389 if (tpdfname) {
2390 type_p->compiled = *type;
2391 *type = calloc(1, sizeof(struct lysc_type_union));
2392 }
2393 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002394 case LY_TYPE_BOOL:
2395 case LY_TYPE_EMPTY:
2396 case LY_TYPE_UNKNOWN: /* just to complete switch */
2397 break;
2398 }
2399 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2400done:
2401 return ret;
2402}
2403
Radek Krejcia3045382018-11-22 14:30:31 +01002404/**
2405 * @brief Compile information about the leaf/leaf-list's type.
2406 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002407 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2408 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2409 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2410 * @param[in] context_name Name of the context node or referencing typedef for logging.
2411 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002412 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2413 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002414 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
2415 * @param[out] dflt Storage for inheriting default value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002416 * @return LY_ERR value.
2417 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002418static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002419lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
2420 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt)
Radek Krejci19a96102018-11-15 13:38:09 +01002421{
2422 LY_ERR ret = LY_SUCCESS;
2423 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002424 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002425 struct type_context {
2426 const struct lysp_tpdf *tpdf;
2427 struct lysp_node *node;
2428 struct lysp_module *mod;
2429 } *tctx, *tctx_prev = NULL;
2430 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002431 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002432 struct ly_set tpdf_chain = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01002433
2434 (*type) = NULL;
2435
2436 tctx = calloc(1, sizeof *tctx);
2437 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002438 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002439 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2440 ret == LY_SUCCESS;
2441 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2442 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2443 if (basetype) {
2444 break;
2445 }
2446
2447 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002448 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2449 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002450 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2451
Radek Krejcicdfecd92018-11-26 11:27:32 +01002452 if (units && !*units) {
2453 /* inherit units */
2454 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2455 }
2456 if (dflt && !*dflt) {
2457 /* inherit default */
2458 DUP_STRING(ctx->ctx, tctx->tpdf->dflt, *dflt);
2459 }
2460 if (dummyloops && (!units || *units) && (!dflt || *dflt)) {
2461 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2462 break;
2463 }
2464
Radek Krejci19a96102018-11-15 13:38:09 +01002465 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002466 /* it is not necessary to continue, the rest of the chain was already compiled,
2467 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002468 basetype = tctx->tpdf->type.compiled->basetype;
2469 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002470 if ((units && !*units) || (dflt && !*dflt)) {
2471 dummyloops = 1;
2472 goto preparenext;
2473 } else {
2474 tctx = NULL;
2475 break;
2476 }
Radek Krejci19a96102018-11-15 13:38:09 +01002477 }
2478
2479 /* store information for the following processing */
2480 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2481
Radek Krejcicdfecd92018-11-26 11:27:32 +01002482preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002483 /* prepare next loop */
2484 tctx_prev = tctx;
2485 tctx = calloc(1, sizeof *tctx);
2486 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2487 }
2488 free(tctx);
2489
2490 /* allocate type according to the basetype */
2491 switch (basetype) {
2492 case LY_TYPE_BINARY:
2493 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002494 break;
2495 case LY_TYPE_BITS:
2496 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002497 break;
2498 case LY_TYPE_BOOL:
2499 case LY_TYPE_EMPTY:
2500 *type = calloc(1, sizeof(struct lysc_type));
2501 break;
2502 case LY_TYPE_DEC64:
2503 *type = calloc(1, sizeof(struct lysc_type_dec));
2504 break;
2505 case LY_TYPE_ENUM:
2506 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002507 break;
2508 case LY_TYPE_IDENT:
2509 *type = calloc(1, sizeof(struct lysc_type_identityref));
2510 break;
2511 case LY_TYPE_INST:
2512 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2513 break;
2514 case LY_TYPE_LEAFREF:
2515 *type = calloc(1, sizeof(struct lysc_type_leafref));
2516 break;
2517 case LY_TYPE_STRING:
2518 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002519 break;
2520 case LY_TYPE_UNION:
2521 *type = calloc(1, sizeof(struct lysc_type_union));
2522 break;
2523 case LY_TYPE_INT8:
2524 case LY_TYPE_UINT8:
2525 case LY_TYPE_INT16:
2526 case LY_TYPE_UINT16:
2527 case LY_TYPE_INT32:
2528 case LY_TYPE_UINT32:
2529 case LY_TYPE_INT64:
2530 case LY_TYPE_UINT64:
2531 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002532 break;
2533 case LY_TYPE_UNKNOWN:
2534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2535 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2536 ret = LY_EVALID;
2537 goto cleanup;
2538 }
2539 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002540 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002541 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2542 ly_data_type2str[basetype]);
2543 free(*type);
2544 (*type) = NULL;
2545 ret = LY_EVALID;
2546 goto cleanup;
2547 }
2548
2549 /* get restrictions from the referred typedefs */
2550 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2551 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002552 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002553 base = tctx->tpdf->type.compiled;
2554 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002555 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002556 /* no change, just use the type information from the base */
2557 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2558 ++base->refcount;
2559 continue;
2560 }
2561
2562 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002563 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2564 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2565 tctx->tpdf->name, ly_data_type2str[basetype]);
2566 ret = LY_EVALID;
2567 goto cleanup;
2568 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2569 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2570 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2571 tctx->tpdf->name, tctx->tpdf->dflt);
2572 ret = LY_EVALID;
2573 goto cleanup;
2574 }
2575
Radek Krejci19a96102018-11-15 13:38:09 +01002576 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002577 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002578 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002579 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2580 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002581 LY_CHECK_GOTO(ret, cleanup);
2582 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002583 }
2584
Radek Krejcic5c27e52018-11-15 14:38:11 +01002585 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002586 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002587 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002588 (*type)->basetype = basetype;
2589 ++(*type)->refcount;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002590 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod, basetype, options, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002591 LY_CHECK_GOTO(ret, cleanup);
2592 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002593 /* no specific restriction in leaf's type definition, copy from the base */
2594 free(*type);
2595 (*type) = base;
2596 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002597 }
2598
2599 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
2600
2601cleanup:
2602 ly_set_erase(&tpdf_chain, free);
2603 return ret;
2604}
2605
2606static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent);
2607
Radek Krejcia3045382018-11-22 14:30:31 +01002608/**
2609 * @brief Compile parsed container node information.
2610 * @param[in] ctx Compile context
2611 * @param[in] node_p Parsed container node.
2612 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2613 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2614 * is enriched with the container-specific information.
2615 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2616 */
Radek Krejci19a96102018-11-15 13:38:09 +01002617static LY_ERR
2618lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2619{
2620 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
2621 struct lysc_node_container *cont = (struct lysc_node_container*)node;
2622 struct lysp_node *child_p;
2623 unsigned int u;
2624 LY_ERR ret = LY_SUCCESS;
2625
2626 COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done);
2627 COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done);
2628
2629 LY_LIST_FOR(cont_p->child, child_p) {
2630 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2631 }
2632
2633 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2634 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2635 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2636
2637done:
2638 return ret;
2639}
2640
Radek Krejcia3045382018-11-22 14:30:31 +01002641/**
2642 * @brief Compile parsed leaf node information.
2643 * @param[in] ctx Compile context
2644 * @param[in] node_p Parsed leaf node.
2645 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2646 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2647 * is enriched with the leaf-specific information.
2648 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2649 */
Radek Krejci19a96102018-11-15 13:38:09 +01002650static LY_ERR
2651lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2652{
2653 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2654 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2655 unsigned int u;
2656 LY_ERR ret = LY_SUCCESS;
2657
2658 COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done);
2659 COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002660 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002661 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2662 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
Radek Krejci43699232018-11-23 14:59:46 +01002663
Radek Krejcicdfecd92018-11-26 11:27:32 +01002664 ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod->parsed, node_p->name, &leaf_p->type, options, &leaf->type,
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002665 leaf->units ? NULL : &leaf->units, leaf->dflt || (leaf->flags & LYS_MAND_TRUE) ? NULL : &leaf->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +01002666 LY_CHECK_GOTO(ret, done);
Radek Krejcia3045382018-11-22 14:30:31 +01002667 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2668 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2669 ly_set_add(&ctx->unres, leaf, 0);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002670 } else if (leaf->type->basetype == LY_TYPE_UNION) {
2671 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
2672 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2673 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2674 ly_set_add(&ctx->unres, leaf, 0);
2675 }
2676 }
Radek Krejci43699232018-11-23 14:59:46 +01002677 } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) {
2678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2679 "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt);
2680 return LY_EVALID;
Radek Krejcia3045382018-11-22 14:30:31 +01002681 }
2682
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002683 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2684
Radek Krejci19a96102018-11-15 13:38:09 +01002685done:
2686 return ret;
2687}
2688
Radek Krejcia3045382018-11-22 14:30:31 +01002689/**
2690 * @brief Compile parsed schema node information.
2691 * @param[in] ctx Compile context
2692 * @param[in] node_p Parsed schema node.
2693 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2694 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
2695 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
2696 * the compile context.
2697 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2698 */
Radek Krejci19a96102018-11-15 13:38:09 +01002699static LY_ERR
2700lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
2701{
2702 LY_ERR ret = LY_EVALID;
2703 struct lysc_node *node, **children;
2704 unsigned int u;
2705 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
2706
2707 switch (node_p->nodetype) {
2708 case LYS_CONTAINER:
2709 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
2710 node_compile_spec = lys_compile_node_container;
2711 break;
2712 case LYS_LEAF:
2713 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
2714 node_compile_spec = lys_compile_node_leaf;
2715 break;
2716 case LYS_LIST:
2717 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
2718 break;
2719 case LYS_LEAFLIST:
2720 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
2721 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002722 case LYS_CHOICE:
2723 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
2724 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002725 case LYS_ANYXML:
2726 case LYS_ANYDATA:
2727 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
2728 break;
2729 default:
2730 LOGINT(ctx->ctx);
2731 return LY_EINT;
2732 }
2733 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
2734 node->nodetype = node_p->nodetype;
2735 node->module = ctx->mod;
2736 node->prev = node;
2737 node->flags = node_p->flags;
2738
2739 /* config */
2740 if (!(node->flags & LYS_CONFIG_MASK)) {
2741 /* config not explicitely set, inherit it from parent */
2742 if (parent) {
2743 node->flags |= parent->flags & LYS_CONFIG_MASK;
2744 } else {
2745 /* default is config true */
2746 node->flags |= LYS_CONFIG_W;
2747 }
2748 }
2749
2750 /* status - it is not inherited by specification, but it does not make sense to have
2751 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2752 if (!(node->flags & LYS_STATUS_MASK)) {
2753 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2754 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2755 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2756 node->flags |= parent->flags & LYS_STATUS_MASK;
2757 } else {
2758 node->flags |= LYS_STATUS_CURR;
2759 }
2760 } else if (parent) {
2761 /* check status compatibility with the parent */
2762 if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
2763 if (node->flags & LYS_STATUS_CURR) {
2764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2765 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2766 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2767 } else { /* LYS_STATUS_DEPRC */
2768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2769 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2770 }
2771 goto error;
2772 }
2773 }
2774
2775 if (!(options & LYSC_OPT_FREE_SP)) {
2776 node->sp = node_p;
2777 }
2778 DUP_STRING(ctx->ctx, node_p->name, node->name);
2779 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
2780
2781 /* nodetype-specific part */
2782 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
2783
2784 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01002785 if (parent) {
2786 if (parent->nodetype == LYS_CHOICE) {
2787 /* TODO exception for cases */
2788 } else if ((children = lysc_node_children(parent))) {
2789 if (!(*children)) {
2790 /* first child */
2791 *children = node;
2792 } else {
2793 /* insert at the end of the parent's children list */
2794 (*children)->prev->next = node;
2795 node->prev = (*children)->prev;
2796 (*children)->prev = node;
2797 }
Radek Krejci19a96102018-11-15 13:38:09 +01002798 }
2799 } else {
2800 /* top-level element */
2801 if (!ctx->mod->compiled->data) {
2802 ctx->mod->compiled->data = node;
2803 } else {
2804 /* insert at the end of the module's top-level nodes list */
2805 ctx->mod->compiled->data->prev->next = node;
2806 node->prev = ctx->mod->compiled->data->prev;
2807 ctx->mod->compiled->data->prev = node;
2808 }
2809 }
2810
2811 return LY_SUCCESS;
2812
2813error:
2814 lysc_node_free(ctx->ctx, node);
2815 return ret;
2816}
2817
Radek Krejcia3045382018-11-22 14:30:31 +01002818/**
2819 * @brief Compile the given YANG module.
2820 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
2821 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2822 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2823 */
Radek Krejci19a96102018-11-15 13:38:09 +01002824LY_ERR
2825lys_compile(struct lys_module *mod, int options)
2826{
2827 struct lysc_ctx ctx = {0};
2828 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002829 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01002830 struct lysp_module *sp;
2831 struct lysp_node *node_p;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002832 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01002833 LY_ERR ret;
2834
2835 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
2836 sp = mod->parsed;
2837
2838 if (sp->submodule) {
2839 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
2840 return LY_EINVAL;
2841 }
2842
2843 ctx.ctx = sp->ctx;
2844 ctx.mod = mod;
2845
2846 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
2847 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
2848 mod_c->ctx = sp->ctx;
2849 mod_c->implemented = sp->implemented;
2850 mod_c->latest_revision = sp->latest_revision;
2851 mod_c->version = sp->version;
2852
2853 DUP_STRING(sp->ctx, sp->name, mod_c->name);
2854 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
2855 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
2856 if (sp->revs) {
2857 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
2858 }
2859 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
2860 COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
2861 COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
2862 if (sp->identities) {
2863 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
2864 }
2865
2866 LY_LIST_FOR(sp->data, node_p) {
2867 ret = lys_compile_node(&ctx, node_p, options, NULL);
2868 LY_CHECK_GOTO(ret, error);
2869 }
2870 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
2871 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
2872
2873 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
2874
Radek Krejcia3045382018-11-22 14:30:31 +01002875 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002876 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
2877 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
2878 * 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 +01002879 for (u = 0; u < ctx.unres.count; ++u) {
2880 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) {
2881 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2882 if (type->basetype == LY_TYPE_LEAFREF) {
2883 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002884 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 +01002885 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002886 } else if (type->basetype == LY_TYPE_UNION) {
2887 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
2888 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
2889 /* validate the path */
2890 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
2891 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
2892 LY_CHECK_GOTO(ret, error);
2893 }
2894 }
Radek Krejcia3045382018-11-22 14:30:31 +01002895 }
2896 }
2897 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01002898 for (u = 0; u < ctx.unres.count; ++u) {
2899 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) {
2900 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2901 if (type->basetype == LY_TYPE_LEAFREF) {
2902 /* store pointer to the real type */
2903 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
2904 typeiter->basetype == LY_TYPE_LEAFREF;
2905 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
2906 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002907 } else if (type->basetype == LY_TYPE_UNION) {
2908 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
2909 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
2910 /* store pointer to the real type */
2911 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
2912 typeiter->basetype == LY_TYPE_LEAFREF;
2913 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
2914 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
2915 }
2916 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01002917 }
2918 }
2919 }
Radek Krejcia3045382018-11-22 14:30:31 +01002920 ly_set_erase(&ctx.unres, NULL);
2921
Radek Krejci19a96102018-11-15 13:38:09 +01002922 if (options & LYSC_OPT_FREE_SP) {
2923 lysp_module_free(mod->parsed);
2924 ((struct lys_module*)mod)->parsed = NULL;
2925 }
2926
2927 ((struct lys_module*)mod)->compiled = mod_c;
2928 return LY_SUCCESS;
2929
2930error:
Radek Krejcia3045382018-11-22 14:30:31 +01002931 ly_set_erase(&ctx.unres, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01002932 lysc_module_free(mod_c, NULL);
2933 ((struct lys_module*)mod)->compiled = NULL;
2934 return ret;
2935}