blob: cc9f94b4c973524283d7f8585ce9637a6b03a3dc [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);
Radek Krejcie86bf772018-12-14 11:39:53 +0100219 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100220 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 */
Radek Krejcie86bf772018-12-14 11:39:53 +0100317 if (ctx->mod_def->parsed->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100318 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;
Radek Krejcie86bf772018-12-14 11:39:53 +0100523 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100524 struct lysc_ident **idref;
525
526 assert(ident || bases);
527
Radek Krejcie86bf772018-12-14 11:39:53 +0100528 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->parsed->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100529 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];
Radek Krejcie86bf772018-12-14 11:39:53 +0100539 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100540 } else {
541 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100542 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100543 }
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;
Radek Krejcie86bf772018-12-14 11:39:53 +0100555 if (mod->compiled && mod->compiled->identities) {
556 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
557 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100558 if (ident) {
559 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100560 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100561 *idref = ident;
562 } else {
563 /* we have match! store the found identity */
564 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100565 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100566 }
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
Radek Krejcie86bf772018-12-14 11:39:53 +01001460 if (base_enums && ctx->mod_def->parsed->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001461 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;
Radek Krejci58d171e2018-11-23 13:50:55 +01001894 unsigned int u, v, count;
1895 struct ly_set features = {0};
1896
1897 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01001898 if (iter->iffeatures) {
1899 LY_ARRAY_FOR(iter->iffeatures, u) {
1900 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
1901 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01001902 }
1903 }
1904 }
1905 }
1906
1907 /* we should have, in features set, a superset of features applicable to the target node.
1908 * So when adding features applicable to the target into the features set, we should not be
1909 * able to actually add any new feature, otherwise it is not a subset of features applicable
1910 * to the leafref itself. */
1911 count = features.count;
1912 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01001913 if (iter->iffeatures) {
1914 LY_ARRAY_FOR(iter->iffeatures, u) {
1915 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
1916 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01001917 /* new feature was added (or LY_EMEM) */
1918 goto cleanup;
1919 }
1920 }
1921 }
1922 }
1923 }
1924 ret = LY_SUCCESS;
1925
1926cleanup:
1927 ly_set_erase(&features, NULL);
1928 return ret;
1929}
1930
1931/**
Radek Krejcia3045382018-11-22 14:30:31 +01001932 * @brief Validate the leafref path.
1933 * @param[in] ctx Compile context
1934 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01001935 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01001936 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1937 */
1938static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01001939lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01001940{
1941 const struct lysc_node *node = NULL, *parent = NULL;
1942 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001943 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01001944 const char *id, *prefix, *name;
1945 size_t prefix_len, name_len;
1946 int parent_times = 0, has_predicate;
1947 unsigned int iter, u;
1948 LY_ERR ret = LY_SUCCESS;
1949
1950 assert(ctx);
1951 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001952 assert(leafref);
1953
1954 /* 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 +01001955
1956 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001957 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01001958 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
1959 if (!iter) { /* first iteration */
1960 /* precess ".." in relative paths */
1961 if (parent_times > 0) {
1962 /* move from the context node */
1963 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
1964 /* path is supposed to be evaluated in data tree, so we have to skip
1965 * all schema nodes that cannot be instantiated in data tree */
1966 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001967 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001968 }
1969 }
1970 }
1971
1972 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001973 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001974 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001975 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001976 }
1977 if (!mod) {
1978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001979 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
1980 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001981 return LY_EVALID;
1982 }
1983
1984 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
1985 if (!node) {
1986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001987 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001988 return LY_EVALID;
1989 }
1990 parent = node;
1991
1992 if (has_predicate) {
1993 /* we have predicate, so the current result must be list */
1994 if (node->nodetype != LYS_LIST) {
1995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1996 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01001997 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01001998 return LY_EVALID;
1999 }
2000
Radek Krejcibade82a2018-12-05 10:13:48 +01002001 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2002 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002003 }
2004
2005 ++iter;
2006 }
2007 if (ret) {
2008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002009 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002010 return LY_EVALID;
2011 }
2012
2013 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2014 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2015 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002016 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002017 return LY_EVALID;
2018 }
2019
2020 /* check status */
2021 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2022 return LY_EVALID;
2023 }
2024
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002025 /* check config */
2026 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2027 if (node->flags & LYS_CONFIG_R) {
2028 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2029 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2030 leafref->path);
2031 return LY_EVALID;
2032 }
2033 }
2034
Radek Krejci412ddfa2018-11-23 11:44:11 +01002035 /* store the target's type and check for circular chain of leafrefs */
2036 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2037 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2038 if (type == (struct lysc_type*)leafref) {
2039 /* circular chain detected */
2040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2041 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2042 return LY_EVALID;
2043 }
2044 }
2045
Radek Krejci58d171e2018-11-23 13:50:55 +01002046 /* check if leafref and its target are under common if-features */
2047 if (lys_compile_leafref_features_validate(startnode, node)) {
2048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2049 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2050 leafref->path);
2051 return LY_EVALID;
2052 }
2053
Radek Krejcia3045382018-11-22 14:30:31 +01002054 return LY_SUCCESS;
2055}
2056
Radek Krejcicdfecd92018-11-26 11:27:32 +01002057static 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,
Radek Krejci01342af2019-01-03 15:18:08 +01002058 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002059/**
2060 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2061 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002062 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2063 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2064 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2065 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002066 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002067 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002068 * @param[in] basetype Base YANG built-in type of the type to compile.
2069 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2070 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2071 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2072 * @param[out] type Newly created type structure with the filled information about the type.
2073 * @return LY_ERR value.
2074 */
Radek Krejci19a96102018-11-15 13:38:09 +01002075static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002076lys_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,
2077 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2078 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002079{
2080 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002081 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002082 struct lysc_type_bin *bin;
2083 struct lysc_type_num *num;
2084 struct lysc_type_str *str;
2085 struct lysc_type_bits *bits;
2086 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002087 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002088 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002089 struct lysc_type_union *un, *un_aux;
2090 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002091
2092 switch (basetype) {
2093 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002094 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002095
2096 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002097 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002098 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002099 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2100 LY_CHECK_RET(ret);
2101 if (!tpdfname) {
2102 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2103 options, u, lys_compile_ext, ret, done);
2104 }
2105 }
2106
2107 if (tpdfname) {
2108 type_p->compiled = *type;
2109 *type = calloc(1, sizeof(struct lysc_type_bin));
2110 }
2111 break;
2112 case LY_TYPE_BITS:
2113 /* RFC 7950 9.7 - bits */
2114 bits = (struct lysc_type_bits*)(*type);
2115 if (type_p->bits) {
2116 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2117 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2118 (struct lysc_type_enum_item**)&bits->bits);
2119 LY_CHECK_RET(ret);
2120 }
2121
Radek Krejci555cb5b2018-11-16 14:54:33 +01002122 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002123 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002124 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002126 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002128 free(*type);
2129 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002130 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002131 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002132 }
2133
2134 if (tpdfname) {
2135 type_p->compiled = *type;
2136 *type = calloc(1, sizeof(struct lysc_type_bits));
2137 }
2138 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002139 case LY_TYPE_DEC64:
2140 dec = (struct lysc_type_dec*)(*type);
2141
2142 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002143 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002144 if (!type_p->fraction_digits) {
2145 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002147 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002148 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002149 free(*type);
2150 *type = NULL;
2151 }
2152 return LY_EVALID;
2153 }
2154 } else if (type_p->fraction_digits) {
2155 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002156 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2158 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002159 tpdfname);
2160 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2162 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002163 free(*type);
2164 *type = NULL;
2165 }
2166 return LY_EVALID;
2167 }
2168 dec->fraction_digits = type_p->fraction_digits;
2169
2170 /* RFC 7950 9.2.4 - range */
2171 if (type_p->range) {
2172 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2173 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2174 LY_CHECK_RET(ret);
2175 if (!tpdfname) {
2176 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2177 options, u, lys_compile_ext, ret, done);
2178 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002179 }
2180
2181 if (tpdfname) {
2182 type_p->compiled = *type;
2183 *type = calloc(1, sizeof(struct lysc_type_dec));
2184 }
2185 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002186 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002187 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002188
2189 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002190 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002191 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002192 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2193 LY_CHECK_RET(ret);
2194 if (!tpdfname) {
2195 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2196 options, u, lys_compile_ext, ret, done);
2197 }
2198 } else if (base && ((struct lysc_type_str*)base)->length) {
2199 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2200 }
2201
2202 /* RFC 7950 9.4.5 - pattern */
2203 if (type_p->patterns) {
2204 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2205 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2206 LY_CHECK_RET(ret);
2207 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2208 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2209 }
2210
2211 if (tpdfname) {
2212 type_p->compiled = *type;
2213 *type = calloc(1, sizeof(struct lysc_type_str));
2214 }
2215 break;
2216 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002217 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002218
2219 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002220 if (type_p->enums) {
2221 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2222 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2223 LY_CHECK_RET(ret);
2224 }
2225
Radek Krejci555cb5b2018-11-16 14:54:33 +01002226 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002227 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002228 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002230 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002231 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002232 free(*type);
2233 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002234 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002235 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002236 }
2237
2238 if (tpdfname) {
2239 type_p->compiled = *type;
2240 *type = calloc(1, sizeof(struct lysc_type_enum));
2241 }
2242 break;
2243 case LY_TYPE_INT8:
2244 case LY_TYPE_UINT8:
2245 case LY_TYPE_INT16:
2246 case LY_TYPE_UINT16:
2247 case LY_TYPE_INT32:
2248 case LY_TYPE_UINT32:
2249 case LY_TYPE_INT64:
2250 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002251 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002252
2253 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002254 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002255 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002256 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2257 LY_CHECK_RET(ret);
2258 if (!tpdfname) {
2259 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2260 options, u, lys_compile_ext, ret, done);
2261 }
2262 }
2263
2264 if (tpdfname) {
2265 type_p->compiled = *type;
2266 *type = calloc(1, sizeof(struct lysc_type_num));
2267 }
2268 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002269 case LY_TYPE_IDENT:
2270 idref = (struct lysc_type_identityref*)(*type);
2271
2272 /* RFC 7950 9.10.2 - base */
2273 if (type_p->bases) {
2274 if (base) {
2275 /* only the directly derived identityrefs can contain base specification */
2276 if (tpdfname) {
2277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002278 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002279 tpdfname);
2280 } else {
2281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002282 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002283 free(*type);
2284 *type = NULL;
2285 }
2286 return LY_EVALID;
2287 }
2288 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2289 LY_CHECK_RET(ret);
2290 }
2291
2292 if (!base && !type_p->flags) {
2293 /* type derived from identityref built-in type must contain at least one base */
2294 if (tpdfname) {
2295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2296 } else {
2297 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2298 free(*type);
2299 *type = NULL;
2300 }
2301 return LY_EVALID;
2302 }
2303
2304 if (tpdfname) {
2305 type_p->compiled = *type;
2306 *type = calloc(1, sizeof(struct lysc_type_identityref));
2307 }
2308 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002309 case LY_TYPE_LEAFREF:
2310 /* RFC 7950 9.9.3 - require-instance */
2311 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002312 if (context_mod->version < LYS_VERSION_1_1) {
2313 if (tpdfname) {
2314 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2315 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2316 } else {
2317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2318 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2319 free(*type);
2320 *type = NULL;
2321 }
2322 return LY_EVALID;
2323 }
Radek Krejcia3045382018-11-22 14:30:31 +01002324 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002325 } else if (base) {
2326 /* inherit */
2327 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002328 } else {
2329 /* default is true */
2330 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2331 }
2332 if (type_p->path) {
2333 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002334 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002335 } else if (base) {
2336 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002337 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002338 } else if (tpdfname) {
2339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2340 return LY_EVALID;
2341 } else {
2342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2343 free(*type);
2344 *type = NULL;
2345 return LY_EVALID;
2346 }
2347 if (tpdfname) {
2348 type_p->compiled = *type;
2349 *type = calloc(1, sizeof(struct lysc_type_leafref));
2350 }
2351 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002352 case LY_TYPE_INST:
2353 /* RFC 7950 9.9.3 - require-instance */
2354 if (type_p->flags & LYS_SET_REQINST) {
2355 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2356 } else {
2357 /* default is true */
2358 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2359 }
2360
2361 if (tpdfname) {
2362 type_p->compiled = *type;
2363 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2364 }
2365 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002366 case LY_TYPE_UNION:
2367 un = (struct lysc_type_union*)(*type);
2368
2369 /* RFC 7950 7.4 - type */
2370 if (type_p->types) {
2371 if (base) {
2372 /* only the directly derived union can contain types specification */
2373 if (tpdfname) {
2374 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2375 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2376 tpdfname);
2377 } else {
2378 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2379 "Invalid type substatement for the type not directly derived from union built-in type.");
2380 free(*type);
2381 *type = NULL;
2382 }
2383 return LY_EVALID;
2384 }
2385 /* compile the type */
2386 additional = 0;
2387 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2388 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002389 ret = lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], options, &un->types[u + additional], NULL);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002390 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2391 /* add space for additional types from the union subtype */
2392 un_aux = (struct lysc_type_union *)un->types[u + additional];
2393 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)));
2394 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2395 un->types = (void*)((uint32_t*)(p) + 1);
2396
2397 /* copy subtypes of the subtype union */
2398 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2399 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2400 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2401 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2402 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2403 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2404 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2405 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2406 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2407 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2408 /* TODO extensions */
2409
2410 } else {
2411 un->types[u + additional] = un_aux->types[v];
2412 ++un_aux->types[v]->refcount;
2413 }
2414 ++additional;
2415 LY_ARRAY_INCREMENT(un->types);
2416 }
2417 /* compensate u increment in main loop */
2418 --additional;
2419
2420 /* free the replaced union subtype */
2421 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2422 } else {
2423 LY_ARRAY_INCREMENT(un->types);
2424 }
2425 LY_CHECK_RET(ret);
2426 }
2427 }
2428
2429 if (!base && !type_p->flags) {
2430 /* type derived from union built-in type must contain at least one type */
2431 if (tpdfname) {
2432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2433 } else {
2434 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2435 free(*type);
2436 *type = NULL;
2437 }
2438 return LY_EVALID;
2439 }
2440
2441 if (tpdfname) {
2442 type_p->compiled = *type;
2443 *type = calloc(1, sizeof(struct lysc_type_union));
2444 }
2445 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002446 case LY_TYPE_BOOL:
2447 case LY_TYPE_EMPTY:
2448 case LY_TYPE_UNKNOWN: /* just to complete switch */
2449 break;
2450 }
2451 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2452done:
2453 return ret;
2454}
2455
Radek Krejcia3045382018-11-22 14:30:31 +01002456/**
2457 * @brief Compile information about the leaf/leaf-list's type.
2458 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002459 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2460 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2461 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2462 * @param[in] context_name Name of the context node or referencing typedef for logging.
2463 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002464 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2465 * @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 +01002466 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002467 * @return LY_ERR value.
2468 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002469static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002470lys_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,
Radek Krejci01342af2019-01-03 15:18:08 +01002471 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002472{
2473 LY_ERR ret = LY_SUCCESS;
2474 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002475 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002476 struct type_context {
2477 const struct lysp_tpdf *tpdf;
2478 struct lysp_node *node;
2479 struct lysp_module *mod;
2480 } *tctx, *tctx_prev = NULL;
2481 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002482 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002483 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002484 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002485
2486 (*type) = NULL;
2487
2488 tctx = calloc(1, sizeof *tctx);
2489 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002490 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002491 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2492 ret == LY_SUCCESS;
2493 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2494 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2495 if (basetype) {
2496 break;
2497 }
2498
2499 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002500 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2501 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002502 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2503
Radek Krejcicdfecd92018-11-26 11:27:32 +01002504 if (units && !*units) {
2505 /* inherit units */
2506 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2507 }
Radek Krejci01342af2019-01-03 15:18:08 +01002508 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002509 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002510 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002511 }
Radek Krejci01342af2019-01-03 15:18:08 +01002512 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002513 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2514 break;
2515 }
2516
Radek Krejci19a96102018-11-15 13:38:09 +01002517 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002518 /* it is not necessary to continue, the rest of the chain was already compiled,
2519 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002520 basetype = tctx->tpdf->type.compiled->basetype;
2521 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002522 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002523 dummyloops = 1;
2524 goto preparenext;
2525 } else {
2526 tctx = NULL;
2527 break;
2528 }
Radek Krejci19a96102018-11-15 13:38:09 +01002529 }
2530
2531 /* store information for the following processing */
2532 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2533
Radek Krejcicdfecd92018-11-26 11:27:32 +01002534preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002535 /* prepare next loop */
2536 tctx_prev = tctx;
2537 tctx = calloc(1, sizeof *tctx);
2538 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2539 }
2540 free(tctx);
2541
2542 /* allocate type according to the basetype */
2543 switch (basetype) {
2544 case LY_TYPE_BINARY:
2545 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002546 break;
2547 case LY_TYPE_BITS:
2548 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002549 break;
2550 case LY_TYPE_BOOL:
2551 case LY_TYPE_EMPTY:
2552 *type = calloc(1, sizeof(struct lysc_type));
2553 break;
2554 case LY_TYPE_DEC64:
2555 *type = calloc(1, sizeof(struct lysc_type_dec));
2556 break;
2557 case LY_TYPE_ENUM:
2558 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002559 break;
2560 case LY_TYPE_IDENT:
2561 *type = calloc(1, sizeof(struct lysc_type_identityref));
2562 break;
2563 case LY_TYPE_INST:
2564 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2565 break;
2566 case LY_TYPE_LEAFREF:
2567 *type = calloc(1, sizeof(struct lysc_type_leafref));
2568 break;
2569 case LY_TYPE_STRING:
2570 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002571 break;
2572 case LY_TYPE_UNION:
2573 *type = calloc(1, sizeof(struct lysc_type_union));
2574 break;
2575 case LY_TYPE_INT8:
2576 case LY_TYPE_UINT8:
2577 case LY_TYPE_INT16:
2578 case LY_TYPE_UINT16:
2579 case LY_TYPE_INT32:
2580 case LY_TYPE_UINT32:
2581 case LY_TYPE_INT64:
2582 case LY_TYPE_UINT64:
2583 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002584 break;
2585 case LY_TYPE_UNKNOWN:
2586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2587 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2588 ret = LY_EVALID;
2589 goto cleanup;
2590 }
2591 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002592 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2594 ly_data_type2str[basetype]);
2595 free(*type);
2596 (*type) = NULL;
2597 ret = LY_EVALID;
2598 goto cleanup;
2599 }
2600
2601 /* get restrictions from the referred typedefs */
2602 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2603 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002604 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002605 base = tctx->tpdf->type.compiled;
2606 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002607 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002608 /* no change, just use the type information from the base */
2609 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2610 ++base->refcount;
2611 continue;
2612 }
2613
2614 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002615 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2617 tctx->tpdf->name, ly_data_type2str[basetype]);
2618 ret = LY_EVALID;
2619 goto cleanup;
2620 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2622 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2623 tctx->tpdf->name, tctx->tpdf->dflt);
2624 ret = LY_EVALID;
2625 goto cleanup;
2626 }
2627
Radek Krejci19a96102018-11-15 13:38:09 +01002628 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002629 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002630 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 +01002631 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2632 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002633 LY_CHECK_GOTO(ret, cleanup);
2634 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002635 }
2636
Radek Krejcic5c27e52018-11-15 14:38:11 +01002637 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002638 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002639 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002640 (*type)->basetype = basetype;
2641 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01002642 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, options, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002643 LY_CHECK_GOTO(ret, cleanup);
2644 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002645 /* no specific restriction in leaf's type definition, copy from the base */
2646 free(*type);
2647 (*type) = base;
2648 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002649 }
Radek Krejci01342af2019-01-03 15:18:08 +01002650 if (!(*type)->dflt) {
2651 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
2652 }
Radek Krejci19a96102018-11-15 13:38:09 +01002653
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
Radek Krejci19a96102018-11-15 13:38:09 +01002681 LY_LIST_FOR(cont_p->child, child_p) {
2682 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2683 }
2684
2685 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2686 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2687 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2688
2689done:
2690 return ret;
2691}
2692
Radek Krejcia3045382018-11-22 14:30:31 +01002693/**
2694 * @brief Compile parsed leaf node information.
2695 * @param[in] ctx Compile context
2696 * @param[in] node_p Parsed leaf node.
2697 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2698 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2699 * is enriched with the leaf-specific information.
2700 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2701 */
Radek Krejci19a96102018-11-15 13:38:09 +01002702static LY_ERR
2703lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2704{
2705 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2706 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2707 unsigned int u;
2708 LY_ERR ret = LY_SUCCESS;
2709
Radek Krejci19a96102018-11-15 13:38:09 +01002710 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002711 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2712 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
Radek Krejci76b3e962018-12-14 17:01:25 +01002713 if (leaf->dflt) {
2714 leaf->flags |= LYS_SET_DFLT;
2715 }
Radek Krejci43699232018-11-23 14:59:46 +01002716
Radek Krejcie86bf772018-12-14 11:39:53 +01002717 ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod_def->parsed, node_p->name, &leaf_p->type, options, &leaf->type,
Radek Krejci01342af2019-01-03 15:18:08 +01002718 leaf->units ? NULL : &leaf->units);
Radek Krejci19a96102018-11-15 13:38:09 +01002719 LY_CHECK_GOTO(ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002720 if (!leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
2721 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
2722 }
Radek Krejcia3045382018-11-22 14:30:31 +01002723 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2724 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2725 ly_set_add(&ctx->unres, leaf, 0);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002726 } else if (leaf->type->basetype == LY_TYPE_UNION) {
2727 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
2728 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2729 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2730 ly_set_add(&ctx->unres, leaf, 0);
2731 }
2732 }
Radek Krejci43699232018-11-23 14:59:46 +01002733 } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) {
2734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2735 "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt);
2736 return LY_EVALID;
Radek Krejcia3045382018-11-22 14:30:31 +01002737 }
2738
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002739 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2740
Radek Krejci19a96102018-11-15 13:38:09 +01002741done:
2742 return ret;
2743}
2744
Radek Krejcia3045382018-11-22 14:30:31 +01002745/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01002746 * @brief Compile parsed leaf-list node information.
2747 * @param[in] ctx Compile context
2748 * @param[in] node_p Parsed leaf-list node.
2749 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2750 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2751 * is enriched with the leaf-list-specific information.
2752 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2753 */
2754static LY_ERR
2755lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2756{
2757 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
2758 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
2759 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002760 LY_ERR ret = LY_SUCCESS;
2761
Radek Krejci0e5d8382018-11-28 16:37:53 +01002762 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
2763 DUP_STRING(ctx->ctx, llist_p->units, llist->units);
2764
2765 if (llist_p->dflts) {
2766 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
2767 LY_ARRAY_FOR(llist_p->dflts, u) {
2768 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
2769 LY_ARRAY_INCREMENT(llist->dflts);
2770 }
2771 }
2772
2773 llist->min = llist_p->min;
Radek Krejcib7408632018-11-28 17:12:11 +01002774 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002775
Radek Krejcie86bf772018-12-14 11:39:53 +01002776 ret = lys_compile_type(ctx, node_p, node_p->flags, ctx->mod_def->parsed, node_p->name, &llist_p->type, options, &llist->type,
Radek Krejci01342af2019-01-03 15:18:08 +01002777 llist->units ? NULL : &llist->units);
Radek Krejci0e5d8382018-11-28 16:37:53 +01002778 LY_CHECK_GOTO(ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002779 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01002780 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002781 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
Radek Krejci0e5d8382018-11-28 16:37:53 +01002782 LY_ARRAY_INCREMENT(llist->dflts);
2783 }
2784
2785 if (llist->type->basetype == LY_TYPE_LEAFREF) {
2786 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2787 ly_set_add(&ctx->unres, llist, 0);
2788 } else if (llist->type->basetype == LY_TYPE_UNION) {
2789 LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) {
2790 if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2791 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2792 ly_set_add(&ctx->unres, llist, 0);
2793 }
2794 }
Radek Krejci6bb080b2018-11-28 17:23:41 +01002795 } else if (llist->type->basetype == LY_TYPE_EMPTY) {
Radek Krejcie86bf772018-12-14 11:39:53 +01002796 if (ctx->mod_def->parsed->version < LYS_VERSION_1_1) {
Radek Krejci6bb080b2018-11-28 17:23:41 +01002797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2798 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2799 return LY_EVALID;
2800 } else if (llist_p->dflts) {
2801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2802 "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]);
2803 return LY_EVALID;
2804 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002805 }
2806
2807 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
2808 /* configuration data values must be unique - so check the default values */
2809 LY_ARRAY_FOR(llist->dflts, u) {
2810 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
2811 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
2812 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2813 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
2814 return LY_EVALID;
2815 }
2816 }
2817 }
2818 }
2819
2820 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2821
2822done:
2823 return ret;
2824}
2825
2826/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002827 * @brief Compile parsed list node information.
2828 * @param[in] ctx Compile context
2829 * @param[in] node_p Parsed list node.
2830 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2831 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2832 * is enriched with the list-specific information.
2833 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2834 */
2835static LY_ERR
2836lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2837{
2838 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
2839 struct lysc_node_list *list = (struct lysc_node_list*)node;
2840 struct lysp_node *child_p;
2841 struct lysc_node_leaf **key, ***unique;
2842 size_t len;
2843 unsigned int u, v;
2844 const char *keystr, *delim;
2845 int config;
2846 LY_ERR ret = LY_SUCCESS;
2847
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002848 list->min = list_p->min;
2849 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2850
2851 LY_LIST_FOR(list_p->child, child_p) {
2852 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
2853 }
2854
2855 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
2856
2857 /* keys */
2858 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
2859 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
2860 return LY_EVALID;
2861 }
2862
2863 /* find all the keys (must be direct children) */
2864 keystr = list_p->key;
2865 while (keystr) {
2866 delim = strpbrk(keystr, " \t\n");
2867 if (delim) {
2868 len = delim - keystr;
2869 while (isspace(*delim)) {
2870 ++delim;
2871 }
2872 } else {
2873 len = strlen(keystr);
2874 }
2875
2876 /* key node must be present */
2877 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
2878 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
2879 if (!(*key)) {
2880 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2881 "The list's key \"%.*s\" not found.", len, keystr);
2882 return LY_EVALID;
2883 }
2884 /* keys must be unique */
2885 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
2886 if (*key == list->keys[u]) {
2887 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2888 "Duplicated key identifier \"%.*s\".", len, keystr);
2889 return LY_EVALID;
2890 }
2891 }
2892 /* key must have the same config flag as the list itself */
2893 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
2894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
2895 return LY_EVALID;
2896 }
Radek Krejcie86bf772018-12-14 11:39:53 +01002897 if (ctx->mod_def->parsed->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002898 /* YANG 1.0 denies key to be of empty type */
2899 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
2900 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2901 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
2902 return LY_EVALID;
2903 }
2904 } else {
2905 /* when and if-feature are illegal on list keys */
2906 if ((*key)->when) {
2907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2908 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
2909 return LY_EVALID;
2910 }
2911 if ((*key)->iffeatures) {
2912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2913 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
2914 return LY_EVALID;
2915 }
2916 }
Radek Krejci76b3e962018-12-14 17:01:25 +01002917
2918 /* check status */
2919 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2920 (*key)->flags, (*key)->module, (*key)->name));
2921
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002922 /* ignore default values of the key */
2923 if ((*key)->dflt) {
2924 lydict_remove(ctx->ctx, (*key)->dflt);
2925 (*key)->dflt = NULL;
2926 }
2927 /* mark leaf as key */
2928 (*key)->flags |= LYS_KEY;
2929
2930 /* next key value */
2931 keystr = delim;
2932 }
2933
2934 /* uniques */
2935 if (list_p->uniques) {
2936 for (v = 0; v < LY_ARRAY_SIZE(list_p->uniques); ++v) {
2937 config = -1;
2938 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2939 keystr = list_p->uniques[v];
2940 while (keystr) {
2941 delim = strpbrk(keystr, " \t\n");
2942 if (delim) {
2943 len = delim - keystr;
2944 while (isspace(*delim)) {
2945 ++delim;
2946 }
2947 } else {
2948 len = strlen(keystr);
2949 }
2950
2951 /* unique node must be present */
2952 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
2953 ret = lys_resolve_descendant_schema_nodeid(ctx, keystr, len, node, LYS_LEAF, (const struct lysc_node**)key);
2954 if (ret != LY_SUCCESS) {
2955 if (ret == LY_EDENIED) {
2956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2957 "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.",
2958 len, keystr, lys_nodetype2str((*key)->nodetype));
2959 }
2960 return LY_EVALID;
2961 }
2962
2963 /* all referenced leafs must be of the same config type */
2964 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
2965 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2966 "Unique statement \"%s\" refers to leafs with different config type.", list_p->uniques[v]);
2967 return LY_EVALID;
2968 } else if ((*key)->flags & LYS_CONFIG_W) {
2969 config = 1;
2970 } else { /* LYS_CONFIG_R */
2971 config = 0;
2972 }
2973
Radek Krejci76b3e962018-12-14 17:01:25 +01002974 /* check status */
2975 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2976 (*key)->flags, (*key)->module, (*key)->name));
2977
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002978 /* mark leaf as unique */
2979 (*key)->flags |= LYS_UNIQUE;
2980
2981 /* next unique value in line */
2982 keystr = delim;
2983 }
2984 /* next unique definition */
2985 }
2986 }
2987
2988 //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done);
2989 //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done);
2990
2991done:
2992 return ret;
2993}
2994
Radek Krejci76b3e962018-12-14 17:01:25 +01002995static LY_ERR
2996lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
2997{
2998 struct lysc_node *iter, *node = (struct lysc_node*)ch;
2999 const char *prefix = NULL, *name;
3000 size_t prefix_len = 0;
3001
3002 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3003 name = strchr(dflt, ':');
3004 if (name) {
3005 prefix = dflt;
3006 prefix_len = name - prefix;
3007 ++name;
3008 } else {
3009 name = dflt;
3010 }
3011 if (prefix && (strncmp(prefix, node->module->compiled->prefix, prefix_len) || node->module->compiled->prefix[prefix_len] != '\0')) {
3012 /* prefixed default case make sense only for the prefix of the schema itself */
3013 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3014 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3015 prefix_len, prefix);
3016 return LY_EVALID;
3017 }
3018 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3019 if (!ch->dflt) {
3020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3021 "Default case \"%s\" not found.", dflt);
3022 return LY_EVALID;
3023 }
3024 /* no mandatory nodes directly under the default case */
3025 LY_LIST_FOR(ch->dflt->child, iter) {
3026 if (iter->flags & LYS_MAND_TRUE) {
3027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3028 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3029 return LY_EVALID;
3030 }
3031 }
Radek Krejci01342af2019-01-03 15:18:08 +01003032 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003033 return LY_SUCCESS;
3034}
3035
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003036/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003037 * @brief Compile parsed choice node information.
3038 * @param[in] ctx Compile context
3039 * @param[in] node_p Parsed choice node.
3040 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3041 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003042 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003043 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3044 */
3045static LY_ERR
3046lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3047{
3048 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3049 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3050 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003051 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003052 LY_ERR ret = LY_SUCCESS;
3053
Radek Krejci056d0a82018-12-06 16:57:25 +01003054 LY_LIST_FOR(ch_p->child, child_p) {
3055 if (child_p->nodetype == LYS_CASE) {
3056 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
3057 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node));
3058 }
3059 } else {
3060 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
3061 }
3062 }
3063
3064 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003065 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003066 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003067 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003068
Radek Krejci9800fb82018-12-13 14:26:23 +01003069 return ret;
3070}
3071
3072/**
3073 * @brief Compile parsed anydata or anyxml node information.
3074 * @param[in] ctx Compile context
3075 * @param[in] node_p Parsed anydata or anyxml node.
3076 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3077 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3078 * is enriched with the any-specific information.
3079 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3080 */
3081static LY_ERR
3082lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3083{
3084 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3085 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3086 unsigned int u;
3087 LY_ERR ret = LY_SUCCESS;
3088
3089 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3090
3091 if (any->flags & LYS_CONFIG_W) {
3092 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3093 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3094 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003095done:
3096 return ret;
3097}
3098
3099static LY_ERR
Radek Krejci76b3e962018-12-14 17:01:25 +01003100lys_compile_status_check(struct lysc_ctx *ctx, uint16_t node_flags, uint16_t parent_flags)
3101{
3102 /* check status compatibility with the parent */
3103 if ((parent_flags & LYS_STATUS_MASK) > (node_flags & LYS_STATUS_MASK)) {
3104 if (node_flags & LYS_STATUS_CURR) {
3105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3106 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3107 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3108 } else { /* LYS_STATUS_DEPRC */
3109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3110 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3111 }
3112 return LY_EVALID;
3113 }
3114 return LY_SUCCESS;
3115}
3116
3117static LY_ERR
3118lys_compile_status(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_node *parent)
Radek Krejci056d0a82018-12-06 16:57:25 +01003119{
3120
3121 /* status - it is not inherited by specification, but it does not make sense to have
3122 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3123 if (!(node->flags & LYS_STATUS_MASK)) {
3124 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
3125 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3126 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3127 node->flags |= parent->flags & LYS_STATUS_MASK;
3128 } else {
3129 node->flags |= LYS_STATUS_CURR;
3130 }
3131 } else if (parent) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003132 return lys_compile_status_check(ctx, node->flags, parent->flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01003133 }
3134 return LY_SUCCESS;
3135}
3136
3137static LY_ERR
3138lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3139 const struct lysc_action *actions, const struct lysc_notif *notifs,
3140 const char *name, void *exclude)
3141{
3142 const struct lysc_node *iter;
3143 unsigned int u;
3144
3145 LY_LIST_FOR(children, iter) {
3146 if (iter != exclude && !strcmp(name, iter->name)) {
3147 goto error;
3148 }
3149 }
3150 LY_ARRAY_FOR(actions, u) {
3151 if (&actions[u] != exclude && !strcmp(name, actions[u].name)) {
3152 goto error;
3153 }
3154 }
3155 LY_ARRAY_FOR(notifs, u) {
3156 if (&notifs[u] != exclude && !strcmp(name, notifs[u].name)) {
3157 goto error;
3158 }
3159 }
3160 return LY_SUCCESS;
3161error:
3162 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition");
3163 return LY_EEXIST;
3164}
3165
3166/**
3167 * @brief Connect the node into the siblings list and check its name uniqueness.
3168 *
3169 * @param[in] ctx Compile context
3170 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3171 * the choice itself is expected instead of a specific case node.
3172 * @param[in] node Schema node to connect into the list.
3173 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3174 */
3175static LY_ERR
3176lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3177{
3178 struct lysc_node **children;
3179
3180 if (node->nodetype == LYS_CASE) {
3181 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3182 } else {
3183 children = lysc_node_children_p(parent);
3184 }
3185 if (children) {
3186 if (!(*children)) {
3187 /* first child */
3188 *children = node;
3189 } else if (*children != node) {
3190 /* by the condition in previous branch we cover the choice/case children
3191 * - the children list is shared by the choice and the the first case, in addition
3192 * the first child of each case must be referenced from the case node. So the node is
3193 * actually always already inserted in case it is the first children - so here such
3194 * a situation actually corresponds to the first branch */
3195 /* insert at the end of the parent's children list */
3196 (*children)->prev->next = node;
3197 node->prev = (*children)->prev;
3198 (*children)->prev = node;
3199
3200 /* check the name uniqueness */
3201 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3202 lysc_node_notifs(parent), node->name, node)) {
3203 return LY_EEXIST;
3204 }
3205 }
3206 }
3207 return LY_SUCCESS;
3208}
3209
3210static struct lysc_node_case*
3211lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3212{
3213 struct lysc_node *iter;
3214 struct lysc_node_case *cs;
3215 unsigned int u;
3216 LY_ERR ret;
3217
3218#define UNIQUE_CHECK(NAME) \
3219 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
3220 if (!strcmp(iter->name, NAME)) { \
3221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3222 return NULL; \
3223 } \
3224 }
3225
3226 if (node_p->nodetype == LYS_CHOICE) {
3227 UNIQUE_CHECK(child->name);
3228
3229 /* we have to add an implicit case node into the parent choice */
3230 cs = calloc(1, sizeof(struct lysc_node_case));
3231 DUP_STRING(ctx->ctx, child->name, cs->name);
3232 cs->flags = ch->flags & LYS_STATUS_MASK;
3233 } else { /* node_p->nodetype == LYS_CASE */
3234 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3235 /* the case is already present since the child is not its first children */
3236 return (struct lysc_node_case*)ch->cases->prev;
3237 }
3238 UNIQUE_CHECK(node_p->name);
3239
3240 /* explicit parent case is not present (this is its first child) */
3241 cs = calloc(1, sizeof(struct lysc_node_case));
3242 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3243 cs->flags = LYS_STATUS_MASK & node_p->flags;
3244 cs->sp = node_p;
3245
3246 /* check the case's status */
3247 LY_CHECK_RET(lys_compile_status(ctx, (struct lysc_node*)cs, (struct lysc_node* )ch), NULL);
3248 COMPILE_MEMBER_GOTO(ctx, node_p->when, cs->when, options, lys_compile_when, ret, error);
3249 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
3250 }
3251 cs->module = ctx->mod;
3252 cs->prev = (struct lysc_node*)cs;
3253 cs->nodetype = LYS_CASE;
3254 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
3255 cs->parent = (struct lysc_node*)ch;
3256 cs->child = child;
3257
3258 return cs;
3259error:
3260 return NULL;
3261
3262#undef UNIQUE_CHECK
3263}
3264
Radek Krejci76b3e962018-12-14 17:01:25 +01003265static LY_ERR
3266lys_compile_refine_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_refine *rfn, int inheriting)
3267{
3268 struct lysc_node *child;
3269 uint16_t config = rfn->flags & LYS_CONFIG_MASK;
3270
3271 if (config == (node->flags & LYS_CONFIG_MASK)) {
3272 /* nothing to do */
3273 return LY_SUCCESS;
3274 }
3275
3276 if (!inheriting) {
3277 /* explicit refine */
3278 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3279 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3280 "Invalid refine of config in \"%s\" - configuration node cannot be child of any state data node.");
3281 return LY_EVALID;
3282 }
3283 }
3284 node->flags &= ~LYS_CONFIG_MASK;
3285 node->flags |= config;
3286
3287 /* inherit the change into the children */
3288 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node), child) {
3289 LY_CHECK_RET(lys_compile_refine_config(ctx, child, rfn, 1));
3290 }
3291
3292 /* TODO actions and notifications */
3293
3294 return LY_SUCCESS;
3295}
3296
Radek Krejci056d0a82018-12-06 16:57:25 +01003297/**
Radek Krejcie86bf772018-12-14 11:39:53 +01003298 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3299 * If present, also apply uses's modificators.
3300 *
3301 * @param[in] ctx Compile context
3302 * @param[in] uses_p Parsed uses schema node.
3303 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3304 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3305 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3306 * the compile context.
3307 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3308 */
3309static LY_ERR
3310lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
3311{
3312 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01003313 struct lysc_node *node, *child;
3314 struct lysc_node_container context_node_fake =
3315 {.nodetype = LYS_CONTAINER,
3316 .module = ctx->mod,
3317 .flags = parent ? parent->flags : 0,
3318 .child = NULL, .next = NULL,
3319 .prev = (struct lysc_node*)&context_node_fake};
Radek Krejcie86bf772018-12-14 11:39:53 +01003320 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01003321 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01003322 int found;
3323 const char *id, *name, *prefix;
3324 size_t prefix_len, name_len;
3325 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01003326 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01003327 LY_ERR ret = LY_EVALID;
3328
3329 /* search for the grouping definition */
3330 found = 0;
3331 id = uses_p->name;
3332 lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3333 if (prefix) {
3334 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
3335 if (!mod) {
3336 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3337 "Invalid prefix used for grouping reference (%s).", uses_p->name);
3338 return LY_EVALID;
3339 }
3340 } else {
3341 mod = ctx->mod_def;
3342 }
3343 if (mod == ctx->mod_def) {
3344 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
3345 grp = lysp_node_groupings(node_p);
3346 LY_ARRAY_FOR(grp, u) {
3347 if (!strcmp(grp[u].name, name)) {
3348 grp = &grp[u];
3349 found = 1;
3350 break;
3351 }
3352 }
3353 }
3354 }
3355 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003356 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01003357 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01003358 if (grp) {
3359 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
3360 if (!strcmp(grp[u].name, name)) {
3361 grp = &grp[u];
3362 found = 1;
3363 }
3364 }
3365 }
3366 if (!found && mod->parsed->includes) {
3367 /* ... and all the submodules */
3368 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
3369 grp = mod->parsed->includes[u].submodule->groupings;
3370 if (grp) {
3371 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
3372 if (!strcmp(grp[v].name, name)) {
3373 grp = &grp[v];
3374 found = 1;
3375 }
3376 }
3377 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003378 }
3379 }
3380 }
3381 if (!found) {
3382 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3383 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3384 return LY_EVALID;
3385 }
3386
3387 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3388 grp_stack_count = ctx->groupings.count;
3389 ly_set_add(&ctx->groupings, (void*)grp, 0);
3390 if (grp_stack_count == ctx->groupings.count) {
3391 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
3392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3393 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3394 return LY_EVALID;
3395 }
3396
3397 /* switch context's mod_def */
3398 mod_old = ctx->mod_def;
3399 ctx->mod_def = mod;
3400
3401 /* check status */
3402 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), error);
3403
Radek Krejcie86bf772018-12-14 11:39:53 +01003404 /* connect the grouping's content */
3405 LY_LIST_FOR(grp->data, node_p) {
3406 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent), error);
Radek Krejci01342af2019-01-03 15:18:08 +01003407 child = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01003408 /* check status between parent (uses in this case) and child - lys_compile_node() compares parent and the new node */
Radek Krejci01342af2019-01-03 15:18:08 +01003409 if (lys_compile_status_check(ctx, child->flags, uses_p->flags)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003410 goto error;
3411 }
Radek Krejci01342af2019-01-03 15:18:08 +01003412 if (uses_p->refines) {
3413 /* some preparation for applying refines */
3414 if (grp->data == node_p) {
3415 /* remember the first child */
3416 context_node_fake.child = child;
3417 }
3418 }
3419 }
3420 LY_LIST_FOR(context_node_fake.child, child) {
3421 child->parent = (struct lysc_node*)&context_node_fake;
3422 }
3423 if (context_node_fake.child) {
3424 child = context_node_fake.child->prev;
3425 context_node_fake.child->prev = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01003426 }
3427
3428 /* apply refine */
3429 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci01342af2019-01-03 15:18:08 +01003430 LY_CHECK_GOTO(lys_resolve_descendant_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, 0, (const struct lysc_node**)&node),
3431 error);
Radek Krejci76b3e962018-12-14 17:01:25 +01003432
3433 /* default value */
3434 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01003435 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3437 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
3438 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
3439 goto error;
3440 }
3441 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
3442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3443 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
3444 rfn->nodeid, lys_nodetype2str(node->nodetype));
3445 goto error;
3446 }
3447 if (node->nodetype == LYS_LEAF) {
3448 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
3449 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01003450 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003451 /* TODO check the default value according to type */
3452 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci01342af2019-01-03 15:18:08 +01003453 if (ctx->mod->compiled->version < 2) {
3454 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3455 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
3456 goto error;
3457 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003458 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
3459 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
3460 }
3461 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01003462 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01003463 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, error);
3464 LY_ARRAY_FOR(rfn->dflts, u) {
3465 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
3466 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
3467 }
3468 /* TODO check the default values according to type */
3469 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01003470 if (((struct lysc_node_choice*)node)->dflt) {
3471 /* unset LYS_SET_DFLT from the current default case */
3472 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
3473 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003474 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), error);
3475 }
3476 }
3477
3478 /* description refine not applicable */
3479 /* reference refine not applicable */
3480
3481 /* config */
3482 if (rfn->flags & LYS_CONFIG_MASK) {
3483 LY_CHECK_GOTO(lys_compile_refine_config(ctx, node, rfn, 0), error);
3484 }
3485
3486 /* mandatory */
3487 if (rfn->flags & LYS_MAND_MASK) {
3488 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
3489 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3490 "Invalid refine of mandatory in \"%s\" - %s cannot hold mandatory statement.",
3491 rfn->nodeid, lys_nodetype2str(node->nodetype));
3492 goto error;
3493 }
3494 /* in compiled flags, only the LYS_MAND_TRUE is present */
3495 if (rfn->flags & LYS_MAND_TRUE) {
3496 /* check if node has default value */
3497 if (node->nodetype & LYS_LEAF) {
3498 if (node->flags & LYS_SET_DFLT) {
3499 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci01342af2019-01-03 15:18:08 +01003500 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003501 goto error;
3502 } else {
3503 /* remove the default value taken from the leaf's type */
3504 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
3505 ((struct lysc_node_leaf*)node)->dflt = NULL;
3506 }
3507 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
3508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci01342af2019-01-03 15:18:08 +01003509 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003510 goto error;
3511 }
3512 if (node->parent && (node->parent->flags & LYS_SET_DFLT)) {
3513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3514 "Invalid refine of mandatory in \"%s\" - %s under the default case.",
3515 rfn->nodeid, lys_nodetype2str(node->nodetype));
3516 goto error;
3517 }
3518
3519 node->flags |= LYS_MAND_TRUE;
3520 } else {
Radek Krejci01342af2019-01-03 15:18:08 +01003521 /* make mandatory false */
Radek Krejci76b3e962018-12-14 17:01:25 +01003522 node->flags &= ~LYS_MAND_TRUE;
Radek Krejci01342af2019-01-03 15:18:08 +01003523 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
3524 /* get the type's default value if any */
3525 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
3526 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003527 }
3528 }
Radek Krejci01342af2019-01-03 15:18:08 +01003529 /* here we must have applied mandatory change, so check possible conflict with default value
3530 * (default added, mandatory left true) */
3531 if ((node->flags & LYS_MAND_TRUE) &&
3532 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
3533 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
3534 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3535 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
3536 goto error;
3537 }
3538 }
3539 /* fix connection of the children nodes from fake context node back into the parent */
3540 if (context_node_fake.child) {
3541 context_node_fake.child->prev = child;
3542 }
3543 LY_LIST_FOR(context_node_fake.child, child) {
3544 child->parent = parent;
Radek Krejcie86bf772018-12-14 11:39:53 +01003545 }
3546
3547 ret = LY_SUCCESS;
3548error:
3549 /* reload previous context's mod_def */
3550 ctx->mod_def = mod_old;
3551 /* remove the grouping from the stack for circular groupings dependency check */
3552 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3553 assert(ctx->groupings.count == grp_stack_count);
3554
3555 return ret;
3556}
3557
3558/**
Radek Krejcia3045382018-11-22 14:30:31 +01003559 * @brief Compile parsed schema node information.
3560 * @param[in] ctx Compile context
3561 * @param[in] node_p Parsed schema node.
3562 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3563 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
3564 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3565 * the compile context.
3566 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3567 */
Radek Krejci19a96102018-11-15 13:38:09 +01003568static LY_ERR
3569lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
3570{
3571 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01003572 struct lysc_node *node;
3573 struct lysc_node_case *cs;
Radek Krejci19a96102018-11-15 13:38:09 +01003574 unsigned int u;
3575 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
3576
3577 switch (node_p->nodetype) {
3578 case LYS_CONTAINER:
3579 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
3580 node_compile_spec = lys_compile_node_container;
3581 break;
3582 case LYS_LEAF:
3583 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
3584 node_compile_spec = lys_compile_node_leaf;
3585 break;
3586 case LYS_LIST:
3587 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003588 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01003589 break;
3590 case LYS_LEAFLIST:
3591 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01003592 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01003593 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003594 case LYS_CHOICE:
3595 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01003596 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01003597 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003598 case LYS_ANYXML:
3599 case LYS_ANYDATA:
3600 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01003601 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01003602 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01003603 case LYS_USES:
3604 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01003605 default:
3606 LOGINT(ctx->ctx);
3607 return LY_EINT;
3608 }
3609 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3610 node->nodetype = node_p->nodetype;
3611 node->module = ctx->mod;
3612 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003613 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01003614
3615 /* config */
3616 if (!(node->flags & LYS_CONFIG_MASK)) {
3617 /* config not explicitely set, inherit it from parent */
3618 if (parent) {
3619 node->flags |= parent->flags & LYS_CONFIG_MASK;
3620 } else {
3621 /* default is config true */
3622 node->flags |= LYS_CONFIG_W;
3623 }
3624 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003625 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3626 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3627 "Configuration node cannot be child of any state data node.");
3628 goto error;
3629 }
Radek Krejci19a96102018-11-15 13:38:09 +01003630
Radek Krejcia6d57732018-11-29 13:40:37 +01003631 /* *list ordering */
3632 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3633 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3634 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
3635 "RPC/action output parameters or notification content (%s).", ctx->path);
3636 node->flags &= ~LYS_ORDBY_MASK;
3637 node->flags |= LYS_ORDBY_SYSTEM;
3638 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3639 /* default ordering is system */
3640 node->flags |= LYS_ORDBY_SYSTEM;
3641 }
3642 }
3643
Radek Krejci19a96102018-11-15 13:38:09 +01003644 /* status - it is not inherited by specification, but it does not make sense to have
3645 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01003646 if (!parent || parent->nodetype != LYS_CHOICE) {
3647 /* in case of choice/case's children, postpone the check to the moment we know if
3648 * the parent is choice (parent here) or some case (so we have to get its flags to check) */
3649 LY_CHECK_GOTO(lys_compile_status(ctx, node, parent), error);
Radek Krejci19a96102018-11-15 13:38:09 +01003650 }
3651
3652 if (!(options & LYSC_OPT_FREE_SP)) {
3653 node->sp = node_p;
3654 }
3655 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci9800fb82018-12-13 14:26:23 +01003656 COMPILE_MEMBER_GOTO(ctx, node_p->when, node->when, options, lys_compile_when, ret, error);
3657 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01003658 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
3659
3660 /* nodetype-specific part */
3661 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
3662
3663 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01003664 if (parent) {
3665 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003666 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
3667 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
3668 /* the postponed status check of the node and its real parent - in case of implicit case,
3669 * it directly gets the same status flags as the choice */
3670 LY_CHECK_GOTO(lys_compile_status(ctx, node, (struct lysc_node*)cs), error);
3671 node->parent = (struct lysc_node*)cs;
3672 } else { /* other than choice */
3673 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01003674 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003675 LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node), LY_EVALID);
Radek Krejci19a96102018-11-15 13:38:09 +01003676 } else {
3677 /* top-level element */
3678 if (!ctx->mod->compiled->data) {
3679 ctx->mod->compiled->data = node;
3680 } else {
3681 /* insert at the end of the module's top-level nodes list */
3682 ctx->mod->compiled->data->prev->next = node;
3683 node->prev = ctx->mod->compiled->data->prev;
3684 ctx->mod->compiled->data->prev = node;
3685 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003686 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
3687 ctx->mod->compiled->notifs, node->name, node)) {
3688 return LY_EVALID;
3689 }
Radek Krejci19a96102018-11-15 13:38:09 +01003690 }
3691
3692 return LY_SUCCESS;
3693
3694error:
3695 lysc_node_free(ctx->ctx, node);
3696 return ret;
3697}
3698
Radek Krejcia3045382018-11-22 14:30:31 +01003699/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01003700 * @brief Compile the given YANG submodule into the main module.
3701 * @param[in] ctx Compile context
3702 * @param[in] inc Include structure from the main module defining the submodule.
3703 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3704 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3705 */
3706LY_ERR
3707lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
3708{
3709 unsigned int u;
3710 LY_ERR ret = LY_SUCCESS;
3711 /* shortcuts */
3712 struct lysp_module *submod = inc->submodule;
3713 struct lysc_module *mainmod = ctx->mod->compiled;
3714
3715 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->features, mainmod->features, options, u, lys_compile_feature, ret, error);
3716 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
3717
3718error:
3719 return ret;
3720}
3721
3722/**
Radek Krejcia3045382018-11-22 14:30:31 +01003723 * @brief Compile the given YANG module.
3724 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
3725 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3726 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3727 */
Radek Krejci19a96102018-11-15 13:38:09 +01003728LY_ERR
3729lys_compile(struct lys_module *mod, int options)
3730{
3731 struct lysc_ctx ctx = {0};
3732 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003733 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01003734 struct lysp_module *sp;
3735 struct lysp_node *node_p;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003736 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01003737 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01003738
3739 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
3740 sp = mod->parsed;
3741
3742 if (sp->submodule) {
3743 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
3744 return LY_EINVAL;
3745 }
3746
3747 ctx.ctx = sp->ctx;
3748 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01003749 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01003750
3751 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
3752 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
3753 mod_c->ctx = sp->ctx;
3754 mod_c->implemented = sp->implemented;
3755 mod_c->latest_revision = sp->latest_revision;
3756 mod_c->version = sp->version;
3757
3758 DUP_STRING(sp->ctx, sp->name, mod_c->name);
3759 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
3760 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
3761 if (sp->revs) {
3762 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
3763 }
3764 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01003765 LY_ARRAY_FOR(sp->includes, u) {
3766 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
3767 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
3768 }
3769 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
3770 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01003771 if (sp->identities) {
3772 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
3773 }
3774
3775 LY_LIST_FOR(sp->data, node_p) {
3776 ret = lys_compile_node(&ctx, node_p, options, NULL);
3777 LY_CHECK_GOTO(ret, error);
3778 }
3779 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
3780 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
3781
3782 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
3783
Radek Krejcia3045382018-11-22 14:30:31 +01003784 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01003785 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
3786 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
3787 * 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 +01003788 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003789 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01003790 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
3791 if (type->basetype == LY_TYPE_LEAFREF) {
3792 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01003793 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 +01003794 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003795 } else if (type->basetype == LY_TYPE_UNION) {
3796 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3797 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3798 /* validate the path */
3799 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
3800 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
3801 LY_CHECK_GOTO(ret, error);
3802 }
3803 }
Radek Krejcia3045382018-11-22 14:30:31 +01003804 }
3805 }
3806 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003807 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003808 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01003809 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
3810 if (type->basetype == LY_TYPE_LEAFREF) {
3811 /* store pointer to the real type */
3812 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
3813 typeiter->basetype == LY_TYPE_LEAFREF;
3814 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3815 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003816 } else if (type->basetype == LY_TYPE_UNION) {
3817 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3818 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3819 /* store pointer to the real type */
3820 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
3821 typeiter->basetype == LY_TYPE_LEAFREF;
3822 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3823 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
3824 }
3825 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003826 }
3827 }
3828 }
Radek Krejcia3045382018-11-22 14:30:31 +01003829 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01003830 ly_set_erase(&ctx.groupings, NULL);
Radek Krejcia3045382018-11-22 14:30:31 +01003831
Radek Krejci19a96102018-11-15 13:38:09 +01003832 if (options & LYSC_OPT_FREE_SP) {
3833 lysp_module_free(mod->parsed);
3834 ((struct lys_module*)mod)->parsed = NULL;
3835 }
3836
3837 ((struct lys_module*)mod)->compiled = mod_c;
3838 return LY_SUCCESS;
3839
3840error:
Radek Krejcia3045382018-11-22 14:30:31 +01003841 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01003842 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01003843 lysc_module_free(mod_c, NULL);
3844 ((struct lys_module*)mod)->compiled = NULL;
3845 return ret;
3846}