blob: 21aac0769e5ae8ad54bc5c2f03c05f2b31f23e89 [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); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010036 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010037 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
38 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010039 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER + __array_offset]); \
40 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
41 } \
42 }
43
44#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \
45 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
47 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
48 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
50 RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
55#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \
56 if (MEMBER_P) { \
57 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
58 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
59 RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \
60 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
61 }
62
Radek Krejcid05cbd92018-12-05 14:26:40 +010063#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
64 if (ARRAY) { \
65 for (unsigned int u = 0; u < LY_ARRAY_SIZE(ARRAY); ++u) { \
66 if (&(ARRAY)[u] != EXCL && (void*)((ARRAY)[u].MEMBER) == (void*)(IDENT)) { \
67 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
68 return LY_EVALID; \
69 } \
70 } \
71 }
72
Radek Krejci19a96102018-11-15 13:38:09 +010073static struct lysc_ext_instance *
74lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
75{
76 /* TODO */
77 (void) ctx;
78 (void) orig;
79 return NULL;
80}
81
82static struct lysc_pattern*
83lysc_pattern_dup(struct lysc_pattern *orig)
84{
85 ++orig->refcount;
86 return orig;
87}
88
89static struct lysc_pattern**
90lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
91{
Radek Krejcid05cbd92018-12-05 14:26:40 +010092 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +010093 unsigned int u;
94
95 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
96 LY_ARRAY_FOR(orig, u) {
97 dup[u] = lysc_pattern_dup(orig[u]);
98 LY_ARRAY_INCREMENT(dup);
99 }
100 return dup;
101}
102
103struct lysc_range*
104lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
105{
106 struct lysc_range *dup;
107 LY_ERR ret;
108
109 dup = calloc(1, sizeof *dup);
110 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
111 if (orig->parts) {
112 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
113 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
114 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
115 }
116 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
117 DUP_STRING(ctx, orig->emsg, dup->emsg);
118 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
119
120 return dup;
121cleanup:
122 free(dup);
123 (void) ret; /* set but not used due to the return type */
124 return NULL;
125}
126
127struct iff_stack {
128 int size;
129 int index; /* first empty item */
130 uint8_t *stack;
131};
132
133static LY_ERR
134iff_stack_push(struct iff_stack *stack, uint8_t value)
135{
136 if (stack->index == stack->size) {
137 stack->size += 4;
138 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
139 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
140 }
141 stack->stack[stack->index++] = value;
142 return LY_SUCCESS;
143}
144
145static uint8_t
146iff_stack_pop(struct iff_stack *stack)
147{
148 stack->index--;
149 return stack->stack[stack->index];
150}
151
152static void
153iff_stack_clean(struct iff_stack *stack)
154{
155 stack->size = 0;
156 free(stack->stack);
157}
158
159static void
160iff_setop(uint8_t *list, uint8_t op, int pos)
161{
162 uint8_t *item;
163 uint8_t mask = 3;
164
165 assert(pos >= 0);
166 assert(op <= 3); /* max 2 bits */
167
168 item = &list[pos / 4];
169 mask = mask << 2 * (pos % 4);
170 *item = (*item) & ~mask;
171 *item = (*item) | (op << 2 * (pos % 4));
172}
173
174#define LYS_IFF_LP 0x04 /* ( */
175#define LYS_IFF_RP 0x08 /* ) */
176
177static struct lysc_feature *
178lysc_feature_find(struct lysc_module *mod, const char *name, size_t len)
179{
180 size_t i;
181 struct lysc_feature *f;
182
183 for (i = 0; i < len; ++i) {
184 if (name[i] == ':') {
185 /* we have a prefixed feature */
186 mod = lysc_module_find_prefix(mod, name, i);
187 LY_CHECK_RET(!mod, NULL);
188
189 name = &name[i + 1];
190 len = len - i - 1;
191 }
192 }
193
194 /* we have the correct module, get the feature */
195 LY_ARRAY_FOR(mod->features, i) {
196 f = &mod->features[i];
197 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
198 return f;
199 }
200 }
201
202 return NULL;
203}
204
205static LY_ERR
206lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext)
207{
208 const char *name;
209 unsigned int u;
210 const struct lys_module *mod;
211 struct lysp_ext *edef = NULL;
212
213 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
214 ext->insubstmt = ext_p->insubstmt;
215 ext->insubstmt_index = ext_p->insubstmt_index;
216
217 /* get module where the extension definition should be placed */
218 for (u = 0; ext_p->name[u] != ':'; ++u);
219 mod = lys_module_find_prefix(ctx->mod, ext_p->name, u);
220 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
221 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
222 LY_EVALID);
223 LY_CHECK_ERR_RET(!mod->parsed->extensions,
224 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
225 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
226 ext_p->name, mod->parsed->name),
227 LY_EVALID);
228 name = &ext_p->name[u + 1];
229 /* find the extension definition there */
230 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
231 if (!strcmp(name, mod->parsed->extensions[u].name)) {
232 edef = &mod->parsed->extensions[u];
233 break;
234 }
235 }
236 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
237 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
238 LY_EVALID);
239 /* TODO plugins */
240
241 return LY_SUCCESS;
242}
243
244static LY_ERR
245lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff)
246{
247 const char *c = *value;
248 int r, rc = EXIT_FAILURE;
249 int i, j, last_not, checkversion = 0;
250 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
251 uint8_t op;
252 struct iff_stack stack = {0, 0, NULL};
253 struct lysc_feature *f;
254
255 assert(c);
256
257 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
258 for (i = j = last_not = 0; c[i]; i++) {
259 if (c[i] == '(') {
260 j++;
261 checkversion = 1;
262 continue;
263 } else if (c[i] == ')') {
264 j--;
265 continue;
266 } else if (isspace(c[i])) {
267 checkversion = 1;
268 continue;
269 }
270
271 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
272 if (c[i + r] == '\0') {
273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
274 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
275 return LY_EVALID;
276 } else if (!isspace(c[i + r])) {
277 /* feature name starting with the not/and/or */
278 last_not = 0;
279 f_size++;
280 } else if (c[i] == 'n') { /* not operation */
281 if (last_not) {
282 /* double not */
283 expr_size = expr_size - 2;
284 last_not = 0;
285 } else {
286 last_not = 1;
287 }
288 } else { /* and, or */
289 f_exp++;
290 /* not a not operation */
291 last_not = 0;
292 }
293 i += r;
294 } else {
295 f_size++;
296 last_not = 0;
297 }
298 expr_size++;
299
300 while (!isspace(c[i])) {
301 if (!c[i] || c[i] == ')') {
302 i--;
303 break;
304 }
305 i++;
306 }
307 }
308 if (j || f_exp != f_size) {
309 /* not matching count of ( and ) */
310 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
311 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
312 return LY_EVALID;
313 }
314
315 if (checkversion || expr_size > 1) {
316 /* check that we have 1.1 module */
317 if (ctx->mod->compiled->version != LYS_VERSION_1_1) {
318 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
319 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
320 return LY_EVALID;
321 }
322 }
323
324 /* allocate the memory */
325 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
326 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
327 stack.stack = malloc(expr_size * sizeof *stack.stack);
328 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
329
330 stack.size = expr_size;
331 f_size--; expr_size--; /* used as indexes from now */
332
333 for (i--; i >= 0; i--) {
334 if (c[i] == ')') {
335 /* push it on stack */
336 iff_stack_push(&stack, LYS_IFF_RP);
337 continue;
338 } else if (c[i] == '(') {
339 /* pop from the stack into result all operators until ) */
340 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
341 iff_setop(iff->expr, op, expr_size--);
342 }
343 continue;
344 } else if (isspace(c[i])) {
345 continue;
346 }
347
348 /* end of operator or operand -> find beginning and get what is it */
349 j = i + 1;
350 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
351 i--;
352 }
353 i++; /* go back by one step */
354
355 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
356 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
357 /* double not */
358 iff_stack_pop(&stack);
359 } else {
360 /* not has the highest priority, so do not pop from the stack
361 * as in case of AND and OR */
362 iff_stack_push(&stack, LYS_IFF_NOT);
363 }
364 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
365 /* as for OR - pop from the stack all operators with the same or higher
366 * priority and store them to the result, then push the AND to the stack */
367 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
368 op = iff_stack_pop(&stack);
369 iff_setop(iff->expr, op, expr_size--);
370 }
371 iff_stack_push(&stack, LYS_IFF_AND);
372 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
373 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
374 op = iff_stack_pop(&stack);
375 iff_setop(iff->expr, op, expr_size--);
376 }
377 iff_stack_push(&stack, LYS_IFF_OR);
378 } else {
379 /* feature name, length is j - i */
380
381 /* add it to the expression */
382 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
383
384 /* now get the link to the feature definition */
385 f = lysc_feature_find(ctx->mod->compiled, &c[i], j - i);
386 LY_CHECK_ERR_GOTO(!f,
387 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
388 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
389 rc = LY_EVALID,
390 error)
391 iff->features[f_size] = f;
392 LY_ARRAY_INCREMENT(iff->features);
393 f_size--;
394 }
395 }
396 while (stack.index) {
397 op = iff_stack_pop(&stack);
398 iff_setop(iff->expr, op, expr_size--);
399 }
400
401 if (++expr_size || ++f_size) {
402 /* not all expected operators and operands found */
403 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
404 "Invalid value \"%s\" of if-feature - processing error.", *value);
405 rc = LY_EINT;
406 } else {
407 rc = LY_SUCCESS;
408 }
409
410error:
411 /* cleanup */
412 iff_stack_clean(&stack);
413
414 return rc;
415}
416
417static LY_ERR
418lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when)
419{
420 unsigned int u;
421 LY_ERR ret = LY_SUCCESS;
422
423 when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
424 LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done);
425 COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done);
426
427done:
428 return ret;
429}
430
431static LY_ERR
432lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
433{
434 unsigned int u;
435 LY_ERR ret = LY_SUCCESS;
436
437 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
438 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
439
440 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
441 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
442 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
443
444done:
445 return ret;
446}
447
448static LY_ERR
449lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
450{
451 unsigned int u;
452 struct lys_module *mod = NULL;
453 struct lysc_module *comp;
454 LY_ERR ret = LY_SUCCESS;
455
456 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
457 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
458 imp->module = imp_p->module;
459
Radek Krejci7f2a5362018-11-28 13:05:37 +0100460 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
461 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100462 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100463 if (!imp->module->parsed) {
464 comp = imp->module->compiled;
465 /* try to get filepath from the compiled version */
466 if (comp->filepath) {
467 mod = (struct lys_module*)lys_parse_path(ctx->ctx, comp->filepath,
468 !strcmp(&comp->filepath[strlen(comp->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
469 if (mod != imp->module) {
470 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
471 comp->filepath, comp->name);
472 mod = NULL;
473 }
474 }
475 if (!mod) {
476 if (lysp_load_module(ctx->ctx, comp->name, comp->revision, 0, 1, &mod)) {
477 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
478 comp->name, ctx->mod->compiled->name);
479 return LY_ENOTFOUND;
480 }
481 }
Radek Krejci19a96102018-11-15 13:38:09 +0100482 }
483
484done:
485 return ret;
486}
487
488static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100489lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100490{
491 unsigned int u;
492 LY_ERR ret = LY_SUCCESS;
493
Radek Krejcid05cbd92018-12-05 14:26:40 +0100494 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100495 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
496 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
497 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
498 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
499 ident->flags = ident_p->flags;
500
501done:
502 return ret;
503}
504
Radek Krejcia3045382018-11-22 14:30:31 +0100505/**
506 * @brief Find and process the referenced base identities from another identity or identityref
507 *
508 * For bases in identity se backlinks to them from the base identities. For identityref, store
509 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
510 * to distinguish these two use cases.
511 *
512 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
513 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
514 * @param[in] ident Referencing identity to work with.
515 * @param[in] bases Array of bases of identityref to fill in.
516 * @return LY_ERR value.
517 */
Radek Krejci19a96102018-11-15 13:38:09 +0100518static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100519lys_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 +0100520{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100521 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100522 const char *s, *name;
523 struct lysc_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100524 struct lysc_ident **idref;
525
526 assert(ident || bases);
527
528 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) {
529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
530 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
531 return LY_EVALID;
532 }
533
534 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
535 s = strchr(bases_p[u], ':');
536 if (s) {
537 /* prefixed identity */
538 name = &s[1];
539 mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]);
540 } else {
541 name = bases_p[u];
542 mod = ctx->mod->compiled;
543 }
544 if (!mod) {
545 if (ident) {
546 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
547 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
548 } else {
549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
550 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
551 }
552 return LY_EVALID;
553 }
554 idref = NULL;
555 if (mod->identities) {
556 for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) {
557 if (!strcmp(name, mod->identities[v].name)) {
558 if (ident) {
559 /* we have match! store the backlink */
560 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
561 *idref = ident;
562 } else {
563 /* we have match! store the found identity */
564 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
565 *idref = &mod->identities[v];
566 }
567 break;
568 }
569 }
570 }
571 if (!idref || !(*idref)) {
572 if (ident) {
573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
574 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
575 } else {
576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
577 "Unable to find base (%s) of identityref.", bases_p[u]);
578 }
579 return LY_EVALID;
580 }
581 }
582 return LY_SUCCESS;
583}
584
Radek Krejcia3045382018-11-22 14:30:31 +0100585/**
586 * @brief For the given array of identities, set the backlinks from all their base identities.
587 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
588 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
589 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
590 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
591 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100592static LY_ERR
593lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
594{
595 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100596
597 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
598 if (!idents_p[i].bases) {
599 continue;
600 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100601 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100602 }
603 return LY_SUCCESS;
604}
605
Radek Krejcia3045382018-11-22 14:30:31 +0100606/**
607 * @brief Create compiled feature structure.
608 * @param[in] ctx Compile context.
609 * @param[in] feature_p Parsed feature definition to compile.
610 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejcid05cbd92018-12-05 14:26:40 +0100611 * @param[in] features List of already compiled features to check name duplicity.
Radek Krejcia3045382018-11-22 14:30:31 +0100612 * @param[in,out] feature Compiled feature structure to fill.
613 * @return LY_ERR value.
614 */
Radek Krejci19a96102018-11-15 13:38:09 +0100615static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100616lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *features, struct lysc_feature *feature)
Radek Krejci19a96102018-11-15 13:38:09 +0100617{
618 unsigned int u, v;
619 LY_ERR ret = LY_SUCCESS;
620 struct lysc_feature **df;
621
Radek Krejcid05cbd92018-12-05 14:26:40 +0100622 COMPILE_CHECK_UNIQUENESS(ctx, features, name, feature, "feature", feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100623 DUP_STRING(ctx->ctx, feature_p->name, feature->name);
624 feature->flags = feature_p->flags;
625
626 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
627 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
628 if (feature->iffeatures) {
629 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
630 if (feature->iffeatures[u].features) {
631 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
632 /* add itself into the dependants list */
633 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
634 *df = feature;
635 }
636 /* TODO check for circular dependency */
637 }
638 }
639 }
640done:
641 return ret;
642}
643
Radek Krejcia3045382018-11-22 14:30:31 +0100644/**
645 * @brief Validate and normalize numeric value from a range definition.
646 * @param[in] ctx Compile context.
647 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
648 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
649 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
650 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
651 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
652 * @param[in] value String value of the range boundary.
653 * @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.
654 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
655 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
656 */
Radek Krejci19a96102018-11-15 13:38:09 +0100657static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100658range_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 +0100659{
Radek Krejci6cba4292018-11-15 17:33:29 +0100660 size_t fraction = 0, size;
661
Radek Krejci19a96102018-11-15 13:38:09 +0100662 *len = 0;
663
664 assert(value);
665 /* parse value */
666 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
667 return LY_EVALID;
668 }
669
670 if ((value[*len] == '-') || (value[*len] == '+')) {
671 ++(*len);
672 }
673
674 while (isdigit(value[*len])) {
675 ++(*len);
676 }
677
678 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100679 if (basetype == LY_TYPE_DEC64) {
680 goto decimal;
681 } else {
682 *valcopy = strndup(value, *len);
683 return LY_SUCCESS;
684 }
Radek Krejci19a96102018-11-15 13:38:09 +0100685 }
686 fraction = *len;
687
688 ++(*len);
689 while (isdigit(value[*len])) {
690 ++(*len);
691 }
692
Radek Krejci6cba4292018-11-15 17:33:29 +0100693 if (basetype == LY_TYPE_DEC64) {
694decimal:
695 assert(frdigits);
696 if (*len - 1 - fraction > frdigits) {
697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
698 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
699 *len, value, frdigits);
700 return LY_EINVAL;
701 }
702 if (fraction) {
703 size = (*len) + (frdigits - ((*len) - 1 - fraction));
704 } else {
705 size = (*len) + frdigits + 1;
706 }
707 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100708 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
709
Radek Krejci6cba4292018-11-15 17:33:29 +0100710 (*valcopy)[size - 1] = '\0';
711 if (fraction) {
712 memcpy(&(*valcopy)[0], &value[0], fraction);
713 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
714 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
715 } else {
716 memcpy(&(*valcopy)[0], &value[0], *len);
717 memset(&(*valcopy)[*len], '0', frdigits);
718 }
Radek Krejci19a96102018-11-15 13:38:09 +0100719 }
720 return LY_SUCCESS;
721}
722
Radek Krejcia3045382018-11-22 14:30:31 +0100723/**
724 * @brief Check that values in range are in ascendant order.
725 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +0100726 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
727 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +0100728 * @param[in] value Current value to check.
729 * @param[in] prev_value The last seen value.
730 * @return LY_SUCCESS or LY_EEXIST for invalid order.
731 */
Radek Krejci19a96102018-11-15 13:38:09 +0100732static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +0100733range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +0100734{
735 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +0100736 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100737 return LY_EEXIST;
738 }
739 } else {
Radek Krejci5969f272018-11-23 10:03:58 +0100740 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100741 return LY_EEXIST;
742 }
743 }
744 return LY_SUCCESS;
745}
746
Radek Krejcia3045382018-11-22 14:30:31 +0100747/**
748 * @brief Set min/max value of the range part.
749 * @param[in] ctx Compile context.
750 * @param[in] part Range part structure to fill.
751 * @param[in] max Flag to distinguish if storing min or max value.
752 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
753 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
754 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
755 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
756 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +0100757 * @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 +0100758 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
759 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
760 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
761 * frdigits value), LY_EMEM.
762 */
Radek Krejci19a96102018-11-15 13:38:09 +0100763static LY_ERR
764range_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 +0100765 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100766{
767 LY_ERR ret = LY_SUCCESS;
768 char *valcopy = NULL;
769 size_t len;
770
771 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100772 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +0100773 LY_CHECK_GOTO(ret, finalize);
774 }
775 if (!valcopy && base_range) {
776 if (max) {
777 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
778 } else {
779 part->min_64 = base_range->parts[0].min_64;
780 }
781 if (!first) {
782 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
783 }
784 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +0100785 }
786
787 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100788 case LY_TYPE_INT8: /* range */
789 if (valcopy) {
790 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
791 } else if (max) {
792 part->max_64 = INT64_C(127);
793 } else {
794 part->min_64 = INT64_C(-128);
795 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100796 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100797 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100798 }
799 break;
800 case LY_TYPE_INT16: /* range */
801 if (valcopy) {
802 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
803 } else if (max) {
804 part->max_64 = INT64_C(32767);
805 } else {
806 part->min_64 = INT64_C(-32768);
807 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100808 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100809 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100810 }
811 break;
812 case LY_TYPE_INT32: /* range */
813 if (valcopy) {
814 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
815 } else if (max) {
816 part->max_64 = INT64_C(2147483647);
817 } else {
818 part->min_64 = INT64_C(-2147483648);
819 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100820 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100821 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100822 }
823 break;
824 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +0100825 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100826 if (valcopy) {
827 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
828 max ? &part->max_64 : &part->min_64);
829 } else if (max) {
830 part->max_64 = INT64_C(9223372036854775807);
831 } else {
832 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
833 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100834 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100835 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100836 }
837 break;
838 case LY_TYPE_UINT8: /* range */
839 if (valcopy) {
840 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
841 } else if (max) {
842 part->max_u64 = UINT64_C(255);
843 } else {
844 part->min_u64 = UINT64_C(0);
845 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100846 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100847 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100848 }
849 break;
850 case LY_TYPE_UINT16: /* range */
851 if (valcopy) {
852 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
853 } else if (max) {
854 part->max_u64 = UINT64_C(65535);
855 } else {
856 part->min_u64 = UINT64_C(0);
857 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100858 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100859 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100860 }
861 break;
862 case LY_TYPE_UINT32: /* range */
863 if (valcopy) {
864 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
865 } else if (max) {
866 part->max_u64 = UINT64_C(4294967295);
867 } else {
868 part->min_u64 = UINT64_C(0);
869 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100870 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100871 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100872 }
873 break;
874 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100875 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +0100876 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +0100877 if (valcopy) {
878 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
879 } else if (max) {
880 part->max_u64 = UINT64_C(18446744073709551615);
881 } else {
882 part->min_u64 = UINT64_C(0);
883 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100884 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100885 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100886 }
887 break;
888 default:
889 LOGINT(ctx->ctx);
890 ret = LY_EINT;
891 }
892
Radek Krejci5969f272018-11-23 10:03:58 +0100893finalize:
Radek Krejci19a96102018-11-15 13:38:09 +0100894 if (ret == LY_EDENIED) {
895 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
896 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
897 length_restr ? "length" : "range", valcopy ? valcopy : *value);
898 } else if (ret == LY_EVALID) {
899 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
900 "Invalid %s restriction - invalid value \"%s\".",
901 length_restr ? "length" : "range", valcopy ? valcopy : *value);
902 } else if (ret == LY_EEXIST) {
903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
904 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100905 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +0100906 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +0100907 } else if (!ret && value) {
908 *value = *value + len;
909 }
910 free(valcopy);
911 return ret;
912}
913
Radek Krejcia3045382018-11-22 14:30:31 +0100914/**
915 * @brief Compile the parsed range restriction.
916 * @param[in] ctx Compile context.
917 * @param[in] range_p Parsed range structure to compile.
918 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
919 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
920 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
921 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
922 * range restriction must be more restrictive than the base_range.
923 * @param[in,out] range Pointer to the created current range structure.
924 * @return LY_ERR value.
925 */
Radek Krejci19a96102018-11-15 13:38:09 +0100926static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100927lys_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 +0100928 struct lysc_range *base_range, struct lysc_range **range)
929{
930 LY_ERR ret = LY_EVALID;
931 const char *expr;
932 struct lysc_range_part *parts = NULL, *part;
933 int range_expected = 0, uns;
934 unsigned int parts_done = 0, u, v;
935
936 assert(range);
937 assert(range_p);
938
939 expr = range_p->arg;
940 while(1) {
941 if (isspace(*expr)) {
942 ++expr;
943 } else if (*expr == '\0') {
944 if (range_expected) {
945 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
946 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
947 length_restr ? "length" : "range", range_p->arg);
948 goto cleanup;
949 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
951 "Invalid %s restriction - unexpected end of the expression (%s).",
952 length_restr ? "length" : "range", range_p->arg);
953 goto cleanup;
954 }
955 parts_done++;
956 break;
957 } else if (!strncmp(expr, "min", 3)) {
958 if (parts) {
959 /* min cannot be used elsewhere than in the first part */
960 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
961 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
962 expr - range_p->arg, range_p->arg);
963 goto cleanup;
964 }
965 expr += 3;
966
967 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +0100968 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 +0100969 part->max_64 = part->min_64;
970 } else if (*expr == '|') {
971 if (!parts || range_expected) {
972 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
973 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
974 goto cleanup;
975 }
976 expr++;
977 parts_done++;
978 /* process next part of the expression */
979 } else if (!strncmp(expr, "..", 2)) {
980 expr += 2;
981 while (isspace(*expr)) {
982 expr++;
983 }
984 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
985 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
986 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
987 goto cleanup;
988 }
989 /* continue expecting the upper boundary */
990 range_expected = 1;
991 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
992 /* number */
993 if (range_expected) {
994 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +0100995 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 +0100996 range_expected = 0;
997 } else {
998 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
999 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 +01001000 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001001 part->max_64 = part->min_64;
1002 }
1003
1004 /* continue with possible another expression part */
1005 } else if (!strncmp(expr, "max", 3)) {
1006 expr += 3;
1007 while (isspace(*expr)) {
1008 expr++;
1009 }
1010 if (*expr != '\0') {
1011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1012 length_restr ? "length" : "range", expr);
1013 goto cleanup;
1014 }
1015 if (range_expected) {
1016 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001017 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 +01001018 range_expected = 0;
1019 } else {
1020 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1021 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 +01001022 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001023 part->min_64 = part->max_64;
1024 }
1025 } else {
1026 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1027 length_restr ? "length" : "range", expr);
1028 goto cleanup;
1029 }
1030 }
1031
1032 /* check with the previous range/length restriction */
1033 if (base_range) {
1034 switch (basetype) {
1035 case LY_TYPE_BINARY:
1036 case LY_TYPE_UINT8:
1037 case LY_TYPE_UINT16:
1038 case LY_TYPE_UINT32:
1039 case LY_TYPE_UINT64:
1040 case LY_TYPE_STRING:
1041 uns = 1;
1042 break;
1043 case LY_TYPE_DEC64:
1044 case LY_TYPE_INT8:
1045 case LY_TYPE_INT16:
1046 case LY_TYPE_INT32:
1047 case LY_TYPE_INT64:
1048 uns = 0;
1049 break;
1050 default:
1051 LOGINT(ctx->ctx);
1052 ret = LY_EINT;
1053 goto cleanup;
1054 }
1055 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1056 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1057 goto baseerror;
1058 }
1059 /* current lower bound is not lower than the base */
1060 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1061 /* base has single value */
1062 if (base_range->parts[v].min_64 == parts[u].min_64) {
1063 /* both lower bounds are the same */
1064 if (parts[u].min_64 != parts[u].max_64) {
1065 /* current continues with a range */
1066 goto baseerror;
1067 } else {
1068 /* equal single values, move both forward */
1069 ++v;
1070 continue;
1071 }
1072 } else {
1073 /* base is single value lower than current range, so the
1074 * value from base range is removed in the current,
1075 * move only base and repeat checking */
1076 ++v;
1077 --u;
1078 continue;
1079 }
1080 } else {
1081 /* base is the range */
1082 if (parts[u].min_64 == parts[u].max_64) {
1083 /* current is a single value */
1084 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1085 /* current is behind the base range, so base range is omitted,
1086 * move the base and keep the current for further check */
1087 ++v;
1088 --u;
1089 } /* else it is within the base range, so move the current, but keep the base */
1090 continue;
1091 } else {
1092 /* both are ranges - check the higher bound, the lower was already checked */
1093 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1094 /* higher bound is higher than the current higher bound */
1095 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1096 /* but the current lower bound is also higher, so the base range is omitted,
1097 * continue with the same current, but move the base */
1098 --u;
1099 ++v;
1100 continue;
1101 }
1102 /* current range starts within the base range but end behind it */
1103 goto baseerror;
1104 } else {
1105 /* current range is smaller than the base,
1106 * move current, but stay with the base */
1107 continue;
1108 }
1109 }
1110 }
1111 }
1112 if (u != parts_done) {
1113baseerror:
1114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1115 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1116 length_restr ? "length" : "range", range_p->arg);
1117 goto cleanup;
1118 }
1119 }
1120
1121 if (!(*range)) {
1122 *range = calloc(1, sizeof **range);
1123 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1124 }
1125
1126 if (range_p->eapptag) {
1127 lydict_remove(ctx->ctx, (*range)->eapptag);
1128 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1129 }
1130 if (range_p->emsg) {
1131 lydict_remove(ctx->ctx, (*range)->emsg);
1132 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1133 }
1134 /* extensions are taken only from the last range by the caller */
1135
1136 (*range)->parts = parts;
1137 parts = NULL;
1138 ret = LY_SUCCESS;
1139cleanup:
1140 /* TODO clean up */
1141 LY_ARRAY_FREE(parts);
1142
1143 return ret;
1144}
1145
1146/**
1147 * @brief Checks pattern syntax.
1148 *
Radek Krejcia3045382018-11-22 14:30:31 +01001149 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001150 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001151 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1152 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001153 */
1154static LY_ERR
1155lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1156{
1157 int idx, idx2, start, end, err_offset, count;
1158 char *perl_regex, *ptr;
1159 const char *err_msg, *orig_ptr;
1160 pcre *precomp;
1161#define URANGE_LEN 19
1162 char *ublock2urange[][2] = {
1163 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1164 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1165 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1166 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1167 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1168 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1169 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1170 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1171 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1172 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1173 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1174 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1175 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1176 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1177 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1178 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1179 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1180 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1181 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1182 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1183 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1184 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1185 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1186 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1187 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1188 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1189 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1190 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1191 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1192 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1193 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1194 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1195 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1196 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1197 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1198 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1199 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1200 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1201 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1202 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1203 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1204 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1205 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1206 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1207 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1208 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1209 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1210 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1211 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1212 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1213 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1214 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1215 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1216 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1217 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1218 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1219 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1220 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1221 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1222 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1223 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1224 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1225 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1226 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1227 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1228 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1229 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1230 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1231 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1232 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1233 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1234 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1235 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1236 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1237 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1238 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1239 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1240 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1241 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1242 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1243 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1244 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1245 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1246 {NULL, NULL}
1247 };
1248
1249 /* adjust the expression to a Perl equivalent
1250 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1251
1252 /* we need to replace all "$" with "\$", count them now */
1253 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1254
1255 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1256 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1257 perl_regex[0] = '\0';
1258
1259 ptr = perl_regex;
1260
1261 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1262 /* we will add line-end anchoring */
1263 ptr[0] = '(';
1264 ++ptr;
1265 }
1266
1267 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1268 if (orig_ptr[0] == '$') {
1269 ptr += sprintf(ptr, "\\$");
1270 } else if (orig_ptr[0] == '^') {
1271 ptr += sprintf(ptr, "\\^");
1272 } else {
1273 ptr[0] = orig_ptr[0];
1274 ++ptr;
1275 }
1276 }
1277
1278 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1279 ptr += sprintf(ptr, ")$");
1280 } else {
1281 ptr[0] = '\0';
1282 ++ptr;
1283 }
1284
1285 /* substitute Unicode Character Blocks with exact Character Ranges */
1286 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1287 start = ptr - perl_regex;
1288
1289 ptr = strchr(ptr, '}');
1290 if (!ptr) {
1291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1292 pattern, perl_regex + start + 2, "unterminated character property");
1293 free(perl_regex);
1294 return LY_EVALID;
1295 }
1296 end = (ptr - perl_regex) + 1;
1297
1298 /* need more space */
1299 if (end - start < URANGE_LEN) {
1300 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1301 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1302 }
1303
1304 /* find our range */
1305 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1306 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1307 break;
1308 }
1309 }
1310 if (!ublock2urange[idx][0]) {
1311 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1312 pattern, perl_regex + start + 5, "unknown block name");
1313 free(perl_regex);
1314 return LY_EVALID;
1315 }
1316
1317 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1318 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1319 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1320 ++count;
1321 }
1322 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1323 --count;
1324 }
1325 }
1326 if (count) {
1327 /* skip brackets */
1328 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1329 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1330 } else {
1331 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1332 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1333 }
1334 }
1335
1336 /* must return 0, already checked during parsing */
1337 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1338 &err_msg, &err_offset, NULL);
1339 if (!precomp) {
1340 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1341 free(perl_regex);
1342 return LY_EVALID;
1343 }
1344 free(perl_regex);
1345
1346 if (pcre_precomp) {
1347 *pcre_precomp = precomp;
1348 } else {
1349 free(precomp);
1350 }
1351
1352 return LY_SUCCESS;
1353
1354#undef URANGE_LEN
1355}
1356
Radek Krejcia3045382018-11-22 14:30:31 +01001357/**
1358 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1359 * @param[in] ctx Compile context.
1360 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1361 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1362 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1363 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1364 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1365 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1366 */
Radek Krejci19a96102018-11-15 13:38:09 +01001367static LY_ERR
1368lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1369 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1370{
1371 struct lysc_pattern **pattern;
1372 unsigned int u, v;
1373 const char *err_msg;
1374 LY_ERR ret = LY_SUCCESS;
1375
1376 /* first, copy the patterns from the base type */
1377 if (base_patterns) {
1378 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1379 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1380 }
1381
1382 LY_ARRAY_FOR(patterns_p, u) {
1383 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1384 *pattern = calloc(1, sizeof **pattern);
1385 ++(*pattern)->refcount;
1386
1387 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1388 LY_CHECK_RET(ret);
1389 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1390 if (err_msg) {
1391 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1392 }
1393
1394 if (patterns_p[u].arg[0] == 0x15) {
1395 (*pattern)->inverted = 1;
1396 }
1397 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1398 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
1399 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1400 options, v, lys_compile_ext, ret, done);
1401 }
1402done:
1403 return ret;
1404}
1405
Radek Krejcia3045382018-11-22 14:30:31 +01001406/**
1407 * @brief map of the possible restrictions combination for the specific built-in type.
1408 */
Radek Krejci19a96102018-11-15 13:38:09 +01001409static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1410 0 /* LY_TYPE_UNKNOWN */,
1411 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001412 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1413 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1414 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1415 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1416 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001417 LYS_SET_BIT /* LY_TYPE_BITS */,
1418 0 /* LY_TYPE_BOOL */,
1419 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1420 0 /* LY_TYPE_EMPTY */,
1421 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1422 LYS_SET_BASE /* LY_TYPE_IDENT */,
1423 LYS_SET_REQINST /* LY_TYPE_INST */,
1424 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001425 LYS_SET_TYPE /* LY_TYPE_UNION */,
1426 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001427 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001428 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001429 LYS_SET_RANGE /* LY_TYPE_INT64 */
1430};
1431
1432/**
1433 * @brief stringification of the YANG built-in data types
1434 */
1435const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1436 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1437 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001438};
1439
Radek Krejcia3045382018-11-22 14:30:31 +01001440/**
1441 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1442 * @param[in] ctx Compile context.
1443 * @param[in] enums_p Array of the parsed enum structures to compile.
1444 * @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.
1445 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1446 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1447 * @param[out] enums Newly created array of the compiled enums information for the current type.
1448 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1449 */
Radek Krejci19a96102018-11-15 13:38:09 +01001450static LY_ERR
1451lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1452 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1453{
1454 LY_ERR ret = LY_SUCCESS;
1455 unsigned int u, v, match;
1456 int32_t value = 0;
1457 uint32_t position = 0;
1458 struct lysc_type_enum_item *e, storage;
1459
1460 if (base_enums && ctx->mod->compiled->version < 2) {
1461 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1462 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1463 return LY_EVALID;
1464 }
1465
1466 LY_ARRAY_FOR(enums_p, u) {
1467 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1468 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
1469 if (base_enums) {
1470 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1471 LY_ARRAY_FOR(base_enums, v) {
1472 if (!strcmp(e->name, base_enums[v].name)) {
1473 break;
1474 }
1475 }
1476 if (v == LY_ARRAY_SIZE(base_enums)) {
1477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1478 "Invalid %s - derived type adds new item \"%s\".",
1479 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1480 return LY_EVALID;
1481 }
1482 match = v;
1483 }
1484
1485 if (basetype == LY_TYPE_ENUM) {
1486 if (enums_p[u].flags & LYS_SET_VALUE) {
1487 e->value = (int32_t)enums_p[u].value;
1488 if (!u || e->value >= value) {
1489 value = e->value + 1;
1490 }
1491 /* check collision with other values */
1492 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1493 if (e->value == (*enums)[v].value) {
1494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1495 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1496 e->value, e->name, (*enums)[v].name);
1497 return LY_EVALID;
1498 }
1499 }
1500 } else if (base_enums) {
1501 /* inherit the assigned value */
1502 e->value = base_enums[match].value;
1503 if (!u || e->value >= value) {
1504 value = e->value + 1;
1505 }
1506 } else {
1507 /* assign value automatically */
1508 if (u && value == INT32_MIN) {
1509 /* counter overflow */
1510 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1511 "Invalid enumeration - it is not possible to auto-assign enum value for "
1512 "\"%s\" since the highest value is already 2147483647.", e->name);
1513 return LY_EVALID;
1514 }
1515 e->value = value++;
1516 }
1517 } else { /* LY_TYPE_BITS */
1518 if (enums_p[u].flags & LYS_SET_VALUE) {
1519 e->value = (int32_t)enums_p[u].value;
1520 if (!u || (uint32_t)e->value >= position) {
1521 position = (uint32_t)e->value + 1;
1522 }
1523 /* check collision with other values */
1524 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1525 if (e->value == (*enums)[v].value) {
1526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1527 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1528 (uint32_t)e->value, e->name, (*enums)[v].name);
1529 return LY_EVALID;
1530 }
1531 }
1532 } else if (base_enums) {
1533 /* inherit the assigned value */
1534 e->value = base_enums[match].value;
1535 if (!u || (uint32_t)e->value >= position) {
1536 position = (uint32_t)e->value + 1;
1537 }
1538 } else {
1539 /* assign value automatically */
1540 if (u && position == 0) {
1541 /* counter overflow */
1542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1543 "Invalid bits - it is not possible to auto-assign bit position for "
1544 "\"%s\" since the highest value is already 4294967295.", e->name);
1545 return LY_EVALID;
1546 }
1547 e->value = position++;
1548 }
1549 }
1550
1551 if (base_enums) {
1552 /* the assigned values must not change from the derived type */
1553 if (e->value != base_enums[match].value) {
1554 if (basetype == LY_TYPE_ENUM) {
1555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1556 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1557 e->name, base_enums[match].value, e->value);
1558 } else {
1559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1560 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1561 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1562 }
1563 return LY_EVALID;
1564 }
1565 }
1566
1567 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1568 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1569
1570 if (basetype == LY_TYPE_BITS) {
1571 /* keep bits ordered by position */
1572 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1573 if (v != u) {
1574 memcpy(&storage, e, sizeof *e);
1575 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1576 memcpy(&(*enums)[v], &storage, sizeof storage);
1577 }
1578 }
1579 }
1580
1581done:
1582 return ret;
1583}
1584
Radek Krejcia3045382018-11-22 14:30:31 +01001585#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1586 for ((NODE) = (NODE)->parent; \
1587 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1588 (NODE) = (NODE)->parent); \
1589 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1591 TERM; \
1592 }
1593
1594/**
1595 * @brief Validate the predicate(s) from the leafref path.
1596 * @param[in] ctx Compile context
1597 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1598 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001599 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001600 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001601 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001602 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1603 */
1604static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001605lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001606 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001607{
1608 LY_ERR ret = LY_EVALID;
1609 const struct lys_module *mod;
1610 const struct lysc_node *src_node, *dst_node;
1611 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1612 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001613 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001614 const char *start, *end, *pke_end;
1615 struct ly_set keys = {0};
1616 int i;
1617
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001618 assert(path_context);
1619
Radek Krejcia3045382018-11-22 14:30:31 +01001620 while (**predicate == '[') {
1621 start = (*predicate)++;
1622
1623 while (isspace(**predicate)) {
1624 ++(*predicate);
1625 }
1626 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1627 while (isspace(**predicate)) {
1628 ++(*predicate);
1629 }
1630 if (**predicate != '=') {
1631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001632 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1633 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001634 goto cleanup;
1635 }
1636 ++(*predicate);
1637 while (isspace(**predicate)) {
1638 ++(*predicate);
1639 }
1640
1641 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1643 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1644 goto cleanup;
1645 }
1646 --pke_end;
1647 while (isspace(*pke_end)) {
1648 --pke_end;
1649 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001650 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01001651 /* localize path-key-expr */
1652 pke_start = path_key_expr = *predicate;
1653 /* move after the current predicate */
1654 *predicate = end + 1;
1655
1656 /* source (must be leaf or leaf-list) */
1657 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001658 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001659 if (!mod) {
1660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1661 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
1662 *predicate - start, start, src_prefix_len, src_prefix, path_context->compiled->name);
1663 goto cleanup;
1664 }
Radek Krejcia3045382018-11-22 14:30:31 +01001665 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001666 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001667 }
Radek Krejcibade82a2018-12-05 10:13:48 +01001668 src_node = NULL;
1669 if (context_node->keys) {
1670 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
1671 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
1672 src_node = (const struct lysc_node*)context_node->keys[u];
1673 break;
1674 }
1675 }
1676 }
Radek Krejcia3045382018-11-22 14:30:31 +01001677 if (!src_node) {
1678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001679 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejcia3045382018-11-22 14:30:31 +01001680 *predicate - start, start, src_len, src, mod->compiled->name);
1681 goto cleanup;
1682 }
Radek Krejcia3045382018-11-22 14:30:31 +01001683
1684 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01001685 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01001686 i = ly_set_add(&keys, (void*)src_node, 0);
1687 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01001688 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01001689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001690 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01001691 *predicate - start, start, src_node->name);
1692 goto cleanup;
1693 }
1694
1695 /* destination */
1696 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01001697 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01001698
1699 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
1700 if (strncmp(path_key_expr, "current()", 9)) {
1701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1702 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
1703 *predicate - start, start);
1704 goto cleanup;
1705 }
1706 path_key_expr += 9;
1707 while (isspace(*path_key_expr)) {
1708 ++path_key_expr;
1709 }
1710
1711 if (*path_key_expr != '/') {
1712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1713 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
1714 *predicate - start, start);
1715 goto cleanup;
1716 }
1717 ++path_key_expr;
1718 while (isspace(*path_key_expr)) {
1719 ++path_key_expr;
1720 }
1721
1722 /* rel-path-keyexpr:
1723 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
1724 while (!strncmp(path_key_expr, "..", 2)) {
1725 ++dest_parent_times;
1726 path_key_expr += 2;
1727 while (isspace(*path_key_expr)) {
1728 ++path_key_expr;
1729 }
1730 if (*path_key_expr != '/') {
1731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1732 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
1733 *predicate - start, start);
1734 goto cleanup;
1735 }
1736 ++path_key_expr;
1737 while (isspace(*path_key_expr)) {
1738 ++path_key_expr;
1739 }
1740
1741 /* path is supposed to be evaluated in data tree, so we have to skip
1742 * all schema nodes that cannot be instantiated in data tree */
1743 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
1744 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
1745 *predicate - start, start);
1746 }
1747 if (!dest_parent_times) {
1748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1749 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
1750 *predicate - start, start);
1751 goto cleanup;
1752 }
1753 if (path_key_expr == pke_end) {
1754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1755 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
1756 *predicate - start, start);
1757 goto cleanup;
1758 }
1759
1760 while(path_key_expr != pke_end) {
1761 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
1762 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01001763 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
1764 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001765 goto cleanup;
1766 }
1767
1768 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001769 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001770 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001771 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001772 }
1773 if (!mod) {
1774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1775 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
1776 *predicate - start, start, dst_len, dst);
1777 goto cleanup;
1778 }
1779
1780 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
1781 if (!dst_node) {
1782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1783 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
1784 *predicate - start, start, path_key_expr - pke_start, pke_start);
1785 goto cleanup;
1786 }
1787 }
1788 if (!(dst_node->nodetype & (dst_node->module->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
1789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001790 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01001791 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
1792 goto cleanup;
1793 }
1794 }
1795
1796 ret = LY_SUCCESS;
1797cleanup:
1798 ly_set_erase(&keys, NULL);
1799 return ret;
1800}
1801
1802/**
1803 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
1804 *
1805 * path-arg = absolute-path / relative-path
1806 * absolute-path = 1*("/" (node-identifier *path-predicate))
1807 * relative-path = 1*(".." "/") descendant-path
1808 *
1809 * @param[in,out] path Path to parse.
1810 * @param[out] prefix Prefix of the token, NULL if there is not any.
1811 * @param[out] pref_len Length of the prefix, 0 if there is not any.
1812 * @param[out] name Name of the token.
1813 * @param[out] nam_len Length of the name.
1814 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
1815 * must not be changed between consecutive calls. -1 if the
1816 * path is absolute.
1817 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
1818 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
1819 */
1820static LY_ERR
1821lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
1822 int *parent_times, int *has_predicate)
1823{
1824 int par_times = 0;
1825
1826 assert(path && *path);
1827 assert(parent_times);
1828 assert(prefix);
1829 assert(prefix_len);
1830 assert(name);
1831 assert(name_len);
1832 assert(has_predicate);
1833
1834 *prefix = NULL;
1835 *prefix_len = 0;
1836 *name = NULL;
1837 *name_len = 0;
1838 *has_predicate = 0;
1839
1840 if (!*parent_times) {
1841 if (!strncmp(*path, "..", 2)) {
1842 *path += 2;
1843 ++par_times;
1844 while (!strncmp(*path, "/..", 3)) {
1845 *path += 3;
1846 ++par_times;
1847 }
1848 }
1849 if (par_times) {
1850 *parent_times = par_times;
1851 } else {
1852 *parent_times = -1;
1853 }
1854 }
1855
1856 if (**path != '/') {
1857 return LY_EINVAL;
1858 }
1859 /* skip '/' */
1860 ++(*path);
1861
1862 /* node-identifier ([prefix:]name) */
1863 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
1864
1865 if ((**path == '/' && (*path)[1]) || !**path) {
1866 /* path continues by another token or this is the last token */
1867 return LY_SUCCESS;
1868 } else if ((*path)[0] != '[') {
1869 /* unexpected character */
1870 return LY_EINVAL;
1871 } else {
1872 /* predicate starting with [ */
1873 *has_predicate = 1;
1874 return LY_SUCCESS;
1875 }
1876}
1877
1878/**
Radek Krejci58d171e2018-11-23 13:50:55 +01001879 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
1880 *
1881 * The set of features used for target must be a subset of features used for the leafref.
1882 * This is not a perfect, we should compare the truth tables but it could require too much resources
1883 * and RFC 7950 does not require it explicitely, so we simplify that.
1884 *
1885 * @param[in] refnode The leafref node.
1886 * @param[in] target Tha target node of the leafref.
1887 * @return LY_SUCCESS or LY_EVALID;
1888 */
1889static LY_ERR
1890lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
1891{
1892 LY_ERR ret = LY_EVALID;
1893 const struct lysc_node *iter;
1894 struct lysc_iffeature **iff;
1895 unsigned int u, v, count;
1896 struct ly_set features = {0};
1897
1898 for (iter = refnode; iter; iter = iter->parent) {
1899 iff = lysc_node_iff(iter);
1900 if (iff && *iff) {
1901 LY_ARRAY_FOR(*iff, u) {
1902 LY_ARRAY_FOR((*iff)[u].features, v) {
1903 LY_CHECK_GOTO(ly_set_add(&features, (*iff)[u].features[v], 0) == -1, cleanup);
1904 }
1905 }
1906 }
1907 }
1908
1909 /* we should have, in features set, a superset of features applicable to the target node.
1910 * So when adding features applicable to the target into the features set, we should not be
1911 * able to actually add any new feature, otherwise it is not a subset of features applicable
1912 * to the leafref itself. */
1913 count = features.count;
1914 for (iter = target; iter; iter = iter->parent) {
1915 iff = lysc_node_iff(iter);
1916 if (iff && *iff) {
1917 LY_ARRAY_FOR(*iff, u) {
1918 LY_ARRAY_FOR((*iff)[u].features, v) {
1919 if ((unsigned int)ly_set_add(&features, (*iff)[u].features[v], 0) >= count) {
1920 /* new feature was added (or LY_EMEM) */
1921 goto cleanup;
1922 }
1923 }
1924 }
1925 }
1926 }
1927 ret = LY_SUCCESS;
1928
1929cleanup:
1930 ly_set_erase(&features, NULL);
1931 return ret;
1932}
1933
1934/**
Radek Krejcia3045382018-11-22 14:30:31 +01001935 * @brief Validate the leafref path.
1936 * @param[in] ctx Compile context
1937 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01001938 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01001939 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1940 */
1941static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01001942lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01001943{
1944 const struct lysc_node *node = NULL, *parent = NULL;
1945 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001946 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01001947 const char *id, *prefix, *name;
1948 size_t prefix_len, name_len;
1949 int parent_times = 0, has_predicate;
1950 unsigned int iter, u;
1951 LY_ERR ret = LY_SUCCESS;
1952
1953 assert(ctx);
1954 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001955 assert(leafref);
1956
1957 /* TODO leafref targets may be not implemented, in such a case we actually could make (we did it in libyang1) such a models implemented */
Radek Krejcia3045382018-11-22 14:30:31 +01001958
1959 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001960 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01001961 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
1962 if (!iter) { /* first iteration */
1963 /* precess ".." in relative paths */
1964 if (parent_times > 0) {
1965 /* move from the context node */
1966 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
1967 /* path is supposed to be evaluated in data tree, so we have to skip
1968 * all schema nodes that cannot be instantiated in data tree */
1969 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001970 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001971 }
1972 }
1973 }
1974
1975 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001976 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001977 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001978 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001979 }
1980 if (!mod) {
1981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001982 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
1983 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001984 return LY_EVALID;
1985 }
1986
1987 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
1988 if (!node) {
1989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001990 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001991 return LY_EVALID;
1992 }
1993 parent = node;
1994
1995 if (has_predicate) {
1996 /* we have predicate, so the current result must be list */
1997 if (node->nodetype != LYS_LIST) {
1998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1999 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002000 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002001 return LY_EVALID;
2002 }
2003
Radek Krejcibade82a2018-12-05 10:13:48 +01002004 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2005 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002006 }
2007
2008 ++iter;
2009 }
2010 if (ret) {
2011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002012 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002013 return LY_EVALID;
2014 }
2015
2016 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2018 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002019 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002020 return LY_EVALID;
2021 }
2022
2023 /* check status */
2024 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2025 return LY_EVALID;
2026 }
2027
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002028 /* check config */
2029 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2030 if (node->flags & LYS_CONFIG_R) {
2031 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2032 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2033 leafref->path);
2034 return LY_EVALID;
2035 }
2036 }
2037
Radek Krejci412ddfa2018-11-23 11:44:11 +01002038 /* store the target's type and check for circular chain of leafrefs */
2039 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2040 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2041 if (type == (struct lysc_type*)leafref) {
2042 /* circular chain detected */
2043 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2044 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2045 return LY_EVALID;
2046 }
2047 }
2048
Radek Krejci58d171e2018-11-23 13:50:55 +01002049 /* check if leafref and its target are under common if-features */
2050 if (lys_compile_leafref_features_validate(startnode, node)) {
2051 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2052 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2053 leafref->path);
2054 return LY_EVALID;
2055 }
2056
Radek Krejcia3045382018-11-22 14:30:31 +01002057 return LY_SUCCESS;
2058}
2059
Radek Krejcicdfecd92018-11-26 11:27:32 +01002060static 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,
2061 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt);
Radek Krejcia3045382018-11-22 14:30:31 +01002062/**
2063 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2064 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002065 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2066 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2067 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2068 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002069 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002070 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002071 * @param[in] basetype Base YANG built-in type of the type to compile.
2072 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2073 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2074 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2075 * @param[out] type Newly created type structure with the filled information about the type.
2076 * @return LY_ERR value.
2077 */
Radek Krejci19a96102018-11-15 13:38:09 +01002078static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002079lys_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,
2080 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2081 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002082{
2083 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002084 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002085 struct lysc_type_bin *bin;
2086 struct lysc_type_num *num;
2087 struct lysc_type_str *str;
2088 struct lysc_type_bits *bits;
2089 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002090 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002091 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002092 struct lysc_type_union *un, *un_aux;
2093 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002094
2095 switch (basetype) {
2096 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002097 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002098
2099 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002100 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002101 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002102 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2103 LY_CHECK_RET(ret);
2104 if (!tpdfname) {
2105 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2106 options, u, lys_compile_ext, ret, done);
2107 }
2108 }
2109
2110 if (tpdfname) {
2111 type_p->compiled = *type;
2112 *type = calloc(1, sizeof(struct lysc_type_bin));
2113 }
2114 break;
2115 case LY_TYPE_BITS:
2116 /* RFC 7950 9.7 - bits */
2117 bits = (struct lysc_type_bits*)(*type);
2118 if (type_p->bits) {
2119 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2120 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2121 (struct lysc_type_enum_item**)&bits->bits);
2122 LY_CHECK_RET(ret);
2123 }
2124
Radek Krejci555cb5b2018-11-16 14:54:33 +01002125 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002126 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002127 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002129 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002130 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002131 free(*type);
2132 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002133 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002134 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002135 }
2136
2137 if (tpdfname) {
2138 type_p->compiled = *type;
2139 *type = calloc(1, sizeof(struct lysc_type_bits));
2140 }
2141 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002142 case LY_TYPE_DEC64:
2143 dec = (struct lysc_type_dec*)(*type);
2144
2145 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002146 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002147 if (!type_p->fraction_digits) {
2148 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002150 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002152 free(*type);
2153 *type = NULL;
2154 }
2155 return LY_EVALID;
2156 }
2157 } else if (type_p->fraction_digits) {
2158 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002159 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002160 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2161 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002162 tpdfname);
2163 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002164 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2165 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002166 free(*type);
2167 *type = NULL;
2168 }
2169 return LY_EVALID;
2170 }
2171 dec->fraction_digits = type_p->fraction_digits;
2172
2173 /* RFC 7950 9.2.4 - range */
2174 if (type_p->range) {
2175 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2176 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2177 LY_CHECK_RET(ret);
2178 if (!tpdfname) {
2179 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2180 options, u, lys_compile_ext, ret, done);
2181 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002182 }
2183
2184 if (tpdfname) {
2185 type_p->compiled = *type;
2186 *type = calloc(1, sizeof(struct lysc_type_dec));
2187 }
2188 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002189 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002190 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002191
2192 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002193 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002194 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002195 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2196 LY_CHECK_RET(ret);
2197 if (!tpdfname) {
2198 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2199 options, u, lys_compile_ext, ret, done);
2200 }
2201 } else if (base && ((struct lysc_type_str*)base)->length) {
2202 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2203 }
2204
2205 /* RFC 7950 9.4.5 - pattern */
2206 if (type_p->patterns) {
2207 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2208 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2209 LY_CHECK_RET(ret);
2210 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2211 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2212 }
2213
2214 if (tpdfname) {
2215 type_p->compiled = *type;
2216 *type = calloc(1, sizeof(struct lysc_type_str));
2217 }
2218 break;
2219 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002220 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002221
2222 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002223 if (type_p->enums) {
2224 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2225 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2226 LY_CHECK_RET(ret);
2227 }
2228
Radek Krejci555cb5b2018-11-16 14:54:33 +01002229 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002230 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002231 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002233 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002234 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002235 free(*type);
2236 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002237 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002238 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002239 }
2240
2241 if (tpdfname) {
2242 type_p->compiled = *type;
2243 *type = calloc(1, sizeof(struct lysc_type_enum));
2244 }
2245 break;
2246 case LY_TYPE_INT8:
2247 case LY_TYPE_UINT8:
2248 case LY_TYPE_INT16:
2249 case LY_TYPE_UINT16:
2250 case LY_TYPE_INT32:
2251 case LY_TYPE_UINT32:
2252 case LY_TYPE_INT64:
2253 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002254 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002255
2256 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002257 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002258 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002259 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2260 LY_CHECK_RET(ret);
2261 if (!tpdfname) {
2262 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2263 options, u, lys_compile_ext, ret, done);
2264 }
2265 }
2266
2267 if (tpdfname) {
2268 type_p->compiled = *type;
2269 *type = calloc(1, sizeof(struct lysc_type_num));
2270 }
2271 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002272 case LY_TYPE_IDENT:
2273 idref = (struct lysc_type_identityref*)(*type);
2274
2275 /* RFC 7950 9.10.2 - base */
2276 if (type_p->bases) {
2277 if (base) {
2278 /* only the directly derived identityrefs can contain base specification */
2279 if (tpdfname) {
2280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002281 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002282 tpdfname);
2283 } else {
2284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002285 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002286 free(*type);
2287 *type = NULL;
2288 }
2289 return LY_EVALID;
2290 }
2291 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2292 LY_CHECK_RET(ret);
2293 }
2294
2295 if (!base && !type_p->flags) {
2296 /* type derived from identityref built-in type must contain at least one base */
2297 if (tpdfname) {
2298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2299 } else {
2300 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2301 free(*type);
2302 *type = NULL;
2303 }
2304 return LY_EVALID;
2305 }
2306
2307 if (tpdfname) {
2308 type_p->compiled = *type;
2309 *type = calloc(1, sizeof(struct lysc_type_identityref));
2310 }
2311 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002312 case LY_TYPE_LEAFREF:
2313 /* RFC 7950 9.9.3 - require-instance */
2314 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002315 if (context_mod->version < LYS_VERSION_1_1) {
2316 if (tpdfname) {
2317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2318 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2319 } else {
2320 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2321 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2322 free(*type);
2323 *type = NULL;
2324 }
2325 return LY_EVALID;
2326 }
Radek Krejcia3045382018-11-22 14:30:31 +01002327 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002328 } else if (base) {
2329 /* inherit */
2330 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002331 } else {
2332 /* default is true */
2333 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2334 }
2335 if (type_p->path) {
2336 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002337 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002338 } else if (base) {
2339 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002340 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002341 } else if (tpdfname) {
2342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2343 return LY_EVALID;
2344 } else {
2345 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2346 free(*type);
2347 *type = NULL;
2348 return LY_EVALID;
2349 }
2350 if (tpdfname) {
2351 type_p->compiled = *type;
2352 *type = calloc(1, sizeof(struct lysc_type_leafref));
2353 }
2354 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002355 case LY_TYPE_INST:
2356 /* RFC 7950 9.9.3 - require-instance */
2357 if (type_p->flags & LYS_SET_REQINST) {
2358 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2359 } else {
2360 /* default is true */
2361 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2362 }
2363
2364 if (tpdfname) {
2365 type_p->compiled = *type;
2366 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2367 }
2368 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002369 case LY_TYPE_UNION:
2370 un = (struct lysc_type_union*)(*type);
2371
2372 /* RFC 7950 7.4 - type */
2373 if (type_p->types) {
2374 if (base) {
2375 /* only the directly derived union can contain types specification */
2376 if (tpdfname) {
2377 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2378 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2379 tpdfname);
2380 } else {
2381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2382 "Invalid type substatement for the type not directly derived from union built-in type.");
2383 free(*type);
2384 *type = NULL;
2385 }
2386 return LY_EVALID;
2387 }
2388 /* compile the type */
2389 additional = 0;
2390 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2391 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
2392 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);
2393 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2394 /* add space for additional types from the union subtype */
2395 un_aux = (struct lysc_type_union *)un->types[u + additional];
2396 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)));
2397 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2398 un->types = (void*)((uint32_t*)(p) + 1);
2399
2400 /* copy subtypes of the subtype union */
2401 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2402 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2403 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2404 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2405 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2406 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2407 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2408 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2409 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2410 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2411 /* TODO extensions */
2412
2413 } else {
2414 un->types[u + additional] = un_aux->types[v];
2415 ++un_aux->types[v]->refcount;
2416 }
2417 ++additional;
2418 LY_ARRAY_INCREMENT(un->types);
2419 }
2420 /* compensate u increment in main loop */
2421 --additional;
2422
2423 /* free the replaced union subtype */
2424 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2425 } else {
2426 LY_ARRAY_INCREMENT(un->types);
2427 }
2428 LY_CHECK_RET(ret);
2429 }
2430 }
2431
2432 if (!base && !type_p->flags) {
2433 /* type derived from union built-in type must contain at least one type */
2434 if (tpdfname) {
2435 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2436 } else {
2437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2438 free(*type);
2439 *type = NULL;
2440 }
2441 return LY_EVALID;
2442 }
2443
2444 if (tpdfname) {
2445 type_p->compiled = *type;
2446 *type = calloc(1, sizeof(struct lysc_type_union));
2447 }
2448 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002449 case LY_TYPE_BOOL:
2450 case LY_TYPE_EMPTY:
2451 case LY_TYPE_UNKNOWN: /* just to complete switch */
2452 break;
2453 }
2454 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2455done:
2456 return ret;
2457}
2458
Radek Krejcia3045382018-11-22 14:30:31 +01002459/**
2460 * @brief Compile information about the leaf/leaf-list's type.
2461 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002462 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2463 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2464 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2465 * @param[in] context_name Name of the context node or referencing typedef for logging.
2466 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002467 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2468 * @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 +01002469 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
2470 * @param[out] dflt Storage for inheriting default value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002471 * @return LY_ERR value.
2472 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002473static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002474lys_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,
2475 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units, const char **dflt)
Radek Krejci19a96102018-11-15 13:38:09 +01002476{
2477 LY_ERR ret = LY_SUCCESS;
2478 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002479 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002480 struct type_context {
2481 const struct lysp_tpdf *tpdf;
2482 struct lysp_node *node;
2483 struct lysp_module *mod;
2484 } *tctx, *tctx_prev = NULL;
2485 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002486 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002487 struct ly_set tpdf_chain = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01002488
2489 (*type) = NULL;
2490
2491 tctx = calloc(1, sizeof *tctx);
2492 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002493 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002494 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2495 ret == LY_SUCCESS;
2496 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2497 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2498 if (basetype) {
2499 break;
2500 }
2501
2502 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002503 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2504 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002505 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2506
Radek Krejcicdfecd92018-11-26 11:27:32 +01002507 if (units && !*units) {
2508 /* inherit units */
2509 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2510 }
2511 if (dflt && !*dflt) {
2512 /* inherit default */
2513 DUP_STRING(ctx->ctx, tctx->tpdf->dflt, *dflt);
2514 }
2515 if (dummyloops && (!units || *units) && (!dflt || *dflt)) {
2516 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2517 break;
2518 }
2519
Radek Krejci19a96102018-11-15 13:38:09 +01002520 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002521 /* it is not necessary to continue, the rest of the chain was already compiled,
2522 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002523 basetype = tctx->tpdf->type.compiled->basetype;
2524 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002525 if ((units && !*units) || (dflt && !*dflt)) {
2526 dummyloops = 1;
2527 goto preparenext;
2528 } else {
2529 tctx = NULL;
2530 break;
2531 }
Radek Krejci19a96102018-11-15 13:38:09 +01002532 }
2533
2534 /* store information for the following processing */
2535 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2536
Radek Krejcicdfecd92018-11-26 11:27:32 +01002537preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002538 /* prepare next loop */
2539 tctx_prev = tctx;
2540 tctx = calloc(1, sizeof *tctx);
2541 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2542 }
2543 free(tctx);
2544
2545 /* allocate type according to the basetype */
2546 switch (basetype) {
2547 case LY_TYPE_BINARY:
2548 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002549 break;
2550 case LY_TYPE_BITS:
2551 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002552 break;
2553 case LY_TYPE_BOOL:
2554 case LY_TYPE_EMPTY:
2555 *type = calloc(1, sizeof(struct lysc_type));
2556 break;
2557 case LY_TYPE_DEC64:
2558 *type = calloc(1, sizeof(struct lysc_type_dec));
2559 break;
2560 case LY_TYPE_ENUM:
2561 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002562 break;
2563 case LY_TYPE_IDENT:
2564 *type = calloc(1, sizeof(struct lysc_type_identityref));
2565 break;
2566 case LY_TYPE_INST:
2567 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2568 break;
2569 case LY_TYPE_LEAFREF:
2570 *type = calloc(1, sizeof(struct lysc_type_leafref));
2571 break;
2572 case LY_TYPE_STRING:
2573 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002574 break;
2575 case LY_TYPE_UNION:
2576 *type = calloc(1, sizeof(struct lysc_type_union));
2577 break;
2578 case LY_TYPE_INT8:
2579 case LY_TYPE_UINT8:
2580 case LY_TYPE_INT16:
2581 case LY_TYPE_UINT16:
2582 case LY_TYPE_INT32:
2583 case LY_TYPE_UINT32:
2584 case LY_TYPE_INT64:
2585 case LY_TYPE_UINT64:
2586 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002587 break;
2588 case LY_TYPE_UNKNOWN:
2589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2590 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2591 ret = LY_EVALID;
2592 goto cleanup;
2593 }
2594 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002595 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2597 ly_data_type2str[basetype]);
2598 free(*type);
2599 (*type) = NULL;
2600 ret = LY_EVALID;
2601 goto cleanup;
2602 }
2603
2604 /* get restrictions from the referred typedefs */
2605 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2606 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002607 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002608 base = tctx->tpdf->type.compiled;
2609 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002610 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002611 /* no change, just use the type information from the base */
2612 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2613 ++base->refcount;
2614 continue;
2615 }
2616
2617 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002618 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2620 tctx->tpdf->name, ly_data_type2str[basetype]);
2621 ret = LY_EVALID;
2622 goto cleanup;
2623 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2625 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2626 tctx->tpdf->name, tctx->tpdf->dflt);
2627 ret = LY_EVALID;
2628 goto cleanup;
2629 }
2630
Radek Krejci19a96102018-11-15 13:38:09 +01002631 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002632 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002633 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 +01002634 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2635 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002636 LY_CHECK_GOTO(ret, cleanup);
2637 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002638 }
2639
Radek Krejcic5c27e52018-11-15 14:38:11 +01002640 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002641 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002642 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002643 (*type)->basetype = basetype;
2644 ++(*type)->refcount;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002645 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 +01002646 LY_CHECK_GOTO(ret, cleanup);
2647 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002648 /* no specific restriction in leaf's type definition, copy from the base */
2649 free(*type);
2650 (*type) = base;
2651 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002652 }
2653
2654 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
2655
2656cleanup:
2657 ly_set_erase(&tpdf_chain, free);
2658 return ret;
2659}
2660
2661static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent);
2662
Radek Krejcia3045382018-11-22 14:30:31 +01002663/**
2664 * @brief Compile parsed container node information.
2665 * @param[in] ctx Compile context
2666 * @param[in] node_p Parsed container node.
2667 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2668 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2669 * is enriched with the container-specific information.
2670 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2671 */
Radek Krejci19a96102018-11-15 13:38:09 +01002672static LY_ERR
2673lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2674{
2675 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
2676 struct lysc_node_container *cont = (struct lysc_node_container*)node;
2677 struct lysp_node *child_p;
2678 unsigned int u;
2679 LY_ERR ret = LY_SUCCESS;
2680
2681 COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done);
2682 COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done);
2683
2684 LY_LIST_FOR(cont_p->child, child_p) {
2685 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2686 }
2687
2688 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2689 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2690 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2691
2692done:
2693 return ret;
2694}
2695
Radek Krejcia3045382018-11-22 14:30:31 +01002696/**
2697 * @brief Compile parsed leaf node information.
2698 * @param[in] ctx Compile context
2699 * @param[in] node_p Parsed leaf node.
2700 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2701 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2702 * is enriched with the leaf-specific information.
2703 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2704 */
Radek Krejci19a96102018-11-15 13:38:09 +01002705static LY_ERR
2706lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2707{
2708 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2709 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2710 unsigned int u;
2711 LY_ERR ret = LY_SUCCESS;
2712
2713 COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done);
2714 COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002715 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002716 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2717 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
Radek Krejci43699232018-11-23 14:59:46 +01002718
Radek Krejcicdfecd92018-11-26 11:27:32 +01002719 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 +01002720 leaf->units ? NULL : &leaf->units, leaf->dflt || (leaf->flags & LYS_MAND_TRUE) ? NULL : &leaf->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +01002721 LY_CHECK_GOTO(ret, done);
Radek Krejcia3045382018-11-22 14:30:31 +01002722 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2723 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2724 ly_set_add(&ctx->unres, leaf, 0);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002725 } else if (leaf->type->basetype == LY_TYPE_UNION) {
2726 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
2727 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2728 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2729 ly_set_add(&ctx->unres, leaf, 0);
2730 }
2731 }
Radek Krejci43699232018-11-23 14:59:46 +01002732 } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) {
2733 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2734 "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt);
2735 return LY_EVALID;
Radek Krejcia3045382018-11-22 14:30:31 +01002736 }
2737
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002738 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2739
Radek Krejci19a96102018-11-15 13:38:09 +01002740done:
2741 return ret;
2742}
2743
Radek Krejcia3045382018-11-22 14:30:31 +01002744/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01002745 * @brief Compile parsed leaf-list node information.
2746 * @param[in] ctx Compile context
2747 * @param[in] node_p Parsed leaf-list node.
2748 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2749 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2750 * is enriched with the leaf-list-specific information.
2751 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2752 */
2753static LY_ERR
2754lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2755{
2756 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
2757 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
2758 unsigned int u, v;
2759 const char *dflt = NULL;
2760 LY_ERR ret = LY_SUCCESS;
2761
2762 COMPILE_MEMBER_GOTO(ctx, llist_p->when, llist->when, options, lys_compile_when, ret, done);
2763 COMPILE_ARRAY_GOTO(ctx, llist_p->iffeatures, llist->iffeatures, options, u, lys_compile_iffeature, ret, done);
2764 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
2765 DUP_STRING(ctx->ctx, llist_p->units, llist->units);
2766
2767 if (llist_p->dflts) {
2768 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
2769 LY_ARRAY_FOR(llist_p->dflts, u) {
2770 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
2771 LY_ARRAY_INCREMENT(llist->dflts);
2772 }
2773 }
2774
2775 llist->min = llist_p->min;
Radek Krejcib7408632018-11-28 17:12:11 +01002776 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002777
2778 ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod->parsed, node_p->name, &llist_p->type, options, &llist->type,
2779 llist->units ? NULL : &llist->units, (llist->dflts || llist->min) ? NULL : &dflt);
2780 LY_CHECK_GOTO(ret, done);
2781 if (dflt) {
2782 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done);
2783 llist->dflts[0] = dflt;
2784 LY_ARRAY_INCREMENT(llist->dflts);
2785 }
2786
2787 if (llist->type->basetype == LY_TYPE_LEAFREF) {
2788 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2789 ly_set_add(&ctx->unres, llist, 0);
2790 } else if (llist->type->basetype == LY_TYPE_UNION) {
2791 LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) {
2792 if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2793 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2794 ly_set_add(&ctx->unres, llist, 0);
2795 }
2796 }
Radek Krejci6bb080b2018-11-28 17:23:41 +01002797 } else if (llist->type->basetype == LY_TYPE_EMPTY) {
2798 if (ctx->mod->compiled->version < LYS_VERSION_1_1) {
2799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2800 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2801 return LY_EVALID;
2802 } else if (llist_p->dflts) {
2803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2804 "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]);
2805 return LY_EVALID;
2806 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002807 }
2808
2809 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
2810 /* configuration data values must be unique - so check the default values */
2811 LY_ARRAY_FOR(llist->dflts, u) {
2812 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
2813 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
2814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2815 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
2816 return LY_EVALID;
2817 }
2818 }
2819 }
2820 }
2821
2822 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2823
2824done:
2825 return ret;
2826}
2827
2828/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002829 * @brief Compile parsed list node information.
2830 * @param[in] ctx Compile context
2831 * @param[in] node_p Parsed list node.
2832 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2833 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2834 * is enriched with the list-specific information.
2835 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2836 */
2837static LY_ERR
2838lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2839{
2840 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
2841 struct lysc_node_list *list = (struct lysc_node_list*)node;
2842 struct lysp_node *child_p;
2843 struct lysc_node_leaf **key, ***unique;
2844 size_t len;
2845 unsigned int u, v;
2846 const char *keystr, *delim;
2847 int config;
2848 LY_ERR ret = LY_SUCCESS;
2849
2850 COMPILE_MEMBER_GOTO(ctx, list_p->when, list->when, options, lys_compile_when, ret, done);
2851 COMPILE_ARRAY_GOTO(ctx, list_p->iffeatures, list->iffeatures, options, u, lys_compile_iffeature, ret, done);
2852 list->min = list_p->min;
2853 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2854
2855 LY_LIST_FOR(list_p->child, child_p) {
2856 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2857 }
2858
2859 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
2860
2861 /* keys */
2862 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
2863 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
2864 return LY_EVALID;
2865 }
2866
2867 /* find all the keys (must be direct children) */
2868 keystr = list_p->key;
2869 while (keystr) {
2870 delim = strpbrk(keystr, " \t\n");
2871 if (delim) {
2872 len = delim - keystr;
2873 while (isspace(*delim)) {
2874 ++delim;
2875 }
2876 } else {
2877 len = strlen(keystr);
2878 }
2879
2880 /* key node must be present */
2881 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
2882 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
2883 if (!(*key)) {
2884 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2885 "The list's key \"%.*s\" not found.", len, keystr);
2886 return LY_EVALID;
2887 }
2888 /* keys must be unique */
2889 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
2890 if (*key == list->keys[u]) {
2891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2892 "Duplicated key identifier \"%.*s\".", len, keystr);
2893 return LY_EVALID;
2894 }
2895 }
2896 /* key must have the same config flag as the list itself */
2897 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
2898 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
2899 return LY_EVALID;
2900 }
2901 if (ctx->mod->compiled->version < LYS_VERSION_1_1) {
2902 /* YANG 1.0 denies key to be of empty type */
2903 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
2904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2905 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
2906 return LY_EVALID;
2907 }
2908 } else {
2909 /* when and if-feature are illegal on list keys */
2910 if ((*key)->when) {
2911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2912 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
2913 return LY_EVALID;
2914 }
2915 if ((*key)->iffeatures) {
2916 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2917 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
2918 return LY_EVALID;
2919 }
2920 }
2921 /* ignore default values of the key */
2922 if ((*key)->dflt) {
2923 lydict_remove(ctx->ctx, (*key)->dflt);
2924 (*key)->dflt = NULL;
2925 }
2926 /* mark leaf as key */
2927 (*key)->flags |= LYS_KEY;
2928
2929 /* next key value */
2930 keystr = delim;
2931 }
2932
2933 /* uniques */
2934 if (list_p->uniques) {
2935 for (v = 0; v < LY_ARRAY_SIZE(list_p->uniques); ++v) {
2936 config = -1;
2937 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2938 keystr = list_p->uniques[v];
2939 while (keystr) {
2940 delim = strpbrk(keystr, " \t\n");
2941 if (delim) {
2942 len = delim - keystr;
2943 while (isspace(*delim)) {
2944 ++delim;
2945 }
2946 } else {
2947 len = strlen(keystr);
2948 }
2949
2950 /* unique node must be present */
2951 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
2952 ret = lys_resolve_descendant_schema_nodeid(ctx, keystr, len, node, LYS_LEAF, (const struct lysc_node**)key);
2953 if (ret != LY_SUCCESS) {
2954 if (ret == LY_EDENIED) {
2955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2956 "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.",
2957 len, keystr, lys_nodetype2str((*key)->nodetype));
2958 }
2959 return LY_EVALID;
2960 }
2961
2962 /* all referenced leafs must be of the same config type */
2963 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
2964 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2965 "Unique statement \"%s\" refers to leafs with different config type.", list_p->uniques[v]);
2966 return LY_EVALID;
2967 } else if ((*key)->flags & LYS_CONFIG_W) {
2968 config = 1;
2969 } else { /* LYS_CONFIG_R */
2970 config = 0;
2971 }
2972
2973 /* mark leaf as unique */
2974 (*key)->flags |= LYS_UNIQUE;
2975
2976 /* next unique value in line */
2977 keystr = delim;
2978 }
2979 /* next unique definition */
2980 }
2981 }
2982
2983 //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done);
2984 //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done);
2985
2986done:
2987 return ret;
2988}
2989
2990/**
Radek Krejcia3045382018-11-22 14:30:31 +01002991 * @brief Compile parsed schema node information.
2992 * @param[in] ctx Compile context
2993 * @param[in] node_p Parsed schema node.
2994 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2995 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
2996 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
2997 * the compile context.
2998 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2999 */
Radek Krejci19a96102018-11-15 13:38:09 +01003000static LY_ERR
3001lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
3002{
3003 LY_ERR ret = LY_EVALID;
3004 struct lysc_node *node, **children;
3005 unsigned int u;
3006 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
3007
3008 switch (node_p->nodetype) {
3009 case LYS_CONTAINER:
3010 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
3011 node_compile_spec = lys_compile_node_container;
3012 break;
3013 case LYS_LEAF:
3014 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
3015 node_compile_spec = lys_compile_node_leaf;
3016 break;
3017 case LYS_LIST:
3018 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003019 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01003020 break;
3021 case LYS_LEAFLIST:
3022 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01003023 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01003024 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003025 case LYS_CHOICE:
3026 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
3027 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003028 case LYS_ANYXML:
3029 case LYS_ANYDATA:
3030 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
3031 break;
3032 default:
3033 LOGINT(ctx->ctx);
3034 return LY_EINT;
3035 }
3036 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3037 node->nodetype = node_p->nodetype;
3038 node->module = ctx->mod;
3039 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003040 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01003041
3042 /* config */
3043 if (!(node->flags & LYS_CONFIG_MASK)) {
3044 /* config not explicitely set, inherit it from parent */
3045 if (parent) {
3046 node->flags |= parent->flags & LYS_CONFIG_MASK;
3047 } else {
3048 /* default is config true */
3049 node->flags |= LYS_CONFIG_W;
3050 }
3051 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003052 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3053 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3054 "Configuration node cannot be child of any state data node.");
3055 goto error;
3056 }
Radek Krejci19a96102018-11-15 13:38:09 +01003057
Radek Krejcia6d57732018-11-29 13:40:37 +01003058 /* *list ordering */
3059 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3060 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3061 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
3062 "RPC/action output parameters or notification content (%s).", ctx->path);
3063 node->flags &= ~LYS_ORDBY_MASK;
3064 node->flags |= LYS_ORDBY_SYSTEM;
3065 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3066 /* default ordering is system */
3067 node->flags |= LYS_ORDBY_SYSTEM;
3068 }
3069 }
3070
Radek Krejci19a96102018-11-15 13:38:09 +01003071 /* status - it is not inherited by specification, but it does not make sense to have
3072 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3073 if (!(node->flags & LYS_STATUS_MASK)) {
3074 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
3075 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3076 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3077 node->flags |= parent->flags & LYS_STATUS_MASK;
3078 } else {
3079 node->flags |= LYS_STATUS_CURR;
3080 }
3081 } else if (parent) {
3082 /* check status compatibility with the parent */
3083 if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
3084 if (node->flags & LYS_STATUS_CURR) {
3085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3086 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3087 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3088 } else { /* LYS_STATUS_DEPRC */
3089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3090 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3091 }
3092 goto error;
3093 }
3094 }
3095
3096 if (!(options & LYSC_OPT_FREE_SP)) {
3097 node->sp = node_p;
3098 }
3099 DUP_STRING(ctx->ctx, node_p->name, node->name);
3100 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
3101
3102 /* nodetype-specific part */
3103 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
3104
3105 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01003106 if (parent) {
3107 if (parent->nodetype == LYS_CHOICE) {
3108 /* TODO exception for cases */
3109 } else if ((children = lysc_node_children(parent))) {
3110 if (!(*children)) {
3111 /* first child */
3112 *children = node;
3113 } else {
3114 /* insert at the end of the parent's children list */
3115 (*children)->prev->next = node;
3116 node->prev = (*children)->prev;
3117 (*children)->prev = node;
3118 }
Radek Krejci19a96102018-11-15 13:38:09 +01003119 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01003120 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01003121 } else {
3122 /* top-level element */
3123 if (!ctx->mod->compiled->data) {
3124 ctx->mod->compiled->data = node;
3125 } else {
3126 /* insert at the end of the module's top-level nodes list */
3127 ctx->mod->compiled->data->prev->next = node;
3128 node->prev = ctx->mod->compiled->data->prev;
3129 ctx->mod->compiled->data->prev = node;
3130 }
3131 }
3132
3133 return LY_SUCCESS;
3134
3135error:
3136 lysc_node_free(ctx->ctx, node);
3137 return ret;
3138}
3139
Radek Krejcia3045382018-11-22 14:30:31 +01003140/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01003141 * @brief Compile the given YANG submodule into the main module.
3142 * @param[in] ctx Compile context
3143 * @param[in] inc Include structure from the main module defining the submodule.
3144 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3145 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3146 */
3147LY_ERR
3148lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
3149{
3150 unsigned int u;
3151 LY_ERR ret = LY_SUCCESS;
3152 /* shortcuts */
3153 struct lysp_module *submod = inc->submodule;
3154 struct lysc_module *mainmod = ctx->mod->compiled;
3155
3156 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->features, mainmod->features, options, u, lys_compile_feature, ret, error);
3157 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
3158
3159error:
3160 return ret;
3161}
3162
3163/**
Radek Krejcia3045382018-11-22 14:30:31 +01003164 * @brief Compile the given YANG module.
3165 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
3166 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3167 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3168 */
Radek Krejci19a96102018-11-15 13:38:09 +01003169LY_ERR
3170lys_compile(struct lys_module *mod, int options)
3171{
3172 struct lysc_ctx ctx = {0};
3173 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003174 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01003175 struct lysp_module *sp;
3176 struct lysp_node *node_p;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003177 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01003178 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01003179
3180 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
3181 sp = mod->parsed;
3182
3183 if (sp->submodule) {
3184 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
3185 return LY_EINVAL;
3186 }
3187
3188 ctx.ctx = sp->ctx;
3189 ctx.mod = mod;
3190
3191 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
3192 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
3193 mod_c->ctx = sp->ctx;
3194 mod_c->implemented = sp->implemented;
3195 mod_c->latest_revision = sp->latest_revision;
3196 mod_c->version = sp->version;
3197
3198 DUP_STRING(sp->ctx, sp->name, mod_c->name);
3199 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
3200 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
3201 if (sp->revs) {
3202 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
3203 }
3204 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01003205 LY_ARRAY_FOR(sp->includes, u) {
3206 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
3207 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
3208 }
3209 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
3210 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01003211 if (sp->identities) {
3212 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
3213 }
3214
3215 LY_LIST_FOR(sp->data, node_p) {
3216 ret = lys_compile_node(&ctx, node_p, options, NULL);
3217 LY_CHECK_GOTO(ret, error);
3218 }
3219 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
3220 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
3221
3222 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
3223
Radek Krejcia3045382018-11-22 14:30:31 +01003224 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01003225 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
3226 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
3227 * 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 +01003228 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003229 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01003230 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
3231 if (type->basetype == LY_TYPE_LEAFREF) {
3232 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01003233 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 +01003234 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003235 } else if (type->basetype == LY_TYPE_UNION) {
3236 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3237 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3238 /* validate the path */
3239 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
3240 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
3241 LY_CHECK_GOTO(ret, error);
3242 }
3243 }
Radek Krejcia3045382018-11-22 14:30:31 +01003244 }
3245 }
3246 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003247 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003248 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01003249 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
3250 if (type->basetype == LY_TYPE_LEAFREF) {
3251 /* store pointer to the real type */
3252 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
3253 typeiter->basetype == LY_TYPE_LEAFREF;
3254 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3255 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003256 } else if (type->basetype == LY_TYPE_UNION) {
3257 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3258 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3259 /* store pointer to the real type */
3260 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
3261 typeiter->basetype == LY_TYPE_LEAFREF;
3262 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3263 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
3264 }
3265 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003266 }
3267 }
3268 }
Radek Krejcia3045382018-11-22 14:30:31 +01003269 ly_set_erase(&ctx.unres, NULL);
3270
Radek Krejci19a96102018-11-15 13:38:09 +01003271 if (options & LYSC_OPT_FREE_SP) {
3272 lysp_module_free(mod->parsed);
3273 ((struct lys_module*)mod)->parsed = NULL;
3274 }
3275
3276 ((struct lys_module*)mod)->compiled = mod_c;
3277 return LY_SUCCESS;
3278
3279error:
Radek Krejcia3045382018-11-22 14:30:31 +01003280 ly_set_erase(&ctx.unres, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01003281 lysc_module_free(mod_c, NULL);
3282 ((struct lys_module*)mod)->compiled = NULL;
3283 return ret;
3284}