blob: 5fc25e1681ce22a41d8c1309676b5acf56d57f3d [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <ctype.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <stdio.h>
Radek Krejci19a96102018-11-15 13:38:09 +010019
20#include "libyang.h"
21#include "context.h"
22#include "tree_schema_internal.h"
23#include "xpath.h"
24
25/**
26 * @brief Duplicate string into dictionary
27 * @param[in] CTX libyang context of the dictionary.
28 * @param[in] ORIG String to duplicate.
29 * @param[out] DUP Where to store the result.
30 */
31#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
32
33#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
34 if (ARRAY_P) { \
35 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
36 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
37 LY_ARRAY_INCREMENT(ARRAY_C); \
38 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER]); \
39 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
40 } \
41 }
42
43#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
44 if (MEMBER_P) { \
45 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
46 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
47 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
48 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
49 }
50
Radek Krejci19a96102018-11-15 13:38:09 +010051static struct lysc_ext_instance *
52lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
53{
54 /* TODO */
55 (void) ctx;
56 (void) orig;
57 return NULL;
58}
59
60static struct lysc_pattern*
61lysc_pattern_dup(struct lysc_pattern *orig)
62{
63 ++orig->refcount;
64 return orig;
65}
66
67static struct lysc_pattern**
68lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
69{
70 struct lysc_pattern **dup;
71 unsigned int u;
72
73 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
74 LY_ARRAY_FOR(orig, u) {
75 dup[u] = lysc_pattern_dup(orig[u]);
76 LY_ARRAY_INCREMENT(dup);
77 }
78 return dup;
79}
80
81struct lysc_range*
82lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
83{
84 struct lysc_range *dup;
85 LY_ERR ret;
86
87 dup = calloc(1, sizeof *dup);
88 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
89 if (orig->parts) {
90 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
91 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
92 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
93 }
94 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
95 DUP_STRING(ctx, orig->emsg, dup->emsg);
96 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
97
98 return dup;
99cleanup:
100 free(dup);
101 (void) ret; /* set but not used due to the return type */
102 return NULL;
103}
104
105struct iff_stack {
106 int size;
107 int index; /* first empty item */
108 uint8_t *stack;
109};
110
111static LY_ERR
112iff_stack_push(struct iff_stack *stack, uint8_t value)
113{
114 if (stack->index == stack->size) {
115 stack->size += 4;
116 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
117 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
118 }
119 stack->stack[stack->index++] = value;
120 return LY_SUCCESS;
121}
122
123static uint8_t
124iff_stack_pop(struct iff_stack *stack)
125{
126 stack->index--;
127 return stack->stack[stack->index];
128}
129
130static void
131iff_stack_clean(struct iff_stack *stack)
132{
133 stack->size = 0;
134 free(stack->stack);
135}
136
137static void
138iff_setop(uint8_t *list, uint8_t op, int pos)
139{
140 uint8_t *item;
141 uint8_t mask = 3;
142
143 assert(pos >= 0);
144 assert(op <= 3); /* max 2 bits */
145
146 item = &list[pos / 4];
147 mask = mask << 2 * (pos % 4);
148 *item = (*item) & ~mask;
149 *item = (*item) | (op << 2 * (pos % 4));
150}
151
152#define LYS_IFF_LP 0x04 /* ( */
153#define LYS_IFF_RP 0x08 /* ) */
154
155static struct lysc_feature *
156lysc_feature_find(struct lysc_module *mod, const char *name, size_t len)
157{
158 size_t i;
159 struct lysc_feature *f;
160
161 for (i = 0; i < len; ++i) {
162 if (name[i] == ':') {
163 /* we have a prefixed feature */
164 mod = lysc_module_find_prefix(mod, name, i);
165 LY_CHECK_RET(!mod, NULL);
166
167 name = &name[i + 1];
168 len = len - i - 1;
169 }
170 }
171
172 /* we have the correct module, get the feature */
173 LY_ARRAY_FOR(mod->features, i) {
174 f = &mod->features[i];
175 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
176 return f;
177 }
178 }
179
180 return NULL;
181}
182
183static LY_ERR
184lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
185{
186 const char *name;
187 unsigned int u;
188 const struct lys_module *mod;
189 struct lysp_ext *edef = NULL;
190
191 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
192 ext->insubstmt = ext_p->insubstmt;
193 ext->insubstmt_index = ext_p->insubstmt_index;
194
195 /* get module where the extension definition should be placed */
196 for (u = 0; ext_p->name[u] != ':'; ++u);
197 mod = lys_module_find_prefix(ctx->mod, ext_p->name, u);
198 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
199 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
200 LY_EVALID);
201 LY_CHECK_ERR_RET(!mod->parsed->extensions,
202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
203 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
204 ext_p->name, mod->parsed->name),
205 LY_EVALID);
206 name = &ext_p->name[u + 1];
207 /* find the extension definition there */
208 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
209 if (!strcmp(name, mod->parsed->extensions[u].name)) {
210 edef = &mod->parsed->extensions[u];
211 break;
212 }
213 }
214 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
215 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
216 LY_EVALID);
217 /* TODO plugins */
218
219 return LY_SUCCESS;
220}
221
222static LY_ERR
223lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
224{
225 const char *c = *value;
226 int r, rc = EXIT_FAILURE;
227 int i, j, last_not, checkversion = 0;
228 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
229 uint8_t op;
230 struct iff_stack stack = {0, 0, NULL};
231 struct lysc_feature *f;
232
233 assert(c);
234
235 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
236 for (i = j = last_not = 0; c[i]; i++) {
237 if (c[i] == '(') {
238 j++;
239 checkversion = 1;
240 continue;
241 } else if (c[i] == ')') {
242 j--;
243 continue;
244 } else if (isspace(c[i])) {
245 checkversion = 1;
246 continue;
247 }
248
249 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
250 if (c[i + r] == '\0') {
251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
252 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
253 return LY_EVALID;
254 } else if (!isspace(c[i + r])) {
255 /* feature name starting with the not/and/or */
256 last_not = 0;
257 f_size++;
258 } else if (c[i] == 'n') { /* not operation */
259 if (last_not) {
260 /* double not */
261 expr_size = expr_size - 2;
262 last_not = 0;
263 } else {
264 last_not = 1;
265 }
266 } else { /* and, or */
267 f_exp++;
268 /* not a not operation */
269 last_not = 0;
270 }
271 i += r;
272 } else {
273 f_size++;
274 last_not = 0;
275 }
276 expr_size++;
277
278 while (!isspace(c[i])) {
279 if (!c[i] || c[i] == ')') {
280 i--;
281 break;
282 }
283 i++;
284 }
285 }
286 if (j || f_exp != f_size) {
287 /* not matching count of ( and ) */
288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
289 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
290 return LY_EVALID;
291 }
292
293 if (checkversion || expr_size > 1) {
294 /* check that we have 1.1 module */
295 if (ctx->mod->compiled->version != LYS_VERSION_1_1) {
296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
297 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
298 return LY_EVALID;
299 }
300 }
301
302 /* allocate the memory */
303 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
304 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
305 stack.stack = malloc(expr_size * sizeof *stack.stack);
306 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
307
308 stack.size = expr_size;
309 f_size--; expr_size--; /* used as indexes from now */
310
311 for (i--; i >= 0; i--) {
312 if (c[i] == ')') {
313 /* push it on stack */
314 iff_stack_push(&stack, LYS_IFF_RP);
315 continue;
316 } else if (c[i] == '(') {
317 /* pop from the stack into result all operators until ) */
318 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
319 iff_setop(iff->expr, op, expr_size--);
320 }
321 continue;
322 } else if (isspace(c[i])) {
323 continue;
324 }
325
326 /* end of operator or operand -> find beginning and get what is it */
327 j = i + 1;
328 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
329 i--;
330 }
331 i++; /* go back by one step */
332
333 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
334 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
335 /* double not */
336 iff_stack_pop(&stack);
337 } else {
338 /* not has the highest priority, so do not pop from the stack
339 * as in case of AND and OR */
340 iff_stack_push(&stack, LYS_IFF_NOT);
341 }
342 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
343 /* as for OR - pop from the stack all operators with the same or higher
344 * priority and store them to the result, then push the AND to the stack */
345 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
346 op = iff_stack_pop(&stack);
347 iff_setop(iff->expr, op, expr_size--);
348 }
349 iff_stack_push(&stack, LYS_IFF_AND);
350 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
351 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
352 op = iff_stack_pop(&stack);
353 iff_setop(iff->expr, op, expr_size--);
354 }
355 iff_stack_push(&stack, LYS_IFF_OR);
356 } else {
357 /* feature name, length is j - i */
358
359 /* add it to the expression */
360 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
361
362 /* now get the link to the feature definition */
363 f = lysc_feature_find(ctx->mod->compiled, &c[i], j - i);
364 LY_CHECK_ERR_GOTO(!f,
365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
366 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
367 rc = LY_EVALID,
368 error)
369 iff->features[f_size] = f;
370 LY_ARRAY_INCREMENT(iff->features);
371 f_size--;
372 }
373 }
374 while (stack.index) {
375 op = iff_stack_pop(&stack);
376 iff_setop(iff->expr, op, expr_size--);
377 }
378
379 if (++expr_size || ++f_size) {
380 /* not all expected operators and operands found */
381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
382 "Invalid value \"%s\" of if-feature - processing error.", *value);
383 rc = LY_EINT;
384 } else {
385 rc = LY_SUCCESS;
386 }
387
388error:
389 /* cleanup */
390 iff_stack_clean(&stack);
391
392 return rc;
393}
394
395static LY_ERR
396lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when)
397{
398 unsigned int u;
399 LY_ERR ret = LY_SUCCESS;
400
401 when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
402 LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done);
403 COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done);
404
405done:
406 return ret;
407}
408
409static LY_ERR
410lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
411{
412 unsigned int u;
413 LY_ERR ret = LY_SUCCESS;
414
415 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
416 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
417
418 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
419 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
420 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
421
422done:
423 return ret;
424}
425
426static LY_ERR
427lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
428{
429 unsigned int u;
430 struct lys_module *mod = NULL;
431 struct lysc_module *comp;
432 LY_ERR ret = LY_SUCCESS;
433
434 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
435 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
436 imp->module = imp_p->module;
437
438 /* make sure that we have both versions (lysp_ and lysc_) of the imported module. To import groupings or
439 * typedefs, the lysp_ is needed. To augment or deviate imported module, we need the lysc_ structure */
440 if (!imp->module->parsed) {
441 comp = imp->module->compiled;
442 /* try to get filepath from the compiled version */
443 if (comp->filepath) {
444 mod = (struct lys_module*)lys_parse_path(ctx->ctx, comp->filepath,
445 !strcmp(&comp->filepath[strlen(comp->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
446 if (mod != imp->module) {
447 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
448 comp->filepath, comp->name);
449 mod = NULL;
450 }
451 }
452 if (!mod) {
453 if (lysp_load_module(ctx->ctx, comp->name, comp->revision, 0, 1, &mod)) {
454 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
455 comp->name, ctx->mod->compiled->name);
456 return LY_ENOTFOUND;
457 }
458 }
459 } else if (!imp->module->compiled) {
460 return lys_compile(imp->module, options);
461 }
462
463done:
464 return ret;
465}
466
467static LY_ERR
468lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *ident)
469{
470 unsigned int u;
471 LY_ERR ret = LY_SUCCESS;
472
473 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
474 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
475 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
476 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
477 ident->flags = ident_p->flags;
478
479done:
480 return ret;
481}
482
Radek Krejcia3045382018-11-22 14:30:31 +0100483/**
484 * @brief Find and process the referenced base identities from another identity or identityref
485 *
486 * For bases in identity se backlinks to them from the base identities. For identityref, store
487 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
488 * to distinguish these two use cases.
489 *
490 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
491 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
492 * @param[in] ident Referencing identity to work with.
493 * @param[in] bases Array of bases of identityref to fill in.
494 * @return LY_ERR value.
495 */
Radek Krejci19a96102018-11-15 13:38:09 +0100496static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100497lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +0100498{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100499 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100500 const char *s, *name;
501 struct lysc_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100502 struct lysc_ident **idref;
503
504 assert(ident || bases);
505
506 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) {
507 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
508 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
509 return LY_EVALID;
510 }
511
512 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
513 s = strchr(bases_p[u], ':');
514 if (s) {
515 /* prefixed identity */
516 name = &s[1];
517 mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]);
518 } else {
519 name = bases_p[u];
520 mod = ctx->mod->compiled;
521 }
522 if (!mod) {
523 if (ident) {
524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
525 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
526 } else {
527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
528 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
529 }
530 return LY_EVALID;
531 }
532 idref = NULL;
533 if (mod->identities) {
534 for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) {
535 if (!strcmp(name, mod->identities[v].name)) {
536 if (ident) {
537 /* we have match! store the backlink */
538 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
539 *idref = ident;
540 } else {
541 /* we have match! store the found identity */
542 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
543 *idref = &mod->identities[v];
544 }
545 break;
546 }
547 }
548 }
549 if (!idref || !(*idref)) {
550 if (ident) {
551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
552 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
553 } else {
554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
555 "Unable to find base (%s) of identityref.", bases_p[u]);
556 }
557 return LY_EVALID;
558 }
559 }
560 return LY_SUCCESS;
561}
562
Radek Krejcia3045382018-11-22 14:30:31 +0100563/**
564 * @brief For the given array of identities, set the backlinks from all their base identities.
565 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
566 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
567 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
568 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
569 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100570static LY_ERR
571lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
572{
573 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100574
575 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
576 if (!idents_p[i].bases) {
577 continue;
578 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100579 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100580 }
581 return LY_SUCCESS;
582}
583
Radek Krejcia3045382018-11-22 14:30:31 +0100584/**
585 * @brief Create compiled feature structure.
586 * @param[in] ctx Compile context.
587 * @param[in] feature_p Parsed feature definition to compile.
588 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
589 * @param[in,out] feature Compiled feature structure to fill.
590 * @return LY_ERR value.
591 */
Radek Krejci19a96102018-11-15 13:38:09 +0100592static LY_ERR
593lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature)
594{
595 unsigned int u, v;
596 LY_ERR ret = LY_SUCCESS;
597 struct lysc_feature **df;
598
599 DUP_STRING(ctx->ctx, feature_p->name, feature->name);
600 feature->flags = feature_p->flags;
601
602 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
603 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
604 if (feature->iffeatures) {
605 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
606 if (feature->iffeatures[u].features) {
607 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
608 /* add itself into the dependants list */
609 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
610 *df = feature;
611 }
612 /* TODO check for circular dependency */
613 }
614 }
615 }
616done:
617 return ret;
618}
619
Radek Krejcia3045382018-11-22 14:30:31 +0100620/**
621 * @brief Validate and normalize numeric value from a range definition.
622 * @param[in] ctx Compile context.
623 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
624 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
625 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
626 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
627 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
628 * @param[in] value String value of the range boundary.
629 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
630 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
631 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
632 */
Radek Krejci19a96102018-11-15 13:38:09 +0100633static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100634range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +0100635{
Radek Krejci6cba4292018-11-15 17:33:29 +0100636 size_t fraction = 0, size;
637
Radek Krejci19a96102018-11-15 13:38:09 +0100638 *len = 0;
639
640 assert(value);
641 /* parse value */
642 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
643 return LY_EVALID;
644 }
645
646 if ((value[*len] == '-') || (value[*len] == '+')) {
647 ++(*len);
648 }
649
650 while (isdigit(value[*len])) {
651 ++(*len);
652 }
653
654 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100655 if (basetype == LY_TYPE_DEC64) {
656 goto decimal;
657 } else {
658 *valcopy = strndup(value, *len);
659 return LY_SUCCESS;
660 }
Radek Krejci19a96102018-11-15 13:38:09 +0100661 }
662 fraction = *len;
663
664 ++(*len);
665 while (isdigit(value[*len])) {
666 ++(*len);
667 }
668
Radek Krejci6cba4292018-11-15 17:33:29 +0100669 if (basetype == LY_TYPE_DEC64) {
670decimal:
671 assert(frdigits);
672 if (*len - 1 - fraction > frdigits) {
673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
674 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
675 *len, value, frdigits);
676 return LY_EINVAL;
677 }
678 if (fraction) {
679 size = (*len) + (frdigits - ((*len) - 1 - fraction));
680 } else {
681 size = (*len) + frdigits + 1;
682 }
683 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100684 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
685
Radek Krejci6cba4292018-11-15 17:33:29 +0100686 (*valcopy)[size - 1] = '\0';
687 if (fraction) {
688 memcpy(&(*valcopy)[0], &value[0], fraction);
689 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
690 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
691 } else {
692 memcpy(&(*valcopy)[0], &value[0], *len);
693 memset(&(*valcopy)[*len], '0', frdigits);
694 }
Radek Krejci19a96102018-11-15 13:38:09 +0100695 }
696 return LY_SUCCESS;
697}
698
Radek Krejcia3045382018-11-22 14:30:31 +0100699/**
700 * @brief Check that values in range are in ascendant order.
701 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +0100702 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
703 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +0100704 * @param[in] value Current value to check.
705 * @param[in] prev_value The last seen value.
706 * @return LY_SUCCESS or LY_EEXIST for invalid order.
707 */
Radek Krejci19a96102018-11-15 13:38:09 +0100708static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +0100709range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +0100710{
711 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +0100712 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100713 return LY_EEXIST;
714 }
715 } else {
Radek Krejci5969f272018-11-23 10:03:58 +0100716 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100717 return LY_EEXIST;
718 }
719 }
720 return LY_SUCCESS;
721}
722
Radek Krejcia3045382018-11-22 14:30:31 +0100723/**
724 * @brief Set min/max value of the range part.
725 * @param[in] ctx Compile context.
726 * @param[in] part Range part structure to fill.
727 * @param[in] max Flag to distinguish if storing min or max value.
728 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
729 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
730 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
731 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
732 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +0100733 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +0100734 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
735 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
736 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
737 * frdigits value), LY_EMEM.
738 */
Radek Krejci19a96102018-11-15 13:38:09 +0100739static LY_ERR
740range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +0100741 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100742{
743 LY_ERR ret = LY_SUCCESS;
744 char *valcopy = NULL;
745 size_t len;
746
747 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100748 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +0100749 LY_CHECK_GOTO(ret, finalize);
750 }
751 if (!valcopy && base_range) {
752 if (max) {
753 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
754 } else {
755 part->min_64 = base_range->parts[0].min_64;
756 }
757 if (!first) {
758 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
759 }
760 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +0100761 }
762
763 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100764 case LY_TYPE_INT8: /* range */
765 if (valcopy) {
766 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
767 } else if (max) {
768 part->max_64 = INT64_C(127);
769 } else {
770 part->min_64 = INT64_C(-128);
771 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100772 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100773 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100774 }
775 break;
776 case LY_TYPE_INT16: /* range */
777 if (valcopy) {
778 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
779 } else if (max) {
780 part->max_64 = INT64_C(32767);
781 } else {
782 part->min_64 = INT64_C(-32768);
783 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100784 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100785 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100786 }
787 break;
788 case LY_TYPE_INT32: /* range */
789 if (valcopy) {
790 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
791 } else if (max) {
792 part->max_64 = INT64_C(2147483647);
793 } else {
794 part->min_64 = INT64_C(-2147483648);
795 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100796 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100797 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100798 }
799 break;
800 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +0100801 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100802 if (valcopy) {
803 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
804 max ? &part->max_64 : &part->min_64);
805 } else if (max) {
806 part->max_64 = INT64_C(9223372036854775807);
807 } else {
808 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
809 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100810 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100811 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100812 }
813 break;
814 case LY_TYPE_UINT8: /* range */
815 if (valcopy) {
816 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
817 } else if (max) {
818 part->max_u64 = UINT64_C(255);
819 } else {
820 part->min_u64 = UINT64_C(0);
821 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100822 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100823 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100824 }
825 break;
826 case LY_TYPE_UINT16: /* range */
827 if (valcopy) {
828 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
829 } else if (max) {
830 part->max_u64 = UINT64_C(65535);
831 } else {
832 part->min_u64 = UINT64_C(0);
833 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100834 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100835 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100836 }
837 break;
838 case LY_TYPE_UINT32: /* range */
839 if (valcopy) {
840 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
841 } else if (max) {
842 part->max_u64 = UINT64_C(4294967295);
843 } else {
844 part->min_u64 = UINT64_C(0);
845 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100846 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100847 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100848 }
849 break;
850 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100851 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +0100852 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +0100853 if (valcopy) {
854 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
855 } else if (max) {
856 part->max_u64 = UINT64_C(18446744073709551615);
857 } else {
858 part->min_u64 = UINT64_C(0);
859 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100860 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100861 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100862 }
863 break;
864 default:
865 LOGINT(ctx->ctx);
866 ret = LY_EINT;
867 }
868
Radek Krejci5969f272018-11-23 10:03:58 +0100869finalize:
Radek Krejci19a96102018-11-15 13:38:09 +0100870 if (ret == LY_EDENIED) {
871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
872 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
873 length_restr ? "length" : "range", valcopy ? valcopy : *value);
874 } else if (ret == LY_EVALID) {
875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
876 "Invalid %s restriction - invalid value \"%s\".",
877 length_restr ? "length" : "range", valcopy ? valcopy : *value);
878 } else if (ret == LY_EEXIST) {
879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
880 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100881 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +0100882 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +0100883 } else if (!ret && value) {
884 *value = *value + len;
885 }
886 free(valcopy);
887 return ret;
888}
889
Radek Krejcia3045382018-11-22 14:30:31 +0100890/**
891 * @brief Compile the parsed range restriction.
892 * @param[in] ctx Compile context.
893 * @param[in] range_p Parsed range structure to compile.
894 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
895 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
896 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
897 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
898 * range restriction must be more restrictive than the base_range.
899 * @param[in,out] range Pointer to the created current range structure.
900 * @return LY_ERR value.
901 */
Radek Krejci19a96102018-11-15 13:38:09 +0100902static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100903lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +0100904 struct lysc_range *base_range, struct lysc_range **range)
905{
906 LY_ERR ret = LY_EVALID;
907 const char *expr;
908 struct lysc_range_part *parts = NULL, *part;
909 int range_expected = 0, uns;
910 unsigned int parts_done = 0, u, v;
911
912 assert(range);
913 assert(range_p);
914
915 expr = range_p->arg;
916 while(1) {
917 if (isspace(*expr)) {
918 ++expr;
919 } else if (*expr == '\0') {
920 if (range_expected) {
921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
922 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
923 length_restr ? "length" : "range", range_p->arg);
924 goto cleanup;
925 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
927 "Invalid %s restriction - unexpected end of the expression (%s).",
928 length_restr ? "length" : "range", range_p->arg);
929 goto cleanup;
930 }
931 parts_done++;
932 break;
933 } else if (!strncmp(expr, "min", 3)) {
934 if (parts) {
935 /* min cannot be used elsewhere than in the first part */
936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
937 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
938 expr - range_p->arg, range_p->arg);
939 goto cleanup;
940 }
941 expr += 3;
942
943 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +0100944 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100945 part->max_64 = part->min_64;
946 } else if (*expr == '|') {
947 if (!parts || range_expected) {
948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
949 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
950 goto cleanup;
951 }
952 expr++;
953 parts_done++;
954 /* process next part of the expression */
955 } else if (!strncmp(expr, "..", 2)) {
956 expr += 2;
957 while (isspace(*expr)) {
958 expr++;
959 }
960 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
961 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
962 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
963 goto cleanup;
964 }
965 /* continue expecting the upper boundary */
966 range_expected = 1;
967 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
968 /* number */
969 if (range_expected) {
970 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100971 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100972 range_expected = 0;
973 } else {
974 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
975 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +0100976 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100977 part->max_64 = part->min_64;
978 }
979
980 /* continue with possible another expression part */
981 } else if (!strncmp(expr, "max", 3)) {
982 expr += 3;
983 while (isspace(*expr)) {
984 expr++;
985 }
986 if (*expr != '\0') {
987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
988 length_restr ? "length" : "range", expr);
989 goto cleanup;
990 }
991 if (range_expected) {
992 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100993 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100994 range_expected = 0;
995 } else {
996 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
997 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +0100998 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100999 part->min_64 = part->max_64;
1000 }
1001 } else {
1002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1003 length_restr ? "length" : "range", expr);
1004 goto cleanup;
1005 }
1006 }
1007
1008 /* check with the previous range/length restriction */
1009 if (base_range) {
1010 switch (basetype) {
1011 case LY_TYPE_BINARY:
1012 case LY_TYPE_UINT8:
1013 case LY_TYPE_UINT16:
1014 case LY_TYPE_UINT32:
1015 case LY_TYPE_UINT64:
1016 case LY_TYPE_STRING:
1017 uns = 1;
1018 break;
1019 case LY_TYPE_DEC64:
1020 case LY_TYPE_INT8:
1021 case LY_TYPE_INT16:
1022 case LY_TYPE_INT32:
1023 case LY_TYPE_INT64:
1024 uns = 0;
1025 break;
1026 default:
1027 LOGINT(ctx->ctx);
1028 ret = LY_EINT;
1029 goto cleanup;
1030 }
1031 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1032 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1033 goto baseerror;
1034 }
1035 /* current lower bound is not lower than the base */
1036 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1037 /* base has single value */
1038 if (base_range->parts[v].min_64 == parts[u].min_64) {
1039 /* both lower bounds are the same */
1040 if (parts[u].min_64 != parts[u].max_64) {
1041 /* current continues with a range */
1042 goto baseerror;
1043 } else {
1044 /* equal single values, move both forward */
1045 ++v;
1046 continue;
1047 }
1048 } else {
1049 /* base is single value lower than current range, so the
1050 * value from base range is removed in the current,
1051 * move only base and repeat checking */
1052 ++v;
1053 --u;
1054 continue;
1055 }
1056 } else {
1057 /* base is the range */
1058 if (parts[u].min_64 == parts[u].max_64) {
1059 /* current is a single value */
1060 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1061 /* current is behind the base range, so base range is omitted,
1062 * move the base and keep the current for further check */
1063 ++v;
1064 --u;
1065 } /* else it is within the base range, so move the current, but keep the base */
1066 continue;
1067 } else {
1068 /* both are ranges - check the higher bound, the lower was already checked */
1069 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1070 /* higher bound is higher than the current higher bound */
1071 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1072 /* but the current lower bound is also higher, so the base range is omitted,
1073 * continue with the same current, but move the base */
1074 --u;
1075 ++v;
1076 continue;
1077 }
1078 /* current range starts within the base range but end behind it */
1079 goto baseerror;
1080 } else {
1081 /* current range is smaller than the base,
1082 * move current, but stay with the base */
1083 continue;
1084 }
1085 }
1086 }
1087 }
1088 if (u != parts_done) {
1089baseerror:
1090 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1091 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1092 length_restr ? "length" : "range", range_p->arg);
1093 goto cleanup;
1094 }
1095 }
1096
1097 if (!(*range)) {
1098 *range = calloc(1, sizeof **range);
1099 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1100 }
1101
1102 if (range_p->eapptag) {
1103 lydict_remove(ctx->ctx, (*range)->eapptag);
1104 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1105 }
1106 if (range_p->emsg) {
1107 lydict_remove(ctx->ctx, (*range)->emsg);
1108 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1109 }
1110 /* extensions are taken only from the last range by the caller */
1111
1112 (*range)->parts = parts;
1113 parts = NULL;
1114 ret = LY_SUCCESS;
1115cleanup:
1116 /* TODO clean up */
1117 LY_ARRAY_FREE(parts);
1118
1119 return ret;
1120}
1121
1122/**
1123 * @brief Checks pattern syntax.
1124 *
Radek Krejcia3045382018-11-22 14:30:31 +01001125 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001126 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001127 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1128 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001129 */
1130static LY_ERR
1131lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1132{
1133 int idx, idx2, start, end, err_offset, count;
1134 char *perl_regex, *ptr;
1135 const char *err_msg, *orig_ptr;
1136 pcre *precomp;
1137#define URANGE_LEN 19
1138 char *ublock2urange[][2] = {
1139 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1140 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1141 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1142 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1143 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1144 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1145 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1146 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1147 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1148 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1149 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1150 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1151 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1152 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1153 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1154 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1155 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1156 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1157 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1158 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1159 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1160 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1161 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1162 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1163 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1164 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1165 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1166 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1167 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1168 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1169 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1170 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1171 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1172 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1173 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1174 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1175 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1176 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1177 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1178 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1179 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1180 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1181 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1182 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1183 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1184 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1185 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1186 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1187 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1188 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1189 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1190 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1191 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1192 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1193 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1194 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1195 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1196 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1197 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1198 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1199 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1200 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1201 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1202 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1203 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1204 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1205 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1206 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1207 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1208 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1209 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1210 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1211 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1212 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1213 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1214 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1215 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1216 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1217 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1218 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1219 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1220 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1221 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1222 {NULL, NULL}
1223 };
1224
1225 /* adjust the expression to a Perl equivalent
1226 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1227
1228 /* we need to replace all "$" with "\$", count them now */
1229 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1230
1231 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1232 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1233 perl_regex[0] = '\0';
1234
1235 ptr = perl_regex;
1236
1237 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1238 /* we will add line-end anchoring */
1239 ptr[0] = '(';
1240 ++ptr;
1241 }
1242
1243 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1244 if (orig_ptr[0] == '$') {
1245 ptr += sprintf(ptr, "\\$");
1246 } else if (orig_ptr[0] == '^') {
1247 ptr += sprintf(ptr, "\\^");
1248 } else {
1249 ptr[0] = orig_ptr[0];
1250 ++ptr;
1251 }
1252 }
1253
1254 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1255 ptr += sprintf(ptr, ")$");
1256 } else {
1257 ptr[0] = '\0';
1258 ++ptr;
1259 }
1260
1261 /* substitute Unicode Character Blocks with exact Character Ranges */
1262 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1263 start = ptr - perl_regex;
1264
1265 ptr = strchr(ptr, '}');
1266 if (!ptr) {
1267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1268 pattern, perl_regex + start + 2, "unterminated character property");
1269 free(perl_regex);
1270 return LY_EVALID;
1271 }
1272 end = (ptr - perl_regex) + 1;
1273
1274 /* need more space */
1275 if (end - start < URANGE_LEN) {
1276 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1277 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1278 }
1279
1280 /* find our range */
1281 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1282 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1283 break;
1284 }
1285 }
1286 if (!ublock2urange[idx][0]) {
1287 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1288 pattern, perl_regex + start + 5, "unknown block name");
1289 free(perl_regex);
1290 return LY_EVALID;
1291 }
1292
1293 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1294 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1295 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1296 ++count;
1297 }
1298 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1299 --count;
1300 }
1301 }
1302 if (count) {
1303 /* skip brackets */
1304 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1305 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1306 } else {
1307 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1308 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1309 }
1310 }
1311
1312 /* must return 0, already checked during parsing */
1313 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1314 &err_msg, &err_offset, NULL);
1315 if (!precomp) {
1316 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1317 free(perl_regex);
1318 return LY_EVALID;
1319 }
1320 free(perl_regex);
1321
1322 if (pcre_precomp) {
1323 *pcre_precomp = precomp;
1324 } else {
1325 free(precomp);
1326 }
1327
1328 return LY_SUCCESS;
1329
1330#undef URANGE_LEN
1331}
1332
Radek Krejcia3045382018-11-22 14:30:31 +01001333/**
1334 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1335 * @param[in] ctx Compile context.
1336 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1337 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1338 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1339 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1340 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1341 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1342 */
Radek Krejci19a96102018-11-15 13:38:09 +01001343static LY_ERR
1344lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1345 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1346{
1347 struct lysc_pattern **pattern;
1348 unsigned int u, v;
1349 const char *err_msg;
1350 LY_ERR ret = LY_SUCCESS;
1351
1352 /* first, copy the patterns from the base type */
1353 if (base_patterns) {
1354 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1355 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1356 }
1357
1358 LY_ARRAY_FOR(patterns_p, u) {
1359 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1360 *pattern = calloc(1, sizeof **pattern);
1361 ++(*pattern)->refcount;
1362
1363 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1364 LY_CHECK_RET(ret);
1365 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1366 if (err_msg) {
1367 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1368 }
1369
1370 if (patterns_p[u].arg[0] == 0x15) {
1371 (*pattern)->inverted = 1;
1372 }
1373 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1374 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
1375 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1376 options, v, lys_compile_ext, ret, done);
1377 }
1378done:
1379 return ret;
1380}
1381
Radek Krejcia3045382018-11-22 14:30:31 +01001382/**
1383 * @brief map of the possible restrictions combination for the specific built-in type.
1384 */
Radek Krejci19a96102018-11-15 13:38:09 +01001385static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1386 0 /* LY_TYPE_UNKNOWN */,
1387 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001388 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1389 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1390 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1391 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1392 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001393 LYS_SET_BIT /* LY_TYPE_BITS */,
1394 0 /* LY_TYPE_BOOL */,
1395 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1396 0 /* LY_TYPE_EMPTY */,
1397 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1398 LYS_SET_BASE /* LY_TYPE_IDENT */,
1399 LYS_SET_REQINST /* LY_TYPE_INST */,
1400 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001401 LYS_SET_TYPE /* LY_TYPE_UNION */,
1402 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001403 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001404 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001405 LYS_SET_RANGE /* LY_TYPE_INT64 */
1406};
1407
1408/**
1409 * @brief stringification of the YANG built-in data types
1410 */
1411const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1412 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1413 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001414};
1415
Radek Krejcia3045382018-11-22 14:30:31 +01001416/**
1417 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1418 * @param[in] ctx Compile context.
1419 * @param[in] enums_p Array of the parsed enum structures to compile.
1420 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1421 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1422 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1423 * @param[out] enums Newly created array of the compiled enums information for the current type.
1424 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1425 */
Radek Krejci19a96102018-11-15 13:38:09 +01001426static LY_ERR
1427lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1428 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1429{
1430 LY_ERR ret = LY_SUCCESS;
1431 unsigned int u, v, match;
1432 int32_t value = 0;
1433 uint32_t position = 0;
1434 struct lysc_type_enum_item *e, storage;
1435
1436 if (base_enums && ctx->mod->compiled->version < 2) {
1437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1438 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1439 return LY_EVALID;
1440 }
1441
1442 LY_ARRAY_FOR(enums_p, u) {
1443 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1444 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
1445 if (base_enums) {
1446 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1447 LY_ARRAY_FOR(base_enums, v) {
1448 if (!strcmp(e->name, base_enums[v].name)) {
1449 break;
1450 }
1451 }
1452 if (v == LY_ARRAY_SIZE(base_enums)) {
1453 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1454 "Invalid %s - derived type adds new item \"%s\".",
1455 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1456 return LY_EVALID;
1457 }
1458 match = v;
1459 }
1460
1461 if (basetype == LY_TYPE_ENUM) {
1462 if (enums_p[u].flags & LYS_SET_VALUE) {
1463 e->value = (int32_t)enums_p[u].value;
1464 if (!u || e->value >= value) {
1465 value = e->value + 1;
1466 }
1467 /* check collision with other values */
1468 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1469 if (e->value == (*enums)[v].value) {
1470 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1471 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1472 e->value, e->name, (*enums)[v].name);
1473 return LY_EVALID;
1474 }
1475 }
1476 } else if (base_enums) {
1477 /* inherit the assigned value */
1478 e->value = base_enums[match].value;
1479 if (!u || e->value >= value) {
1480 value = e->value + 1;
1481 }
1482 } else {
1483 /* assign value automatically */
1484 if (u && value == INT32_MIN) {
1485 /* counter overflow */
1486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1487 "Invalid enumeration - it is not possible to auto-assign enum value for "
1488 "\"%s\" since the highest value is already 2147483647.", e->name);
1489 return LY_EVALID;
1490 }
1491 e->value = value++;
1492 }
1493 } else { /* LY_TYPE_BITS */
1494 if (enums_p[u].flags & LYS_SET_VALUE) {
1495 e->value = (int32_t)enums_p[u].value;
1496 if (!u || (uint32_t)e->value >= position) {
1497 position = (uint32_t)e->value + 1;
1498 }
1499 /* check collision with other values */
1500 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1501 if (e->value == (*enums)[v].value) {
1502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1503 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1504 (uint32_t)e->value, e->name, (*enums)[v].name);
1505 return LY_EVALID;
1506 }
1507 }
1508 } else if (base_enums) {
1509 /* inherit the assigned value */
1510 e->value = base_enums[match].value;
1511 if (!u || (uint32_t)e->value >= position) {
1512 position = (uint32_t)e->value + 1;
1513 }
1514 } else {
1515 /* assign value automatically */
1516 if (u && position == 0) {
1517 /* counter overflow */
1518 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1519 "Invalid bits - it is not possible to auto-assign bit position for "
1520 "\"%s\" since the highest value is already 4294967295.", e->name);
1521 return LY_EVALID;
1522 }
1523 e->value = position++;
1524 }
1525 }
1526
1527 if (base_enums) {
1528 /* the assigned values must not change from the derived type */
1529 if (e->value != base_enums[match].value) {
1530 if (basetype == LY_TYPE_ENUM) {
1531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1532 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1533 e->name, base_enums[match].value, e->value);
1534 } else {
1535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1536 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1537 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1538 }
1539 return LY_EVALID;
1540 }
1541 }
1542
1543 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1544 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1545
1546 if (basetype == LY_TYPE_BITS) {
1547 /* keep bits ordered by position */
1548 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1549 if (v != u) {
1550 memcpy(&storage, e, sizeof *e);
1551 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1552 memcpy(&(*enums)[v], &storage, sizeof storage);
1553 }
1554 }
1555 }
1556
1557done:
1558 return ret;
1559}
1560
Radek Krejcia3045382018-11-22 14:30:31 +01001561#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1562 for ((NODE) = (NODE)->parent; \
1563 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1564 (NODE) = (NODE)->parent); \
1565 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1567 TERM; \
1568 }
1569
1570/**
1571 * @brief Validate the predicate(s) from the leafref path.
1572 * @param[in] ctx Compile context
1573 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1574 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
1575 * @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
1580lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate,
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 Krejci96a0bfd2018-11-22 15:25:06 +01001631 mod = path_context;
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;
1655 dst_node = context_node;
1656
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 Krejci96a0bfd2018-11-22 15:25:06 +01001729 mod = path_context;
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 Krejci412ddfa2018-11-23 11:44:11 +01001936 mod = leafref->path_context;
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 Krejci412ddfa2018-11-23 11:44:11 +01001962 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, 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/**
2689 * @brief Compile parsed schema node information.
2690 * @param[in] ctx Compile context
2691 * @param[in] node_p Parsed schema node.
2692 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2693 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
2694 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
2695 * the compile context.
2696 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2697 */
Radek Krejci19a96102018-11-15 13:38:09 +01002698static LY_ERR
2699lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
2700{
2701 LY_ERR ret = LY_EVALID;
2702 struct lysc_node *node, **children;
2703 unsigned int u;
2704 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
2705
2706 switch (node_p->nodetype) {
2707 case LYS_CONTAINER:
2708 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
2709 node_compile_spec = lys_compile_node_container;
2710 break;
2711 case LYS_LEAF:
2712 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
2713 node_compile_spec = lys_compile_node_leaf;
2714 break;
2715 case LYS_LIST:
2716 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
2717 break;
2718 case LYS_LEAFLIST:
2719 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
2720 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002721 case LYS_CHOICE:
2722 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
2723 break;
Radek Krejci19a96102018-11-15 13:38:09 +01002724 case LYS_ANYXML:
2725 case LYS_ANYDATA:
2726 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
2727 break;
2728 default:
2729 LOGINT(ctx->ctx);
2730 return LY_EINT;
2731 }
2732 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
2733 node->nodetype = node_p->nodetype;
2734 node->module = ctx->mod;
2735 node->prev = node;
2736 node->flags = node_p->flags;
2737
2738 /* config */
2739 if (!(node->flags & LYS_CONFIG_MASK)) {
2740 /* config not explicitely set, inherit it from parent */
2741 if (parent) {
2742 node->flags |= parent->flags & LYS_CONFIG_MASK;
2743 } else {
2744 /* default is config true */
2745 node->flags |= LYS_CONFIG_W;
2746 }
2747 }
2748
2749 /* status - it is not inherited by specification, but it does not make sense to have
2750 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2751 if (!(node->flags & LYS_STATUS_MASK)) {
2752 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
2753 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2754 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2755 node->flags |= parent->flags & LYS_STATUS_MASK;
2756 } else {
2757 node->flags |= LYS_STATUS_CURR;
2758 }
2759 } else if (parent) {
2760 /* check status compatibility with the parent */
2761 if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
2762 if (node->flags & LYS_STATUS_CURR) {
2763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2764 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2765 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2766 } else { /* LYS_STATUS_DEPRC */
2767 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2768 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2769 }
2770 goto error;
2771 }
2772 }
2773
2774 if (!(options & LYSC_OPT_FREE_SP)) {
2775 node->sp = node_p;
2776 }
2777 DUP_STRING(ctx->ctx, node_p->name, node->name);
2778 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
2779
2780 /* nodetype-specific part */
2781 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
2782
2783 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01002784 if (parent) {
2785 if (parent->nodetype == LYS_CHOICE) {
2786 /* TODO exception for cases */
2787 } else if ((children = lysc_node_children(parent))) {
2788 if (!(*children)) {
2789 /* first child */
2790 *children = node;
2791 } else {
2792 /* insert at the end of the parent's children list */
2793 (*children)->prev->next = node;
2794 node->prev = (*children)->prev;
2795 (*children)->prev = node;
2796 }
Radek Krejci19a96102018-11-15 13:38:09 +01002797 }
2798 } else {
2799 /* top-level element */
2800 if (!ctx->mod->compiled->data) {
2801 ctx->mod->compiled->data = node;
2802 } else {
2803 /* insert at the end of the module's top-level nodes list */
2804 ctx->mod->compiled->data->prev->next = node;
2805 node->prev = ctx->mod->compiled->data->prev;
2806 ctx->mod->compiled->data->prev = node;
2807 }
2808 }
2809
2810 return LY_SUCCESS;
2811
2812error:
2813 lysc_node_free(ctx->ctx, node);
2814 return ret;
2815}
2816
Radek Krejcia3045382018-11-22 14:30:31 +01002817/**
2818 * @brief Compile the given YANG module.
2819 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
2820 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2821 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2822 */
Radek Krejci19a96102018-11-15 13:38:09 +01002823LY_ERR
2824lys_compile(struct lys_module *mod, int options)
2825{
2826 struct lysc_ctx ctx = {0};
2827 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002828 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01002829 struct lysp_module *sp;
2830 struct lysp_node *node_p;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002831 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01002832 LY_ERR ret;
2833
2834 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
2835 sp = mod->parsed;
2836
2837 if (sp->submodule) {
2838 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
2839 return LY_EINVAL;
2840 }
2841
2842 ctx.ctx = sp->ctx;
2843 ctx.mod = mod;
2844
2845 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
2846 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
2847 mod_c->ctx = sp->ctx;
2848 mod_c->implemented = sp->implemented;
2849 mod_c->latest_revision = sp->latest_revision;
2850 mod_c->version = sp->version;
2851
2852 DUP_STRING(sp->ctx, sp->name, mod_c->name);
2853 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
2854 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
2855 if (sp->revs) {
2856 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
2857 }
2858 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
2859 COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
2860 COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
2861 if (sp->identities) {
2862 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
2863 }
2864
2865 LY_LIST_FOR(sp->data, node_p) {
2866 ret = lys_compile_node(&ctx, node_p, options, NULL);
2867 LY_CHECK_GOTO(ret, error);
2868 }
2869 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
2870 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
2871
2872 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
2873
Radek Krejcia3045382018-11-22 14:30:31 +01002874 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002875 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
2876 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
2877 * 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 +01002878 for (u = 0; u < ctx.unres.count; ++u) {
2879 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) {
2880 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2881 if (type->basetype == LY_TYPE_LEAFREF) {
2882 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01002883 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 +01002884 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002885 } else if (type->basetype == LY_TYPE_UNION) {
2886 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
2887 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
2888 /* validate the path */
2889 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
2890 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
2891 LY_CHECK_GOTO(ret, error);
2892 }
2893 }
Radek Krejcia3045382018-11-22 14:30:31 +01002894 }
2895 }
2896 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01002897 for (u = 0; u < ctx.unres.count; ++u) {
2898 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) {
2899 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
2900 if (type->basetype == LY_TYPE_LEAFREF) {
2901 /* store pointer to the real type */
2902 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
2903 typeiter->basetype == LY_TYPE_LEAFREF;
2904 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
2905 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002906 } else if (type->basetype == LY_TYPE_UNION) {
2907 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
2908 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
2909 /* store pointer to the real type */
2910 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
2911 typeiter->basetype == LY_TYPE_LEAFREF;
2912 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
2913 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
2914 }
2915 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01002916 }
2917 }
2918 }
Radek Krejcia3045382018-11-22 14:30:31 +01002919 ly_set_erase(&ctx.unres, NULL);
2920
Radek Krejci19a96102018-11-15 13:38:09 +01002921 if (options & LYSC_OPT_FREE_SP) {
2922 lysp_module_free(mod->parsed);
2923 ((struct lys_module*)mod)->parsed = NULL;
2924 }
2925
2926 ((struct lys_module*)mod)->compiled = mod_c;
2927 return LY_SUCCESS;
2928
2929error:
Radek Krejcia3045382018-11-22 14:30:31 +01002930 ly_set_erase(&ctx.unres, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01002931 lysc_module_free(mod_c, NULL);
2932 ((struct lys_module*)mod)->compiled = NULL;
2933 return ret;
2934}