blob: 02d7ba3a2ccd5e99e3e64e7ec3ddf70f95aea03d [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <ctype.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <stdio.h>
Radek Krejci19a96102018-11-15 13:38:09 +010019
20#include "libyang.h"
21#include "context.h"
22#include "tree_schema_internal.h"
23#include "xpath.h"
24
25/**
26 * @brief Duplicate string into dictionary
27 * @param[in] CTX libyang context of the dictionary.
28 * @param[in] ORIG String to duplicate.
29 * @param[out] DUP Where to store the result.
30 */
31#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
32
33#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
34 if (ARRAY_P) { \
35 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
36 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
37 LY_ARRAY_INCREMENT(ARRAY_C); \
38 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER]); \
39 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
40 } \
41 }
42
43#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
44 if (MEMBER_P) { \
45 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
46 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
47 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
48 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
49 }
50
Radek Krejci19a96102018-11-15 13:38:09 +010051static struct lysc_ext_instance *
52lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
53{
54 /* TODO */
55 (void) ctx;
56 (void) orig;
57 return NULL;
58}
59
60static struct lysc_pattern*
61lysc_pattern_dup(struct lysc_pattern *orig)
62{
63 ++orig->refcount;
64 return orig;
65}
66
67static struct lysc_pattern**
68lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
69{
70 struct lysc_pattern **dup;
71 unsigned int u;
72
73 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
74 LY_ARRAY_FOR(orig, u) {
75 dup[u] = lysc_pattern_dup(orig[u]);
76 LY_ARRAY_INCREMENT(dup);
77 }
78 return dup;
79}
80
81struct lysc_range*
82lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
83{
84 struct lysc_range *dup;
85 LY_ERR ret;
86
87 dup = calloc(1, sizeof *dup);
88 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
89 if (orig->parts) {
90 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
91 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
92 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
93 }
94 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
95 DUP_STRING(ctx, orig->emsg, dup->emsg);
96 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
97
98 return dup;
99cleanup:
100 free(dup);
101 (void) ret; /* set but not used due to the return type */
102 return NULL;
103}
104
105struct iff_stack {
106 int size;
107 int index; /* first empty item */
108 uint8_t *stack;
109};
110
111static LY_ERR
112iff_stack_push(struct iff_stack *stack, uint8_t value)
113{
114 if (stack->index == stack->size) {
115 stack->size += 4;
116 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
117 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
118 }
119 stack->stack[stack->index++] = value;
120 return LY_SUCCESS;
121}
122
123static uint8_t
124iff_stack_pop(struct iff_stack *stack)
125{
126 stack->index--;
127 return stack->stack[stack->index];
128}
129
130static void
131iff_stack_clean(struct iff_stack *stack)
132{
133 stack->size = 0;
134 free(stack->stack);
135}
136
137static void
138iff_setop(uint8_t *list, uint8_t op, int pos)
139{
140 uint8_t *item;
141 uint8_t mask = 3;
142
143 assert(pos >= 0);
144 assert(op <= 3); /* max 2 bits */
145
146 item = &list[pos / 4];
147 mask = mask << 2 * (pos % 4);
148 *item = (*item) & ~mask;
149 *item = (*item) | (op << 2 * (pos % 4));
150}
151
152#define LYS_IFF_LP 0x04 /* ( */
153#define LYS_IFF_RP 0x08 /* ) */
154
155static struct lysc_feature *
156lysc_feature_find(struct lysc_module *mod, const char *name, size_t len)
157{
158 size_t i;
159 struct lysc_feature *f;
160
161 for (i = 0; i < len; ++i) {
162 if (name[i] == ':') {
163 /* we have a prefixed feature */
164 mod = lysc_module_find_prefix(mod, name, i);
165 LY_CHECK_RET(!mod, NULL);
166
167 name = &name[i + 1];
168 len = len - i - 1;
169 }
170 }
171
172 /* we have the correct module, get the feature */
173 LY_ARRAY_FOR(mod->features, i) {
174 f = &mod->features[i];
175 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
176 return f;
177 }
178 }
179
180 return NULL;
181}
182
183static LY_ERR
184lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
185{
186 const char *name;
187 unsigned int u;
188 const struct lys_module *mod;
189 struct lysp_ext *edef = NULL;
190
191 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
192 ext->insubstmt = ext_p->insubstmt;
193 ext->insubstmt_index = ext_p->insubstmt_index;
194
195 /* get module where the extension definition should be placed */
196 for (u = 0; ext_p->name[u] != ':'; ++u);
197 mod = lys_module_find_prefix(ctx->mod, ext_p->name, u);
198 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
199 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
200 LY_EVALID);
201 LY_CHECK_ERR_RET(!mod->parsed->extensions,
202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
203 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
204 ext_p->name, mod->parsed->name),
205 LY_EVALID);
206 name = &ext_p->name[u + 1];
207 /* find the extension definition there */
208 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
209 if (!strcmp(name, mod->parsed->extensions[u].name)) {
210 edef = &mod->parsed->extensions[u];
211 break;
212 }
213 }
214 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
215 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
216 LY_EVALID);
217 /* TODO plugins */
218
219 return LY_SUCCESS;
220}
221
222static LY_ERR
223lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
224{
225 const char *c = *value;
226 int r, rc = EXIT_FAILURE;
227 int i, j, last_not, checkversion = 0;
228 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
229 uint8_t op;
230 struct iff_stack stack = {0, 0, NULL};
231 struct lysc_feature *f;
232
233 assert(c);
234
235 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
236 for (i = j = last_not = 0; c[i]; i++) {
237 if (c[i] == '(') {
238 j++;
239 checkversion = 1;
240 continue;
241 } else if (c[i] == ')') {
242 j--;
243 continue;
244 } else if (isspace(c[i])) {
245 checkversion = 1;
246 continue;
247 }
248
249 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
250 if (c[i + r] == '\0') {
251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
252 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
253 return LY_EVALID;
254 } else if (!isspace(c[i + r])) {
255 /* feature name starting with the not/and/or */
256 last_not = 0;
257 f_size++;
258 } else if (c[i] == 'n') { /* not operation */
259 if (last_not) {
260 /* double not */
261 expr_size = expr_size - 2;
262 last_not = 0;
263 } else {
264 last_not = 1;
265 }
266 } else { /* and, or */
267 f_exp++;
268 /* not a not operation */
269 last_not = 0;
270 }
271 i += r;
272 } else {
273 f_size++;
274 last_not = 0;
275 }
276 expr_size++;
277
278 while (!isspace(c[i])) {
279 if (!c[i] || c[i] == ')') {
280 i--;
281 break;
282 }
283 i++;
284 }
285 }
286 if (j || f_exp != f_size) {
287 /* not matching count of ( and ) */
288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
289 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
290 return LY_EVALID;
291 }
292
293 if (checkversion || expr_size > 1) {
294 /* check that we have 1.1 module */
295 if (ctx->mod->compiled->version != LYS_VERSION_1_1) {
296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
297 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
298 return LY_EVALID;
299 }
300 }
301
302 /* allocate the memory */
303 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
304 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
305 stack.stack = malloc(expr_size * sizeof *stack.stack);
306 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
307
308 stack.size = expr_size;
309 f_size--; expr_size--; /* used as indexes from now */
310
311 for (i--; i >= 0; i--) {
312 if (c[i] == ')') {
313 /* push it on stack */
314 iff_stack_push(&stack, LYS_IFF_RP);
315 continue;
316 } else if (c[i] == '(') {
317 /* pop from the stack into result all operators until ) */
318 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
319 iff_setop(iff->expr, op, expr_size--);
320 }
321 continue;
322 } else if (isspace(c[i])) {
323 continue;
324 }
325
326 /* end of operator or operand -> find beginning and get what is it */
327 j = i + 1;
328 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
329 i--;
330 }
331 i++; /* go back by one step */
332
333 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
334 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
335 /* double not */
336 iff_stack_pop(&stack);
337 } else {
338 /* not has the highest priority, so do not pop from the stack
339 * as in case of AND and OR */
340 iff_stack_push(&stack, LYS_IFF_NOT);
341 }
342 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
343 /* as for OR - pop from the stack all operators with the same or higher
344 * priority and store them to the result, then push the AND to the stack */
345 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
346 op = iff_stack_pop(&stack);
347 iff_setop(iff->expr, op, expr_size--);
348 }
349 iff_stack_push(&stack, LYS_IFF_AND);
350 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
351 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
352 op = iff_stack_pop(&stack);
353 iff_setop(iff->expr, op, expr_size--);
354 }
355 iff_stack_push(&stack, LYS_IFF_OR);
356 } else {
357 /* feature name, length is j - i */
358
359 /* add it to the expression */
360 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
361
362 /* now get the link to the feature definition */
363 f = lysc_feature_find(ctx->mod->compiled, &c[i], j - i);
364 LY_CHECK_ERR_GOTO(!f,
365 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
366 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
367 rc = LY_EVALID,
368 error)
369 iff->features[f_size] = f;
370 LY_ARRAY_INCREMENT(iff->features);
371 f_size--;
372 }
373 }
374 while (stack.index) {
375 op = iff_stack_pop(&stack);
376 iff_setop(iff->expr, op, expr_size--);
377 }
378
379 if (++expr_size || ++f_size) {
380 /* not all expected operators and operands found */
381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
382 "Invalid value \"%s\" of if-feature - processing error.", *value);
383 rc = LY_EINT;
384 } else {
385 rc = LY_SUCCESS;
386 }
387
388error:
389 /* cleanup */
390 iff_stack_clean(&stack);
391
392 return rc;
393}
394
395static LY_ERR
396lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when)
397{
398 unsigned int u;
399 LY_ERR ret = LY_SUCCESS;
400
401 when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
402 LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done);
403 COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done);
404
405done:
406 return ret;
407}
408
409static LY_ERR
410lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
411{
412 unsigned int u;
413 LY_ERR ret = LY_SUCCESS;
414
415 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
416 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
417
418 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
419 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
420 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
421
422done:
423 return ret;
424}
425
426static LY_ERR
427lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
428{
429 unsigned int u;
430 struct lys_module *mod = NULL;
431 struct lysc_module *comp;
432 LY_ERR ret = LY_SUCCESS;
433
434 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
435 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
436 imp->module = imp_p->module;
437
Radek Krejci7f2a5362018-11-28 13:05:37 +0100438 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
439 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100440 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100441 if (!imp->module->parsed) {
442 comp = imp->module->compiled;
443 /* try to get filepath from the compiled version */
444 if (comp->filepath) {
445 mod = (struct lys_module*)lys_parse_path(ctx->ctx, comp->filepath,
446 !strcmp(&comp->filepath[strlen(comp->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
447 if (mod != imp->module) {
448 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
449 comp->filepath, comp->name);
450 mod = NULL;
451 }
452 }
453 if (!mod) {
454 if (lysp_load_module(ctx->ctx, comp->name, comp->revision, 0, 1, &mod)) {
455 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
456 comp->name, ctx->mod->compiled->name);
457 return LY_ENOTFOUND;
458 }
459 }
Radek Krejci19a96102018-11-15 13:38:09 +0100460 }
461
462done:
463 return ret;
464}
465
466static LY_ERR
467lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *ident)
468{
469 unsigned int u;
470 LY_ERR ret = LY_SUCCESS;
471
472 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
473 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
474 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
475 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
476 ident->flags = ident_p->flags;
477
478done:
479 return ret;
480}
481
Radek Krejcia3045382018-11-22 14:30:31 +0100482/**
483 * @brief Find and process the referenced base identities from another identity or identityref
484 *
485 * For bases in identity se backlinks to them from the base identities. For identityref, store
486 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
487 * to distinguish these two use cases.
488 *
489 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
490 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
491 * @param[in] ident Referencing identity to work with.
492 * @param[in] bases Array of bases of identityref to fill in.
493 * @return LY_ERR value.
494 */
Radek Krejci19a96102018-11-15 13:38:09 +0100495static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100496lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +0100497{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100498 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100499 const char *s, *name;
500 struct lysc_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100501 struct lysc_ident **idref;
502
503 assert(ident || bases);
504
505 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) {
506 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
507 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
508 return LY_EVALID;
509 }
510
511 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
512 s = strchr(bases_p[u], ':');
513 if (s) {
514 /* prefixed identity */
515 name = &s[1];
516 mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]);
517 } else {
518 name = bases_p[u];
519 mod = ctx->mod->compiled;
520 }
521 if (!mod) {
522 if (ident) {
523 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
524 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
525 } else {
526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
527 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
528 }
529 return LY_EVALID;
530 }
531 idref = NULL;
532 if (mod->identities) {
533 for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) {
534 if (!strcmp(name, mod->identities[v].name)) {
535 if (ident) {
536 /* we have match! store the backlink */
537 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
538 *idref = ident;
539 } else {
540 /* we have match! store the found identity */
541 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
542 *idref = &mod->identities[v];
543 }
544 break;
545 }
546 }
547 }
548 if (!idref || !(*idref)) {
549 if (ident) {
550 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
551 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
552 } else {
553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
554 "Unable to find base (%s) of identityref.", bases_p[u]);
555 }
556 return LY_EVALID;
557 }
558 }
559 return LY_SUCCESS;
560}
561
Radek Krejcia3045382018-11-22 14:30:31 +0100562/**
563 * @brief For the given array of identities, set the backlinks from all their base identities.
564 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
565 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
566 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
567 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
568 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100569static LY_ERR
570lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
571{
572 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100573
574 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
575 if (!idents_p[i].bases) {
576 continue;
577 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100578 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100579 }
580 return LY_SUCCESS;
581}
582
Radek Krejcia3045382018-11-22 14:30:31 +0100583/**
584 * @brief Create compiled feature structure.
585 * @param[in] ctx Compile context.
586 * @param[in] feature_p Parsed feature definition to compile.
587 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
588 * @param[in,out] feature Compiled feature structure to fill.
589 * @return LY_ERR value.
590 */
Radek Krejci19a96102018-11-15 13:38:09 +0100591static LY_ERR
592lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature)
593{
594 unsigned int u, v;
595 LY_ERR ret = LY_SUCCESS;
596 struct lysc_feature **df;
597
598 DUP_STRING(ctx->ctx, feature_p->name, feature->name);
599 feature->flags = feature_p->flags;
600
601 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
602 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
603 if (feature->iffeatures) {
604 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
605 if (feature->iffeatures[u].features) {
606 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
607 /* add itself into the dependants list */
608 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
609 *df = feature;
610 }
611 /* TODO check for circular dependency */
612 }
613 }
614 }
615done:
616 return ret;
617}
618
Radek Krejcia3045382018-11-22 14:30:31 +0100619/**
620 * @brief Validate and normalize numeric value from a range definition.
621 * @param[in] ctx Compile context.
622 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
623 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
624 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
625 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
626 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
627 * @param[in] value String value of the range boundary.
628 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
629 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
630 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
631 */
Radek Krejci19a96102018-11-15 13:38:09 +0100632static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100633range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +0100634{
Radek Krejci6cba4292018-11-15 17:33:29 +0100635 size_t fraction = 0, size;
636
Radek Krejci19a96102018-11-15 13:38:09 +0100637 *len = 0;
638
639 assert(value);
640 /* parse value */
641 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
642 return LY_EVALID;
643 }
644
645 if ((value[*len] == '-') || (value[*len] == '+')) {
646 ++(*len);
647 }
648
649 while (isdigit(value[*len])) {
650 ++(*len);
651 }
652
653 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100654 if (basetype == LY_TYPE_DEC64) {
655 goto decimal;
656 } else {
657 *valcopy = strndup(value, *len);
658 return LY_SUCCESS;
659 }
Radek Krejci19a96102018-11-15 13:38:09 +0100660 }
661 fraction = *len;
662
663 ++(*len);
664 while (isdigit(value[*len])) {
665 ++(*len);
666 }
667
Radek Krejci6cba4292018-11-15 17:33:29 +0100668 if (basetype == LY_TYPE_DEC64) {
669decimal:
670 assert(frdigits);
671 if (*len - 1 - fraction > frdigits) {
672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
673 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
674 *len, value, frdigits);
675 return LY_EINVAL;
676 }
677 if (fraction) {
678 size = (*len) + (frdigits - ((*len) - 1 - fraction));
679 } else {
680 size = (*len) + frdigits + 1;
681 }
682 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100683 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
684
Radek Krejci6cba4292018-11-15 17:33:29 +0100685 (*valcopy)[size - 1] = '\0';
686 if (fraction) {
687 memcpy(&(*valcopy)[0], &value[0], fraction);
688 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
689 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
690 } else {
691 memcpy(&(*valcopy)[0], &value[0], *len);
692 memset(&(*valcopy)[*len], '0', frdigits);
693 }
Radek Krejci19a96102018-11-15 13:38:09 +0100694 }
695 return LY_SUCCESS;
696}
697
Radek Krejcia3045382018-11-22 14:30:31 +0100698/**
699 * @brief Check that values in range are in ascendant order.
700 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +0100701 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
702 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +0100703 * @param[in] value Current value to check.
704 * @param[in] prev_value The last seen value.
705 * @return LY_SUCCESS or LY_EEXIST for invalid order.
706 */
Radek Krejci19a96102018-11-15 13:38:09 +0100707static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +0100708range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +0100709{
710 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +0100711 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100712 return LY_EEXIST;
713 }
714 } else {
Radek Krejci5969f272018-11-23 10:03:58 +0100715 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100716 return LY_EEXIST;
717 }
718 }
719 return LY_SUCCESS;
720}
721
Radek Krejcia3045382018-11-22 14:30:31 +0100722/**
723 * @brief Set min/max value of the range part.
724 * @param[in] ctx Compile context.
725 * @param[in] part Range part structure to fill.
726 * @param[in] max Flag to distinguish if storing min or max value.
727 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
728 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
729 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
730 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
731 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +0100732 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +0100733 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
734 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
735 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
736 * frdigits value), LY_EMEM.
737 */
Radek Krejci19a96102018-11-15 13:38:09 +0100738static LY_ERR
739range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +0100740 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100741{
742 LY_ERR ret = LY_SUCCESS;
743 char *valcopy = NULL;
744 size_t len;
745
746 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100747 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +0100748 LY_CHECK_GOTO(ret, finalize);
749 }
750 if (!valcopy && base_range) {
751 if (max) {
752 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
753 } else {
754 part->min_64 = base_range->parts[0].min_64;
755 }
756 if (!first) {
757 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
758 }
759 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +0100760 }
761
762 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100763 case LY_TYPE_INT8: /* range */
764 if (valcopy) {
765 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
766 } else if (max) {
767 part->max_64 = INT64_C(127);
768 } else {
769 part->min_64 = INT64_C(-128);
770 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100771 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100772 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100773 }
774 break;
775 case LY_TYPE_INT16: /* range */
776 if (valcopy) {
777 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
778 } else if (max) {
779 part->max_64 = INT64_C(32767);
780 } else {
781 part->min_64 = INT64_C(-32768);
782 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100783 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100784 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100785 }
786 break;
787 case LY_TYPE_INT32: /* range */
788 if (valcopy) {
789 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
790 } else if (max) {
791 part->max_64 = INT64_C(2147483647);
792 } else {
793 part->min_64 = INT64_C(-2147483648);
794 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100795 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100796 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100797 }
798 break;
799 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +0100800 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100801 if (valcopy) {
802 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
803 max ? &part->max_64 : &part->min_64);
804 } else if (max) {
805 part->max_64 = INT64_C(9223372036854775807);
806 } else {
807 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
808 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100809 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100810 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100811 }
812 break;
813 case LY_TYPE_UINT8: /* range */
814 if (valcopy) {
815 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
816 } else if (max) {
817 part->max_u64 = UINT64_C(255);
818 } else {
819 part->min_u64 = UINT64_C(0);
820 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100821 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100822 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100823 }
824 break;
825 case LY_TYPE_UINT16: /* range */
826 if (valcopy) {
827 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
828 } else if (max) {
829 part->max_u64 = UINT64_C(65535);
830 } else {
831 part->min_u64 = UINT64_C(0);
832 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100833 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100834 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100835 }
836 break;
837 case LY_TYPE_UINT32: /* range */
838 if (valcopy) {
839 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
840 } else if (max) {
841 part->max_u64 = UINT64_C(4294967295);
842 } else {
843 part->min_u64 = UINT64_C(0);
844 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100845 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100846 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100847 }
848 break;
849 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100850 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +0100851 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +0100852 if (valcopy) {
853 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
854 } else if (max) {
855 part->max_u64 = UINT64_C(18446744073709551615);
856 } else {
857 part->min_u64 = UINT64_C(0);
858 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100859 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100860 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100861 }
862 break;
863 default:
864 LOGINT(ctx->ctx);
865 ret = LY_EINT;
866 }
867
Radek Krejci5969f272018-11-23 10:03:58 +0100868finalize:
Radek Krejci19a96102018-11-15 13:38:09 +0100869 if (ret == LY_EDENIED) {
870 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
871 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
872 length_restr ? "length" : "range", valcopy ? valcopy : *value);
873 } else if (ret == LY_EVALID) {
874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
875 "Invalid %s restriction - invalid value \"%s\".",
876 length_restr ? "length" : "range", valcopy ? valcopy : *value);
877 } else if (ret == LY_EEXIST) {
878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
879 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100880 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +0100881 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +0100882 } else if (!ret && value) {
883 *value = *value + len;
884 }
885 free(valcopy);
886 return ret;
887}
888
Radek Krejcia3045382018-11-22 14:30:31 +0100889/**
890 * @brief Compile the parsed range restriction.
891 * @param[in] ctx Compile context.
892 * @param[in] range_p Parsed range structure to compile.
893 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
894 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
895 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
896 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
897 * range restriction must be more restrictive than the base_range.
898 * @param[in,out] range Pointer to the created current range structure.
899 * @return LY_ERR value.
900 */
Radek Krejci19a96102018-11-15 13:38:09 +0100901static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100902lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +0100903 struct lysc_range *base_range, struct lysc_range **range)
904{
905 LY_ERR ret = LY_EVALID;
906 const char *expr;
907 struct lysc_range_part *parts = NULL, *part;
908 int range_expected = 0, uns;
909 unsigned int parts_done = 0, u, v;
910
911 assert(range);
912 assert(range_p);
913
914 expr = range_p->arg;
915 while(1) {
916 if (isspace(*expr)) {
917 ++expr;
918 } else if (*expr == '\0') {
919 if (range_expected) {
920 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
921 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
922 length_restr ? "length" : "range", range_p->arg);
923 goto cleanup;
924 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
926 "Invalid %s restriction - unexpected end of the expression (%s).",
927 length_restr ? "length" : "range", range_p->arg);
928 goto cleanup;
929 }
930 parts_done++;
931 break;
932 } else if (!strncmp(expr, "min", 3)) {
933 if (parts) {
934 /* min cannot be used elsewhere than in the first part */
935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
936 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
937 expr - range_p->arg, range_p->arg);
938 goto cleanup;
939 }
940 expr += 3;
941
942 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +0100943 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100944 part->max_64 = part->min_64;
945 } else if (*expr == '|') {
946 if (!parts || range_expected) {
947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
948 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
949 goto cleanup;
950 }
951 expr++;
952 parts_done++;
953 /* process next part of the expression */
954 } else if (!strncmp(expr, "..", 2)) {
955 expr += 2;
956 while (isspace(*expr)) {
957 expr++;
958 }
959 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
960 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
961 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
962 goto cleanup;
963 }
964 /* continue expecting the upper boundary */
965 range_expected = 1;
966 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
967 /* number */
968 if (range_expected) {
969 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100970 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100971 range_expected = 0;
972 } else {
973 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
974 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +0100975 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100976 part->max_64 = part->min_64;
977 }
978
979 /* continue with possible another expression part */
980 } else if (!strncmp(expr, "max", 3)) {
981 expr += 3;
982 while (isspace(*expr)) {
983 expr++;
984 }
985 if (*expr != '\0') {
986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
987 length_restr ? "length" : "range", expr);
988 goto cleanup;
989 }
990 if (range_expected) {
991 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100992 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100993 range_expected = 0;
994 } else {
995 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
996 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +0100997 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100998 part->min_64 = part->max_64;
999 }
1000 } else {
1001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1002 length_restr ? "length" : "range", expr);
1003 goto cleanup;
1004 }
1005 }
1006
1007 /* check with the previous range/length restriction */
1008 if (base_range) {
1009 switch (basetype) {
1010 case LY_TYPE_BINARY:
1011 case LY_TYPE_UINT8:
1012 case LY_TYPE_UINT16:
1013 case LY_TYPE_UINT32:
1014 case LY_TYPE_UINT64:
1015 case LY_TYPE_STRING:
1016 uns = 1;
1017 break;
1018 case LY_TYPE_DEC64:
1019 case LY_TYPE_INT8:
1020 case LY_TYPE_INT16:
1021 case LY_TYPE_INT32:
1022 case LY_TYPE_INT64:
1023 uns = 0;
1024 break;
1025 default:
1026 LOGINT(ctx->ctx);
1027 ret = LY_EINT;
1028 goto cleanup;
1029 }
1030 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1031 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1032 goto baseerror;
1033 }
1034 /* current lower bound is not lower than the base */
1035 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1036 /* base has single value */
1037 if (base_range->parts[v].min_64 == parts[u].min_64) {
1038 /* both lower bounds are the same */
1039 if (parts[u].min_64 != parts[u].max_64) {
1040 /* current continues with a range */
1041 goto baseerror;
1042 } else {
1043 /* equal single values, move both forward */
1044 ++v;
1045 continue;
1046 }
1047 } else {
1048 /* base is single value lower than current range, so the
1049 * value from base range is removed in the current,
1050 * move only base and repeat checking */
1051 ++v;
1052 --u;
1053 continue;
1054 }
1055 } else {
1056 /* base is the range */
1057 if (parts[u].min_64 == parts[u].max_64) {
1058 /* current is a single value */
1059 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1060 /* current is behind the base range, so base range is omitted,
1061 * move the base and keep the current for further check */
1062 ++v;
1063 --u;
1064 } /* else it is within the base range, so move the current, but keep the base */
1065 continue;
1066 } else {
1067 /* both are ranges - check the higher bound, the lower was already checked */
1068 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1069 /* higher bound is higher than the current higher bound */
1070 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1071 /* but the current lower bound is also higher, so the base range is omitted,
1072 * continue with the same current, but move the base */
1073 --u;
1074 ++v;
1075 continue;
1076 }
1077 /* current range starts within the base range but end behind it */
1078 goto baseerror;
1079 } else {
1080 /* current range is smaller than the base,
1081 * move current, but stay with the base */
1082 continue;
1083 }
1084 }
1085 }
1086 }
1087 if (u != parts_done) {
1088baseerror:
1089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1090 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1091 length_restr ? "length" : "range", range_p->arg);
1092 goto cleanup;
1093 }
1094 }
1095
1096 if (!(*range)) {
1097 *range = calloc(1, sizeof **range);
1098 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1099 }
1100
1101 if (range_p->eapptag) {
1102 lydict_remove(ctx->ctx, (*range)->eapptag);
1103 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1104 }
1105 if (range_p->emsg) {
1106 lydict_remove(ctx->ctx, (*range)->emsg);
1107 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1108 }
1109 /* extensions are taken only from the last range by the caller */
1110
1111 (*range)->parts = parts;
1112 parts = NULL;
1113 ret = LY_SUCCESS;
1114cleanup:
1115 /* TODO clean up */
1116 LY_ARRAY_FREE(parts);
1117
1118 return ret;
1119}
1120
1121/**
1122 * @brief Checks pattern syntax.
1123 *
Radek Krejcia3045382018-11-22 14:30:31 +01001124 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001125 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001126 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1127 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001128 */
1129static LY_ERR
1130lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1131{
1132 int idx, idx2, start, end, err_offset, count;
1133 char *perl_regex, *ptr;
1134 const char *err_msg, *orig_ptr;
1135 pcre *precomp;
1136#define URANGE_LEN 19
1137 char *ublock2urange[][2] = {
1138 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1139 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1140 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1141 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1142 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1143 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1144 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1145 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1146 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1147 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1148 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1149 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1150 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1151 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1152 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1153 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1154 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1155 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1156 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1157 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1158 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1159 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1160 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1161 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1162 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1163 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1164 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1165 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1166 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1167 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1168 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1169 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1170 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1171 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1172 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1173 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1174 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1175 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1176 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1177 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1178 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1179 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1180 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1181 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1182 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1183 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1184 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1185 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1186 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1187 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1188 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1189 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1190 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1191 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1192 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1193 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1194 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1195 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1196 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1197 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1198 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1199 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1200 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1201 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1202 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1203 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1204 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1205 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1206 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1207 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1208 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1209 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1210 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1211 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1212 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1213 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1214 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1215 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1216 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1217 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1218 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1219 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1220 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1221 {NULL, NULL}
1222 };
1223
1224 /* adjust the expression to a Perl equivalent
1225 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1226
1227 /* we need to replace all "$" with "\$", count them now */
1228 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1229
1230 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1231 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1232 perl_regex[0] = '\0';
1233
1234 ptr = perl_regex;
1235
1236 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1237 /* we will add line-end anchoring */
1238 ptr[0] = '(';
1239 ++ptr;
1240 }
1241
1242 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1243 if (orig_ptr[0] == '$') {
1244 ptr += sprintf(ptr, "\\$");
1245 } else if (orig_ptr[0] == '^') {
1246 ptr += sprintf(ptr, "\\^");
1247 } else {
1248 ptr[0] = orig_ptr[0];
1249 ++ptr;
1250 }
1251 }
1252
1253 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1254 ptr += sprintf(ptr, ")$");
1255 } else {
1256 ptr[0] = '\0';
1257 ++ptr;
1258 }
1259
1260 /* substitute Unicode Character Blocks with exact Character Ranges */
1261 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1262 start = ptr - perl_regex;
1263
1264 ptr = strchr(ptr, '}');
1265 if (!ptr) {
1266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1267 pattern, perl_regex + start + 2, "unterminated character property");
1268 free(perl_regex);
1269 return LY_EVALID;
1270 }
1271 end = (ptr - perl_regex) + 1;
1272
1273 /* need more space */
1274 if (end - start < URANGE_LEN) {
1275 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1276 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1277 }
1278
1279 /* find our range */
1280 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1281 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1282 break;
1283 }
1284 }
1285 if (!ublock2urange[idx][0]) {
1286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1287 pattern, perl_regex + start + 5, "unknown block name");
1288 free(perl_regex);
1289 return LY_EVALID;
1290 }
1291
1292 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1293 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1294 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1295 ++count;
1296 }
1297 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1298 --count;
1299 }
1300 }
1301 if (count) {
1302 /* skip brackets */
1303 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1304 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1305 } else {
1306 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1307 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1308 }
1309 }
1310
1311 /* must return 0, already checked during parsing */
1312 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1313 &err_msg, &err_offset, NULL);
1314 if (!precomp) {
1315 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1316 free(perl_regex);
1317 return LY_EVALID;
1318 }
1319 free(perl_regex);
1320
1321 if (pcre_precomp) {
1322 *pcre_precomp = precomp;
1323 } else {
1324 free(precomp);
1325 }
1326
1327 return LY_SUCCESS;
1328
1329#undef URANGE_LEN
1330}
1331
Radek Krejcia3045382018-11-22 14:30:31 +01001332/**
1333 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1334 * @param[in] ctx Compile context.
1335 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1336 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1337 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1338 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1339 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1340 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1341 */
Radek Krejci19a96102018-11-15 13:38:09 +01001342static LY_ERR
1343lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1344 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1345{
1346 struct lysc_pattern **pattern;
1347 unsigned int u, v;
1348 const char *err_msg;
1349 LY_ERR ret = LY_SUCCESS;
1350
1351 /* first, copy the patterns from the base type */
1352 if (base_patterns) {
1353 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1354 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1355 }
1356
1357 LY_ARRAY_FOR(patterns_p, u) {
1358 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1359 *pattern = calloc(1, sizeof **pattern);
1360 ++(*pattern)->refcount;
1361
1362 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1363 LY_CHECK_RET(ret);
1364 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1365 if (err_msg) {
1366 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1367 }
1368
1369 if (patterns_p[u].arg[0] == 0x15) {
1370 (*pattern)->inverted = 1;
1371 }
1372 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1373 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
1374 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1375 options, v, lys_compile_ext, ret, done);
1376 }
1377done:
1378 return ret;
1379}
1380
Radek Krejcia3045382018-11-22 14:30:31 +01001381/**
1382 * @brief map of the possible restrictions combination for the specific built-in type.
1383 */
Radek Krejci19a96102018-11-15 13:38:09 +01001384static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1385 0 /* LY_TYPE_UNKNOWN */,
1386 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001387 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1388 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1389 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1390 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1391 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001392 LYS_SET_BIT /* LY_TYPE_BITS */,
1393 0 /* LY_TYPE_BOOL */,
1394 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1395 0 /* LY_TYPE_EMPTY */,
1396 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1397 LYS_SET_BASE /* LY_TYPE_IDENT */,
1398 LYS_SET_REQINST /* LY_TYPE_INST */,
1399 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001400 LYS_SET_TYPE /* LY_TYPE_UNION */,
1401 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001402 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001403 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001404 LYS_SET_RANGE /* LY_TYPE_INT64 */
1405};
1406
1407/**
1408 * @brief stringification of the YANG built-in data types
1409 */
1410const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1411 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1412 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001413};
1414
Radek Krejcia3045382018-11-22 14:30:31 +01001415/**
1416 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1417 * @param[in] ctx Compile context.
1418 * @param[in] enums_p Array of the parsed enum structures to compile.
1419 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1420 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1421 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1422 * @param[out] enums Newly created array of the compiled enums information for the current type.
1423 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1424 */
Radek Krejci19a96102018-11-15 13:38:09 +01001425static LY_ERR
1426lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1427 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1428{
1429 LY_ERR ret = LY_SUCCESS;
1430 unsigned int u, v, match;
1431 int32_t value = 0;
1432 uint32_t position = 0;
1433 struct lysc_type_enum_item *e, storage;
1434
1435 if (base_enums && ctx->mod->compiled->version < 2) {
1436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1437 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1438 return LY_EVALID;
1439 }
1440
1441 LY_ARRAY_FOR(enums_p, u) {
1442 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1443 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
1444 if (base_enums) {
1445 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1446 LY_ARRAY_FOR(base_enums, v) {
1447 if (!strcmp(e->name, base_enums[v].name)) {
1448 break;
1449 }
1450 }
1451 if (v == LY_ARRAY_SIZE(base_enums)) {
1452 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1453 "Invalid %s - derived type adds new item \"%s\".",
1454 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1455 return LY_EVALID;
1456 }
1457 match = v;
1458 }
1459
1460 if (basetype == LY_TYPE_ENUM) {
1461 if (enums_p[u].flags & LYS_SET_VALUE) {
1462 e->value = (int32_t)enums_p[u].value;
1463 if (!u || e->value >= value) {
1464 value = e->value + 1;
1465 }
1466 /* check collision with other values */
1467 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1468 if (e->value == (*enums)[v].value) {
1469 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1470 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1471 e->value, e->name, (*enums)[v].name);
1472 return LY_EVALID;
1473 }
1474 }
1475 } else if (base_enums) {
1476 /* inherit the assigned value */
1477 e->value = base_enums[match].value;
1478 if (!u || e->value >= value) {
1479 value = e->value + 1;
1480 }
1481 } else {
1482 /* assign value automatically */
1483 if (u && value == INT32_MIN) {
1484 /* counter overflow */
1485 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1486 "Invalid enumeration - it is not possible to auto-assign enum value for "
1487 "\"%s\" since the highest value is already 2147483647.", e->name);
1488 return LY_EVALID;
1489 }
1490 e->value = value++;
1491 }
1492 } else { /* LY_TYPE_BITS */
1493 if (enums_p[u].flags & LYS_SET_VALUE) {
1494 e->value = (int32_t)enums_p[u].value;
1495 if (!u || (uint32_t)e->value >= position) {
1496 position = (uint32_t)e->value + 1;
1497 }
1498 /* check collision with other values */
1499 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1500 if (e->value == (*enums)[v].value) {
1501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1502 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1503 (uint32_t)e->value, e->name, (*enums)[v].name);
1504 return LY_EVALID;
1505 }
1506 }
1507 } else if (base_enums) {
1508 /* inherit the assigned value */
1509 e->value = base_enums[match].value;
1510 if (!u || (uint32_t)e->value >= position) {
1511 position = (uint32_t)e->value + 1;
1512 }
1513 } else {
1514 /* assign value automatically */
1515 if (u && position == 0) {
1516 /* counter overflow */
1517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1518 "Invalid bits - it is not possible to auto-assign bit position for "
1519 "\"%s\" since the highest value is already 4294967295.", e->name);
1520 return LY_EVALID;
1521 }
1522 e->value = position++;
1523 }
1524 }
1525
1526 if (base_enums) {
1527 /* the assigned values must not change from the derived type */
1528 if (e->value != base_enums[match].value) {
1529 if (basetype == LY_TYPE_ENUM) {
1530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1531 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1532 e->name, base_enums[match].value, e->value);
1533 } else {
1534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1535 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1536 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1537 }
1538 return LY_EVALID;
1539 }
1540 }
1541
1542 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1543 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1544
1545 if (basetype == LY_TYPE_BITS) {
1546 /* keep bits ordered by position */
1547 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1548 if (v != u) {
1549 memcpy(&storage, e, sizeof *e);
1550 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1551 memcpy(&(*enums)[v], &storage, sizeof storage);
1552 }
1553 }
1554 }
1555
1556done:
1557 return ret;
1558}
1559
Radek Krejcia3045382018-11-22 14:30:31 +01001560#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1561 for ((NODE) = (NODE)->parent; \
1562 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1563 (NODE) = (NODE)->parent); \
1564 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1565 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1566 TERM; \
1567 }
1568
1569/**
1570 * @brief Validate the predicate(s) from the leafref path.
1571 * @param[in] ctx Compile context
1572 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1573 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001574 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001575 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001576 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001577 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1578 */
1579static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001580lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001581 const struct lysc_node_list *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;
Radek Krejcibade82a2018-12-05 10:13:48 +01001588 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001589 const char *start, *end, *pke_end;
1590 struct ly_set keys = {0};
1591 int i;
1592
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001593 assert(path_context);
1594
Radek Krejcia3045382018-11-22 14:30:31 +01001595 while (**predicate == '[') {
1596 start = (*predicate)++;
1597
1598 while (isspace(**predicate)) {
1599 ++(*predicate);
1600 }
1601 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1602 while (isspace(**predicate)) {
1603 ++(*predicate);
1604 }
1605 if (**predicate != '=') {
1606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001607 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1608 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001609 goto cleanup;
1610 }
1611 ++(*predicate);
1612 while (isspace(**predicate)) {
1613 ++(*predicate);
1614 }
1615
1616 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1618 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1619 goto cleanup;
1620 }
1621 --pke_end;
1622 while (isspace(*pke_end)) {
1623 --pke_end;
1624 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001625 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01001626 /* localize path-key-expr */
1627 pke_start = path_key_expr = *predicate;
1628 /* move after the current predicate */
1629 *predicate = end + 1;
1630
1631 /* source (must be leaf or leaf-list) */
1632 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001633 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001634 if (!mod) {
1635 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1636 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
1637 *predicate - start, start, src_prefix_len, src_prefix, path_context->compiled->name);
1638 goto cleanup;
1639 }
Radek Krejcia3045382018-11-22 14:30:31 +01001640 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001641 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001642 }
Radek Krejcibade82a2018-12-05 10:13:48 +01001643 src_node = NULL;
1644 if (context_node->keys) {
1645 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
1646 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
1647 src_node = (const struct lysc_node*)context_node->keys[u];
1648 break;
1649 }
1650 }
1651 }
Radek Krejcia3045382018-11-22 14:30:31 +01001652 if (!src_node) {
1653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001654 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejcia3045382018-11-22 14:30:31 +01001655 *predicate - start, start, src_len, src, mod->compiled->name);
1656 goto cleanup;
1657 }
Radek Krejcia3045382018-11-22 14:30:31 +01001658
1659 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01001660 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01001661 i = ly_set_add(&keys, (void*)src_node, 0);
1662 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01001663 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01001664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001665 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01001666 *predicate - start, start, src_node->name);
1667 goto cleanup;
1668 }
1669
1670 /* destination */
1671 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01001672 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01001673
1674 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
1675 if (strncmp(path_key_expr, "current()", 9)) {
1676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1677 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
1678 *predicate - start, start);
1679 goto cleanup;
1680 }
1681 path_key_expr += 9;
1682 while (isspace(*path_key_expr)) {
1683 ++path_key_expr;
1684 }
1685
1686 if (*path_key_expr != '/') {
1687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1688 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
1689 *predicate - start, start);
1690 goto cleanup;
1691 }
1692 ++path_key_expr;
1693 while (isspace(*path_key_expr)) {
1694 ++path_key_expr;
1695 }
1696
1697 /* rel-path-keyexpr:
1698 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
1699 while (!strncmp(path_key_expr, "..", 2)) {
1700 ++dest_parent_times;
1701 path_key_expr += 2;
1702 while (isspace(*path_key_expr)) {
1703 ++path_key_expr;
1704 }
1705 if (*path_key_expr != '/') {
1706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1707 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
1708 *predicate - start, start);
1709 goto cleanup;
1710 }
1711 ++path_key_expr;
1712 while (isspace(*path_key_expr)) {
1713 ++path_key_expr;
1714 }
1715
1716 /* path is supposed to be evaluated in data tree, so we have to skip
1717 * all schema nodes that cannot be instantiated in data tree */
1718 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
1719 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
1720 *predicate - start, start);
1721 }
1722 if (!dest_parent_times) {
1723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1724 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
1725 *predicate - start, start);
1726 goto cleanup;
1727 }
1728 if (path_key_expr == pke_end) {
1729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1730 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
1731 *predicate - start, start);
1732 goto cleanup;
1733 }
1734
1735 while(path_key_expr != pke_end) {
1736 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
1737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01001738 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
1739 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001740 goto cleanup;
1741 }
1742
1743 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001744 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001745 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001746 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001747 }
1748 if (!mod) {
1749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1750 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
1751 *predicate - start, start, dst_len, dst);
1752 goto cleanup;
1753 }
1754
1755 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
1756 if (!dst_node) {
1757 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1758 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
1759 *predicate - start, start, path_key_expr - pke_start, pke_start);
1760 goto cleanup;
1761 }
1762 }
1763 if (!(dst_node->nodetype & (dst_node->module->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
1764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001765 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01001766 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
1767 goto cleanup;
1768 }
1769 }
1770
1771 ret = LY_SUCCESS;
1772cleanup:
1773 ly_set_erase(&keys, NULL);
1774 return ret;
1775}
1776
1777/**
1778 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
1779 *
1780 * path-arg = absolute-path / relative-path
1781 * absolute-path = 1*("/" (node-identifier *path-predicate))
1782 * relative-path = 1*(".." "/") descendant-path
1783 *
1784 * @param[in,out] path Path to parse.
1785 * @param[out] prefix Prefix of the token, NULL if there is not any.
1786 * @param[out] pref_len Length of the prefix, 0 if there is not any.
1787 * @param[out] name Name of the token.
1788 * @param[out] nam_len Length of the name.
1789 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
1790 * must not be changed between consecutive calls. -1 if the
1791 * path is absolute.
1792 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
1793 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
1794 */
1795static LY_ERR
1796lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
1797 int *parent_times, int *has_predicate)
1798{
1799 int par_times = 0;
1800
1801 assert(path && *path);
1802 assert(parent_times);
1803 assert(prefix);
1804 assert(prefix_len);
1805 assert(name);
1806 assert(name_len);
1807 assert(has_predicate);
1808
1809 *prefix = NULL;
1810 *prefix_len = 0;
1811 *name = NULL;
1812 *name_len = 0;
1813 *has_predicate = 0;
1814
1815 if (!*parent_times) {
1816 if (!strncmp(*path, "..", 2)) {
1817 *path += 2;
1818 ++par_times;
1819 while (!strncmp(*path, "/..", 3)) {
1820 *path += 3;
1821 ++par_times;
1822 }
1823 }
1824 if (par_times) {
1825 *parent_times = par_times;
1826 } else {
1827 *parent_times = -1;
1828 }
1829 }
1830
1831 if (**path != '/') {
1832 return LY_EINVAL;
1833 }
1834 /* skip '/' */
1835 ++(*path);
1836
1837 /* node-identifier ([prefix:]name) */
1838 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
1839
1840 if ((**path == '/' && (*path)[1]) || !**path) {
1841 /* path continues by another token or this is the last token */
1842 return LY_SUCCESS;
1843 } else if ((*path)[0] != '[') {
1844 /* unexpected character */
1845 return LY_EINVAL;
1846 } else {
1847 /* predicate starting with [ */
1848 *has_predicate = 1;
1849 return LY_SUCCESS;
1850 }
1851}
1852
1853/**
Radek Krejci58d171e2018-11-23 13:50:55 +01001854 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
1855 *
1856 * The set of features used for target must be a subset of features used for the leafref.
1857 * This is not a perfect, we should compare the truth tables but it could require too much resources
1858 * and RFC 7950 does not require it explicitely, so we simplify that.
1859 *
1860 * @param[in] refnode The leafref node.
1861 * @param[in] target Tha target node of the leafref.
1862 * @return LY_SUCCESS or LY_EVALID;
1863 */
1864static LY_ERR
1865lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
1866{
1867 LY_ERR ret = LY_EVALID;
1868 const struct lysc_node *iter;
1869 struct lysc_iffeature **iff;
1870 unsigned int u, v, count;
1871 struct ly_set features = {0};
1872
1873 for (iter = refnode; iter; iter = iter->parent) {
1874 iff = lysc_node_iff(iter);
1875 if (iff && *iff) {
1876 LY_ARRAY_FOR(*iff, u) {
1877 LY_ARRAY_FOR((*iff)[u].features, v) {
1878 LY_CHECK_GOTO(ly_set_add(&features, (*iff)[u].features[v], 0) == -1, cleanup);
1879 }
1880 }
1881 }
1882 }
1883
1884 /* we should have, in features set, a superset of features applicable to the target node.
1885 * So when adding features applicable to the target into the features set, we should not be
1886 * able to actually add any new feature, otherwise it is not a subset of features applicable
1887 * to the leafref itself. */
1888 count = features.count;
1889 for (iter = target; iter; iter = iter->parent) {
1890 iff = lysc_node_iff(iter);
1891 if (iff && *iff) {
1892 LY_ARRAY_FOR(*iff, u) {
1893 LY_ARRAY_FOR((*iff)[u].features, v) {
1894 if ((unsigned int)ly_set_add(&features, (*iff)[u].features[v], 0) >= count) {
1895 /* new feature was added (or LY_EMEM) */
1896 goto cleanup;
1897 }
1898 }
1899 }
1900 }
1901 }
1902 ret = LY_SUCCESS;
1903
1904cleanup:
1905 ly_set_erase(&features, NULL);
1906 return ret;
1907}
1908
1909/**
Radek Krejcia3045382018-11-22 14:30:31 +01001910 * @brief Validate the leafref path.
1911 * @param[in] ctx Compile context
1912 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01001913 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01001914 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1915 */
1916static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01001917lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01001918{
1919 const struct lysc_node *node = NULL, *parent = NULL;
1920 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001921 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01001922 const char *id, *prefix, *name;
1923 size_t prefix_len, name_len;
1924 int parent_times = 0, has_predicate;
1925 unsigned int iter, u;
1926 LY_ERR ret = LY_SUCCESS;
1927
1928 assert(ctx);
1929 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001930 assert(leafref);
1931
1932 /* 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 +01001933
1934 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001935 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01001936 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
1937 if (!iter) { /* first iteration */
1938 /* precess ".." in relative paths */
1939 if (parent_times > 0) {
1940 /* move from the context node */
1941 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
1942 /* path is supposed to be evaluated in data tree, so we have to skip
1943 * all schema nodes that cannot be instantiated in data tree */
1944 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001945 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001946 }
1947 }
1948 }
1949
1950 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001951 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001952 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001953 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001954 }
1955 if (!mod) {
1956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001957 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
1958 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001959 return LY_EVALID;
1960 }
1961
1962 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
1963 if (!node) {
1964 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001965 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001966 return LY_EVALID;
1967 }
1968 parent = node;
1969
1970 if (has_predicate) {
1971 /* we have predicate, so the current result must be list */
1972 if (node->nodetype != LYS_LIST) {
1973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1974 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001975 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001976 return LY_EVALID;
1977 }
1978
Radek Krejcibade82a2018-12-05 10:13:48 +01001979 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
1980 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01001981 }
1982
1983 ++iter;
1984 }
1985 if (ret) {
1986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001987 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001988 return LY_EVALID;
1989 }
1990
1991 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1992 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1993 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001994 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001995 return LY_EVALID;
1996 }
1997
1998 /* check status */
1999 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2000 return LY_EVALID;
2001 }
2002
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002003 /* check config */
2004 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2005 if (node->flags & LYS_CONFIG_R) {
2006 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2007 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2008 leafref->path);
2009 return LY_EVALID;
2010 }
2011 }
2012
Radek Krejci412ddfa2018-11-23 11:44:11 +01002013 /* store the target's type and check for circular chain of leafrefs */
2014 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2015 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2016 if (type == (struct lysc_type*)leafref) {
2017 /* circular chain detected */
2018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2019 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2020 return LY_EVALID;
2021 }
2022 }
2023
Radek Krejci58d171e2018-11-23 13:50:55 +01002024 /* check if leafref and its target are under common if-features */
2025 if (lys_compile_leafref_features_validate(startnode, node)) {
2026 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2027 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2028 leafref->path);
2029 return LY_EVALID;
2030 }
2031
Radek Krejcia3045382018-11-22 14:30:31 +01002032 return LY_SUCCESS;
2033}
2034
Radek Krejcicdfecd92018-11-26 11:27:32 +01002035static 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,
2036 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt);
Radek Krejcia3045382018-11-22 14:30:31 +01002037/**
2038 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2039 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002040 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2041 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2042 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2043 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002044 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002045 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002046 * @param[in] basetype Base YANG built-in type of the type to compile.
2047 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2048 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2049 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2050 * @param[out] type Newly created type structure with the filled information about the type.
2051 * @return LY_ERR value.
2052 */
Radek Krejci19a96102018-11-15 13:38:09 +01002053static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002054lys_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,
2055 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2056 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002057{
2058 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002059 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002060 struct lysc_type_bin *bin;
2061 struct lysc_type_num *num;
2062 struct lysc_type_str *str;
2063 struct lysc_type_bits *bits;
2064 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002065 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002066 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002067 struct lysc_type_union *un, *un_aux;
2068 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002069
2070 switch (basetype) {
2071 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002072 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002073
2074 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002075 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002076 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002077 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2078 LY_CHECK_RET(ret);
2079 if (!tpdfname) {
2080 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2081 options, u, lys_compile_ext, ret, done);
2082 }
2083 }
2084
2085 if (tpdfname) {
2086 type_p->compiled = *type;
2087 *type = calloc(1, sizeof(struct lysc_type_bin));
2088 }
2089 break;
2090 case LY_TYPE_BITS:
2091 /* RFC 7950 9.7 - bits */
2092 bits = (struct lysc_type_bits*)(*type);
2093 if (type_p->bits) {
2094 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2095 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2096 (struct lysc_type_enum_item**)&bits->bits);
2097 LY_CHECK_RET(ret);
2098 }
2099
Radek Krejci555cb5b2018-11-16 14:54:33 +01002100 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002101 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002102 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002104 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002106 free(*type);
2107 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002108 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002109 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002110 }
2111
2112 if (tpdfname) {
2113 type_p->compiled = *type;
2114 *type = calloc(1, sizeof(struct lysc_type_bits));
2115 }
2116 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002117 case LY_TYPE_DEC64:
2118 dec = (struct lysc_type_dec*)(*type);
2119
2120 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002121 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002122 if (!type_p->fraction_digits) {
2123 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002124 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002125 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002127 free(*type);
2128 *type = NULL;
2129 }
2130 return LY_EVALID;
2131 }
2132 } else if (type_p->fraction_digits) {
2133 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002134 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2136 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002137 tpdfname);
2138 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2140 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002141 free(*type);
2142 *type = NULL;
2143 }
2144 return LY_EVALID;
2145 }
2146 dec->fraction_digits = type_p->fraction_digits;
2147
2148 /* RFC 7950 9.2.4 - range */
2149 if (type_p->range) {
2150 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2151 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2152 LY_CHECK_RET(ret);
2153 if (!tpdfname) {
2154 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2155 options, u, lys_compile_ext, ret, done);
2156 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002157 }
2158
2159 if (tpdfname) {
2160 type_p->compiled = *type;
2161 *type = calloc(1, sizeof(struct lysc_type_dec));
2162 }
2163 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002164 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002165 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002166
2167 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002168 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002169 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002170 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2171 LY_CHECK_RET(ret);
2172 if (!tpdfname) {
2173 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2174 options, u, lys_compile_ext, ret, done);
2175 }
2176 } else if (base && ((struct lysc_type_str*)base)->length) {
2177 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2178 }
2179
2180 /* RFC 7950 9.4.5 - pattern */
2181 if (type_p->patterns) {
2182 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2183 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2184 LY_CHECK_RET(ret);
2185 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2186 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2187 }
2188
2189 if (tpdfname) {
2190 type_p->compiled = *type;
2191 *type = calloc(1, sizeof(struct lysc_type_str));
2192 }
2193 break;
2194 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002195 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002196
2197 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002198 if (type_p->enums) {
2199 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2200 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2201 LY_CHECK_RET(ret);
2202 }
2203
Radek Krejci555cb5b2018-11-16 14:54:33 +01002204 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002205 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002206 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002208 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002209 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002210 free(*type);
2211 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002212 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002213 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002214 }
2215
2216 if (tpdfname) {
2217 type_p->compiled = *type;
2218 *type = calloc(1, sizeof(struct lysc_type_enum));
2219 }
2220 break;
2221 case LY_TYPE_INT8:
2222 case LY_TYPE_UINT8:
2223 case LY_TYPE_INT16:
2224 case LY_TYPE_UINT16:
2225 case LY_TYPE_INT32:
2226 case LY_TYPE_UINT32:
2227 case LY_TYPE_INT64:
2228 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002229 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002230
2231 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002232 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002233 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002234 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2235 LY_CHECK_RET(ret);
2236 if (!tpdfname) {
2237 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2238 options, u, lys_compile_ext, ret, done);
2239 }
2240 }
2241
2242 if (tpdfname) {
2243 type_p->compiled = *type;
2244 *type = calloc(1, sizeof(struct lysc_type_num));
2245 }
2246 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002247 case LY_TYPE_IDENT:
2248 idref = (struct lysc_type_identityref*)(*type);
2249
2250 /* RFC 7950 9.10.2 - base */
2251 if (type_p->bases) {
2252 if (base) {
2253 /* only the directly derived identityrefs can contain base specification */
2254 if (tpdfname) {
2255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002256 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002257 tpdfname);
2258 } else {
2259 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002260 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002261 free(*type);
2262 *type = NULL;
2263 }
2264 return LY_EVALID;
2265 }
2266 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2267 LY_CHECK_RET(ret);
2268 }
2269
2270 if (!base && !type_p->flags) {
2271 /* type derived from identityref built-in type must contain at least one base */
2272 if (tpdfname) {
2273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2274 } else {
2275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2276 free(*type);
2277 *type = NULL;
2278 }
2279 return LY_EVALID;
2280 }
2281
2282 if (tpdfname) {
2283 type_p->compiled = *type;
2284 *type = calloc(1, sizeof(struct lysc_type_identityref));
2285 }
2286 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002287 case LY_TYPE_LEAFREF:
2288 /* RFC 7950 9.9.3 - require-instance */
2289 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002290 if (context_mod->version < LYS_VERSION_1_1) {
2291 if (tpdfname) {
2292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2293 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2294 } else {
2295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2296 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2297 free(*type);
2298 *type = NULL;
2299 }
2300 return LY_EVALID;
2301 }
Radek Krejcia3045382018-11-22 14:30:31 +01002302 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002303 } else if (base) {
2304 /* inherit */
2305 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002306 } else {
2307 /* default is true */
2308 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2309 }
2310 if (type_p->path) {
2311 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002312 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002313 } else if (base) {
2314 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002315 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002316 } else if (tpdfname) {
2317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2318 return LY_EVALID;
2319 } else {
2320 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2321 free(*type);
2322 *type = NULL;
2323 return LY_EVALID;
2324 }
2325 if (tpdfname) {
2326 type_p->compiled = *type;
2327 *type = calloc(1, sizeof(struct lysc_type_leafref));
2328 }
2329 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002330 case LY_TYPE_INST:
2331 /* RFC 7950 9.9.3 - require-instance */
2332 if (type_p->flags & LYS_SET_REQINST) {
2333 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2334 } else {
2335 /* default is true */
2336 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2337 }
2338
2339 if (tpdfname) {
2340 type_p->compiled = *type;
2341 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2342 }
2343 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002344 case LY_TYPE_UNION:
2345 un = (struct lysc_type_union*)(*type);
2346
2347 /* RFC 7950 7.4 - type */
2348 if (type_p->types) {
2349 if (base) {
2350 /* only the directly derived union can contain types specification */
2351 if (tpdfname) {
2352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2353 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2354 tpdfname);
2355 } else {
2356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2357 "Invalid type substatement for the type not directly derived from union built-in type.");
2358 free(*type);
2359 *type = NULL;
2360 }
2361 return LY_EVALID;
2362 }
2363 /* compile the type */
2364 additional = 0;
2365 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2366 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
2367 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);
2368 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2369 /* add space for additional types from the union subtype */
2370 un_aux = (struct lysc_type_union *)un->types[u + additional];
2371 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)));
2372 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2373 un->types = (void*)((uint32_t*)(p) + 1);
2374
2375 /* copy subtypes of the subtype union */
2376 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2377 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2378 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2379 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2380 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2381 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2382 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2383 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2384 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2385 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2386 /* TODO extensions */
2387
2388 } else {
2389 un->types[u + additional] = un_aux->types[v];
2390 ++un_aux->types[v]->refcount;
2391 }
2392 ++additional;
2393 LY_ARRAY_INCREMENT(un->types);
2394 }
2395 /* compensate u increment in main loop */
2396 --additional;
2397
2398 /* free the replaced union subtype */
2399 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2400 } else {
2401 LY_ARRAY_INCREMENT(un->types);
2402 }
2403 LY_CHECK_RET(ret);
2404 }
2405 }
2406
2407 if (!base && !type_p->flags) {
2408 /* type derived from union built-in type must contain at least one type */
2409 if (tpdfname) {
2410 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2411 } else {
2412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2413 free(*type);
2414 *type = NULL;
2415 }
2416 return LY_EVALID;
2417 }
2418
2419 if (tpdfname) {
2420 type_p->compiled = *type;
2421 *type = calloc(1, sizeof(struct lysc_type_union));
2422 }
2423 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002424 case LY_TYPE_BOOL:
2425 case LY_TYPE_EMPTY:
2426 case LY_TYPE_UNKNOWN: /* just to complete switch */
2427 break;
2428 }
2429 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2430done:
2431 return ret;
2432}
2433
Radek Krejcia3045382018-11-22 14:30:31 +01002434/**
2435 * @brief Compile information about the leaf/leaf-list's type.
2436 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002437 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2438 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2439 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2440 * @param[in] context_name Name of the context node or referencing typedef for logging.
2441 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002442 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2443 * @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 +01002444 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
2445 * @param[out] dflt Storage for inheriting default value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002446 * @return LY_ERR value.
2447 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002448static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002449lys_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,
2450 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt)
Radek Krejci19a96102018-11-15 13:38:09 +01002451{
2452 LY_ERR ret = LY_SUCCESS;
2453 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002454 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002455 struct type_context {
2456 const struct lysp_tpdf *tpdf;
2457 struct lysp_node *node;
2458 struct lysp_module *mod;
2459 } *tctx, *tctx_prev = NULL;
2460 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002461 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002462 struct ly_set tpdf_chain = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01002463
2464 (*type) = NULL;
2465
2466 tctx = calloc(1, sizeof *tctx);
2467 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002468 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002469 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2470 ret == LY_SUCCESS;
2471 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2472 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2473 if (basetype) {
2474 break;
2475 }
2476
2477 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002478 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2479 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002480 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2481
Radek Krejcicdfecd92018-11-26 11:27:32 +01002482 if (units && !*units) {
2483 /* inherit units */
2484 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2485 }
2486 if (dflt && !*dflt) {
2487 /* inherit default */
2488 DUP_STRING(ctx->ctx, tctx->tpdf->dflt, *dflt);
2489 }
2490 if (dummyloops && (!units || *units) && (!dflt || *dflt)) {
2491 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2492 break;
2493 }
2494
Radek Krejci19a96102018-11-15 13:38:09 +01002495 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002496 /* it is not necessary to continue, the rest of the chain was already compiled,
2497 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002498 basetype = tctx->tpdf->type.compiled->basetype;
2499 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002500 if ((units && !*units) || (dflt && !*dflt)) {
2501 dummyloops = 1;
2502 goto preparenext;
2503 } else {
2504 tctx = NULL;
2505 break;
2506 }
Radek Krejci19a96102018-11-15 13:38:09 +01002507 }
2508
2509 /* store information for the following processing */
2510 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2511
Radek Krejcicdfecd92018-11-26 11:27:32 +01002512preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002513 /* prepare next loop */
2514 tctx_prev = tctx;
2515 tctx = calloc(1, sizeof *tctx);
2516 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2517 }
2518 free(tctx);
2519
2520 /* allocate type according to the basetype */
2521 switch (basetype) {
2522 case LY_TYPE_BINARY:
2523 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002524 break;
2525 case LY_TYPE_BITS:
2526 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002527 break;
2528 case LY_TYPE_BOOL:
2529 case LY_TYPE_EMPTY:
2530 *type = calloc(1, sizeof(struct lysc_type));
2531 break;
2532 case LY_TYPE_DEC64:
2533 *type = calloc(1, sizeof(struct lysc_type_dec));
2534 break;
2535 case LY_TYPE_ENUM:
2536 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002537 break;
2538 case LY_TYPE_IDENT:
2539 *type = calloc(1, sizeof(struct lysc_type_identityref));
2540 break;
2541 case LY_TYPE_INST:
2542 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2543 break;
2544 case LY_TYPE_LEAFREF:
2545 *type = calloc(1, sizeof(struct lysc_type_leafref));
2546 break;
2547 case LY_TYPE_STRING:
2548 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002549 break;
2550 case LY_TYPE_UNION:
2551 *type = calloc(1, sizeof(struct lysc_type_union));
2552 break;
2553 case LY_TYPE_INT8:
2554 case LY_TYPE_UINT8:
2555 case LY_TYPE_INT16:
2556 case LY_TYPE_UINT16:
2557 case LY_TYPE_INT32:
2558 case LY_TYPE_UINT32:
2559 case LY_TYPE_INT64:
2560 case LY_TYPE_UINT64:
2561 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002562 break;
2563 case LY_TYPE_UNKNOWN:
2564 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2565 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2566 ret = LY_EVALID;
2567 goto cleanup;
2568 }
2569 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002570 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2572 ly_data_type2str[basetype]);
2573 free(*type);
2574 (*type) = NULL;
2575 ret = LY_EVALID;
2576 goto cleanup;
2577 }
2578
2579 /* get restrictions from the referred typedefs */
2580 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2581 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002582 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002583 base = tctx->tpdf->type.compiled;
2584 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002585 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002586 /* no change, just use the type information from the base */
2587 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2588 ++base->refcount;
2589 continue;
2590 }
2591
2592 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002593 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2595 tctx->tpdf->name, ly_data_type2str[basetype]);
2596 ret = LY_EVALID;
2597 goto cleanup;
2598 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2599 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2600 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2601 tctx->tpdf->name, tctx->tpdf->dflt);
2602 ret = LY_EVALID;
2603 goto cleanup;
2604 }
2605
Radek Krejci19a96102018-11-15 13:38:09 +01002606 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002607 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002608 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 +01002609 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2610 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002611 LY_CHECK_GOTO(ret, cleanup);
2612 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002613 }
2614
Radek Krejcic5c27e52018-11-15 14:38:11 +01002615 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002616 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002617 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002618 (*type)->basetype = basetype;
2619 ++(*type)->refcount;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002620 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 +01002621 LY_CHECK_GOTO(ret, cleanup);
2622 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002623 /* no specific restriction in leaf's type definition, copy from the base */
2624 free(*type);
2625 (*type) = base;
2626 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002627 }
2628
2629 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
2630
2631cleanup:
2632 ly_set_erase(&tpdf_chain, free);
2633 return ret;
2634}
2635
2636static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent);
2637
Radek Krejcia3045382018-11-22 14:30:31 +01002638/**
2639 * @brief Compile parsed container node information.
2640 * @param[in] ctx Compile context
2641 * @param[in] node_p Parsed container node.
2642 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2643 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2644 * is enriched with the container-specific information.
2645 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2646 */
Radek Krejci19a96102018-11-15 13:38:09 +01002647static LY_ERR
2648lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2649{
2650 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
2651 struct lysc_node_container *cont = (struct lysc_node_container*)node;
2652 struct lysp_node *child_p;
2653 unsigned int u;
2654 LY_ERR ret = LY_SUCCESS;
2655
2656 COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done);
2657 COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done);
2658
2659 LY_LIST_FOR(cont_p->child, child_p) {
2660 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2661 }
2662
2663 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2664 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2665 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2666
2667done:
2668 return ret;
2669}
2670
Radek Krejcia3045382018-11-22 14:30:31 +01002671/**
2672 * @brief Compile parsed leaf node information.
2673 * @param[in] ctx Compile context
2674 * @param[in] node_p Parsed leaf node.
2675 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2676 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2677 * is enriched with the leaf-specific information.
2678 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2679 */
Radek Krejci19a96102018-11-15 13:38:09 +01002680static LY_ERR
2681lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2682{
2683 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2684 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2685 unsigned int u;
2686 LY_ERR ret = LY_SUCCESS;
2687
2688 COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done);
2689 COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002690 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002691 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2692 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
Radek Krejci43699232018-11-23 14:59:46 +01002693
Radek Krejcicdfecd92018-11-26 11:27:32 +01002694 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 +01002695 leaf->units ? NULL : &leaf->units, leaf->dflt || (leaf->flags & LYS_MAND_TRUE) ? NULL : &leaf->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +01002696 LY_CHECK_GOTO(ret, done);
Radek Krejcia3045382018-11-22 14:30:31 +01002697 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2698 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2699 ly_set_add(&ctx->unres, leaf, 0);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002700 } else if (leaf->type->basetype == LY_TYPE_UNION) {
2701 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
2702 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2703 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2704 ly_set_add(&ctx->unres, leaf, 0);
2705 }
2706 }
Radek Krejci43699232018-11-23 14:59:46 +01002707 } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) {
2708 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2709 "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt);
2710 return LY_EVALID;
Radek Krejcia3045382018-11-22 14:30:31 +01002711 }
2712
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002713 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2714
Radek Krejci19a96102018-11-15 13:38:09 +01002715done:
2716 return ret;
2717}
2718
Radek Krejcia3045382018-11-22 14:30:31 +01002719/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01002720 * @brief Compile parsed leaf-list node information.
2721 * @param[in] ctx Compile context
2722 * @param[in] node_p Parsed leaf-list node.
2723 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2724 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2725 * is enriched with the leaf-list-specific information.
2726 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2727 */
2728static LY_ERR
2729lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2730{
2731 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
2732 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
2733 unsigned int u, v;
2734 const char *dflt = NULL;
2735 LY_ERR ret = LY_SUCCESS;
2736
2737 COMPILE_MEMBER_GOTO(ctx, llist_p->when, llist->when, options, lys_compile_when, ret, done);
2738 COMPILE_ARRAY_GOTO(ctx, llist_p->iffeatures, llist->iffeatures, options, u, lys_compile_iffeature, ret, done);
2739 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
2740 DUP_STRING(ctx->ctx, llist_p->units, llist->units);
2741
2742 if (llist_p->dflts) {
2743 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
2744 LY_ARRAY_FOR(llist_p->dflts, u) {
2745 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
2746 LY_ARRAY_INCREMENT(llist->dflts);
2747 }
2748 }
2749
2750 llist->min = llist_p->min;
Radek Krejcib7408632018-11-28 17:12:11 +01002751 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002752
2753 ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod->parsed, node_p->name, &llist_p->type, options, &llist->type,
2754 llist->units ? NULL : &llist->units, (llist->dflts || llist->min) ? NULL : &dflt);
2755 LY_CHECK_GOTO(ret, done);
2756 if (dflt) {
2757 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done);
2758 llist->dflts[0] = dflt;
2759 LY_ARRAY_INCREMENT(llist->dflts);
2760 }
2761
2762 if (llist->type->basetype == LY_TYPE_LEAFREF) {
2763 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2764 ly_set_add(&ctx->unres, llist, 0);
2765 } else if (llist->type->basetype == LY_TYPE_UNION) {
2766 LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) {
2767 if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2768 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2769 ly_set_add(&ctx->unres, llist, 0);
2770 }
2771 }
Radek Krejci6bb080b2018-11-28 17:23:41 +01002772 } else if (llist->type->basetype == LY_TYPE_EMPTY) {
2773 if (ctx->mod->compiled->version < LYS_VERSION_1_1) {
2774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2775 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2776 return LY_EVALID;
2777 } else if (llist_p->dflts) {
2778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2779 "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]);
2780 return LY_EVALID;
2781 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002782 }
2783
2784 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
2785 /* configuration data values must be unique - so check the default values */
2786 LY_ARRAY_FOR(llist->dflts, u) {
2787 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
2788 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2790 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
2791 return LY_EVALID;
2792 }
2793 }
2794 }
2795 }
2796
2797 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2798
2799done:
2800 return ret;
2801}
2802
2803/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002804 * @brief Compile parsed list node information.
2805 * @param[in] ctx Compile context
2806 * @param[in] node_p Parsed list node.
2807 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2808 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2809 * is enriched with the list-specific information.
2810 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2811 */
2812static LY_ERR
2813lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2814{
2815 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
2816 struct lysc_node_list *list = (struct lysc_node_list*)node;
2817 struct lysp_node *child_p;
2818 struct lysc_node_leaf **key, ***unique;
2819 size_t len;
2820 unsigned int u, v;
2821 const char *keystr, *delim;
2822 int config;
2823 LY_ERR ret = LY_SUCCESS;
2824
2825 COMPILE_MEMBER_GOTO(ctx, list_p->when, list->when, options, lys_compile_when, ret, done);
2826 COMPILE_ARRAY_GOTO(ctx, list_p->iffeatures, list->iffeatures, options, u, lys_compile_iffeature, ret, done);
2827 list->min = list_p->min;
2828 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2829
2830 LY_LIST_FOR(list_p->child, child_p) {
2831 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2832 }
2833
2834 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
2835
2836 /* keys */
2837 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
2838 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
2839 return LY_EVALID;
2840 }
2841
2842 /* find all the keys (must be direct children) */
2843 keystr = list_p->key;
2844 while (keystr) {
2845 delim = strpbrk(keystr, " \t\n");
2846 if (delim) {
2847 len = delim - keystr;
2848 while (isspace(*delim)) {
2849 ++delim;
2850 }
2851 } else {
2852 len = strlen(keystr);
2853 }
2854
2855 /* key node must be present */
2856 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
2857 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
2858 if (!(*key)) {
2859 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2860 "The list's key \"%.*s\" not found.", len, keystr);
2861 return LY_EVALID;
2862 }
2863 /* keys must be unique */
2864 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
2865 if (*key == list->keys[u]) {
2866 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2867 "Duplicated key identifier \"%.*s\".", len, keystr);
2868 return LY_EVALID;
2869 }
2870 }
2871 /* key must have the same config flag as the list itself */
2872 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
2873 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
2874 return LY_EVALID;
2875 }
2876 if (ctx->mod->compiled->version < LYS_VERSION_1_1) {
2877 /* YANG 1.0 denies key to be of empty type */
2878 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
2879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2880 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
2881 return LY_EVALID;
2882 }
2883 } else {
2884 /* when and if-feature are illegal on list keys */
2885 if ((*key)->when) {
2886 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2887 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
2888 return LY_EVALID;
2889 }
2890 if ((*key)->iffeatures) {
2891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2892 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
2893 return LY_EVALID;
2894 }
2895 }
2896 /* ignore default values of the key */
2897 if ((*key)->dflt) {
2898 lydict_remove(ctx->ctx, (*key)->dflt);
2899 (*key)->dflt = NULL;
2900 }
2901 /* mark leaf as key */
2902 (*key)->flags |= LYS_KEY;
2903
2904 /* next key value */
2905 keystr = delim;
2906 }
2907
2908 /* uniques */
2909 if (list_p->uniques) {
2910 for (v = 0; v < LY_ARRAY_SIZE(list_p->uniques); ++v) {
2911 config = -1;
2912 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2913 keystr = list_p->uniques[v];
2914 while (keystr) {
2915 delim = strpbrk(keystr, " \t\n");
2916 if (delim) {
2917 len = delim - keystr;
2918 while (isspace(*delim)) {
2919 ++delim;
2920 }
2921 } else {
2922 len = strlen(keystr);
2923 }
2924
2925 /* unique node must be present */
2926 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
2927 ret = lys_resolve_descendant_schema_nodeid(ctx, keystr, len, node, LYS_LEAF, (const struct lysc_node**)key);
2928 if (ret != LY_SUCCESS) {
2929 if (ret == LY_EDENIED) {
2930 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2931 "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.",
2932 len, keystr, lys_nodetype2str((*key)->nodetype));
2933 }
2934 return LY_EVALID;
2935 }
2936
2937 /* all referenced leafs must be of the same config type */
2938 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
2939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2940 "Unique statement \"%s\" refers to leafs with different config type.", list_p->uniques[v]);
2941 return LY_EVALID;
2942 } else if ((*key)->flags & LYS_CONFIG_W) {
2943 config = 1;
2944 } else { /* LYS_CONFIG_R */
2945 config = 0;
2946 }
2947
2948 /* mark leaf as unique */
2949 (*key)->flags |= LYS_UNIQUE;
2950
2951 /* next unique value in line */
2952 keystr = delim;
2953 }
2954 /* next unique definition */
2955 }
2956 }
2957
2958 //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done);
2959 //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done);
2960
2961done:
2962 return ret;
2963}
2964
2965/**
Radek Krejcia3045382018-11-22 14:30:31 +01002966 * @brief Compile parsed schema node information.
2967 * @param[in] ctx Compile context
2968 * @param[in] node_p Parsed schema node.
2969 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2970 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
2971 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
2972 * the compile context.
2973 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2974 */
Radek Krejci19a96102018-11-15 13:38:09 +01002975static LY_ERR
2976lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
2977{
2978 LY_ERR ret = LY_EVALID;
2979 struct lysc_node *node, **children;
2980 unsigned int u;
2981 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
2982
2983 switch (node_p->nodetype) {
2984 case LYS_CONTAINER:
2985 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
2986 node_compile_spec = lys_compile_node_container;
2987 break;
2988 case LYS_LEAF:
2989 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
2990 node_compile_spec = lys_compile_node_leaf;
2991 break;
2992 case LYS_LIST:
2993 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002994 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01002995 break;
2996 case LYS_LEAFLIST:
2997 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01002998 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01002999 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003000 case LYS_CHOICE:
3001 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
3002 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003003 case LYS_ANYXML:
3004 case LYS_ANYDATA:
3005 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
3006 break;
3007 default:
3008 LOGINT(ctx->ctx);
3009 return LY_EINT;
3010 }
3011 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3012 node->nodetype = node_p->nodetype;
3013 node->module = ctx->mod;
3014 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003015 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01003016
3017 /* config */
3018 if (!(node->flags & LYS_CONFIG_MASK)) {
3019 /* config not explicitely set, inherit it from parent */
3020 if (parent) {
3021 node->flags |= parent->flags & LYS_CONFIG_MASK;
3022 } else {
3023 /* default is config true */
3024 node->flags |= LYS_CONFIG_W;
3025 }
3026 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003027 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3028 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3029 "Configuration node cannot be child of any state data node.");
3030 goto error;
3031 }
Radek Krejci19a96102018-11-15 13:38:09 +01003032
Radek Krejcia6d57732018-11-29 13:40:37 +01003033 /* *list ordering */
3034 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3035 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3036 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
3037 "RPC/action output parameters or notification content (%s).", ctx->path);
3038 node->flags &= ~LYS_ORDBY_MASK;
3039 node->flags |= LYS_ORDBY_SYSTEM;
3040 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3041 /* default ordering is system */
3042 node->flags |= LYS_ORDBY_SYSTEM;
3043 }
3044 }
3045
Radek Krejci19a96102018-11-15 13:38:09 +01003046 /* status - it is not inherited by specification, but it does not make sense to have
3047 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3048 if (!(node->flags & LYS_STATUS_MASK)) {
3049 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
3050 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3051 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3052 node->flags |= parent->flags & LYS_STATUS_MASK;
3053 } else {
3054 node->flags |= LYS_STATUS_CURR;
3055 }
3056 } else if (parent) {
3057 /* check status compatibility with the parent */
3058 if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
3059 if (node->flags & LYS_STATUS_CURR) {
3060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3061 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3062 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3063 } else { /* LYS_STATUS_DEPRC */
3064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3065 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3066 }
3067 goto error;
3068 }
3069 }
3070
3071 if (!(options & LYSC_OPT_FREE_SP)) {
3072 node->sp = node_p;
3073 }
3074 DUP_STRING(ctx->ctx, node_p->name, node->name);
3075 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
3076
3077 /* nodetype-specific part */
3078 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
3079
3080 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01003081 if (parent) {
3082 if (parent->nodetype == LYS_CHOICE) {
3083 /* TODO exception for cases */
3084 } else if ((children = lysc_node_children(parent))) {
3085 if (!(*children)) {
3086 /* first child */
3087 *children = node;
3088 } else {
3089 /* insert at the end of the parent's children list */
3090 (*children)->prev->next = node;
3091 node->prev = (*children)->prev;
3092 (*children)->prev = node;
3093 }
Radek Krejci19a96102018-11-15 13:38:09 +01003094 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01003095 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01003096 } else {
3097 /* top-level element */
3098 if (!ctx->mod->compiled->data) {
3099 ctx->mod->compiled->data = node;
3100 } else {
3101 /* insert at the end of the module's top-level nodes list */
3102 ctx->mod->compiled->data->prev->next = node;
3103 node->prev = ctx->mod->compiled->data->prev;
3104 ctx->mod->compiled->data->prev = node;
3105 }
3106 }
3107
3108 return LY_SUCCESS;
3109
3110error:
3111 lysc_node_free(ctx->ctx, node);
3112 return ret;
3113}
3114
Radek Krejcia3045382018-11-22 14:30:31 +01003115/**
3116 * @brief Compile the given YANG module.
3117 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
3118 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3119 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3120 */
Radek Krejci19a96102018-11-15 13:38:09 +01003121LY_ERR
3122lys_compile(struct lys_module *mod, int options)
3123{
3124 struct lysc_ctx ctx = {0};
3125 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003126 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01003127 struct lysp_module *sp;
3128 struct lysp_node *node_p;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003129 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01003130 LY_ERR ret;
3131
3132 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
3133 sp = mod->parsed;
3134
3135 if (sp->submodule) {
3136 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
3137 return LY_EINVAL;
3138 }
3139
3140 ctx.ctx = sp->ctx;
3141 ctx.mod = mod;
3142
3143 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
3144 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
3145 mod_c->ctx = sp->ctx;
3146 mod_c->implemented = sp->implemented;
3147 mod_c->latest_revision = sp->latest_revision;
3148 mod_c->version = sp->version;
3149
3150 DUP_STRING(sp->ctx, sp->name, mod_c->name);
3151 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
3152 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
3153 if (sp->revs) {
3154 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
3155 }
3156 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
3157 COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
3158 COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
3159 if (sp->identities) {
3160 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
3161 }
3162
3163 LY_LIST_FOR(sp->data, node_p) {
3164 ret = lys_compile_node(&ctx, node_p, options, NULL);
3165 LY_CHECK_GOTO(ret, error);
3166 }
3167 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
3168 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
3169
3170 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
3171
Radek Krejcia3045382018-11-22 14:30:31 +01003172 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01003173 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
3174 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
3175 * 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 +01003176 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003177 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01003178 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
3179 if (type->basetype == LY_TYPE_LEAFREF) {
3180 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01003181 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 +01003182 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003183 } else if (type->basetype == LY_TYPE_UNION) {
3184 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3185 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3186 /* validate the path */
3187 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
3188 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
3189 LY_CHECK_GOTO(ret, error);
3190 }
3191 }
Radek Krejcia3045382018-11-22 14:30:31 +01003192 }
3193 }
3194 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003195 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003196 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01003197 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
3198 if (type->basetype == LY_TYPE_LEAFREF) {
3199 /* store pointer to the real type */
3200 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
3201 typeiter->basetype == LY_TYPE_LEAFREF;
3202 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3203 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003204 } else if (type->basetype == LY_TYPE_UNION) {
3205 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3206 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3207 /* store pointer to the real type */
3208 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
3209 typeiter->basetype == LY_TYPE_LEAFREF;
3210 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3211 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
3212 }
3213 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003214 }
3215 }
3216 }
Radek Krejcia3045382018-11-22 14:30:31 +01003217 ly_set_erase(&ctx.unres, NULL);
3218
Radek Krejci19a96102018-11-15 13:38:09 +01003219 if (options & LYSC_OPT_FREE_SP) {
3220 lysp_module_free(mod->parsed);
3221 ((struct lys_module*)mod)->parsed = NULL;
3222 }
3223
3224 ((struct lys_module*)mod)->compiled = mod_c;
3225 return LY_SUCCESS;
3226
3227error:
Radek Krejcia3045382018-11-22 14:30:31 +01003228 ly_set_erase(&ctx.unres, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01003229 lysc_module_free(mod_c, NULL);
3230 ((struct lys_module*)mod)->compiled = NULL;
3231 return ret;
3232}