blob: 47ee438203edd07d831769117041d3f1075f17d0 [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
Radek Krejci7f2a5362018-11-28 13:05:37 +0100438 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
439 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100440 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100441 if (!imp->module->parsed) {
442 comp = imp->module->compiled;
443 /* try to get filepath from the compiled version */
444 if (comp->filepath) {
445 mod = (struct lys_module*)lys_parse_path(ctx->ctx, comp->filepath,
446 !strcmp(&comp->filepath[strlen(comp->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
447 if (mod != imp->module) {
448 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
449 comp->filepath, comp->name);
450 mod = NULL;
451 }
452 }
453 if (!mod) {
454 if (lysp_load_module(ctx->ctx, comp->name, comp->revision, 0, 1, &mod)) {
455 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
456 comp->name, ctx->mod->compiled->name);
457 return LY_ENOTFOUND;
458 }
459 }
Radek Krejci19a96102018-11-15 13:38:09 +0100460 }
461
462done:
463 return ret;
464}
465
466static LY_ERR
467lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *ident)
468{
469 unsigned int u;
470 LY_ERR ret = LY_SUCCESS;
471
472 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
473 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
474 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
475 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
476 ident->flags = ident_p->flags;
477
478done:
479 return ret;
480}
481
Radek Krejcia3045382018-11-22 14:30:31 +0100482/**
483 * @brief Find and process the referenced base identities from another identity or identityref
484 *
485 * For bases in identity se backlinks to them from the base identities. For identityref, store
486 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
487 * to distinguish these two use cases.
488 *
489 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
490 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
491 * @param[in] ident Referencing identity to work with.
492 * @param[in] bases Array of bases of identityref to fill in.
493 * @return LY_ERR value.
494 */
Radek Krejci19a96102018-11-15 13:38:09 +0100495static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100496lys_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 +0100497{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100498 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100499 const char *s, *name;
500 struct lysc_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100501 struct lysc_ident **idref;
502
503 assert(ident || bases);
504
505 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) {
506 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
507 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
508 return LY_EVALID;
509 }
510
511 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
512 s = strchr(bases_p[u], ':');
513 if (s) {
514 /* prefixed identity */
515 name = &s[1];
516 mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]);
517 } else {
518 name = bases_p[u];
519 mod = ctx->mod->compiled;
520 }
521 if (!mod) {
522 if (ident) {
523 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
524 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
525 } else {
526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
527 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
528 }
529 return LY_EVALID;
530 }
531 idref = NULL;
532 if (mod->identities) {
533 for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) {
534 if (!strcmp(name, mod->identities[v].name)) {
535 if (ident) {
536 /* we have match! store the backlink */
537 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
538 *idref = ident;
539 } else {
540 /* we have match! store the found identity */
541 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
542 *idref = &mod->identities[v];
543 }
544 break;
545 }
546 }
547 }
548 if (!idref || !(*idref)) {
549 if (ident) {
550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
551 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
552 } else {
553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
554 "Unable to find base (%s) of identityref.", bases_p[u]);
555 }
556 return LY_EVALID;
557 }
558 }
559 return LY_SUCCESS;
560}
561
Radek Krejcia3045382018-11-22 14:30:31 +0100562/**
563 * @brief For the given array of identities, set the backlinks from all their base identities.
564 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
565 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
566 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
567 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
568 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100569static LY_ERR
570lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
571{
572 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100573
574 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
575 if (!idents_p[i].bases) {
576 continue;
577 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100578 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100579 }
580 return LY_SUCCESS;
581}
582
Radek Krejcia3045382018-11-22 14:30:31 +0100583/**
584 * @brief Create compiled feature structure.
585 * @param[in] ctx Compile context.
586 * @param[in] feature_p Parsed feature definition to compile.
587 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
588 * @param[in,out] feature Compiled feature structure to fill.
589 * @return LY_ERR value.
590 */
Radek Krejci19a96102018-11-15 13:38:09 +0100591static LY_ERR
592lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature)
593{
594 unsigned int u, v;
595 LY_ERR ret = LY_SUCCESS;
596 struct lysc_feature **df;
597
598 DUP_STRING(ctx->ctx, feature_p->name, feature->name);
599 feature->flags = feature_p->flags;
600
601 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
602 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
603 if (feature->iffeatures) {
604 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
605 if (feature->iffeatures[u].features) {
606 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
607 /* add itself into the dependants list */
608 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
609 *df = feature;
610 }
611 /* TODO check for circular dependency */
612 }
613 }
614 }
615done:
616 return ret;
617}
618
Radek Krejcia3045382018-11-22 14:30:31 +0100619/**
620 * @brief Validate and normalize numeric value from a range definition.
621 * @param[in] ctx Compile context.
622 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
623 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
624 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
625 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
626 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
627 * @param[in] value String value of the range boundary.
628 * @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.
629 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
630 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
631 */
Radek Krejci19a96102018-11-15 13:38:09 +0100632static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100633range_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 +0100634{
Radek Krejci6cba4292018-11-15 17:33:29 +0100635 size_t fraction = 0, size;
636
Radek Krejci19a96102018-11-15 13:38:09 +0100637 *len = 0;
638
639 assert(value);
640 /* parse value */
641 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
642 return LY_EVALID;
643 }
644
645 if ((value[*len] == '-') || (value[*len] == '+')) {
646 ++(*len);
647 }
648
649 while (isdigit(value[*len])) {
650 ++(*len);
651 }
652
653 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100654 if (basetype == LY_TYPE_DEC64) {
655 goto decimal;
656 } else {
657 *valcopy = strndup(value, *len);
658 return LY_SUCCESS;
659 }
Radek Krejci19a96102018-11-15 13:38:09 +0100660 }
661 fraction = *len;
662
663 ++(*len);
664 while (isdigit(value[*len])) {
665 ++(*len);
666 }
667
Radek Krejci6cba4292018-11-15 17:33:29 +0100668 if (basetype == LY_TYPE_DEC64) {
669decimal:
670 assert(frdigits);
671 if (*len - 1 - fraction > frdigits) {
672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
673 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
674 *len, value, frdigits);
675 return LY_EINVAL;
676 }
677 if (fraction) {
678 size = (*len) + (frdigits - ((*len) - 1 - fraction));
679 } else {
680 size = (*len) + frdigits + 1;
681 }
682 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100683 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
684
Radek Krejci6cba4292018-11-15 17:33:29 +0100685 (*valcopy)[size - 1] = '\0';
686 if (fraction) {
687 memcpy(&(*valcopy)[0], &value[0], fraction);
688 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
689 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
690 } else {
691 memcpy(&(*valcopy)[0], &value[0], *len);
692 memset(&(*valcopy)[*len], '0', frdigits);
693 }
Radek Krejci19a96102018-11-15 13:38:09 +0100694 }
695 return LY_SUCCESS;
696}
697
Radek Krejcia3045382018-11-22 14:30:31 +0100698/**
699 * @brief Check that values in range are in ascendant order.
700 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +0100701 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
702 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +0100703 * @param[in] value Current value to check.
704 * @param[in] prev_value The last seen value.
705 * @return LY_SUCCESS or LY_EEXIST for invalid order.
706 */
Radek Krejci19a96102018-11-15 13:38:09 +0100707static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +0100708range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +0100709{
710 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +0100711 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100712 return LY_EEXIST;
713 }
714 } else {
Radek Krejci5969f272018-11-23 10:03:58 +0100715 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100716 return LY_EEXIST;
717 }
718 }
719 return LY_SUCCESS;
720}
721
Radek Krejcia3045382018-11-22 14:30:31 +0100722/**
723 * @brief Set min/max value of the range part.
724 * @param[in] ctx Compile context.
725 * @param[in] part Range part structure to fill.
726 * @param[in] max Flag to distinguish if storing min or max value.
727 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
728 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
729 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
730 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
731 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +0100732 * @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 +0100733 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
734 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
735 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
736 * frdigits value), LY_EMEM.
737 */
Radek Krejci19a96102018-11-15 13:38:09 +0100738static LY_ERR
739range_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 +0100740 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100741{
742 LY_ERR ret = LY_SUCCESS;
743 char *valcopy = NULL;
744 size_t len;
745
746 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100747 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +0100748 LY_CHECK_GOTO(ret, finalize);
749 }
750 if (!valcopy && base_range) {
751 if (max) {
752 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
753 } else {
754 part->min_64 = base_range->parts[0].min_64;
755 }
756 if (!first) {
757 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
758 }
759 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +0100760 }
761
762 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100763 case LY_TYPE_INT8: /* range */
764 if (valcopy) {
765 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
766 } else if (max) {
767 part->max_64 = INT64_C(127);
768 } else {
769 part->min_64 = INT64_C(-128);
770 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100771 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100772 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100773 }
774 break;
775 case LY_TYPE_INT16: /* range */
776 if (valcopy) {
777 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
778 } else if (max) {
779 part->max_64 = INT64_C(32767);
780 } else {
781 part->min_64 = INT64_C(-32768);
782 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100783 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100784 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100785 }
786 break;
787 case LY_TYPE_INT32: /* range */
788 if (valcopy) {
789 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
790 } else if (max) {
791 part->max_64 = INT64_C(2147483647);
792 } else {
793 part->min_64 = INT64_C(-2147483648);
794 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100795 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100796 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100797 }
798 break;
799 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +0100800 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100801 if (valcopy) {
802 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
803 max ? &part->max_64 : &part->min_64);
804 } else if (max) {
805 part->max_64 = INT64_C(9223372036854775807);
806 } else {
807 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
808 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100809 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100810 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100811 }
812 break;
813 case LY_TYPE_UINT8: /* range */
814 if (valcopy) {
815 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
816 } else if (max) {
817 part->max_u64 = UINT64_C(255);
818 } else {
819 part->min_u64 = UINT64_C(0);
820 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100821 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100822 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100823 }
824 break;
825 case LY_TYPE_UINT16: /* range */
826 if (valcopy) {
827 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
828 } else if (max) {
829 part->max_u64 = UINT64_C(65535);
830 } else {
831 part->min_u64 = UINT64_C(0);
832 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100833 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100834 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100835 }
836 break;
837 case LY_TYPE_UINT32: /* range */
838 if (valcopy) {
839 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
840 } else if (max) {
841 part->max_u64 = UINT64_C(4294967295);
842 } else {
843 part->min_u64 = UINT64_C(0);
844 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100845 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100846 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100847 }
848 break;
849 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100850 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +0100851 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +0100852 if (valcopy) {
853 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
854 } else if (max) {
855 part->max_u64 = UINT64_C(18446744073709551615);
856 } else {
857 part->min_u64 = UINT64_C(0);
858 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100859 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100860 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100861 }
862 break;
863 default:
864 LOGINT(ctx->ctx);
865 ret = LY_EINT;
866 }
867
Radek Krejci5969f272018-11-23 10:03:58 +0100868finalize:
Radek Krejci19a96102018-11-15 13:38:09 +0100869 if (ret == LY_EDENIED) {
870 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
871 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
872 length_restr ? "length" : "range", valcopy ? valcopy : *value);
873 } else if (ret == LY_EVALID) {
874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
875 "Invalid %s restriction - invalid value \"%s\".",
876 length_restr ? "length" : "range", valcopy ? valcopy : *value);
877 } else if (ret == LY_EEXIST) {
878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
879 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100880 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +0100881 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +0100882 } else if (!ret && value) {
883 *value = *value + len;
884 }
885 free(valcopy);
886 return ret;
887}
888
Radek Krejcia3045382018-11-22 14:30:31 +0100889/**
890 * @brief Compile the parsed range restriction.
891 * @param[in] ctx Compile context.
892 * @param[in] range_p Parsed range structure to compile.
893 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
894 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
895 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
896 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
897 * range restriction must be more restrictive than the base_range.
898 * @param[in,out] range Pointer to the created current range structure.
899 * @return LY_ERR value.
900 */
Radek Krejci19a96102018-11-15 13:38:09 +0100901static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100902lys_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 +0100903 struct lysc_range *base_range, struct lysc_range **range)
904{
905 LY_ERR ret = LY_EVALID;
906 const char *expr;
907 struct lysc_range_part *parts = NULL, *part;
908 int range_expected = 0, uns;
909 unsigned int parts_done = 0, u, v;
910
911 assert(range);
912 assert(range_p);
913
914 expr = range_p->arg;
915 while(1) {
916 if (isspace(*expr)) {
917 ++expr;
918 } else if (*expr == '\0') {
919 if (range_expected) {
920 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
921 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
922 length_restr ? "length" : "range", range_p->arg);
923 goto cleanup;
924 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
926 "Invalid %s restriction - unexpected end of the expression (%s).",
927 length_restr ? "length" : "range", range_p->arg);
928 goto cleanup;
929 }
930 parts_done++;
931 break;
932 } else if (!strncmp(expr, "min", 3)) {
933 if (parts) {
934 /* min cannot be used elsewhere than in the first part */
935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
936 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
937 expr - range_p->arg, range_p->arg);
938 goto cleanup;
939 }
940 expr += 3;
941
942 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +0100943 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 +0100944 part->max_64 = part->min_64;
945 } else if (*expr == '|') {
946 if (!parts || range_expected) {
947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
948 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
949 goto cleanup;
950 }
951 expr++;
952 parts_done++;
953 /* process next part of the expression */
954 } else if (!strncmp(expr, "..", 2)) {
955 expr += 2;
956 while (isspace(*expr)) {
957 expr++;
958 }
959 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
960 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
961 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
962 goto cleanup;
963 }
964 /* continue expecting the upper boundary */
965 range_expected = 1;
966 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
967 /* number */
968 if (range_expected) {
969 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100970 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 +0100971 range_expected = 0;
972 } else {
973 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
974 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 +0100975 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100976 part->max_64 = part->min_64;
977 }
978
979 /* continue with possible another expression part */
980 } else if (!strncmp(expr, "max", 3)) {
981 expr += 3;
982 while (isspace(*expr)) {
983 expr++;
984 }
985 if (*expr != '\0') {
986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
987 length_restr ? "length" : "range", expr);
988 goto cleanup;
989 }
990 if (range_expected) {
991 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100992 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 +0100993 range_expected = 0;
994 } else {
995 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
996 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 +0100997 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100998 part->min_64 = part->max_64;
999 }
1000 } else {
1001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1002 length_restr ? "length" : "range", expr);
1003 goto cleanup;
1004 }
1005 }
1006
1007 /* check with the previous range/length restriction */
1008 if (base_range) {
1009 switch (basetype) {
1010 case LY_TYPE_BINARY:
1011 case LY_TYPE_UINT8:
1012 case LY_TYPE_UINT16:
1013 case LY_TYPE_UINT32:
1014 case LY_TYPE_UINT64:
1015 case LY_TYPE_STRING:
1016 uns = 1;
1017 break;
1018 case LY_TYPE_DEC64:
1019 case LY_TYPE_INT8:
1020 case LY_TYPE_INT16:
1021 case LY_TYPE_INT32:
1022 case LY_TYPE_INT64:
1023 uns = 0;
1024 break;
1025 default:
1026 LOGINT(ctx->ctx);
1027 ret = LY_EINT;
1028 goto cleanup;
1029 }
1030 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1031 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1032 goto baseerror;
1033 }
1034 /* current lower bound is not lower than the base */
1035 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1036 /* base has single value */
1037 if (base_range->parts[v].min_64 == parts[u].min_64) {
1038 /* both lower bounds are the same */
1039 if (parts[u].min_64 != parts[u].max_64) {
1040 /* current continues with a range */
1041 goto baseerror;
1042 } else {
1043 /* equal single values, move both forward */
1044 ++v;
1045 continue;
1046 }
1047 } else {
1048 /* base is single value lower than current range, so the
1049 * value from base range is removed in the current,
1050 * move only base and repeat checking */
1051 ++v;
1052 --u;
1053 continue;
1054 }
1055 } else {
1056 /* base is the range */
1057 if (parts[u].min_64 == parts[u].max_64) {
1058 /* current is a single value */
1059 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1060 /* current is behind the base range, so base range is omitted,
1061 * move the base and keep the current for further check */
1062 ++v;
1063 --u;
1064 } /* else it is within the base range, so move the current, but keep the base */
1065 continue;
1066 } else {
1067 /* both are ranges - check the higher bound, the lower was already checked */
1068 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1069 /* higher bound is higher than the current higher bound */
1070 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1071 /* but the current lower bound is also higher, so the base range is omitted,
1072 * continue with the same current, but move the base */
1073 --u;
1074 ++v;
1075 continue;
1076 }
1077 /* current range starts within the base range but end behind it */
1078 goto baseerror;
1079 } else {
1080 /* current range is smaller than the base,
1081 * move current, but stay with the base */
1082 continue;
1083 }
1084 }
1085 }
1086 }
1087 if (u != parts_done) {
1088baseerror:
1089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1090 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1091 length_restr ? "length" : "range", range_p->arg);
1092 goto cleanup;
1093 }
1094 }
1095
1096 if (!(*range)) {
1097 *range = calloc(1, sizeof **range);
1098 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1099 }
1100
1101 if (range_p->eapptag) {
1102 lydict_remove(ctx->ctx, (*range)->eapptag);
1103 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1104 }
1105 if (range_p->emsg) {
1106 lydict_remove(ctx->ctx, (*range)->emsg);
1107 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1108 }
1109 /* extensions are taken only from the last range by the caller */
1110
1111 (*range)->parts = parts;
1112 parts = NULL;
1113 ret = LY_SUCCESS;
1114cleanup:
1115 /* TODO clean up */
1116 LY_ARRAY_FREE(parts);
1117
1118 return ret;
1119}
1120
1121/**
1122 * @brief Checks pattern syntax.
1123 *
Radek Krejcia3045382018-11-22 14:30:31 +01001124 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001125 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001126 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1127 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001128 */
1129static LY_ERR
1130lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1131{
1132 int idx, idx2, start, end, err_offset, count;
1133 char *perl_regex, *ptr;
1134 const char *err_msg, *orig_ptr;
1135 pcre *precomp;
1136#define URANGE_LEN 19
1137 char *ublock2urange[][2] = {
1138 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1139 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1140 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1141 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1142 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1143 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1144 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1145 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1146 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1147 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1148 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1149 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1150 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1151 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1152 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1153 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1154 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1155 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1156 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1157 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1158 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1159 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1160 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1161 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1162 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1163 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1164 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1165 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1166 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1167 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1168 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1169 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1170 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1171 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1172 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1173 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1174 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1175 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1176 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1177 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1178 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1179 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1180 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1181 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1182 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1183 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1184 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1185 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1186 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1187 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1188 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1189 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1190 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1191 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1192 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1193 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1194 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1195 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1196 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1197 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1198 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1199 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1200 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1201 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1202 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1203 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1204 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1205 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1206 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1207 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1208 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1209 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1210 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1211 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1212 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1213 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1214 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1215 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1216 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1217 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1218 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1219 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1220 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1221 {NULL, NULL}
1222 };
1223
1224 /* adjust the expression to a Perl equivalent
1225 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1226
1227 /* we need to replace all "$" with "\$", count them now */
1228 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1229
1230 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1231 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1232 perl_regex[0] = '\0';
1233
1234 ptr = perl_regex;
1235
1236 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1237 /* we will add line-end anchoring */
1238 ptr[0] = '(';
1239 ++ptr;
1240 }
1241
1242 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1243 if (orig_ptr[0] == '$') {
1244 ptr += sprintf(ptr, "\\$");
1245 } else if (orig_ptr[0] == '^') {
1246 ptr += sprintf(ptr, "\\^");
1247 } else {
1248 ptr[0] = orig_ptr[0];
1249 ++ptr;
1250 }
1251 }
1252
1253 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1254 ptr += sprintf(ptr, ")$");
1255 } else {
1256 ptr[0] = '\0';
1257 ++ptr;
1258 }
1259
1260 /* substitute Unicode Character Blocks with exact Character Ranges */
1261 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1262 start = ptr - perl_regex;
1263
1264 ptr = strchr(ptr, '}');
1265 if (!ptr) {
1266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1267 pattern, perl_regex + start + 2, "unterminated character property");
1268 free(perl_regex);
1269 return LY_EVALID;
1270 }
1271 end = (ptr - perl_regex) + 1;
1272
1273 /* need more space */
1274 if (end - start < URANGE_LEN) {
1275 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1276 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1277 }
1278
1279 /* find our range */
1280 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1281 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1282 break;
1283 }
1284 }
1285 if (!ublock2urange[idx][0]) {
1286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1287 pattern, perl_regex + start + 5, "unknown block name");
1288 free(perl_regex);
1289 return LY_EVALID;
1290 }
1291
1292 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1293 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1294 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1295 ++count;
1296 }
1297 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1298 --count;
1299 }
1300 }
1301 if (count) {
1302 /* skip brackets */
1303 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1304 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1305 } else {
1306 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1307 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1308 }
1309 }
1310
1311 /* must return 0, already checked during parsing */
1312 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1313 &err_msg, &err_offset, NULL);
1314 if (!precomp) {
1315 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1316 free(perl_regex);
1317 return LY_EVALID;
1318 }
1319 free(perl_regex);
1320
1321 if (pcre_precomp) {
1322 *pcre_precomp = precomp;
1323 } else {
1324 free(precomp);
1325 }
1326
1327 return LY_SUCCESS;
1328
1329#undef URANGE_LEN
1330}
1331
Radek Krejcia3045382018-11-22 14:30:31 +01001332/**
1333 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1334 * @param[in] ctx Compile context.
1335 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1336 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1337 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1338 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1339 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1340 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1341 */
Radek Krejci19a96102018-11-15 13:38:09 +01001342static LY_ERR
1343lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1344 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1345{
1346 struct lysc_pattern **pattern;
1347 unsigned int u, v;
1348 const char *err_msg;
1349 LY_ERR ret = LY_SUCCESS;
1350
1351 /* first, copy the patterns from the base type */
1352 if (base_patterns) {
1353 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1354 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1355 }
1356
1357 LY_ARRAY_FOR(patterns_p, u) {
1358 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1359 *pattern = calloc(1, sizeof **pattern);
1360 ++(*pattern)->refcount;
1361
1362 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1363 LY_CHECK_RET(ret);
1364 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1365 if (err_msg) {
1366 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1367 }
1368
1369 if (patterns_p[u].arg[0] == 0x15) {
1370 (*pattern)->inverted = 1;
1371 }
1372 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1373 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
1374 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1375 options, v, lys_compile_ext, ret, done);
1376 }
1377done:
1378 return ret;
1379}
1380
Radek Krejcia3045382018-11-22 14:30:31 +01001381/**
1382 * @brief map of the possible restrictions combination for the specific built-in type.
1383 */
Radek Krejci19a96102018-11-15 13:38:09 +01001384static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1385 0 /* LY_TYPE_UNKNOWN */,
1386 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001387 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1388 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1389 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1390 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1391 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001392 LYS_SET_BIT /* LY_TYPE_BITS */,
1393 0 /* LY_TYPE_BOOL */,
1394 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1395 0 /* LY_TYPE_EMPTY */,
1396 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1397 LYS_SET_BASE /* LY_TYPE_IDENT */,
1398 LYS_SET_REQINST /* LY_TYPE_INST */,
1399 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001400 LYS_SET_TYPE /* LY_TYPE_UNION */,
1401 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001402 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001403 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001404 LYS_SET_RANGE /* LY_TYPE_INT64 */
1405};
1406
1407/**
1408 * @brief stringification of the YANG built-in data types
1409 */
1410const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1411 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1412 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001413};
1414
Radek Krejcia3045382018-11-22 14:30:31 +01001415/**
1416 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1417 * @param[in] ctx Compile context.
1418 * @param[in] enums_p Array of the parsed enum structures to compile.
1419 * @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.
1420 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1421 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1422 * @param[out] enums Newly created array of the compiled enums information for the current type.
1423 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1424 */
Radek Krejci19a96102018-11-15 13:38:09 +01001425static LY_ERR
1426lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1427 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1428{
1429 LY_ERR ret = LY_SUCCESS;
1430 unsigned int u, v, match;
1431 int32_t value = 0;
1432 uint32_t position = 0;
1433 struct lysc_type_enum_item *e, storage;
1434
1435 if (base_enums && ctx->mod->compiled->version < 2) {
1436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1437 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1438 return LY_EVALID;
1439 }
1440
1441 LY_ARRAY_FOR(enums_p, u) {
1442 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1443 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
1444 if (base_enums) {
1445 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1446 LY_ARRAY_FOR(base_enums, v) {
1447 if (!strcmp(e->name, base_enums[v].name)) {
1448 break;
1449 }
1450 }
1451 if (v == LY_ARRAY_SIZE(base_enums)) {
1452 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1453 "Invalid %s - derived type adds new item \"%s\".",
1454 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1455 return LY_EVALID;
1456 }
1457 match = v;
1458 }
1459
1460 if (basetype == LY_TYPE_ENUM) {
1461 if (enums_p[u].flags & LYS_SET_VALUE) {
1462 e->value = (int32_t)enums_p[u].value;
1463 if (!u || e->value >= value) {
1464 value = e->value + 1;
1465 }
1466 /* check collision with other values */
1467 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1468 if (e->value == (*enums)[v].value) {
1469 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1470 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1471 e->value, e->name, (*enums)[v].name);
1472 return LY_EVALID;
1473 }
1474 }
1475 } else if (base_enums) {
1476 /* inherit the assigned value */
1477 e->value = base_enums[match].value;
1478 if (!u || e->value >= value) {
1479 value = e->value + 1;
1480 }
1481 } else {
1482 /* assign value automatically */
1483 if (u && value == INT32_MIN) {
1484 /* counter overflow */
1485 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1486 "Invalid enumeration - it is not possible to auto-assign enum value for "
1487 "\"%s\" since the highest value is already 2147483647.", e->name);
1488 return LY_EVALID;
1489 }
1490 e->value = value++;
1491 }
1492 } else { /* LY_TYPE_BITS */
1493 if (enums_p[u].flags & LYS_SET_VALUE) {
1494 e->value = (int32_t)enums_p[u].value;
1495 if (!u || (uint32_t)e->value >= position) {
1496 position = (uint32_t)e->value + 1;
1497 }
1498 /* check collision with other values */
1499 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1500 if (e->value == (*enums)[v].value) {
1501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1502 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1503 (uint32_t)e->value, e->name, (*enums)[v].name);
1504 return LY_EVALID;
1505 }
1506 }
1507 } else if (base_enums) {
1508 /* inherit the assigned value */
1509 e->value = base_enums[match].value;
1510 if (!u || (uint32_t)e->value >= position) {
1511 position = (uint32_t)e->value + 1;
1512 }
1513 } else {
1514 /* assign value automatically */
1515 if (u && position == 0) {
1516 /* counter overflow */
1517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1518 "Invalid bits - it is not possible to auto-assign bit position for "
1519 "\"%s\" since the highest value is already 4294967295.", e->name);
1520 return LY_EVALID;
1521 }
1522 e->value = position++;
1523 }
1524 }
1525
1526 if (base_enums) {
1527 /* the assigned values must not change from the derived type */
1528 if (e->value != base_enums[match].value) {
1529 if (basetype == LY_TYPE_ENUM) {
1530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1531 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1532 e->name, base_enums[match].value, e->value);
1533 } else {
1534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1535 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1536 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1537 }
1538 return LY_EVALID;
1539 }
1540 }
1541
1542 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1543 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1544
1545 if (basetype == LY_TYPE_BITS) {
1546 /* keep bits ordered by position */
1547 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1548 if (v != u) {
1549 memcpy(&storage, e, sizeof *e);
1550 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1551 memcpy(&(*enums)[v], &storage, sizeof storage);
1552 }
1553 }
1554 }
1555
1556done:
1557 return ret;
1558}
1559
Radek Krejcia3045382018-11-22 14:30:31 +01001560#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1561 for ((NODE) = (NODE)->parent; \
1562 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1563 (NODE) = (NODE)->parent); \
1564 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1565 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1566 TERM; \
1567 }
1568
1569/**
1570 * @brief Validate the predicate(s) from the leafref path.
1571 * @param[in] ctx Compile context
1572 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1573 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001574 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001575 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001576 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001577 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1578 */
1579static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001580lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001581 const struct lysc_node *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001582{
1583 LY_ERR ret = LY_EVALID;
1584 const struct lys_module *mod;
1585 const struct lysc_node *src_node, *dst_node;
1586 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1587 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
1588 unsigned int dest_parent_times;
1589 const char *start, *end, *pke_end;
1590 struct ly_set keys = {0};
1591 int i;
1592
1593 while (**predicate == '[') {
1594 start = (*predicate)++;
1595
1596 while (isspace(**predicate)) {
1597 ++(*predicate);
1598 }
1599 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1600 while (isspace(**predicate)) {
1601 ++(*predicate);
1602 }
1603 if (**predicate != '=') {
1604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1605 "Invalid leafref path predicate \"%.*s\" - missing \"=\".", *predicate - start + 1, *predicate);
1606 goto cleanup;
1607 }
1608 ++(*predicate);
1609 while (isspace(**predicate)) {
1610 ++(*predicate);
1611 }
1612
1613 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1615 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1616 goto cleanup;
1617 }
1618 --pke_end;
1619 while (isspace(*pke_end)) {
1620 --pke_end;
1621 }
1622 /* localize path-key-expr */
1623 pke_start = path_key_expr = *predicate;
1624 /* move after the current predicate */
1625 *predicate = end + 1;
1626
1627 /* source (must be leaf or leaf-list) */
1628 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001629 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001630 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001631 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001632 }
1633 src_node = lys_child(context_node, mod, src, src_len,
1634 mod->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST, LYS_GETNEXT_NOSTATECHECK);
1635 if (!src_node) {
1636 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1637 "Invalid leafref path predicate \"%.*s\" - key node \"%.*s\" from module \"%s\" not found.",
1638 *predicate - start, start, src_len, src, mod->compiled->name);
1639 goto cleanup;
1640 }
1641 /* TODO - check the src_node is really a key of the context_node */
1642
1643 /* check that there is only one predicate for the */
1644 i = ly_set_add(&keys, (void*)src_node, 0);
1645 LY_CHECK_GOTO(i == -1, cleanup);
1646 if (keys.count != (unsigned int)i + 1) {
1647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1648 "Invalid leafref path predicate \"%.*s\" - multiple equality test for the key %s.",
1649 *predicate - start, start, src_node->name);
1650 goto cleanup;
1651 }
1652
1653 /* destination */
1654 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01001655 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01001656
1657 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
1658 if (strncmp(path_key_expr, "current()", 9)) {
1659 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1660 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
1661 *predicate - start, start);
1662 goto cleanup;
1663 }
1664 path_key_expr += 9;
1665 while (isspace(*path_key_expr)) {
1666 ++path_key_expr;
1667 }
1668
1669 if (*path_key_expr != '/') {
1670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1671 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
1672 *predicate - start, start);
1673 goto cleanup;
1674 }
1675 ++path_key_expr;
1676 while (isspace(*path_key_expr)) {
1677 ++path_key_expr;
1678 }
1679
1680 /* rel-path-keyexpr:
1681 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
1682 while (!strncmp(path_key_expr, "..", 2)) {
1683 ++dest_parent_times;
1684 path_key_expr += 2;
1685 while (isspace(*path_key_expr)) {
1686 ++path_key_expr;
1687 }
1688 if (*path_key_expr != '/') {
1689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1690 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
1691 *predicate - start, start);
1692 goto cleanup;
1693 }
1694 ++path_key_expr;
1695 while (isspace(*path_key_expr)) {
1696 ++path_key_expr;
1697 }
1698
1699 /* path is supposed to be evaluated in data tree, so we have to skip
1700 * all schema nodes that cannot be instantiated in data tree */
1701 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
1702 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
1703 *predicate - start, start);
1704 }
1705 if (!dest_parent_times) {
1706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1707 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
1708 *predicate - start, start);
1709 goto cleanup;
1710 }
1711 if (path_key_expr == pke_end) {
1712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1713 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
1714 *predicate - start, start);
1715 goto cleanup;
1716 }
1717
1718 while(path_key_expr != pke_end) {
1719 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
1720 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1721 "Invalid node identifier in leafref path predicate - character %d (%.*s).",
1722 path_key_expr - start, *predicate - start, start);
1723 goto cleanup;
1724 }
1725
1726 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001727 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001728 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001729 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001730 }
1731 if (!mod) {
1732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1733 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
1734 *predicate - start, start, dst_len, dst);
1735 goto cleanup;
1736 }
1737
1738 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
1739 if (!dst_node) {
1740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1741 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
1742 *predicate - start, start, path_key_expr - pke_start, pke_start);
1743 goto cleanup;
1744 }
1745 }
1746 if (!(dst_node->nodetype & (dst_node->module->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
1747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1748 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s.",
1749 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
1750 goto cleanup;
1751 }
1752 }
1753
1754 ret = LY_SUCCESS;
1755cleanup:
1756 ly_set_erase(&keys, NULL);
1757 return ret;
1758}
1759
1760/**
1761 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
1762 *
1763 * path-arg = absolute-path / relative-path
1764 * absolute-path = 1*("/" (node-identifier *path-predicate))
1765 * relative-path = 1*(".." "/") descendant-path
1766 *
1767 * @param[in,out] path Path to parse.
1768 * @param[out] prefix Prefix of the token, NULL if there is not any.
1769 * @param[out] pref_len Length of the prefix, 0 if there is not any.
1770 * @param[out] name Name of the token.
1771 * @param[out] nam_len Length of the name.
1772 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
1773 * must not be changed between consecutive calls. -1 if the
1774 * path is absolute.
1775 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
1776 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
1777 */
1778static LY_ERR
1779lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
1780 int *parent_times, int *has_predicate)
1781{
1782 int par_times = 0;
1783
1784 assert(path && *path);
1785 assert(parent_times);
1786 assert(prefix);
1787 assert(prefix_len);
1788 assert(name);
1789 assert(name_len);
1790 assert(has_predicate);
1791
1792 *prefix = NULL;
1793 *prefix_len = 0;
1794 *name = NULL;
1795 *name_len = 0;
1796 *has_predicate = 0;
1797
1798 if (!*parent_times) {
1799 if (!strncmp(*path, "..", 2)) {
1800 *path += 2;
1801 ++par_times;
1802 while (!strncmp(*path, "/..", 3)) {
1803 *path += 3;
1804 ++par_times;
1805 }
1806 }
1807 if (par_times) {
1808 *parent_times = par_times;
1809 } else {
1810 *parent_times = -1;
1811 }
1812 }
1813
1814 if (**path != '/') {
1815 return LY_EINVAL;
1816 }
1817 /* skip '/' */
1818 ++(*path);
1819
1820 /* node-identifier ([prefix:]name) */
1821 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
1822
1823 if ((**path == '/' && (*path)[1]) || !**path) {
1824 /* path continues by another token or this is the last token */
1825 return LY_SUCCESS;
1826 } else if ((*path)[0] != '[') {
1827 /* unexpected character */
1828 return LY_EINVAL;
1829 } else {
1830 /* predicate starting with [ */
1831 *has_predicate = 1;
1832 return LY_SUCCESS;
1833 }
1834}
1835
1836/**
Radek Krejci58d171e2018-11-23 13:50:55 +01001837 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
1838 *
1839 * The set of features used for target must be a subset of features used for the leafref.
1840 * This is not a perfect, we should compare the truth tables but it could require too much resources
1841 * and RFC 7950 does not require it explicitely, so we simplify that.
1842 *
1843 * @param[in] refnode The leafref node.
1844 * @param[in] target Tha target node of the leafref.
1845 * @return LY_SUCCESS or LY_EVALID;
1846 */
1847static LY_ERR
1848lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
1849{
1850 LY_ERR ret = LY_EVALID;
1851 const struct lysc_node *iter;
1852 struct lysc_iffeature **iff;
1853 unsigned int u, v, count;
1854 struct ly_set features = {0};
1855
1856 for (iter = refnode; iter; iter = iter->parent) {
1857 iff = lysc_node_iff(iter);
1858 if (iff && *iff) {
1859 LY_ARRAY_FOR(*iff, u) {
1860 LY_ARRAY_FOR((*iff)[u].features, v) {
1861 LY_CHECK_GOTO(ly_set_add(&features, (*iff)[u].features[v], 0) == -1, cleanup);
1862 }
1863 }
1864 }
1865 }
1866
1867 /* we should have, in features set, a superset of features applicable to the target node.
1868 * So when adding features applicable to the target into the features set, we should not be
1869 * able to actually add any new feature, otherwise it is not a subset of features applicable
1870 * to the leafref itself. */
1871 count = features.count;
1872 for (iter = target; iter; iter = iter->parent) {
1873 iff = lysc_node_iff(iter);
1874 if (iff && *iff) {
1875 LY_ARRAY_FOR(*iff, u) {
1876 LY_ARRAY_FOR((*iff)[u].features, v) {
1877 if ((unsigned int)ly_set_add(&features, (*iff)[u].features[v], 0) >= count) {
1878 /* new feature was added (or LY_EMEM) */
1879 goto cleanup;
1880 }
1881 }
1882 }
1883 }
1884 }
1885 ret = LY_SUCCESS;
1886
1887cleanup:
1888 ly_set_erase(&features, NULL);
1889 return ret;
1890}
1891
1892/**
Radek Krejcia3045382018-11-22 14:30:31 +01001893 * @brief Validate the leafref path.
1894 * @param[in] ctx Compile context
1895 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01001896 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01001897 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1898 */
1899static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01001900lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01001901{
1902 const struct lysc_node *node = NULL, *parent = NULL;
1903 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001904 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01001905 const char *id, *prefix, *name;
1906 size_t prefix_len, name_len;
1907 int parent_times = 0, has_predicate;
1908 unsigned int iter, u;
1909 LY_ERR ret = LY_SUCCESS;
1910
1911 assert(ctx);
1912 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001913 assert(leafref);
1914
1915 /* 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 +01001916
1917 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001918 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01001919 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
1920 if (!iter) { /* first iteration */
1921 /* precess ".." in relative paths */
1922 if (parent_times > 0) {
1923 /* move from the context node */
1924 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
1925 /* path is supposed to be evaluated in data tree, so we have to skip
1926 * all schema nodes that cannot be instantiated in data tree */
1927 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001928 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001929 }
1930 }
1931 }
1932
1933 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001934 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001935 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001936 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001937 }
1938 if (!mod) {
1939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001940 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
1941 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001942 return LY_EVALID;
1943 }
1944
1945 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
1946 if (!node) {
1947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001948 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001949 return LY_EVALID;
1950 }
1951 parent = node;
1952
1953 if (has_predicate) {
1954 /* we have predicate, so the current result must be list */
1955 if (node->nodetype != LYS_LIST) {
1956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1957 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001958 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001959 return LY_EVALID;
1960 }
1961
Radek Krejci4ce68932018-11-28 12:53:36 +01001962 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, node, leafref->path_context), LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01001963 }
1964
1965 ++iter;
1966 }
1967 if (ret) {
1968 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001969 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001970 return LY_EVALID;
1971 }
1972
1973 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1974 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1975 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001976 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001977 return LY_EVALID;
1978 }
1979
1980 /* check status */
1981 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
1982 return LY_EVALID;
1983 }
1984
Radek Krejcib57cf1e2018-11-23 14:07:05 +01001985 /* check config */
1986 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
1987 if (node->flags & LYS_CONFIG_R) {
1988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1989 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
1990 leafref->path);
1991 return LY_EVALID;
1992 }
1993 }
1994
Radek Krejci412ddfa2018-11-23 11:44:11 +01001995 /* store the target's type and check for circular chain of leafrefs */
1996 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
1997 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
1998 if (type == (struct lysc_type*)leafref) {
1999 /* circular chain detected */
2000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2001 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2002 return LY_EVALID;
2003 }
2004 }
2005
Radek Krejci58d171e2018-11-23 13:50:55 +01002006 /* check if leafref and its target are under common if-features */
2007 if (lys_compile_leafref_features_validate(startnode, node)) {
2008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2009 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2010 leafref->path);
2011 return LY_EVALID;
2012 }
2013
Radek Krejcia3045382018-11-22 14:30:31 +01002014 return LY_SUCCESS;
2015}
2016
Radek Krejcicdfecd92018-11-26 11:27:32 +01002017static 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,
2018 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt);
Radek Krejcia3045382018-11-22 14:30:31 +01002019/**
2020 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2021 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002022 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2023 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2024 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2025 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002026 * @param[in] type_p Parsed type to compile.
2027 * @param[in] basetype Base YANG built-in type of the type to compile.
2028 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2029 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2030 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2031 * @param[out] type Newly created type structure with the filled information about the type.
2032 * @return LY_ERR value.
2033 */
Radek Krejci19a96102018-11-15 13:38:09 +01002034static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002035lys_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,
2036 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2037 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002038{
2039 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002040 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002041 struct lysc_type_bin *bin;
2042 struct lysc_type_num *num;
2043 struct lysc_type_str *str;
2044 struct lysc_type_bits *bits;
2045 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002046 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002047 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002048 struct lysc_type_union *un, *un_aux;
2049 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002050
2051 switch (basetype) {
2052 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002053 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002054
2055 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002056 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002057 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002058 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2059 LY_CHECK_RET(ret);
2060 if (!tpdfname) {
2061 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2062 options, u, lys_compile_ext, ret, done);
2063 }
2064 }
2065
2066 if (tpdfname) {
2067 type_p->compiled = *type;
2068 *type = calloc(1, sizeof(struct lysc_type_bin));
2069 }
2070 break;
2071 case LY_TYPE_BITS:
2072 /* RFC 7950 9.7 - bits */
2073 bits = (struct lysc_type_bits*)(*type);
2074 if (type_p->bits) {
2075 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2076 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2077 (struct lysc_type_enum_item**)&bits->bits);
2078 LY_CHECK_RET(ret);
2079 }
2080
Radek Krejci555cb5b2018-11-16 14:54:33 +01002081 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002082 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002083 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002085 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002086 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002087 free(*type);
2088 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002089 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002090 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002091 }
2092
2093 if (tpdfname) {
2094 type_p->compiled = *type;
2095 *type = calloc(1, sizeof(struct lysc_type_bits));
2096 }
2097 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002098 case LY_TYPE_DEC64:
2099 dec = (struct lysc_type_dec*)(*type);
2100
2101 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002102 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002103 if (!type_p->fraction_digits) {
2104 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002106 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002108 free(*type);
2109 *type = NULL;
2110 }
2111 return LY_EVALID;
2112 }
2113 } else if (type_p->fraction_digits) {
2114 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002115 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2117 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002118 tpdfname);
2119 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002120 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2121 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002122 free(*type);
2123 *type = NULL;
2124 }
2125 return LY_EVALID;
2126 }
2127 dec->fraction_digits = type_p->fraction_digits;
2128
2129 /* RFC 7950 9.2.4 - range */
2130 if (type_p->range) {
2131 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2132 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2133 LY_CHECK_RET(ret);
2134 if (!tpdfname) {
2135 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2136 options, u, lys_compile_ext, ret, done);
2137 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002138 }
2139
2140 if (tpdfname) {
2141 type_p->compiled = *type;
2142 *type = calloc(1, sizeof(struct lysc_type_dec));
2143 }
2144 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002145 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002146 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002147
2148 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002149 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002150 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002151 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2152 LY_CHECK_RET(ret);
2153 if (!tpdfname) {
2154 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2155 options, u, lys_compile_ext, ret, done);
2156 }
2157 } else if (base && ((struct lysc_type_str*)base)->length) {
2158 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2159 }
2160
2161 /* RFC 7950 9.4.5 - pattern */
2162 if (type_p->patterns) {
2163 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2164 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2165 LY_CHECK_RET(ret);
2166 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2167 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2168 }
2169
2170 if (tpdfname) {
2171 type_p->compiled = *type;
2172 *type = calloc(1, sizeof(struct lysc_type_str));
2173 }
2174 break;
2175 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002176 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002177
2178 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002179 if (type_p->enums) {
2180 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2181 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2182 LY_CHECK_RET(ret);
2183 }
2184
Radek Krejci555cb5b2018-11-16 14:54:33 +01002185 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002186 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002187 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002188 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002189 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002191 free(*type);
2192 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002193 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002194 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002195 }
2196
2197 if (tpdfname) {
2198 type_p->compiled = *type;
2199 *type = calloc(1, sizeof(struct lysc_type_enum));
2200 }
2201 break;
2202 case LY_TYPE_INT8:
2203 case LY_TYPE_UINT8:
2204 case LY_TYPE_INT16:
2205 case LY_TYPE_UINT16:
2206 case LY_TYPE_INT32:
2207 case LY_TYPE_UINT32:
2208 case LY_TYPE_INT64:
2209 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002210 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002211
2212 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002213 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002214 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002215 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2216 LY_CHECK_RET(ret);
2217 if (!tpdfname) {
2218 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2219 options, u, lys_compile_ext, ret, done);
2220 }
2221 }
2222
2223 if (tpdfname) {
2224 type_p->compiled = *type;
2225 *type = calloc(1, sizeof(struct lysc_type_num));
2226 }
2227 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002228 case LY_TYPE_IDENT:
2229 idref = (struct lysc_type_identityref*)(*type);
2230
2231 /* RFC 7950 9.10.2 - base */
2232 if (type_p->bases) {
2233 if (base) {
2234 /* only the directly derived identityrefs can contain base specification */
2235 if (tpdfname) {
2236 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002237 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002238 tpdfname);
2239 } else {
2240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002241 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002242 free(*type);
2243 *type = NULL;
2244 }
2245 return LY_EVALID;
2246 }
2247 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2248 LY_CHECK_RET(ret);
2249 }
2250
2251 if (!base && !type_p->flags) {
2252 /* type derived from identityref built-in type must contain at least one base */
2253 if (tpdfname) {
2254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2255 } else {
2256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2257 free(*type);
2258 *type = NULL;
2259 }
2260 return LY_EVALID;
2261 }
2262
2263 if (tpdfname) {
2264 type_p->compiled = *type;
2265 *type = calloc(1, sizeof(struct lysc_type_identityref));
2266 }
2267 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002268 case LY_TYPE_LEAFREF:
2269 /* RFC 7950 9.9.3 - require-instance */
2270 if (type_p->flags & LYS_SET_REQINST) {
2271 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002272 } else if (base) {
2273 /* inherit */
2274 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002275 } else {
2276 /* default is true */
2277 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2278 }
2279 if (type_p->path) {
2280 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002281 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002282 } else if (base) {
2283 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002284 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002285 } else if (tpdfname) {
2286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2287 return LY_EVALID;
2288 } else {
2289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2290 free(*type);
2291 *type = NULL;
2292 return LY_EVALID;
2293 }
2294 if (tpdfname) {
2295 type_p->compiled = *type;
2296 *type = calloc(1, sizeof(struct lysc_type_leafref));
2297 }
2298 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002299 case LY_TYPE_INST:
2300 /* RFC 7950 9.9.3 - require-instance */
2301 if (type_p->flags & LYS_SET_REQINST) {
2302 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2303 } else {
2304 /* default is true */
2305 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2306 }
2307
2308 if (tpdfname) {
2309 type_p->compiled = *type;
2310 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2311 }
2312 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002313 case LY_TYPE_UNION:
2314 un = (struct lysc_type_union*)(*type);
2315
2316 /* RFC 7950 7.4 - type */
2317 if (type_p->types) {
2318 if (base) {
2319 /* only the directly derived union can contain types specification */
2320 if (tpdfname) {
2321 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2322 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2323 tpdfname);
2324 } else {
2325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2326 "Invalid type substatement for the type not directly derived from union built-in type.");
2327 free(*type);
2328 *type = NULL;
2329 }
2330 return LY_EVALID;
2331 }
2332 /* compile the type */
2333 additional = 0;
2334 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2335 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
2336 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);
2337 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2338 /* add space for additional types from the union subtype */
2339 un_aux = (struct lysc_type_union *)un->types[u + additional];
2340 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)));
2341 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2342 un->types = (void*)((uint32_t*)(p) + 1);
2343
2344 /* copy subtypes of the subtype union */
2345 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2346 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2347 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2348 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2349 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2350 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2351 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2352 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2353 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2354 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2355 /* TODO extensions */
2356
2357 } else {
2358 un->types[u + additional] = un_aux->types[v];
2359 ++un_aux->types[v]->refcount;
2360 }
2361 ++additional;
2362 LY_ARRAY_INCREMENT(un->types);
2363 }
2364 /* compensate u increment in main loop */
2365 --additional;
2366
2367 /* free the replaced union subtype */
2368 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2369 } else {
2370 LY_ARRAY_INCREMENT(un->types);
2371 }
2372 LY_CHECK_RET(ret);
2373 }
2374 }
2375
2376 if (!base && !type_p->flags) {
2377 /* type derived from union built-in type must contain at least one type */
2378 if (tpdfname) {
2379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2380 } else {
2381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2382 free(*type);
2383 *type = NULL;
2384 }
2385 return LY_EVALID;
2386 }
2387
2388 if (tpdfname) {
2389 type_p->compiled = *type;
2390 *type = calloc(1, sizeof(struct lysc_type_union));
2391 }
2392 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002393 case LY_TYPE_BOOL:
2394 case LY_TYPE_EMPTY:
2395 case LY_TYPE_UNKNOWN: /* just to complete switch */
2396 break;
2397 }
2398 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2399done:
2400 return ret;
2401}
2402
Radek Krejcia3045382018-11-22 14:30:31 +01002403/**
2404 * @brief Compile information about the leaf/leaf-list's type.
2405 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002406 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2407 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2408 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2409 * @param[in] context_name Name of the context node or referencing typedef for logging.
2410 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002411 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2412 * @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 +01002413 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
2414 * @param[out] dflt Storage for inheriting default value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002415 * @return LY_ERR value.
2416 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002417static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002418lys_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,
2419 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt)
Radek Krejci19a96102018-11-15 13:38:09 +01002420{
2421 LY_ERR ret = LY_SUCCESS;
2422 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002423 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002424 struct type_context {
2425 const struct lysp_tpdf *tpdf;
2426 struct lysp_node *node;
2427 struct lysp_module *mod;
2428 } *tctx, *tctx_prev = NULL;
2429 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002430 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002431 struct ly_set tpdf_chain = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01002432
2433 (*type) = NULL;
2434
2435 tctx = calloc(1, sizeof *tctx);
2436 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002437 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002438 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2439 ret == LY_SUCCESS;
2440 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2441 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2442 if (basetype) {
2443 break;
2444 }
2445
2446 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002447 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2448 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002449 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2450
Radek Krejcicdfecd92018-11-26 11:27:32 +01002451 if (units && !*units) {
2452 /* inherit units */
2453 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2454 }
2455 if (dflt && !*dflt) {
2456 /* inherit default */
2457 DUP_STRING(ctx->ctx, tctx->tpdf->dflt, *dflt);
2458 }
2459 if (dummyloops && (!units || *units) && (!dflt || *dflt)) {
2460 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2461 break;
2462 }
2463
Radek Krejci19a96102018-11-15 13:38:09 +01002464 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002465 /* it is not necessary to continue, the rest of the chain was already compiled,
2466 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002467 basetype = tctx->tpdf->type.compiled->basetype;
2468 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002469 if ((units && !*units) || (dflt && !*dflt)) {
2470 dummyloops = 1;
2471 goto preparenext;
2472 } else {
2473 tctx = NULL;
2474 break;
2475 }
Radek Krejci19a96102018-11-15 13:38:09 +01002476 }
2477
2478 /* store information for the following processing */
2479 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2480
Radek Krejcicdfecd92018-11-26 11:27:32 +01002481preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002482 /* prepare next loop */
2483 tctx_prev = tctx;
2484 tctx = calloc(1, sizeof *tctx);
2485 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2486 }
2487 free(tctx);
2488
2489 /* allocate type according to the basetype */
2490 switch (basetype) {
2491 case LY_TYPE_BINARY:
2492 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002493 break;
2494 case LY_TYPE_BITS:
2495 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002496 break;
2497 case LY_TYPE_BOOL:
2498 case LY_TYPE_EMPTY:
2499 *type = calloc(1, sizeof(struct lysc_type));
2500 break;
2501 case LY_TYPE_DEC64:
2502 *type = calloc(1, sizeof(struct lysc_type_dec));
2503 break;
2504 case LY_TYPE_ENUM:
2505 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002506 break;
2507 case LY_TYPE_IDENT:
2508 *type = calloc(1, sizeof(struct lysc_type_identityref));
2509 break;
2510 case LY_TYPE_INST:
2511 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2512 break;
2513 case LY_TYPE_LEAFREF:
2514 *type = calloc(1, sizeof(struct lysc_type_leafref));
2515 break;
2516 case LY_TYPE_STRING:
2517 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002518 break;
2519 case LY_TYPE_UNION:
2520 *type = calloc(1, sizeof(struct lysc_type_union));
2521 break;
2522 case LY_TYPE_INT8:
2523 case LY_TYPE_UINT8:
2524 case LY_TYPE_INT16:
2525 case LY_TYPE_UINT16:
2526 case LY_TYPE_INT32:
2527 case LY_TYPE_UINT32:
2528 case LY_TYPE_INT64:
2529 case LY_TYPE_UINT64:
2530 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002531 break;
2532 case LY_TYPE_UNKNOWN:
2533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2534 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2535 ret = LY_EVALID;
2536 goto cleanup;
2537 }
2538 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002539 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002540 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2541 ly_data_type2str[basetype]);
2542 free(*type);
2543 (*type) = NULL;
2544 ret = LY_EVALID;
2545 goto cleanup;
2546 }
2547
2548 /* get restrictions from the referred typedefs */
2549 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2550 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002551 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002552 base = tctx->tpdf->type.compiled;
2553 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002554 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002555 /* no change, just use the type information from the base */
2556 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2557 ++base->refcount;
2558 continue;
2559 }
2560
2561 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002562 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2564 tctx->tpdf->name, ly_data_type2str[basetype]);
2565 ret = LY_EVALID;
2566 goto cleanup;
2567 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2569 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2570 tctx->tpdf->name, tctx->tpdf->dflt);
2571 ret = LY_EVALID;
2572 goto cleanup;
2573 }
2574
Radek Krejci19a96102018-11-15 13:38:09 +01002575 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002576 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002577 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 +01002578 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2579 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002580 LY_CHECK_GOTO(ret, cleanup);
2581 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002582 }
2583
Radek Krejcic5c27e52018-11-15 14:38:11 +01002584 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002585 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002586 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002587 (*type)->basetype = basetype;
2588 ++(*type)->refcount;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002589 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 +01002590 LY_CHECK_GOTO(ret, cleanup);
2591 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002592 /* no specific restriction in leaf's type definition, copy from the base */
2593 free(*type);
2594 (*type) = base;
2595 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002596 }
2597
2598 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
2599
2600cleanup:
2601 ly_set_erase(&tpdf_chain, free);
2602 return ret;
2603}
2604
2605static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent);
2606
Radek Krejcia3045382018-11-22 14:30:31 +01002607/**
2608 * @brief Compile parsed container node information.
2609 * @param[in] ctx Compile context
2610 * @param[in] node_p Parsed container node.
2611 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2612 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2613 * is enriched with the container-specific information.
2614 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2615 */
Radek Krejci19a96102018-11-15 13:38:09 +01002616static LY_ERR
2617lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2618{
2619 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
2620 struct lysc_node_container *cont = (struct lysc_node_container*)node;
2621 struct lysp_node *child_p;
2622 unsigned int u;
2623 LY_ERR ret = LY_SUCCESS;
2624
2625 COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done);
2626 COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done);
2627
2628 LY_LIST_FOR(cont_p->child, child_p) {
2629 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2630 }
2631
2632 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2633 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2634 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2635
2636done:
2637 return ret;
2638}
2639
Radek Krejcia3045382018-11-22 14:30:31 +01002640/**
2641 * @brief Compile parsed leaf node information.
2642 * @param[in] ctx Compile context
2643 * @param[in] node_p Parsed leaf node.
2644 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2645 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2646 * is enriched with the leaf-specific information.
2647 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2648 */
Radek Krejci19a96102018-11-15 13:38:09 +01002649static LY_ERR
2650lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2651{
2652 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2653 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2654 unsigned int u;
2655 LY_ERR ret = LY_SUCCESS;
2656
2657 COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done);
2658 COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002659 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002660 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2661 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
Radek Krejci43699232018-11-23 14:59:46 +01002662
Radek Krejcicdfecd92018-11-26 11:27:32 +01002663 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 +01002664 leaf->units ? NULL : &leaf->units, leaf->dflt || (leaf->flags & LYS_MAND_TRUE) ? NULL : &leaf->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +01002665 LY_CHECK_GOTO(ret, done);
Radek Krejcia3045382018-11-22 14:30:31 +01002666 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2667 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2668 ly_set_add(&ctx->unres, leaf, 0);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002669 } else if (leaf->type->basetype == LY_TYPE_UNION) {
2670 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
2671 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2672 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2673 ly_set_add(&ctx->unres, leaf, 0);
2674 }
2675 }
Radek Krejci43699232018-11-23 14:59:46 +01002676 } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) {
2677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2678 "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt);
2679 return LY_EVALID;
Radek Krejcia3045382018-11-22 14:30:31 +01002680 }
2681
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002682 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2683
Radek Krejci19a96102018-11-15 13:38:09 +01002684done:
2685 return ret;
2686}
2687
Radek Krejcia3045382018-11-22 14:30:31 +01002688/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01002689 * @brief Compile parsed leaf-list node information.
2690 * @param[in] ctx Compile context
2691 * @param[in] node_p Parsed leaf-list node.
2692 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2693 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2694 * is enriched with the leaf-list-specific information.
2695 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2696 */
2697static LY_ERR
2698lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2699{
2700 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
2701 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
2702 unsigned int u, v;
2703 const char *dflt = NULL;
2704 LY_ERR ret = LY_SUCCESS;
2705
2706 COMPILE_MEMBER_GOTO(ctx, llist_p->when, llist->when, options, lys_compile_when, ret, done);
2707 COMPILE_ARRAY_GOTO(ctx, llist_p->iffeatures, llist->iffeatures, options, u, lys_compile_iffeature, ret, done);
2708 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
2709 DUP_STRING(ctx->ctx, llist_p->units, llist->units);
2710
2711 if (llist_p->dflts) {
2712 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
2713 LY_ARRAY_FOR(llist_p->dflts, u) {
2714 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
2715 LY_ARRAY_INCREMENT(llist->dflts);
2716 }
2717 }
2718
2719 llist->min = llist_p->min;
Radek Krejcib7408632018-11-28 17:12:11 +01002720 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002721
2722 ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod->parsed, node_p->name, &llist_p->type, options, &llist->type,
2723 llist->units ? NULL : &llist->units, (llist->dflts || llist->min) ? NULL : &dflt);
2724 LY_CHECK_GOTO(ret, done);
2725 if (dflt) {
2726 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done);
2727 llist->dflts[0] = dflt;
2728 LY_ARRAY_INCREMENT(llist->dflts);
2729 }
2730
2731 if (llist->type->basetype == LY_TYPE_LEAFREF) {
2732 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2733 ly_set_add(&ctx->unres, llist, 0);
2734 } else if (llist->type->basetype == LY_TYPE_UNION) {
2735 LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) {
2736 if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2737 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2738 ly_set_add(&ctx->unres, llist, 0);
2739 }
2740 }
Radek Krejci6bb080b2018-11-28 17:23:41 +01002741 } else if (llist->type->basetype == LY_TYPE_EMPTY) {
2742 if (ctx->mod->compiled->version < LYS_VERSION_1_1) {
2743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2744 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2745 return LY_EVALID;
2746 } else if (llist_p->dflts) {
2747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2748 "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]);
2749 return LY_EVALID;
2750 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002751 }
2752
2753 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
2754 /* configuration data values must be unique - so check the default values */
2755 LY_ARRAY_FOR(llist->dflts, u) {
2756 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
2757 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
2758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2759 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
2760 return LY_EVALID;
2761 }
2762 }
2763 }
2764 }
2765
2766 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2767
2768done:
2769 return ret;
2770}
2771
2772/**
Radek Krejcia3045382018-11-22 14:30:31 +01002773 * @brief Compile parsed schema node information.
2774 * @param[in] ctx Compile context
2775 * @param[in] node_p Parsed schema node.
2776 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2777 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
2778 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
2779 * the compile context.
2780 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2781 */
Radek Krejci19a96102018-11-15 13:38:09 +01002782static LY_ERR
2783lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
2784{
2785 LY_ERR ret = LY_EVALID;
2786 struct lysc_node *node, **children;
2787 unsigned int u;
2788 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
2789
2790 switch (node_p->nodetype) {
2791 case LYS_CONTAINER:
2792 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
2793 node_compile_spec = lys_compile_node_container;
2794 break;
2795 case LYS_LEAF:
2796 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
2797 node_compile_spec = lys_compile_node_leaf;
2798 break;
2799 case LYS_LIST:
2800 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
2801 break;
2802 case LYS_LEAFLIST:
2803 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01002804 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01002805 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002806 case LYS_CHOICE:
2807 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
2808 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002809 case LYS_ANYXML:
2810 case LYS_ANYDATA:
2811 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
2812 break;
2813 default:
2814 LOGINT(ctx->ctx);
2815 return LY_EINT;
2816 }
2817 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
2818 node->nodetype = node_p->nodetype;
2819 node->module = ctx->mod;
2820 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002821 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002822
2823 /* config */
2824 if (!(node->flags & LYS_CONFIG_MASK)) {
2825 /* config not explicitely set, inherit it from parent */
2826 if (parent) {
2827 node->flags |= parent->flags & LYS_CONFIG_MASK;
2828 } else {
2829 /* default is config true */
2830 node->flags |= LYS_CONFIG_W;
2831 }
2832 }
2833
Radek Krejcia6d57732018-11-29 13:40:37 +01002834 /* *list ordering */
2835 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
2836 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
2837 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
2838 "RPC/action output parameters or notification content (%s).", ctx->path);
2839 node->flags &= ~LYS_ORDBY_MASK;
2840 node->flags |= LYS_ORDBY_SYSTEM;
2841 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2842 /* default ordering is system */
2843 node->flags |= LYS_ORDBY_SYSTEM;
2844 }
2845 }
2846
Radek Krejci19a96102018-11-15 13:38:09 +01002847 /* status - it is not inherited by specification, but it does not make sense to have
2848 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2849 if (!(node->flags & LYS_STATUS_MASK)) {
2850 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2851 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2852 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2853 node->flags |= parent->flags & LYS_STATUS_MASK;
2854 } else {
2855 node->flags |= LYS_STATUS_CURR;
2856 }
2857 } else if (parent) {
2858 /* check status compatibility with the parent */
2859 if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
2860 if (node->flags & LYS_STATUS_CURR) {
2861 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2862 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2863 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2864 } else { /* LYS_STATUS_DEPRC */
2865 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2866 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2867 }
2868 goto error;
2869 }
2870 }
2871
2872 if (!(options & LYSC_OPT_FREE_SP)) {
2873 node->sp = node_p;
2874 }
2875 DUP_STRING(ctx->ctx, node_p->name, node->name);
2876 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
2877
2878 /* nodetype-specific part */
2879 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
2880
2881 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01002882 if (parent) {
2883 if (parent->nodetype == LYS_CHOICE) {
2884 /* TODO exception for cases */
2885 } else if ((children = lysc_node_children(parent))) {
2886 if (!(*children)) {
2887 /* first child */
2888 *children = node;
2889 } else {
2890 /* insert at the end of the parent's children list */
2891 (*children)->prev->next = node;
2892 node->prev = (*children)->prev;
2893 (*children)->prev = node;
2894 }
Radek Krejci19a96102018-11-15 13:38:09 +01002895 }
2896 } else {
2897 /* top-level element */
2898 if (!ctx->mod->compiled->data) {
2899 ctx->mod->compiled->data = node;
2900 } else {
2901 /* insert at the end of the module's top-level nodes list */
2902 ctx->mod->compiled->data->prev->next = node;
2903 node->prev = ctx->mod->compiled->data->prev;
2904 ctx->mod->compiled->data->prev = node;
2905 }
2906 }
2907
2908 return LY_SUCCESS;
2909
2910error:
2911 lysc_node_free(ctx->ctx, node);
2912 return ret;
2913}
2914
Radek Krejcia3045382018-11-22 14:30:31 +01002915/**
2916 * @brief Compile the given YANG module.
2917 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
2918 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2919 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2920 */
Radek Krejci19a96102018-11-15 13:38:09 +01002921LY_ERR
2922lys_compile(struct lys_module *mod, int options)
2923{
2924 struct lysc_ctx ctx = {0};
2925 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002926 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01002927 struct lysp_module *sp;
2928 struct lysp_node *node_p;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002929 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01002930 LY_ERR ret;
2931
2932 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
2933 sp = mod->parsed;
2934
2935 if (sp->submodule) {
2936 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
2937 return LY_EINVAL;
2938 }
2939
2940 ctx.ctx = sp->ctx;
2941 ctx.mod = mod;
2942
2943 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
2944 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
2945 mod_c->ctx = sp->ctx;
2946 mod_c->implemented = sp->implemented;
2947 mod_c->latest_revision = sp->latest_revision;
2948 mod_c->version = sp->version;
2949
2950 DUP_STRING(sp->ctx, sp->name, mod_c->name);
2951 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
2952 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
2953 if (sp->revs) {
2954 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
2955 }
2956 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
2957 COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
2958 COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
2959 if (sp->identities) {
2960 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
2961 }
2962
2963 LY_LIST_FOR(sp->data, node_p) {
2964 ret = lys_compile_node(&ctx, node_p, options, NULL);
2965 LY_CHECK_GOTO(ret, error);
2966 }
2967 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
2968 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
2969
2970 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
2971
Radek Krejcia3045382018-11-22 14:30:31 +01002972 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002973 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
2974 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
2975 * 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 +01002976 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01002977 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002978 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2979 if (type->basetype == LY_TYPE_LEAFREF) {
2980 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002981 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 +01002982 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002983 } else if (type->basetype == LY_TYPE_UNION) {
2984 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
2985 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
2986 /* validate the path */
2987 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
2988 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
2989 LY_CHECK_GOTO(ret, error);
2990 }
2991 }
Radek Krejcia3045382018-11-22 14:30:31 +01002992 }
2993 }
2994 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01002995 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01002996 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002997 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2998 if (type->basetype == LY_TYPE_LEAFREF) {
2999 /* store pointer to the real type */
3000 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
3001 typeiter->basetype == LY_TYPE_LEAFREF;
3002 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3003 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003004 } else if (type->basetype == LY_TYPE_UNION) {
3005 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3006 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3007 /* store pointer to the real type */
3008 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
3009 typeiter->basetype == LY_TYPE_LEAFREF;
3010 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3011 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
3012 }
3013 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003014 }
3015 }
3016 }
Radek Krejcia3045382018-11-22 14:30:31 +01003017 ly_set_erase(&ctx.unres, NULL);
3018
Radek Krejci19a96102018-11-15 13:38:09 +01003019 if (options & LYSC_OPT_FREE_SP) {
3020 lysp_module_free(mod->parsed);
3021 ((struct lys_module*)mod)->parsed = NULL;
3022 }
3023
3024 ((struct lys_module*)mod)->compiled = mod_c;
3025 return LY_SUCCESS;
3026
3027error:
Radek Krejcia3045382018-11-22 14:30:31 +01003028 ly_set_erase(&ctx.unres, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01003029 lysc_module_free(mod_c, NULL);
3030 ((struct lys_module*)mod)->compiled = NULL;
3031 return ret;
3032}