blob: 598ccf300b77e03c637ecc7ae63297e26d697893 [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.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100226 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100227 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 Krejci0bcdaed2019-01-10 10:21:34 +0100317 if (ctx->mod_def->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 */
Radek Krejcif0089082019-01-07 16:42:01 +0100385 if (!ctx->mod_def->compiled) {
386 /* TODO - permanently switched off feature - link to some static feature and update when the schema gets implemented */
387 LOGINT(ctx->ctx);
388 goto error;
389 } else {
390 f = lysc_feature_find(ctx->mod_def->compiled, &c[i], j - i);
391 }
Radek Krejci19a96102018-11-15 13:38:09 +0100392 LY_CHECK_ERR_GOTO(!f,
393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
394 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
395 rc = LY_EVALID,
396 error)
397 iff->features[f_size] = f;
398 LY_ARRAY_INCREMENT(iff->features);
399 f_size--;
400 }
401 }
402 while (stack.index) {
403 op = iff_stack_pop(&stack);
404 iff_setop(iff->expr, op, expr_size--);
405 }
406
407 if (++expr_size || ++f_size) {
408 /* not all expected operators and operands found */
409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
410 "Invalid value \"%s\" of if-feature - processing error.", *value);
411 rc = LY_EINT;
412 } else {
413 rc = LY_SUCCESS;
414 }
415
416error:
417 /* cleanup */
418 iff_stack_clean(&stack);
419
420 return rc;
421}
422
423static LY_ERR
424lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when)
425{
426 unsigned int u;
427 LY_ERR ret = LY_SUCCESS;
428
429 when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcic8b31002019-01-08 10:24:45 +0100430 DUP_STRING(ctx->ctx, when_p->dsc, when->dsc);
431 DUP_STRING(ctx->ctx, when_p->ref, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100432 LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done);
433 COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done);
434
435done:
436 return ret;
437}
438
439static LY_ERR
440lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must)
441{
442 unsigned int u;
443 LY_ERR ret = LY_SUCCESS;
444
445 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
446 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
447
448 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
449 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100450 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
451 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100452 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done);
453
454done:
455 return ret;
456}
457
458static LY_ERR
459lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
460{
461 unsigned int u;
462 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100463 LY_ERR ret = LY_SUCCESS;
464
465 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
466 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done);
467 imp->module = imp_p->module;
468
Radek Krejci7f2a5362018-11-28 13:05:37 +0100469 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
470 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100471 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100472 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100473 /* try to use filepath if present */
474 if (imp->module->filepath) {
475 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
476 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100477 if (mod != imp->module) {
478 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100479 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100480 mod = NULL;
481 }
482 }
483 if (!mod) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100484 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->compiled->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100485 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100486 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100487 return LY_ENOTFOUND;
488 }
489 }
Radek Krejci19a96102018-11-15 13:38:09 +0100490 }
491
492done:
493 return ret;
494}
495
496static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100497lys_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 +0100498{
499 unsigned int u;
500 LY_ERR ret = LY_SUCCESS;
501
Radek Krejcid05cbd92018-12-05 14:26:40 +0100502 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100503 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100504 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
505 DUP_STRING(ctx->ctx, ident_p->ref, ident->dsc);
Radek Krejci19a96102018-11-15 13:38:09 +0100506 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done);
507 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
508 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done);
509 ident->flags = ident_p->flags;
510
511done:
512 return ret;
513}
514
Radek Krejcia3045382018-11-22 14:30:31 +0100515/**
516 * @brief Find and process the referenced base identities from another identity or identityref
517 *
518 * For bases in identity se backlinks to them from the base identities. For identityref, store
519 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
520 * to distinguish these two use cases.
521 *
522 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
523 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
524 * @param[in] ident Referencing identity to work with.
525 * @param[in] bases Array of bases of identityref to fill in.
526 * @return LY_ERR value.
527 */
Radek Krejci19a96102018-11-15 13:38:09 +0100528static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100529lys_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 +0100530{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100531 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100532 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100533 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100534 struct lysc_ident **idref;
535
536 assert(ident || bases);
537
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100538 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100539 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
540 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
541 return LY_EVALID;
542 }
543
544 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
545 s = strchr(bases_p[u], ':');
546 if (s) {
547 /* prefixed identity */
548 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100549 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100550 } else {
551 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100552 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100553 }
554 if (!mod) {
555 if (ident) {
556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
557 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
558 } else {
559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
560 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
561 }
562 return LY_EVALID;
563 }
564 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100565 if (mod->compiled && mod->compiled->identities) {
566 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
567 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100568 if (ident) {
569 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100570 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100571 *idref = ident;
572 } else {
573 /* we have match! store the found identity */
574 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100575 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100576 }
577 break;
578 }
579 }
580 }
581 if (!idref || !(*idref)) {
582 if (ident) {
583 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
584 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
585 } else {
586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
587 "Unable to find base (%s) of identityref.", bases_p[u]);
588 }
589 return LY_EVALID;
590 }
591 }
592 return LY_SUCCESS;
593}
594
Radek Krejcia3045382018-11-22 14:30:31 +0100595/**
596 * @brief For the given array of identities, set the backlinks from all their base identities.
597 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
598 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
599 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
600 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
601 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100602static LY_ERR
603lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
604{
605 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100606
607 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
608 if (!idents_p[i].bases) {
609 continue;
610 }
Radek Krejci555cb5b2018-11-16 14:54:33 +0100611 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci19a96102018-11-15 13:38:09 +0100612 }
613 return LY_SUCCESS;
614}
615
Radek Krejcia3045382018-11-22 14:30:31 +0100616/**
617 * @brief Create compiled feature structure.
618 * @param[in] ctx Compile context.
619 * @param[in] feature_p Parsed feature definition to compile.
620 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
Radek Krejcid05cbd92018-12-05 14:26:40 +0100621 * @param[in] features List of already compiled features to check name duplicity.
Radek Krejcia3045382018-11-22 14:30:31 +0100622 * @param[in,out] feature Compiled feature structure to fill.
623 * @return LY_ERR value.
624 */
Radek Krejci19a96102018-11-15 13:38:09 +0100625static LY_ERR
Radek Krejcid05cbd92018-12-05 14:26:40 +0100626lys_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 +0100627{
628 unsigned int u, v;
629 LY_ERR ret = LY_SUCCESS;
630 struct lysc_feature **df;
631
Radek Krejcid05cbd92018-12-05 14:26:40 +0100632 COMPILE_CHECK_UNIQUENESS(ctx, features, name, feature, "feature", feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100633 DUP_STRING(ctx->ctx, feature_p->name, feature->name);
Radek Krejcic8b31002019-01-08 10:24:45 +0100634 DUP_STRING(ctx->ctx, feature_p->dsc, feature->dsc);
635 DUP_STRING(ctx->ctx, feature_p->ref, feature->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100636 feature->flags = feature_p->flags;
637
638 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
639 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
640 if (feature->iffeatures) {
641 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
642 if (feature->iffeatures[u].features) {
643 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
644 /* add itself into the dependants list */
645 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
646 *df = feature;
647 }
648 /* TODO check for circular dependency */
649 }
650 }
651 }
652done:
653 return ret;
654}
655
Radek Krejcia3045382018-11-22 14:30:31 +0100656/**
657 * @brief Validate and normalize numeric value from a range definition.
658 * @param[in] ctx Compile context.
659 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
660 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
661 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
662 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
663 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
664 * @param[in] value String value of the range boundary.
665 * @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.
666 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
667 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
668 */
Radek Krejci19a96102018-11-15 13:38:09 +0100669static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100670range_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 +0100671{
Radek Krejci6cba4292018-11-15 17:33:29 +0100672 size_t fraction = 0, size;
673
Radek Krejci19a96102018-11-15 13:38:09 +0100674 *len = 0;
675
676 assert(value);
677 /* parse value */
678 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
679 return LY_EVALID;
680 }
681
682 if ((value[*len] == '-') || (value[*len] == '+')) {
683 ++(*len);
684 }
685
686 while (isdigit(value[*len])) {
687 ++(*len);
688 }
689
690 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100691 if (basetype == LY_TYPE_DEC64) {
692 goto decimal;
693 } else {
694 *valcopy = strndup(value, *len);
695 return LY_SUCCESS;
696 }
Radek Krejci19a96102018-11-15 13:38:09 +0100697 }
698 fraction = *len;
699
700 ++(*len);
701 while (isdigit(value[*len])) {
702 ++(*len);
703 }
704
Radek Krejci6cba4292018-11-15 17:33:29 +0100705 if (basetype == LY_TYPE_DEC64) {
706decimal:
707 assert(frdigits);
708 if (*len - 1 - fraction > frdigits) {
709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
710 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
711 *len, value, frdigits);
712 return LY_EINVAL;
713 }
714 if (fraction) {
715 size = (*len) + (frdigits - ((*len) - 1 - fraction));
716 } else {
717 size = (*len) + frdigits + 1;
718 }
719 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100720 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
721
Radek Krejci6cba4292018-11-15 17:33:29 +0100722 (*valcopy)[size - 1] = '\0';
723 if (fraction) {
724 memcpy(&(*valcopy)[0], &value[0], fraction);
725 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
726 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
727 } else {
728 memcpy(&(*valcopy)[0], &value[0], *len);
729 memset(&(*valcopy)[*len], '0', frdigits);
730 }
Radek Krejci19a96102018-11-15 13:38:09 +0100731 }
732 return LY_SUCCESS;
733}
734
Radek Krejcia3045382018-11-22 14:30:31 +0100735/**
736 * @brief Check that values in range are in ascendant order.
737 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +0100738 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
739 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +0100740 * @param[in] value Current value to check.
741 * @param[in] prev_value The last seen value.
742 * @return LY_SUCCESS or LY_EEXIST for invalid order.
743 */
Radek Krejci19a96102018-11-15 13:38:09 +0100744static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +0100745range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +0100746{
747 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +0100748 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100749 return LY_EEXIST;
750 }
751 } else {
Radek Krejci5969f272018-11-23 10:03:58 +0100752 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100753 return LY_EEXIST;
754 }
755 }
756 return LY_SUCCESS;
757}
758
Radek Krejcia3045382018-11-22 14:30:31 +0100759/**
760 * @brief Set min/max value of the range part.
761 * @param[in] ctx Compile context.
762 * @param[in] part Range part structure to fill.
763 * @param[in] max Flag to distinguish if storing min or max value.
764 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
765 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
766 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
767 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
768 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +0100769 * @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 +0100770 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
771 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
772 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
773 * frdigits value), LY_EMEM.
774 */
Radek Krejci19a96102018-11-15 13:38:09 +0100775static LY_ERR
776range_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 +0100777 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100778{
779 LY_ERR ret = LY_SUCCESS;
780 char *valcopy = NULL;
781 size_t len;
782
783 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100784 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +0100785 LY_CHECK_GOTO(ret, finalize);
786 }
787 if (!valcopy && base_range) {
788 if (max) {
789 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
790 } else {
791 part->min_64 = base_range->parts[0].min_64;
792 }
793 if (!first) {
794 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
795 }
796 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +0100797 }
798
799 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100800 case LY_TYPE_INT8: /* range */
801 if (valcopy) {
802 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
803 } else if (max) {
804 part->max_64 = INT64_C(127);
805 } else {
806 part->min_64 = INT64_C(-128);
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_INT16: /* range */
813 if (valcopy) {
814 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
815 } else if (max) {
816 part->max_64 = INT64_C(32767);
817 } else {
818 part->min_64 = INT64_C(-32768);
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_INT32: /* range */
825 if (valcopy) {
826 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
827 } else if (max) {
828 part->max_64 = INT64_C(2147483647);
829 } else {
830 part->min_64 = INT64_C(-2147483648);
831 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100832 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100833 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100834 }
835 break;
836 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +0100837 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100838 if (valcopy) {
839 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
840 max ? &part->max_64 : &part->min_64);
841 } else if (max) {
842 part->max_64 = INT64_C(9223372036854775807);
843 } else {
844 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
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(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100848 }
849 break;
850 case LY_TYPE_UINT8: /* range */
851 if (valcopy) {
852 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
853 } else if (max) {
854 part->max_u64 = UINT64_C(255);
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_UINT16: /* range */
863 if (valcopy) {
864 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
865 } else if (max) {
866 part->max_u64 = UINT64_C(65535);
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_UINT32: /* range */
875 if (valcopy) {
876 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
877 } else if (max) {
878 part->max_u64 = UINT64_C(4294967295);
879 } else {
880 part->min_u64 = UINT64_C(0);
881 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100882 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100883 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100884 }
885 break;
886 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +0100887 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +0100888 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +0100889 if (valcopy) {
890 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
891 } else if (max) {
892 part->max_u64 = UINT64_C(18446744073709551615);
893 } else {
894 part->min_u64 = UINT64_C(0);
895 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100896 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +0100897 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +0100898 }
899 break;
900 default:
901 LOGINT(ctx->ctx);
902 ret = LY_EINT;
903 }
904
Radek Krejci5969f272018-11-23 10:03:58 +0100905finalize:
Radek Krejci19a96102018-11-15 13:38:09 +0100906 if (ret == LY_EDENIED) {
907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
908 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
909 length_restr ? "length" : "range", valcopy ? valcopy : *value);
910 } else if (ret == LY_EVALID) {
911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
912 "Invalid %s restriction - invalid value \"%s\".",
913 length_restr ? "length" : "range", valcopy ? valcopy : *value);
914 } else if (ret == LY_EEXIST) {
915 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
916 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100917 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +0100918 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +0100919 } else if (!ret && value) {
920 *value = *value + len;
921 }
922 free(valcopy);
923 return ret;
924}
925
Radek Krejcia3045382018-11-22 14:30:31 +0100926/**
927 * @brief Compile the parsed range restriction.
928 * @param[in] ctx Compile context.
929 * @param[in] range_p Parsed range structure to compile.
930 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
931 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
932 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
933 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
934 * range restriction must be more restrictive than the base_range.
935 * @param[in,out] range Pointer to the created current range structure.
936 * @return LY_ERR value.
937 */
Radek Krejci19a96102018-11-15 13:38:09 +0100938static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100939lys_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 +0100940 struct lysc_range *base_range, struct lysc_range **range)
941{
942 LY_ERR ret = LY_EVALID;
943 const char *expr;
944 struct lysc_range_part *parts = NULL, *part;
945 int range_expected = 0, uns;
946 unsigned int parts_done = 0, u, v;
947
948 assert(range);
949 assert(range_p);
950
951 expr = range_p->arg;
952 while(1) {
953 if (isspace(*expr)) {
954 ++expr;
955 } else if (*expr == '\0') {
956 if (range_expected) {
957 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
958 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
959 length_restr ? "length" : "range", range_p->arg);
960 goto cleanup;
961 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
962 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
963 "Invalid %s restriction - unexpected end of the expression (%s).",
964 length_restr ? "length" : "range", range_p->arg);
965 goto cleanup;
966 }
967 parts_done++;
968 break;
969 } else if (!strncmp(expr, "min", 3)) {
970 if (parts) {
971 /* min cannot be used elsewhere than in the first part */
972 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
973 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
974 expr - range_p->arg, range_p->arg);
975 goto cleanup;
976 }
977 expr += 3;
978
979 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +0100980 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 +0100981 part->max_64 = part->min_64;
982 } else if (*expr == '|') {
983 if (!parts || range_expected) {
984 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
985 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
986 goto cleanup;
987 }
988 expr++;
989 parts_done++;
990 /* process next part of the expression */
991 } else if (!strncmp(expr, "..", 2)) {
992 expr += 2;
993 while (isspace(*expr)) {
994 expr++;
995 }
996 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
998 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
999 goto cleanup;
1000 }
1001 /* continue expecting the upper boundary */
1002 range_expected = 1;
1003 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1004 /* number */
1005 if (range_expected) {
1006 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001007 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 +01001008 range_expected = 0;
1009 } else {
1010 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1011 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 +01001012 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001013 part->max_64 = part->min_64;
1014 }
1015
1016 /* continue with possible another expression part */
1017 } else if (!strncmp(expr, "max", 3)) {
1018 expr += 3;
1019 while (isspace(*expr)) {
1020 expr++;
1021 }
1022 if (*expr != '\0') {
1023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1024 length_restr ? "length" : "range", expr);
1025 goto cleanup;
1026 }
1027 if (range_expected) {
1028 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001029 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 +01001030 range_expected = 0;
1031 } else {
1032 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1033 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 +01001034 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001035 part->min_64 = part->max_64;
1036 }
1037 } else {
1038 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1039 length_restr ? "length" : "range", expr);
1040 goto cleanup;
1041 }
1042 }
1043
1044 /* check with the previous range/length restriction */
1045 if (base_range) {
1046 switch (basetype) {
1047 case LY_TYPE_BINARY:
1048 case LY_TYPE_UINT8:
1049 case LY_TYPE_UINT16:
1050 case LY_TYPE_UINT32:
1051 case LY_TYPE_UINT64:
1052 case LY_TYPE_STRING:
1053 uns = 1;
1054 break;
1055 case LY_TYPE_DEC64:
1056 case LY_TYPE_INT8:
1057 case LY_TYPE_INT16:
1058 case LY_TYPE_INT32:
1059 case LY_TYPE_INT64:
1060 uns = 0;
1061 break;
1062 default:
1063 LOGINT(ctx->ctx);
1064 ret = LY_EINT;
1065 goto cleanup;
1066 }
1067 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1068 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1069 goto baseerror;
1070 }
1071 /* current lower bound is not lower than the base */
1072 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1073 /* base has single value */
1074 if (base_range->parts[v].min_64 == parts[u].min_64) {
1075 /* both lower bounds are the same */
1076 if (parts[u].min_64 != parts[u].max_64) {
1077 /* current continues with a range */
1078 goto baseerror;
1079 } else {
1080 /* equal single values, move both forward */
1081 ++v;
1082 continue;
1083 }
1084 } else {
1085 /* base is single value lower than current range, so the
1086 * value from base range is removed in the current,
1087 * move only base and repeat checking */
1088 ++v;
1089 --u;
1090 continue;
1091 }
1092 } else {
1093 /* base is the range */
1094 if (parts[u].min_64 == parts[u].max_64) {
1095 /* current is a single value */
1096 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1097 /* current is behind the base range, so base range is omitted,
1098 * move the base and keep the current for further check */
1099 ++v;
1100 --u;
1101 } /* else it is within the base range, so move the current, but keep the base */
1102 continue;
1103 } else {
1104 /* both are ranges - check the higher bound, the lower was already checked */
1105 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1106 /* higher bound is higher than the current higher bound */
1107 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1108 /* but the current lower bound is also higher, so the base range is omitted,
1109 * continue with the same current, but move the base */
1110 --u;
1111 ++v;
1112 continue;
1113 }
1114 /* current range starts within the base range but end behind it */
1115 goto baseerror;
1116 } else {
1117 /* current range is smaller than the base,
1118 * move current, but stay with the base */
1119 continue;
1120 }
1121 }
1122 }
1123 }
1124 if (u != parts_done) {
1125baseerror:
1126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1127 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1128 length_restr ? "length" : "range", range_p->arg);
1129 goto cleanup;
1130 }
1131 }
1132
1133 if (!(*range)) {
1134 *range = calloc(1, sizeof **range);
1135 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1136 }
1137
Radek Krejcic8b31002019-01-08 10:24:45 +01001138 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001139 if (range_p->eapptag) {
1140 lydict_remove(ctx->ctx, (*range)->eapptag);
1141 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1142 }
1143 if (range_p->emsg) {
1144 lydict_remove(ctx->ctx, (*range)->emsg);
1145 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1146 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001147 if (range_p->dsc) {
1148 lydict_remove(ctx->ctx, (*range)->dsc);
1149 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1150 }
1151 if (range_p->ref) {
1152 lydict_remove(ctx->ctx, (*range)->ref);
1153 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1154 }
Radek Krejci19a96102018-11-15 13:38:09 +01001155 /* extensions are taken only from the last range by the caller */
1156
1157 (*range)->parts = parts;
1158 parts = NULL;
1159 ret = LY_SUCCESS;
1160cleanup:
1161 /* TODO clean up */
1162 LY_ARRAY_FREE(parts);
1163
1164 return ret;
1165}
1166
1167/**
1168 * @brief Checks pattern syntax.
1169 *
Radek Krejcia3045382018-11-22 14:30:31 +01001170 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001171 * @param[in] pattern Pattern to check.
Radek Krejcia3045382018-11-22 14:30:31 +01001172 * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed.
1173 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001174 */
1175static LY_ERR
1176lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1177{
1178 int idx, idx2, start, end, err_offset, count;
1179 char *perl_regex, *ptr;
1180 const char *err_msg, *orig_ptr;
1181 pcre *precomp;
1182#define URANGE_LEN 19
1183 char *ublock2urange[][2] = {
1184 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1185 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1186 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1187 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1188 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1189 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1190 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1191 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1192 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1193 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1194 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1195 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1196 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1197 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1198 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1199 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1200 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1201 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1202 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1203 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1204 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1205 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1206 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1207 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1208 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1209 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1210 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1211 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1212 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1213 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1214 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1215 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1216 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1217 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1218 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1219 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1220 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1221 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1222 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1223 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1224 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1225 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1226 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1227 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1228 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1229 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1230 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1231 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1232 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1233 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1234 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1235 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1236 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1237 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1238 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1239 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1240 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1241 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1242 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1243 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1244 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1245 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1246 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1247 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1248 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1249 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1250 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1251 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1252 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1253 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1254 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1255 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1256 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1257 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1258 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1259 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1260 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1261 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1262 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1263 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1264 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1265 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1266 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1267 {NULL, NULL}
1268 };
1269
1270 /* adjust the expression to a Perl equivalent
1271 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1272
1273 /* we need to replace all "$" with "\$", count them now */
1274 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1275
1276 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1277 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1278 perl_regex[0] = '\0';
1279
1280 ptr = perl_regex;
1281
1282 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1283 /* we will add line-end anchoring */
1284 ptr[0] = '(';
1285 ++ptr;
1286 }
1287
1288 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1289 if (orig_ptr[0] == '$') {
1290 ptr += sprintf(ptr, "\\$");
1291 } else if (orig_ptr[0] == '^') {
1292 ptr += sprintf(ptr, "\\^");
1293 } else {
1294 ptr[0] = orig_ptr[0];
1295 ++ptr;
1296 }
1297 }
1298
1299 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1300 ptr += sprintf(ptr, ")$");
1301 } else {
1302 ptr[0] = '\0';
1303 ++ptr;
1304 }
1305
1306 /* substitute Unicode Character Blocks with exact Character Ranges */
1307 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1308 start = ptr - perl_regex;
1309
1310 ptr = strchr(ptr, '}');
1311 if (!ptr) {
1312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1313 pattern, perl_regex + start + 2, "unterminated character property");
1314 free(perl_regex);
1315 return LY_EVALID;
1316 }
1317 end = (ptr - perl_regex) + 1;
1318
1319 /* need more space */
1320 if (end - start < URANGE_LEN) {
1321 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1322 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1323 }
1324
1325 /* find our range */
1326 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1327 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1328 break;
1329 }
1330 }
1331 if (!ublock2urange[idx][0]) {
1332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1333 pattern, perl_regex + start + 5, "unknown block name");
1334 free(perl_regex);
1335 return LY_EVALID;
1336 }
1337
1338 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1339 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1340 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1341 ++count;
1342 }
1343 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1344 --count;
1345 }
1346 }
1347 if (count) {
1348 /* skip brackets */
1349 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1350 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1351 } else {
1352 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1353 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1354 }
1355 }
1356
1357 /* must return 0, already checked during parsing */
1358 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1359 &err_msg, &err_offset, NULL);
1360 if (!precomp) {
1361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1362 free(perl_regex);
1363 return LY_EVALID;
1364 }
1365 free(perl_regex);
1366
1367 if (pcre_precomp) {
1368 *pcre_precomp = precomp;
1369 } else {
1370 free(precomp);
1371 }
1372
1373 return LY_SUCCESS;
1374
1375#undef URANGE_LEN
1376}
1377
Radek Krejcia3045382018-11-22 14:30:31 +01001378/**
1379 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1380 * @param[in] ctx Compile context.
1381 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1382 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1383 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1384 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1385 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1386 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1387 */
Radek Krejci19a96102018-11-15 13:38:09 +01001388static LY_ERR
1389lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1390 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1391{
1392 struct lysc_pattern **pattern;
1393 unsigned int u, v;
1394 const char *err_msg;
1395 LY_ERR ret = LY_SUCCESS;
1396
1397 /* first, copy the patterns from the base type */
1398 if (base_patterns) {
1399 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1400 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1401 }
1402
1403 LY_ARRAY_FOR(patterns_p, u) {
1404 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1405 *pattern = calloc(1, sizeof **pattern);
1406 ++(*pattern)->refcount;
1407
1408 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1409 LY_CHECK_RET(ret);
1410 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1411 if (err_msg) {
1412 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1413 }
1414
1415 if (patterns_p[u].arg[0] == 0x15) {
1416 (*pattern)->inverted = 1;
1417 }
1418 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1419 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001420 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1421 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001422 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1423 options, v, lys_compile_ext, ret, done);
1424 }
1425done:
1426 return ret;
1427}
1428
Radek Krejcia3045382018-11-22 14:30:31 +01001429/**
1430 * @brief map of the possible restrictions combination for the specific built-in type.
1431 */
Radek Krejci19a96102018-11-15 13:38:09 +01001432static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1433 0 /* LY_TYPE_UNKNOWN */,
1434 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001435 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1436 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1437 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1438 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1439 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001440 LYS_SET_BIT /* LY_TYPE_BITS */,
1441 0 /* LY_TYPE_BOOL */,
1442 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1443 0 /* LY_TYPE_EMPTY */,
1444 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1445 LYS_SET_BASE /* LY_TYPE_IDENT */,
1446 LYS_SET_REQINST /* LY_TYPE_INST */,
1447 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001448 LYS_SET_TYPE /* LY_TYPE_UNION */,
1449 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001450 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001451 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001452 LYS_SET_RANGE /* LY_TYPE_INT64 */
1453};
1454
1455/**
1456 * @brief stringification of the YANG built-in data types
1457 */
1458const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1459 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1460 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001461};
1462
Radek Krejcia3045382018-11-22 14:30:31 +01001463/**
1464 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1465 * @param[in] ctx Compile context.
1466 * @param[in] enums_p Array of the parsed enum structures to compile.
1467 * @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.
1468 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
1469 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1470 * @param[out] enums Newly created array of the compiled enums information for the current type.
1471 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1472 */
Radek Krejci19a96102018-11-15 13:38:09 +01001473static LY_ERR
1474lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1475 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1476{
1477 LY_ERR ret = LY_SUCCESS;
1478 unsigned int u, v, match;
1479 int32_t value = 0;
1480 uint32_t position = 0;
1481 struct lysc_type_enum_item *e, storage;
1482
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001483 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1485 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1486 return LY_EVALID;
1487 }
1488
1489 LY_ARRAY_FOR(enums_p, u) {
1490 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1491 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001492 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1493 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci19a96102018-11-15 13:38:09 +01001494 if (base_enums) {
1495 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1496 LY_ARRAY_FOR(base_enums, v) {
1497 if (!strcmp(e->name, base_enums[v].name)) {
1498 break;
1499 }
1500 }
1501 if (v == LY_ARRAY_SIZE(base_enums)) {
1502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1503 "Invalid %s - derived type adds new item \"%s\".",
1504 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1505 return LY_EVALID;
1506 }
1507 match = v;
1508 }
1509
1510 if (basetype == LY_TYPE_ENUM) {
1511 if (enums_p[u].flags & LYS_SET_VALUE) {
1512 e->value = (int32_t)enums_p[u].value;
1513 if (!u || e->value >= value) {
1514 value = e->value + 1;
1515 }
1516 /* check collision with other values */
1517 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1518 if (e->value == (*enums)[v].value) {
1519 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1520 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1521 e->value, e->name, (*enums)[v].name);
1522 return LY_EVALID;
1523 }
1524 }
1525 } else if (base_enums) {
1526 /* inherit the assigned value */
1527 e->value = base_enums[match].value;
1528 if (!u || e->value >= value) {
1529 value = e->value + 1;
1530 }
1531 } else {
1532 /* assign value automatically */
1533 if (u && value == INT32_MIN) {
1534 /* counter overflow */
1535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1536 "Invalid enumeration - it is not possible to auto-assign enum value for "
1537 "\"%s\" since the highest value is already 2147483647.", e->name);
1538 return LY_EVALID;
1539 }
1540 e->value = value++;
1541 }
1542 } else { /* LY_TYPE_BITS */
1543 if (enums_p[u].flags & LYS_SET_VALUE) {
1544 e->value = (int32_t)enums_p[u].value;
1545 if (!u || (uint32_t)e->value >= position) {
1546 position = (uint32_t)e->value + 1;
1547 }
1548 /* check collision with other values */
1549 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1550 if (e->value == (*enums)[v].value) {
1551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1552 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1553 (uint32_t)e->value, e->name, (*enums)[v].name);
1554 return LY_EVALID;
1555 }
1556 }
1557 } else if (base_enums) {
1558 /* inherit the assigned value */
1559 e->value = base_enums[match].value;
1560 if (!u || (uint32_t)e->value >= position) {
1561 position = (uint32_t)e->value + 1;
1562 }
1563 } else {
1564 /* assign value automatically */
1565 if (u && position == 0) {
1566 /* counter overflow */
1567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1568 "Invalid bits - it is not possible to auto-assign bit position for "
1569 "\"%s\" since the highest value is already 4294967295.", e->name);
1570 return LY_EVALID;
1571 }
1572 e->value = position++;
1573 }
1574 }
1575
1576 if (base_enums) {
1577 /* the assigned values must not change from the derived type */
1578 if (e->value != base_enums[match].value) {
1579 if (basetype == LY_TYPE_ENUM) {
1580 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1581 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1582 e->name, base_enums[match].value, e->value);
1583 } else {
1584 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1585 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1586 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1587 }
1588 return LY_EVALID;
1589 }
1590 }
1591
1592 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1593 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1594
1595 if (basetype == LY_TYPE_BITS) {
1596 /* keep bits ordered by position */
1597 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1598 if (v != u) {
1599 memcpy(&storage, e, sizeof *e);
1600 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1601 memcpy(&(*enums)[v], &storage, sizeof storage);
1602 }
1603 }
1604 }
1605
1606done:
1607 return ret;
1608}
1609
Radek Krejcia3045382018-11-22 14:30:31 +01001610#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
1611 for ((NODE) = (NODE)->parent; \
1612 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
1613 (NODE) = (NODE)->parent); \
1614 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
1615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
1616 TERM; \
1617 }
1618
1619/**
1620 * @brief Validate the predicate(s) from the leafref path.
1621 * @param[in] ctx Compile context
1622 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
1623 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01001624 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01001625 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001626 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01001627 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1628 */
1629static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01001630lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01001631 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01001632{
1633 LY_ERR ret = LY_EVALID;
1634 const struct lys_module *mod;
1635 const struct lysc_node *src_node, *dst_node;
1636 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
1637 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01001638 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01001639 const char *start, *end, *pke_end;
1640 struct ly_set keys = {0};
1641 int i;
1642
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001643 assert(path_context);
1644
Radek Krejcia3045382018-11-22 14:30:31 +01001645 while (**predicate == '[') {
1646 start = (*predicate)++;
1647
1648 while (isspace(**predicate)) {
1649 ++(*predicate);
1650 }
1651 LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
1652 while (isspace(**predicate)) {
1653 ++(*predicate);
1654 }
1655 if (**predicate != '=') {
1656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001657 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
1658 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001659 goto cleanup;
1660 }
1661 ++(*predicate);
1662 while (isspace(**predicate)) {
1663 ++(*predicate);
1664 }
1665
1666 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
1667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1668 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
1669 goto cleanup;
1670 }
1671 --pke_end;
1672 while (isspace(*pke_end)) {
1673 --pke_end;
1674 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01001675 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01001676 /* localize path-key-expr */
1677 pke_start = path_key_expr = *predicate;
1678 /* move after the current predicate */
1679 *predicate = end + 1;
1680
1681 /* source (must be leaf or leaf-list) */
1682 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001683 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001684 if (!mod) {
1685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1686 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001687 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001688 goto cleanup;
1689 }
Radek Krejcia3045382018-11-22 14:30:31 +01001690 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001691 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001692 }
Radek Krejcibade82a2018-12-05 10:13:48 +01001693 src_node = NULL;
1694 if (context_node->keys) {
1695 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
1696 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
1697 src_node = (const struct lysc_node*)context_node->keys[u];
1698 break;
1699 }
1700 }
1701 }
Radek Krejcia3045382018-11-22 14:30:31 +01001702 if (!src_node) {
1703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001704 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001705 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01001706 goto cleanup;
1707 }
Radek Krejcia3045382018-11-22 14:30:31 +01001708
1709 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01001710 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01001711 i = ly_set_add(&keys, (void*)src_node, 0);
1712 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01001713 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01001714 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001715 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01001716 *predicate - start, start, src_node->name);
1717 goto cleanup;
1718 }
1719
1720 /* destination */
1721 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01001722 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01001723
1724 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
1725 if (strncmp(path_key_expr, "current()", 9)) {
1726 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1727 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
1728 *predicate - start, start);
1729 goto cleanup;
1730 }
1731 path_key_expr += 9;
1732 while (isspace(*path_key_expr)) {
1733 ++path_key_expr;
1734 }
1735
1736 if (*path_key_expr != '/') {
1737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1738 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
1739 *predicate - start, start);
1740 goto cleanup;
1741 }
1742 ++path_key_expr;
1743 while (isspace(*path_key_expr)) {
1744 ++path_key_expr;
1745 }
1746
1747 /* rel-path-keyexpr:
1748 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
1749 while (!strncmp(path_key_expr, "..", 2)) {
1750 ++dest_parent_times;
1751 path_key_expr += 2;
1752 while (isspace(*path_key_expr)) {
1753 ++path_key_expr;
1754 }
1755 if (*path_key_expr != '/') {
1756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1757 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
1758 *predicate - start, start);
1759 goto cleanup;
1760 }
1761 ++path_key_expr;
1762 while (isspace(*path_key_expr)) {
1763 ++path_key_expr;
1764 }
1765
1766 /* path is supposed to be evaluated in data tree, so we have to skip
1767 * all schema nodes that cannot be instantiated in data tree */
1768 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
1769 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
1770 *predicate - start, start);
1771 }
1772 if (!dest_parent_times) {
1773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1774 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
1775 *predicate - start, start);
1776 goto cleanup;
1777 }
1778 if (path_key_expr == pke_end) {
1779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1780 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
1781 *predicate - start, start);
1782 goto cleanup;
1783 }
1784
1785 while(path_key_expr != pke_end) {
1786 if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
1787 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01001788 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
1789 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01001790 goto cleanup;
1791 }
1792
1793 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01001794 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001795 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01001796 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01001797 }
1798 if (!mod) {
1799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1800 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.",
1801 *predicate - start, start, dst_len, dst);
1802 goto cleanup;
1803 }
1804
1805 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
1806 if (!dst_node) {
1807 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1808 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.",
1809 *predicate - start, start, path_key_expr - pke_start, pke_start);
1810 goto cleanup;
1811 }
1812 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001813 if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcia3045382018-11-22 14:30:31 +01001814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01001815 "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01001816 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
1817 goto cleanup;
1818 }
1819 }
1820
1821 ret = LY_SUCCESS;
1822cleanup:
1823 ly_set_erase(&keys, NULL);
1824 return ret;
1825}
1826
1827/**
1828 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
1829 *
1830 * path-arg = absolute-path / relative-path
1831 * absolute-path = 1*("/" (node-identifier *path-predicate))
1832 * relative-path = 1*(".." "/") descendant-path
1833 *
1834 * @param[in,out] path Path to parse.
1835 * @param[out] prefix Prefix of the token, NULL if there is not any.
1836 * @param[out] pref_len Length of the prefix, 0 if there is not any.
1837 * @param[out] name Name of the token.
1838 * @param[out] nam_len Length of the name.
1839 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
1840 * must not be changed between consecutive calls. -1 if the
1841 * path is absolute.
1842 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
1843 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
1844 */
1845static LY_ERR
1846lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
1847 int *parent_times, int *has_predicate)
1848{
1849 int par_times = 0;
1850
1851 assert(path && *path);
1852 assert(parent_times);
1853 assert(prefix);
1854 assert(prefix_len);
1855 assert(name);
1856 assert(name_len);
1857 assert(has_predicate);
1858
1859 *prefix = NULL;
1860 *prefix_len = 0;
1861 *name = NULL;
1862 *name_len = 0;
1863 *has_predicate = 0;
1864
1865 if (!*parent_times) {
1866 if (!strncmp(*path, "..", 2)) {
1867 *path += 2;
1868 ++par_times;
1869 while (!strncmp(*path, "/..", 3)) {
1870 *path += 3;
1871 ++par_times;
1872 }
1873 }
1874 if (par_times) {
1875 *parent_times = par_times;
1876 } else {
1877 *parent_times = -1;
1878 }
1879 }
1880
1881 if (**path != '/') {
1882 return LY_EINVAL;
1883 }
1884 /* skip '/' */
1885 ++(*path);
1886
1887 /* node-identifier ([prefix:]name) */
1888 LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len));
1889
1890 if ((**path == '/' && (*path)[1]) || !**path) {
1891 /* path continues by another token or this is the last token */
1892 return LY_SUCCESS;
1893 } else if ((*path)[0] != '[') {
1894 /* unexpected character */
1895 return LY_EINVAL;
1896 } else {
1897 /* predicate starting with [ */
1898 *has_predicate = 1;
1899 return LY_SUCCESS;
1900 }
1901}
1902
1903/**
Radek Krejci58d171e2018-11-23 13:50:55 +01001904 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
1905 *
1906 * The set of features used for target must be a subset of features used for the leafref.
1907 * This is not a perfect, we should compare the truth tables but it could require too much resources
1908 * and RFC 7950 does not require it explicitely, so we simplify that.
1909 *
1910 * @param[in] refnode The leafref node.
1911 * @param[in] target Tha target node of the leafref.
1912 * @return LY_SUCCESS or LY_EVALID;
1913 */
1914static LY_ERR
1915lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
1916{
1917 LY_ERR ret = LY_EVALID;
1918 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01001919 unsigned int u, v, count;
1920 struct ly_set features = {0};
1921
1922 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01001923 if (iter->iffeatures) {
1924 LY_ARRAY_FOR(iter->iffeatures, u) {
1925 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
1926 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01001927 }
1928 }
1929 }
1930 }
1931
1932 /* we should have, in features set, a superset of features applicable to the target node.
1933 * So when adding features applicable to the target into the features set, we should not be
1934 * able to actually add any new feature, otherwise it is not a subset of features applicable
1935 * to the leafref itself. */
1936 count = features.count;
1937 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01001938 if (iter->iffeatures) {
1939 LY_ARRAY_FOR(iter->iffeatures, u) {
1940 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
1941 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01001942 /* new feature was added (or LY_EMEM) */
1943 goto cleanup;
1944 }
1945 }
1946 }
1947 }
1948 }
1949 ret = LY_SUCCESS;
1950
1951cleanup:
1952 ly_set_erase(&features, NULL);
1953 return ret;
1954}
1955
1956/**
Radek Krejcia3045382018-11-22 14:30:31 +01001957 * @brief Validate the leafref path.
1958 * @param[in] ctx Compile context
1959 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01001960 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01001961 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1962 */
1963static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01001964lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01001965{
1966 const struct lysc_node *node = NULL, *parent = NULL;
1967 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001968 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01001969 const char *id, *prefix, *name;
1970 size_t prefix_len, name_len;
1971 int parent_times = 0, has_predicate;
1972 unsigned int iter, u;
1973 LY_ERR ret = LY_SUCCESS;
1974
1975 assert(ctx);
1976 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01001977 assert(leafref);
1978
1979 /* 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 +01001980
1981 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01001982 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01001983 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
1984 if (!iter) { /* first iteration */
1985 /* precess ".." in relative paths */
1986 if (parent_times > 0) {
1987 /* move from the context node */
1988 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
1989 /* path is supposed to be evaluated in data tree, so we have to skip
1990 * all schema nodes that cannot be instantiated in data tree */
1991 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01001992 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01001993 }
1994 }
1995 }
1996
1997 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01001998 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01001999 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002000 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002001 }
2002 if (!mod) {
2003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002004 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2005 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002006 return LY_EVALID;
2007 }
2008
2009 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2010 if (!node) {
2011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002012 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002013 return LY_EVALID;
2014 }
2015 parent = node;
2016
2017 if (has_predicate) {
2018 /* we have predicate, so the current result must be list */
2019 if (node->nodetype != LYS_LIST) {
2020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2021 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002022 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002023 return LY_EVALID;
2024 }
2025
Radek Krejcibade82a2018-12-05 10:13:48 +01002026 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2027 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002028 }
2029
2030 ++iter;
2031 }
2032 if (ret) {
2033 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002034 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002035 return LY_EVALID;
2036 }
2037
2038 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2039 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2040 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002041 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002042 return LY_EVALID;
2043 }
2044
2045 /* check status */
2046 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2047 return LY_EVALID;
2048 }
2049
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002050 /* check config */
2051 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2052 if (node->flags & LYS_CONFIG_R) {
2053 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2054 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2055 leafref->path);
2056 return LY_EVALID;
2057 }
2058 }
2059
Radek Krejci412ddfa2018-11-23 11:44:11 +01002060 /* store the target's type and check for circular chain of leafrefs */
2061 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2062 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2063 if (type == (struct lysc_type*)leafref) {
2064 /* circular chain detected */
2065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2066 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2067 return LY_EVALID;
2068 }
2069 }
2070
Radek Krejci58d171e2018-11-23 13:50:55 +01002071 /* check if leafref and its target are under common if-features */
2072 if (lys_compile_leafref_features_validate(startnode, node)) {
2073 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2074 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2075 leafref->path);
2076 return LY_EVALID;
2077 }
2078
Radek Krejcia3045382018-11-22 14:30:31 +01002079 return LY_SUCCESS;
2080}
2081
Radek Krejcicdfecd92018-11-26 11:27:32 +01002082static 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 +01002083 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002084/**
2085 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2086 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002087 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2088 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2089 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2090 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002091 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002092 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002093 * @param[in] basetype Base YANG built-in type of the type to compile.
2094 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2095 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2096 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2097 * @param[out] type Newly created type structure with the filled information about the type.
2098 * @return LY_ERR value.
2099 */
Radek Krejci19a96102018-11-15 13:38:09 +01002100static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002101lys_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,
2102 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, const char *tpdfname,
2103 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002104{
2105 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002106 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002107 struct lysc_type_bin *bin;
2108 struct lysc_type_num *num;
2109 struct lysc_type_str *str;
2110 struct lysc_type_bits *bits;
2111 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002112 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002113 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002114 struct lysc_type_union *un, *un_aux;
2115 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002116
2117 switch (basetype) {
2118 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002119 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002120
2121 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002122 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002123 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002124 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
2125 LY_CHECK_RET(ret);
2126 if (!tpdfname) {
2127 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
2128 options, u, lys_compile_ext, ret, done);
2129 }
2130 }
2131
2132 if (tpdfname) {
2133 type_p->compiled = *type;
2134 *type = calloc(1, sizeof(struct lysc_type_bin));
2135 }
2136 break;
2137 case LY_TYPE_BITS:
2138 /* RFC 7950 9.7 - bits */
2139 bits = (struct lysc_type_bits*)(*type);
2140 if (type_p->bits) {
2141 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
2142 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2143 (struct lysc_type_enum_item**)&bits->bits);
2144 LY_CHECK_RET(ret);
2145 }
2146
Radek Krejci555cb5b2018-11-16 14:54:33 +01002147 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002148 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002149 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002151 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002153 free(*type);
2154 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002155 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002156 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002157 }
2158
2159 if (tpdfname) {
2160 type_p->compiled = *type;
2161 *type = calloc(1, sizeof(struct lysc_type_bits));
2162 }
2163 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002164 case LY_TYPE_DEC64:
2165 dec = (struct lysc_type_dec*)(*type);
2166
2167 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002168 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002169 if (!type_p->fraction_digits) {
2170 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002171 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002172 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002173 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002174 free(*type);
2175 *type = NULL;
2176 }
2177 return LY_EVALID;
2178 }
2179 } else if (type_p->fraction_digits) {
2180 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002181 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2183 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002184 tpdfname);
2185 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2187 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002188 free(*type);
2189 *type = NULL;
2190 }
2191 return LY_EVALID;
2192 }
2193 dec->fraction_digits = type_p->fraction_digits;
2194
2195 /* RFC 7950 9.2.4 - range */
2196 if (type_p->range) {
2197 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2198 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
2199 LY_CHECK_RET(ret);
2200 if (!tpdfname) {
2201 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
2202 options, u, lys_compile_ext, ret, done);
2203 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002204 }
2205
2206 if (tpdfname) {
2207 type_p->compiled = *type;
2208 *type = calloc(1, sizeof(struct lysc_type_dec));
2209 }
2210 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002211 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002212 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002213
2214 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002215 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002216 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002217 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
2218 LY_CHECK_RET(ret);
2219 if (!tpdfname) {
2220 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
2221 options, u, lys_compile_ext, ret, done);
2222 }
2223 } else if (base && ((struct lysc_type_str*)base)->length) {
2224 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2225 }
2226
2227 /* RFC 7950 9.4.5 - pattern */
2228 if (type_p->patterns) {
2229 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
2230 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
2231 LY_CHECK_RET(ret);
2232 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2233 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2234 }
2235
2236 if (tpdfname) {
2237 type_p->compiled = *type;
2238 *type = calloc(1, sizeof(struct lysc_type_str));
2239 }
2240 break;
2241 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002242 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002243
2244 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002245 if (type_p->enums) {
2246 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
2247 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
2248 LY_CHECK_RET(ret);
2249 }
2250
Radek Krejci555cb5b2018-11-16 14:54:33 +01002251 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002252 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002253 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002255 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002257 free(*type);
2258 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002259 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002260 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002261 }
2262
2263 if (tpdfname) {
2264 type_p->compiled = *type;
2265 *type = calloc(1, sizeof(struct lysc_type_enum));
2266 }
2267 break;
2268 case LY_TYPE_INT8:
2269 case LY_TYPE_UINT8:
2270 case LY_TYPE_INT16:
2271 case LY_TYPE_UINT16:
2272 case LY_TYPE_INT32:
2273 case LY_TYPE_UINT32:
2274 case LY_TYPE_INT64:
2275 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002276 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002277
2278 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002279 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01002280 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01002281 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
2282 LY_CHECK_RET(ret);
2283 if (!tpdfname) {
2284 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
2285 options, u, lys_compile_ext, ret, done);
2286 }
2287 }
2288
2289 if (tpdfname) {
2290 type_p->compiled = *type;
2291 *type = calloc(1, sizeof(struct lysc_type_num));
2292 }
2293 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002294 case LY_TYPE_IDENT:
2295 idref = (struct lysc_type_identityref*)(*type);
2296
2297 /* RFC 7950 9.10.2 - base */
2298 if (type_p->bases) {
2299 if (base) {
2300 /* only the directly derived identityrefs can contain base specification */
2301 if (tpdfname) {
2302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002303 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002304 tpdfname);
2305 } else {
2306 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002307 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002308 free(*type);
2309 *type = NULL;
2310 }
2311 return LY_EVALID;
2312 }
2313 ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases);
2314 LY_CHECK_RET(ret);
2315 }
2316
2317 if (!base && !type_p->flags) {
2318 /* type derived from identityref built-in type must contain at least one base */
2319 if (tpdfname) {
2320 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2321 } else {
2322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2323 free(*type);
2324 *type = NULL;
2325 }
2326 return LY_EVALID;
2327 }
2328
2329 if (tpdfname) {
2330 type_p->compiled = *type;
2331 *type = calloc(1, sizeof(struct lysc_type_identityref));
2332 }
2333 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002334 case LY_TYPE_LEAFREF:
2335 /* RFC 7950 9.9.3 - require-instance */
2336 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002337 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002338 if (tpdfname) {
2339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2340 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2341 } else {
2342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2343 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2344 free(*type);
2345 *type = NULL;
2346 }
2347 return LY_EVALID;
2348 }
Radek Krejcia3045382018-11-22 14:30:31 +01002349 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002350 } else if (base) {
2351 /* inherit */
2352 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002353 } else {
2354 /* default is true */
2355 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2356 }
2357 if (type_p->path) {
2358 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002359 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002360 } else if (base) {
2361 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002362 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002363 } else if (tpdfname) {
2364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2365 return LY_EVALID;
2366 } else {
2367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2368 free(*type);
2369 *type = NULL;
2370 return LY_EVALID;
2371 }
2372 if (tpdfname) {
2373 type_p->compiled = *type;
2374 *type = calloc(1, sizeof(struct lysc_type_leafref));
2375 }
2376 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002377 case LY_TYPE_INST:
2378 /* RFC 7950 9.9.3 - require-instance */
2379 if (type_p->flags & LYS_SET_REQINST) {
2380 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2381 } else {
2382 /* default is true */
2383 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2384 }
2385
2386 if (tpdfname) {
2387 type_p->compiled = *type;
2388 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2389 }
2390 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002391 case LY_TYPE_UNION:
2392 un = (struct lysc_type_union*)(*type);
2393
2394 /* RFC 7950 7.4 - type */
2395 if (type_p->types) {
2396 if (base) {
2397 /* only the directly derived union can contain types specification */
2398 if (tpdfname) {
2399 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2400 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2401 tpdfname);
2402 } else {
2403 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2404 "Invalid type substatement for the type not directly derived from union built-in type.");
2405 free(*type);
2406 *type = NULL;
2407 }
2408 return LY_EVALID;
2409 }
2410 /* compile the type */
2411 additional = 0;
2412 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2413 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejci01342af2019-01-03 15:18:08 +01002414 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 +01002415 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2416 /* add space for additional types from the union subtype */
2417 un_aux = (struct lysc_type_union *)un->types[u + additional];
2418 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)));
2419 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2420 un->types = (void*)((uint32_t*)(p) + 1);
2421
2422 /* copy subtypes of the subtype union */
2423 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2424 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2425 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2426 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2427 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2428 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2429 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2430 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2431 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2432 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2433 /* TODO extensions */
2434
2435 } else {
2436 un->types[u + additional] = un_aux->types[v];
2437 ++un_aux->types[v]->refcount;
2438 }
2439 ++additional;
2440 LY_ARRAY_INCREMENT(un->types);
2441 }
2442 /* compensate u increment in main loop */
2443 --additional;
2444
2445 /* free the replaced union subtype */
2446 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2447 } else {
2448 LY_ARRAY_INCREMENT(un->types);
2449 }
2450 LY_CHECK_RET(ret);
2451 }
2452 }
2453
2454 if (!base && !type_p->flags) {
2455 /* type derived from union built-in type must contain at least one type */
2456 if (tpdfname) {
2457 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2458 } else {
2459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2460 free(*type);
2461 *type = NULL;
2462 }
2463 return LY_EVALID;
2464 }
2465
2466 if (tpdfname) {
2467 type_p->compiled = *type;
2468 *type = calloc(1, sizeof(struct lysc_type_union));
2469 }
2470 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002471 case LY_TYPE_BOOL:
2472 case LY_TYPE_EMPTY:
2473 case LY_TYPE_UNKNOWN: /* just to complete switch */
2474 break;
2475 }
2476 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2477done:
2478 return ret;
2479}
2480
Radek Krejcia3045382018-11-22 14:30:31 +01002481/**
2482 * @brief Compile information about the leaf/leaf-list's type.
2483 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002484 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2485 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2486 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2487 * @param[in] context_name Name of the context node or referencing typedef for logging.
2488 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002489 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2490 * @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 +01002491 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002492 * @return LY_ERR value.
2493 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002494static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002495lys_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 +01002496 struct lysp_type *type_p, int options, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002497{
2498 LY_ERR ret = LY_SUCCESS;
2499 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002500 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002501 struct type_context {
2502 const struct lysp_tpdf *tpdf;
2503 struct lysp_node *node;
2504 struct lysp_module *mod;
2505 } *tctx, *tctx_prev = NULL;
2506 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002507 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002508 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002509 const char *dflt = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002510
2511 (*type) = NULL;
2512
2513 tctx = calloc(1, sizeof *tctx);
2514 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002515 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002516 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2517 ret == LY_SUCCESS;
2518 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2519 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2520 if (basetype) {
2521 break;
2522 }
2523
2524 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002525 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
2526 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01002527 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2528
Radek Krejcicdfecd92018-11-26 11:27:32 +01002529 if (units && !*units) {
2530 /* inherit units */
2531 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
2532 }
Radek Krejci01342af2019-01-03 15:18:08 +01002533 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002534 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01002535 dflt = tctx->tpdf->dflt;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002536 }
Radek Krejci01342af2019-01-03 15:18:08 +01002537 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002538 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2539 break;
2540 }
2541
Radek Krejci19a96102018-11-15 13:38:09 +01002542 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002543 /* it is not necessary to continue, the rest of the chain was already compiled,
2544 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01002545 basetype = tctx->tpdf->type.compiled->basetype;
2546 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01002547 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01002548 dummyloops = 1;
2549 goto preparenext;
2550 } else {
2551 tctx = NULL;
2552 break;
2553 }
Radek Krejci19a96102018-11-15 13:38:09 +01002554 }
2555
2556 /* store information for the following processing */
2557 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
2558
Radek Krejcicdfecd92018-11-26 11:27:32 +01002559preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01002560 /* prepare next loop */
2561 tctx_prev = tctx;
2562 tctx = calloc(1, sizeof *tctx);
2563 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2564 }
2565 free(tctx);
2566
2567 /* allocate type according to the basetype */
2568 switch (basetype) {
2569 case LY_TYPE_BINARY:
2570 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01002571 break;
2572 case LY_TYPE_BITS:
2573 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01002574 break;
2575 case LY_TYPE_BOOL:
2576 case LY_TYPE_EMPTY:
2577 *type = calloc(1, sizeof(struct lysc_type));
2578 break;
2579 case LY_TYPE_DEC64:
2580 *type = calloc(1, sizeof(struct lysc_type_dec));
2581 break;
2582 case LY_TYPE_ENUM:
2583 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01002584 break;
2585 case LY_TYPE_IDENT:
2586 *type = calloc(1, sizeof(struct lysc_type_identityref));
2587 break;
2588 case LY_TYPE_INST:
2589 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2590 break;
2591 case LY_TYPE_LEAFREF:
2592 *type = calloc(1, sizeof(struct lysc_type_leafref));
2593 break;
2594 case LY_TYPE_STRING:
2595 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01002596 break;
2597 case LY_TYPE_UNION:
2598 *type = calloc(1, sizeof(struct lysc_type_union));
2599 break;
2600 case LY_TYPE_INT8:
2601 case LY_TYPE_UINT8:
2602 case LY_TYPE_INT16:
2603 case LY_TYPE_UINT16:
2604 case LY_TYPE_INT32:
2605 case LY_TYPE_UINT32:
2606 case LY_TYPE_INT64:
2607 case LY_TYPE_UINT64:
2608 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01002609 break;
2610 case LY_TYPE_UNKNOWN:
2611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2612 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2613 ret = LY_EVALID;
2614 goto cleanup;
2615 }
2616 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002617 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01002618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
2619 ly_data_type2str[basetype]);
2620 free(*type);
2621 (*type) = NULL;
2622 ret = LY_EVALID;
2623 goto cleanup;
2624 }
2625
2626 /* get restrictions from the referred typedefs */
2627 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2628 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci43699232018-11-23 14:59:46 +01002629 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01002630 base = tctx->tpdf->type.compiled;
2631 continue;
Radek Krejci43699232018-11-23 14:59:46 +01002632 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01002633 /* no change, just use the type information from the base */
2634 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2635 ++base->refcount;
2636 continue;
2637 }
2638
2639 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01002640 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
2641 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
2642 tctx->tpdf->name, ly_data_type2str[basetype]);
2643 ret = LY_EVALID;
2644 goto cleanup;
2645 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
2646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2647 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2648 tctx->tpdf->name, tctx->tpdf->dflt);
2649 ret = LY_EVALID;
2650 goto cleanup;
2651 }
2652
Radek Krejci19a96102018-11-15 13:38:09 +01002653 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002654 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002655 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 +01002656 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
2657 basetype, options, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002658 LY_CHECK_GOTO(ret, cleanup);
2659 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002660 }
2661
Radek Krejcic5c27e52018-11-15 14:38:11 +01002662 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01002663 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01002664 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01002665 (*type)->basetype = basetype;
2666 ++(*type)->refcount;
Radek Krejcie86bf772018-12-14 11:39:53 +01002667 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 +01002668 LY_CHECK_GOTO(ret, cleanup);
2669 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01002670 /* no specific restriction in leaf's type definition, copy from the base */
2671 free(*type);
2672 (*type) = base;
2673 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01002674 }
Radek Krejci01342af2019-01-03 15:18:08 +01002675 if (!(*type)->dflt) {
2676 DUP_STRING(ctx->ctx, dflt, (*type)->dflt);
2677 }
Radek Krejci19a96102018-11-15 13:38:09 +01002678
2679 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
2680
2681cleanup:
2682 ly_set_erase(&tpdf_chain, free);
2683 return ret;
2684}
2685
Radek Krejcib1b59152019-01-07 13:21:56 +01002686static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01002687
Radek Krejcia3045382018-11-22 14:30:31 +01002688/**
2689 * @brief Compile parsed container node information.
2690 * @param[in] ctx Compile context
2691 * @param[in] node_p Parsed container node.
2692 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2693 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2694 * is enriched with the container-specific information.
2695 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2696 */
Radek Krejci19a96102018-11-15 13:38:09 +01002697static LY_ERR
2698lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2699{
2700 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
2701 struct lysc_node_container *cont = (struct lysc_node_container*)node;
2702 struct lysp_node *child_p;
2703 unsigned int u;
2704 LY_ERR ret = LY_SUCCESS;
2705
Radek Krejci19a96102018-11-15 13:38:09 +01002706 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01002707 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01002708 }
2709
2710 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
2711 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
2712 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
2713
2714done:
2715 return ret;
2716}
2717
Radek Krejcia3045382018-11-22 14:30:31 +01002718/**
2719 * @brief Compile parsed leaf node information.
2720 * @param[in] ctx Compile context
2721 * @param[in] node_p Parsed leaf node.
2722 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2723 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2724 * is enriched with the leaf-specific information.
2725 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2726 */
Radek Krejci19a96102018-11-15 13:38:09 +01002727static LY_ERR
2728lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2729{
2730 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
2731 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
2732 unsigned int u;
2733 LY_ERR ret = LY_SUCCESS;
2734
Radek Krejci19a96102018-11-15 13:38:09 +01002735 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002736 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
2737 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
Radek Krejci76b3e962018-12-14 17:01:25 +01002738 if (leaf->dflt) {
2739 leaf->flags |= LYS_SET_DFLT;
2740 }
Radek Krejci43699232018-11-23 14:59:46 +01002741
Radek Krejcie86bf772018-12-14 11:39:53 +01002742 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 +01002743 leaf->units ? NULL : &leaf->units);
Radek Krejci19a96102018-11-15 13:38:09 +01002744 LY_CHECK_GOTO(ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002745 if (!leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
2746 DUP_STRING(ctx->ctx, leaf->type->dflt, leaf->dflt);
2747 }
Radek Krejcia3045382018-11-22 14:30:31 +01002748 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
2749 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2750 ly_set_add(&ctx->unres, leaf, 0);
Radek Krejcicdfecd92018-11-26 11:27:32 +01002751 } else if (leaf->type->basetype == LY_TYPE_UNION) {
2752 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
2753 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2754 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2755 ly_set_add(&ctx->unres, leaf, 0);
2756 }
2757 }
Radek Krejci43699232018-11-23 14:59:46 +01002758 } else if (leaf->type->basetype == LY_TYPE_EMPTY && leaf_p->dflt) {
2759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2760 "Leaf of type \"empty\" must not have a default value (%s).",leaf_p->dflt);
2761 return LY_EVALID;
Radek Krejcia3045382018-11-22 14:30:31 +01002762 }
2763
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002764 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2765
Radek Krejci19a96102018-11-15 13:38:09 +01002766done:
2767 return ret;
2768}
2769
Radek Krejcia3045382018-11-22 14:30:31 +01002770/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01002771 * @brief Compile parsed leaf-list node information.
2772 * @param[in] ctx Compile context
2773 * @param[in] node_p Parsed leaf-list node.
2774 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2775 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2776 * is enriched with the leaf-list-specific information.
2777 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2778 */
2779static LY_ERR
2780lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2781{
2782 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
2783 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
2784 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002785 LY_ERR ret = LY_SUCCESS;
2786
Radek Krejci0e5d8382018-11-28 16:37:53 +01002787 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, options, u, lys_compile_must, ret, done);
2788 DUP_STRING(ctx->ctx, llist_p->units, llist->units);
2789
2790 if (llist_p->dflts) {
2791 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
2792 LY_ARRAY_FOR(llist_p->dflts, u) {
2793 DUP_STRING(ctx->ctx, llist_p->dflts[u], llist->dflts[u]);
2794 LY_ARRAY_INCREMENT(llist->dflts);
2795 }
2796 }
2797
2798 llist->min = llist_p->min;
Radek Krejcib7408632018-11-28 17:12:11 +01002799 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01002800
Radek Krejcie86bf772018-12-14 11:39:53 +01002801 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 +01002802 llist->units ? NULL : &llist->units);
Radek Krejci0e5d8382018-11-28 16:37:53 +01002803 LY_CHECK_GOTO(ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002804 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01002805 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, 1, ret, done);
Radek Krejci01342af2019-01-03 15:18:08 +01002806 DUP_STRING(ctx->ctx, llist->type->dflt, llist->dflts[0]);
Radek Krejci0e5d8382018-11-28 16:37:53 +01002807 LY_ARRAY_INCREMENT(llist->dflts);
2808 }
2809
2810 if (llist->type->basetype == LY_TYPE_LEAFREF) {
2811 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2812 ly_set_add(&ctx->unres, llist, 0);
2813 } else if (llist->type->basetype == LY_TYPE_UNION) {
2814 LY_ARRAY_FOR(((struct lysc_type_union*)llist->type)->types, u) {
2815 if (((struct lysc_type_union*)llist->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2816 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
2817 ly_set_add(&ctx->unres, llist, 0);
2818 }
2819 }
Radek Krejci6bb080b2018-11-28 17:23:41 +01002820 } else if (llist->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002821 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci6bb080b2018-11-28 17:23:41 +01002822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2823 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2824 return LY_EVALID;
2825 } else if (llist_p->dflts) {
2826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2827 "Leaf-list of type \"empty\" must not have a default value (%s).", llist_p->dflts[0]);
2828 return LY_EVALID;
2829 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01002830 }
2831
2832 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
2833 /* configuration data values must be unique - so check the default values */
2834 LY_ARRAY_FOR(llist->dflts, u) {
2835 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
2836 if (!strcmp(llist->dflts[u], llist->dflts[v])) {
2837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2838 "Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[v]);
2839 return LY_EVALID;
2840 }
2841 }
2842 }
2843 }
2844
2845 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
2846
2847done:
2848 return ret;
2849}
2850
2851/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002852 * @brief Compile parsed list node information.
2853 * @param[in] ctx Compile context
2854 * @param[in] node_p Parsed list node.
2855 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
2856 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2857 * is enriched with the list-specific information.
2858 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2859 */
2860static LY_ERR
2861lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
2862{
2863 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
2864 struct lysc_node_list *list = (struct lysc_node_list*)node;
2865 struct lysp_node *child_p;
2866 struct lysc_node_leaf **key, ***unique;
2867 size_t len;
2868 unsigned int u, v;
2869 const char *keystr, *delim;
2870 int config;
2871 LY_ERR ret = LY_SUCCESS;
2872
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002873 list->min = list_p->min;
2874 list->max = list_p->max ? list_p->max : (uint32_t)-1;
2875
2876 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01002877 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002878 }
2879
2880 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, options, u, lys_compile_must, ret, done);
2881
2882 /* keys */
2883 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
2884 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
2885 return LY_EVALID;
2886 }
2887
2888 /* find all the keys (must be direct children) */
2889 keystr = list_p->key;
2890 while (keystr) {
2891 delim = strpbrk(keystr, " \t\n");
2892 if (delim) {
2893 len = delim - keystr;
2894 while (isspace(*delim)) {
2895 ++delim;
2896 }
2897 } else {
2898 len = strlen(keystr);
2899 }
2900
2901 /* key node must be present */
2902 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
2903 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
2904 if (!(*key)) {
2905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2906 "The list's key \"%.*s\" not found.", len, keystr);
2907 return LY_EVALID;
2908 }
2909 /* keys must be unique */
2910 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
2911 if (*key == list->keys[u]) {
2912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2913 "Duplicated key identifier \"%.*s\".", len, keystr);
2914 return LY_EVALID;
2915 }
2916 }
2917 /* key must have the same config flag as the list itself */
2918 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
2919 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
2920 return LY_EVALID;
2921 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002922 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002923 /* YANG 1.0 denies key to be of empty type */
2924 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
2925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2926 "Key of a list can be of type \"empty\" only in YANG 1.1 modules.");
2927 return LY_EVALID;
2928 }
2929 } else {
2930 /* when and if-feature are illegal on list keys */
2931 if ((*key)->when) {
2932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2933 "List's key \"%s\" must not have any \"when\" statement.", (*key)->name);
2934 return LY_EVALID;
2935 }
2936 if ((*key)->iffeatures) {
2937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2938 "List's key \"%s\" must not have any \"if-feature\" statement.", (*key)->name);
2939 return LY_EVALID;
2940 }
2941 }
Radek Krejci76b3e962018-12-14 17:01:25 +01002942
2943 /* check status */
2944 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
2945 (*key)->flags, (*key)->module, (*key)->name));
2946
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002947 /* ignore default values of the key */
2948 if ((*key)->dflt) {
2949 lydict_remove(ctx->ctx, (*key)->dflt);
2950 (*key)->dflt = NULL;
2951 }
2952 /* mark leaf as key */
2953 (*key)->flags |= LYS_KEY;
2954
2955 /* next key value */
2956 keystr = delim;
2957 }
2958
2959 /* uniques */
2960 if (list_p->uniques) {
2961 for (v = 0; v < LY_ARRAY_SIZE(list_p->uniques); ++v) {
2962 config = -1;
2963 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2964 keystr = list_p->uniques[v];
2965 while (keystr) {
2966 delim = strpbrk(keystr, " \t\n");
2967 if (delim) {
2968 len = delim - keystr;
2969 while (isspace(*delim)) {
2970 ++delim;
2971 }
2972 } else {
2973 len = strlen(keystr);
2974 }
2975
2976 /* unique node must be present */
2977 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
2978 ret = lys_resolve_descendant_schema_nodeid(ctx, keystr, len, node, LYS_LEAF, (const struct lysc_node**)key);
2979 if (ret != LY_SUCCESS) {
2980 if (ret == LY_EDENIED) {
2981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2982 "Unique's descendant-schema-nodeid \"%.*s\" refers to a %s node instead of a leaf.",
2983 len, keystr, lys_nodetype2str((*key)->nodetype));
2984 }
2985 return LY_EVALID;
2986 }
2987
2988 /* all referenced leafs must be of the same config type */
2989 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
2990 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2991 "Unique statement \"%s\" refers to leafs with different config type.", list_p->uniques[v]);
2992 return LY_EVALID;
2993 } else if ((*key)->flags & LYS_CONFIG_W) {
2994 config = 1;
2995 } else { /* LYS_CONFIG_R */
2996 config = 0;
2997 }
2998
Radek Krejci76b3e962018-12-14 17:01:25 +01002999 /* check status */
3000 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3001 (*key)->flags, (*key)->module, (*key)->name));
3002
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003003 /* mark leaf as unique */
3004 (*key)->flags |= LYS_UNIQUE;
3005
3006 /* next unique value in line */
3007 keystr = delim;
3008 }
3009 /* next unique definition */
3010 }
3011 }
3012
3013 //COMPILE_ARRAY_GOTO(ctx, list_p->actions, list->actions, options, u, lys_compile_action, ret, done);
3014 //COMPILE_ARRAY_GOTO(ctx, list_p->notifs, list->notifs, options, u, lys_compile_notif, ret, done);
3015
3016done:
3017 return ret;
3018}
3019
Radek Krejci76b3e962018-12-14 17:01:25 +01003020static LY_ERR
3021lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3022{
3023 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3024 const char *prefix = NULL, *name;
3025 size_t prefix_len = 0;
3026
3027 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3028 name = strchr(dflt, ':');
3029 if (name) {
3030 prefix = dflt;
3031 prefix_len = name - prefix;
3032 ++name;
3033 } else {
3034 name = dflt;
3035 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003036 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003037 /* prefixed default case make sense only for the prefix of the schema itself */
3038 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3039 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3040 prefix_len, prefix);
3041 return LY_EVALID;
3042 }
3043 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3044 if (!ch->dflt) {
3045 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3046 "Default case \"%s\" not found.", dflt);
3047 return LY_EVALID;
3048 }
3049 /* no mandatory nodes directly under the default case */
3050 LY_LIST_FOR(ch->dflt->child, iter) {
3051 if (iter->flags & LYS_MAND_TRUE) {
3052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3053 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3054 return LY_EVALID;
3055 }
3056 }
Radek Krejci01342af2019-01-03 15:18:08 +01003057 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003058 return LY_SUCCESS;
3059}
3060
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003061/**
Radek Krejci056d0a82018-12-06 16:57:25 +01003062 * @brief Compile parsed choice node information.
3063 * @param[in] ctx Compile context
3064 * @param[in] node_p Parsed choice node.
3065 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3066 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01003067 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01003068 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3069 */
3070static LY_ERR
3071lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3072{
3073 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
3074 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
3075 struct lysp_node *child_p, *case_child_p;
Radek Krejcia9026eb2018-12-12 16:04:47 +01003076 struct lys_module;
Radek Krejci056d0a82018-12-06 16:57:25 +01003077 LY_ERR ret = LY_SUCCESS;
3078
Radek Krejci056d0a82018-12-06 16:57:25 +01003079 LY_LIST_FOR(ch_p->child, child_p) {
3080 if (child_p->nodetype == LYS_CASE) {
3081 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003082 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003083 }
3084 } else {
Radek Krejcib1b59152019-01-07 13:21:56 +01003085 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01003086 }
3087 }
3088
3089 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01003090 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003091 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01003092 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003093
Radek Krejci9800fb82018-12-13 14:26:23 +01003094 return ret;
3095}
3096
3097/**
3098 * @brief Compile parsed anydata or anyxml node information.
3099 * @param[in] ctx Compile context
3100 * @param[in] node_p Parsed anydata or anyxml node.
3101 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3102 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3103 * is enriched with the any-specific information.
3104 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3105 */
3106static LY_ERR
3107lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
3108{
3109 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
3110 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
3111 unsigned int u;
3112 LY_ERR ret = LY_SUCCESS;
3113
3114 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, options, u, lys_compile_must, ret, done);
3115
3116 if (any->flags & LYS_CONFIG_W) {
3117 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
3118 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
3119 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003120done:
3121 return ret;
3122}
3123
3124static LY_ERR
Radek Krejci76b3e962018-12-14 17:01:25 +01003125lys_compile_status_check(struct lysc_ctx *ctx, uint16_t node_flags, uint16_t parent_flags)
3126{
3127 /* check status compatibility with the parent */
3128 if ((parent_flags & LYS_STATUS_MASK) > (node_flags & LYS_STATUS_MASK)) {
3129 if (node_flags & LYS_STATUS_CURR) {
3130 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3131 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3132 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3133 } else { /* LYS_STATUS_DEPRC */
3134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3135 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3136 }
3137 return LY_EVALID;
3138 }
3139 return LY_SUCCESS;
3140}
3141
3142static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01003143lys_compile_status(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t parent_flags)
Radek Krejci056d0a82018-12-06 16:57:25 +01003144{
Radek Krejci056d0a82018-12-06 16:57:25 +01003145 /* status - it is not inherited by specification, but it does not make sense to have
3146 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3147 if (!(node->flags & LYS_STATUS_MASK)) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003148 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3149 if ((parent_flags & 0x3) != 0x3) {
3150 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3151 * combination of bits (0x3) which marks the uses_status value */
3152 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3153 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3154 }
3155 node->flags |= parent_flags & LYS_STATUS_MASK;
Radek Krejci056d0a82018-12-06 16:57:25 +01003156 } else {
3157 node->flags |= LYS_STATUS_CURR;
3158 }
Radek Krejcib1b59152019-01-07 13:21:56 +01003159 } else if (parent_flags & LYS_STATUS_MASK) {
3160 return lys_compile_status_check(ctx, node->flags, parent_flags);
Radek Krejci056d0a82018-12-06 16:57:25 +01003161 }
3162 return LY_SUCCESS;
3163}
3164
3165static LY_ERR
3166lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3167 const struct lysc_action *actions, const struct lysc_notif *notifs,
3168 const char *name, void *exclude)
3169{
3170 const struct lysc_node *iter;
3171 unsigned int u;
3172
3173 LY_LIST_FOR(children, iter) {
3174 if (iter != exclude && !strcmp(name, iter->name)) {
3175 goto error;
3176 }
3177 }
3178 LY_ARRAY_FOR(actions, u) {
3179 if (&actions[u] != exclude && !strcmp(name, actions[u].name)) {
3180 goto error;
3181 }
3182 }
3183 LY_ARRAY_FOR(notifs, u) {
3184 if (&notifs[u] != exclude && !strcmp(name, notifs[u].name)) {
3185 goto error;
3186 }
3187 }
3188 return LY_SUCCESS;
3189error:
3190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition");
3191 return LY_EEXIST;
3192}
3193
3194/**
3195 * @brief Connect the node into the siblings list and check its name uniqueness.
3196 *
3197 * @param[in] ctx Compile context
3198 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
3199 * the choice itself is expected instead of a specific case node.
3200 * @param[in] node Schema node to connect into the list.
3201 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
3202 */
3203static LY_ERR
3204lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
3205{
3206 struct lysc_node **children;
3207
3208 if (node->nodetype == LYS_CASE) {
3209 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
3210 } else {
3211 children = lysc_node_children_p(parent);
3212 }
3213 if (children) {
3214 if (!(*children)) {
3215 /* first child */
3216 *children = node;
3217 } else if (*children != node) {
3218 /* by the condition in previous branch we cover the choice/case children
3219 * - the children list is shared by the choice and the the first case, in addition
3220 * the first child of each case must be referenced from the case node. So the node is
3221 * actually always already inserted in case it is the first children - so here such
3222 * a situation actually corresponds to the first branch */
3223 /* insert at the end of the parent's children list */
3224 (*children)->prev->next = node;
3225 node->prev = (*children)->prev;
3226 (*children)->prev = node;
3227
3228 /* check the name uniqueness */
3229 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
3230 lysc_node_notifs(parent), node->name, node)) {
3231 return LY_EEXIST;
3232 }
3233 }
3234 }
3235 return LY_SUCCESS;
3236}
3237
3238static struct lysc_node_case*
3239lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node_choice *ch, struct lysc_node *child)
3240{
3241 struct lysc_node *iter;
3242 struct lysc_node_case *cs;
3243 unsigned int u;
3244 LY_ERR ret;
3245
3246#define UNIQUE_CHECK(NAME) \
3247 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
3248 if (!strcmp(iter->name, NAME)) { \
3249 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
3250 return NULL; \
3251 } \
3252 }
3253
3254 if (node_p->nodetype == LYS_CHOICE) {
3255 UNIQUE_CHECK(child->name);
3256
3257 /* we have to add an implicit case node into the parent choice */
3258 cs = calloc(1, sizeof(struct lysc_node_case));
3259 DUP_STRING(ctx->ctx, child->name, cs->name);
3260 cs->flags = ch->flags & LYS_STATUS_MASK;
3261 } else { /* node_p->nodetype == LYS_CASE */
3262 if (ch->cases && (node_p == ch->cases->prev->sp)) {
3263 /* the case is already present since the child is not its first children */
3264 return (struct lysc_node_case*)ch->cases->prev;
3265 }
3266 UNIQUE_CHECK(node_p->name);
3267
3268 /* explicit parent case is not present (this is its first child) */
3269 cs = calloc(1, sizeof(struct lysc_node_case));
3270 DUP_STRING(ctx->ctx, node_p->name, cs->name);
3271 cs->flags = LYS_STATUS_MASK & node_p->flags;
3272 cs->sp = node_p;
3273
Radek Krejcib1b59152019-01-07 13:21:56 +01003274 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
3275 LY_CHECK_RET(lys_compile_status(ctx, (struct lysc_node*)cs, ch->flags), NULL);
Radek Krejci056d0a82018-12-06 16:57:25 +01003276 COMPILE_MEMBER_GOTO(ctx, node_p->when, cs->when, options, lys_compile_when, ret, error);
3277 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, options, u, lys_compile_iffeature, ret, error);
3278 }
3279 cs->module = ctx->mod;
3280 cs->prev = (struct lysc_node*)cs;
3281 cs->nodetype = LYS_CASE;
3282 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
3283 cs->parent = (struct lysc_node*)ch;
3284 cs->child = child;
3285
3286 return cs;
3287error:
3288 return NULL;
3289
3290#undef UNIQUE_CHECK
3291}
3292
Radek Krejci76b3e962018-12-14 17:01:25 +01003293static LY_ERR
3294lys_compile_refine_config(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_refine *rfn, int inheriting)
3295{
3296 struct lysc_node *child;
3297 uint16_t config = rfn->flags & LYS_CONFIG_MASK;
3298
3299 if (config == (node->flags & LYS_CONFIG_MASK)) {
3300 /* nothing to do */
3301 return LY_SUCCESS;
3302 }
3303
3304 if (!inheriting) {
3305 /* explicit refine */
3306 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
3307 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcib1b59152019-01-07 13:21:56 +01003308 "Invalid refine of config in \"%s\" - configuration node cannot be child of any state data node.",
3309 rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003310 return LY_EVALID;
3311 }
3312 }
3313 node->flags &= ~LYS_CONFIG_MASK;
3314 node->flags |= config;
3315
3316 /* inherit the change into the children */
3317 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node), child) {
3318 LY_CHECK_RET(lys_compile_refine_config(ctx, child, rfn, 1));
3319 }
3320
3321 /* TODO actions and notifications */
3322
3323 return LY_SUCCESS;
3324}
3325
Radek Krejci056d0a82018-12-06 16:57:25 +01003326/**
Radek Krejcie86bf772018-12-14 11:39:53 +01003327 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3328 * If present, also apply uses's modificators.
3329 *
3330 * @param[in] ctx Compile context
3331 * @param[in] uses_p Parsed uses schema node.
3332 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3333 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3334 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3335 * the compile context.
3336 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3337 */
3338static LY_ERR
3339lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, int options, struct lysc_node *parent)
3340{
3341 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01003342 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01003343 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01003344 struct lysc_node_container context_node_fake =
3345 {.nodetype = LYS_CONTAINER,
3346 .module = ctx->mod,
3347 .flags = parent ? parent->flags : 0,
3348 .child = NULL, .next = NULL,
3349 .prev = (struct lysc_node*)&context_node_fake};
Radek Krejcie86bf772018-12-14 11:39:53 +01003350 const struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01003351 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01003352 int found;
3353 const char *id, *name, *prefix;
3354 size_t prefix_len, name_len;
3355 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01003356 struct lysp_refine *rfn;
Radek Krejcie86bf772018-12-14 11:39:53 +01003357 LY_ERR ret = LY_EVALID;
Radek Krejcif2271f12019-01-07 16:42:23 +01003358 uint32_t min, max;
3359 struct ly_set refined = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01003360
3361 /* search for the grouping definition */
3362 found = 0;
3363 id = uses_p->name;
3364 lys_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
3365 if (prefix) {
3366 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
3367 if (!mod) {
3368 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3369 "Invalid prefix used for grouping reference (%s).", uses_p->name);
3370 return LY_EVALID;
3371 }
3372 } else {
3373 mod = ctx->mod_def;
3374 }
3375 if (mod == ctx->mod_def) {
3376 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
3377 grp = lysp_node_groupings(node_p);
3378 LY_ARRAY_FOR(grp, u) {
3379 if (!strcmp(grp[u].name, name)) {
3380 grp = &grp[u];
3381 found = 1;
3382 break;
3383 }
3384 }
3385 }
3386 }
3387 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003388 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01003389 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01003390 if (grp) {
3391 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
3392 if (!strcmp(grp[u].name, name)) {
3393 grp = &grp[u];
3394 found = 1;
3395 }
3396 }
3397 }
3398 if (!found && mod->parsed->includes) {
3399 /* ... and all the submodules */
3400 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
3401 grp = mod->parsed->includes[u].submodule->groupings;
3402 if (grp) {
3403 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
3404 if (!strcmp(grp[v].name, name)) {
3405 grp = &grp[v];
3406 found = 1;
3407 }
3408 }
3409 }
Radek Krejcie86bf772018-12-14 11:39:53 +01003410 }
3411 }
3412 }
3413 if (!found) {
3414 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3415 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3416 return LY_EVALID;
3417 }
3418
3419 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3420 grp_stack_count = ctx->groupings.count;
3421 ly_set_add(&ctx->groupings, (void*)grp, 0);
3422 if (grp_stack_count == ctx->groupings.count) {
3423 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
3424 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3425 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3426 return LY_EVALID;
3427 }
3428
3429 /* switch context's mod_def */
3430 mod_old = ctx->mod_def;
3431 ctx->mod_def = mod;
3432
3433 /* check status */
3434 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), error);
3435
Radek Krejcie86bf772018-12-14 11:39:53 +01003436 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003437 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
3438 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, options, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), error);
Radek Krejci01342af2019-01-03 15:18:08 +01003439 child = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev;
Radek Krejci01342af2019-01-03 15:18:08 +01003440 if (uses_p->refines) {
3441 /* some preparation for applying refines */
3442 if (grp->data == node_p) {
3443 /* remember the first child */
3444 context_node_fake.child = child;
3445 }
3446 }
3447 }
3448 LY_LIST_FOR(context_node_fake.child, child) {
3449 child->parent = (struct lysc_node*)&context_node_fake;
3450 }
3451 if (context_node_fake.child) {
3452 child = context_node_fake.child->prev;
3453 context_node_fake.child->prev = parent ? lysc_node_children(parent)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01003454 }
3455
Radek Krejci12fb9142019-01-08 09:45:30 +01003456 /* TODO: apply augment */
3457
Radek Krejcif0089082019-01-07 16:42:01 +01003458 /* reload previous context's mod_def */
3459 ctx->mod_def = mod_old;
3460
Radek Krejci76b3e962018-12-14 17:01:25 +01003461 /* apply refine */
3462 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci01342af2019-01-03 15:18:08 +01003463 LY_CHECK_GOTO(lys_resolve_descendant_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, 0, (const struct lysc_node**)&node),
3464 error);
Radek Krejcif2271f12019-01-07 16:42:23 +01003465 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01003466
3467 /* default value */
3468 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01003469 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003470 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3471 "Invalid refine of default in \"%s\" - %s cannot hold %d default values.",
3472 rfn->nodeid, lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
3473 goto error;
3474 }
3475 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
3476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3477 "Invalid refine of default in \"%s\" - %s cannot hold default value(s).",
3478 rfn->nodeid, lys_nodetype2str(node->nodetype));
3479 goto error;
3480 }
3481 if (node->nodetype == LYS_LEAF) {
3482 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
3483 DUP_STRING(ctx->ctx, rfn->dflts[0], ((struct lysc_node_leaf*)node)->dflt);
Radek Krejci01342af2019-01-03 15:18:08 +01003484 node->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003485 /* TODO check the default value according to type */
3486 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003487 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01003488 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3489 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
3490 goto error;
3491 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003492 LY_ARRAY_FOR(((struct lysc_node_leaflist*)node)->dflts, u) {
3493 lydict_remove(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts[u]);
3494 }
3495 LY_ARRAY_FREE(((struct lysc_node_leaflist*)node)->dflts);
Radek Krejci01342af2019-01-03 15:18:08 +01003496 ((struct lysc_node_leaflist*)node)->dflts = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01003497 LY_ARRAY_CREATE_GOTO(ctx->ctx, ((struct lysc_node_leaflist*)node)->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, error);
3498 LY_ARRAY_FOR(rfn->dflts, u) {
3499 LY_ARRAY_INCREMENT(((struct lysc_node_leaflist*)node)->dflts);
3500 DUP_STRING(ctx->ctx, rfn->dflts[u], ((struct lysc_node_leaflist*)node)->dflts[u]);
3501 }
3502 /* TODO check the default values according to type */
3503 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01003504 if (((struct lysc_node_choice*)node)->dflt) {
3505 /* unset LYS_SET_DFLT from the current default case */
3506 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
3507 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003508 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), error);
3509 }
3510 }
3511
Radek Krejci12fb9142019-01-08 09:45:30 +01003512 /* description */
3513 if (rfn->dsc) {
3514 FREE_STRING(ctx->ctx, node->dsc);
3515 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
3516 }
3517
3518 /* reference */
3519 if (rfn->ref) {
3520 FREE_STRING(ctx->ctx, node->ref);
3521 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
3522 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003523
3524 /* config */
3525 if (rfn->flags & LYS_CONFIG_MASK) {
3526 LY_CHECK_GOTO(lys_compile_refine_config(ctx, node, rfn, 0), error);
3527 }
3528
3529 /* mandatory */
3530 if (rfn->flags & LYS_MAND_MASK) {
3531 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
3532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3533 "Invalid refine of mandatory in \"%s\" - %s cannot hold mandatory statement.",
3534 rfn->nodeid, lys_nodetype2str(node->nodetype));
3535 goto error;
3536 }
3537 /* in compiled flags, only the LYS_MAND_TRUE is present */
3538 if (rfn->flags & LYS_MAND_TRUE) {
3539 /* check if node has default value */
3540 if (node->nodetype & LYS_LEAF) {
3541 if (node->flags & LYS_SET_DFLT) {
3542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci01342af2019-01-03 15:18:08 +01003543 "Invalid refine of mandatory in \"%s\" - leaf already has \"default\" statement.", rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003544 goto error;
3545 } else {
3546 /* remove the default value taken from the leaf's type */
3547 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->dflt);
3548 ((struct lysc_node_leaf*)node)->dflt = NULL;
3549 }
3550 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
3551 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci01342af2019-01-03 15:18:08 +01003552 "Invalid refine of mandatory in \"%s\" - choice already has \"default\" statement.", rfn->nodeid);
Radek Krejci76b3e962018-12-14 17:01:25 +01003553 goto error;
3554 }
3555 if (node->parent && (node->parent->flags & LYS_SET_DFLT)) {
3556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3557 "Invalid refine of mandatory in \"%s\" - %s under the default case.",
3558 rfn->nodeid, lys_nodetype2str(node->nodetype));
3559 goto error;
3560 }
3561
3562 node->flags |= LYS_MAND_TRUE;
3563 } else {
Radek Krejci01342af2019-01-03 15:18:08 +01003564 /* make mandatory false */
Radek Krejci76b3e962018-12-14 17:01:25 +01003565 node->flags &= ~LYS_MAND_TRUE;
Radek Krejci01342af2019-01-03 15:18:08 +01003566 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt) {
3567 /* get the type's default value if any */
3568 DUP_STRING(ctx->ctx, ((struct lysc_node_leaf*)node)->type->dflt, ((struct lysc_node_leaf*)node)->dflt);
3569 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003570 }
3571 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01003572
3573 /* presence */
3574 if (rfn->presence) {
3575 if (node->nodetype != LYS_CONTAINER) {
3576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3577 "Invalid refine of presence statement in \"%s\" - %s cannot hold the presence statement.",
3578 rfn->nodeid, lys_nodetype2str(node->nodetype));
3579 goto error;
3580 }
3581 node->flags |= LYS_PRESENCE;
3582 }
Radek Krejci9a564c92019-01-07 14:53:57 +01003583
3584 /* must */
3585 if (rfn->musts) {
3586 switch (node->nodetype) {
3587 case LYS_LEAF:
3588 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, options, u, lys_compile_must, ret, error);
3589 break;
3590 case LYS_LEAFLIST:
3591 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, options, u, lys_compile_must, ret, error);
3592 break;
3593 case LYS_LIST:
3594 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, options, u, lys_compile_must, ret, error);
3595 break;
3596 case LYS_CONTAINER:
3597 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, options, u, lys_compile_must, ret, error);
3598 break;
3599 case LYS_ANYXML:
3600 case LYS_ANYDATA:
3601 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, options, u, lys_compile_must, ret, error);
3602 break;
3603 default:
3604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3605 "Invalid refine of must statement in \"%s\" - %s cannot hold any must statement.",
3606 rfn->nodeid, lys_nodetype2str(node->nodetype));
3607 goto error;
3608 }
3609 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01003610
3611 /* min/max-elements */
3612 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
3613 switch (node->nodetype) {
3614 case LYS_LEAFLIST:
3615 if (rfn->flags & LYS_SET_MAX) {
3616 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
3617 }
3618 if (rfn->flags & LYS_SET_MIN) {
3619 ((struct lysc_node_leaflist*)node)->min = rfn->min;
3620 }
3621 break;
3622 case LYS_LIST:
3623 if (rfn->flags & LYS_SET_MAX) {
3624 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
3625 }
3626 if (rfn->flags & LYS_SET_MIN) {
3627 ((struct lysc_node_list*)node)->min = rfn->min;
3628 }
3629 break;
3630 default:
3631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3632 "Invalid refine of %s statement in \"%s\" - %s cannot hold this statement.",
3633 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid, lys_nodetype2str(node->nodetype));
3634 goto error;
3635 }
3636 }
Radek Krejcif0089082019-01-07 16:42:01 +01003637
3638 /* if-feature */
3639 if (rfn->iffeatures) {
3640 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
3641 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
3642 }
Radek Krejci01342af2019-01-03 15:18:08 +01003643 }
3644 /* fix connection of the children nodes from fake context node back into the parent */
3645 if (context_node_fake.child) {
3646 context_node_fake.child->prev = child;
3647 }
3648 LY_LIST_FOR(context_node_fake.child, child) {
3649 child->parent = parent;
Radek Krejcie86bf772018-12-14 11:39:53 +01003650 }
3651
Radek Krejcif2271f12019-01-07 16:42:23 +01003652 /* do some additional checks of the changed nodes when all the refines are applied */
3653 for (u = 0; u < refined.count; ++u) {
3654 node = (struct lysc_node*)refined.objs[u];
3655 rfn = &uses_p->refines[u];
3656
3657 /* check possible conflict with default value (default added, mandatory left true) */
3658 if ((node->flags & LYS_MAND_TRUE) &&
3659 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
3660 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
3661 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3662 "Invalid refine of default in \"%s\" - the node is mandatory.", rfn->nodeid);
3663 goto error;
3664 }
3665
3666 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
3667 if (node->nodetype == LYS_LIST) {
3668 min = ((struct lysc_node_list*)node)->min;
3669 max = ((struct lysc_node_list*)node)->max;
3670 } else {
3671 min = ((struct lysc_node_leaflist*)node)->min;
3672 max = ((struct lysc_node_leaflist*)node)->max;
3673 }
3674 if (min > max) {
3675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3676 "Invalid refine of %s statement in \"%s\" - \"min-elements\" is bigger than \"max-elements\".",
3677 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", rfn->nodeid);
3678 goto error;
3679 }
3680 }
3681 }
3682
Radek Krejcie86bf772018-12-14 11:39:53 +01003683 ret = LY_SUCCESS;
3684error:
3685 /* reload previous context's mod_def */
3686 ctx->mod_def = mod_old;
3687 /* remove the grouping from the stack for circular groupings dependency check */
3688 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3689 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01003690 ly_set_erase(&refined, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01003691
3692 return ret;
3693}
3694
3695/**
Radek Krejcia3045382018-11-22 14:30:31 +01003696 * @brief Compile parsed schema node information.
3697 * @param[in] ctx Compile context
3698 * @param[in] node_p Parsed schema node.
3699 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3700 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
3701 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3702 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01003703 * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags).
3704 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01003705 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3706 */
Radek Krejci19a96102018-11-15 13:38:09 +01003707static LY_ERR
Radek Krejcib1b59152019-01-07 13:21:56 +01003708lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent, uint16_t uses_status)
Radek Krejci19a96102018-11-15 13:38:09 +01003709{
3710 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01003711 struct lysc_node *node;
3712 struct lysc_node_case *cs;
Radek Krejci19a96102018-11-15 13:38:09 +01003713 unsigned int u;
3714 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
3715
3716 switch (node_p->nodetype) {
3717 case LYS_CONTAINER:
3718 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
3719 node_compile_spec = lys_compile_node_container;
3720 break;
3721 case LYS_LEAF:
3722 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
3723 node_compile_spec = lys_compile_node_leaf;
3724 break;
3725 case LYS_LIST:
3726 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003727 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01003728 break;
3729 case LYS_LEAFLIST:
3730 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01003731 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01003732 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003733 case LYS_CHOICE:
3734 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01003735 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01003736 break;
Radek Krejci19a96102018-11-15 13:38:09 +01003737 case LYS_ANYXML:
3738 case LYS_ANYDATA:
3739 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01003740 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01003741 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01003742 case LYS_USES:
3743 return lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, options, parent);
Radek Krejci19a96102018-11-15 13:38:09 +01003744 default:
3745 LOGINT(ctx->ctx);
3746 return LY_EINT;
3747 }
3748 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3749 node->nodetype = node_p->nodetype;
3750 node->module = ctx->mod;
3751 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003752 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01003753
3754 /* config */
3755 if (!(node->flags & LYS_CONFIG_MASK)) {
3756 /* config not explicitely set, inherit it from parent */
3757 if (parent) {
3758 node->flags |= parent->flags & LYS_CONFIG_MASK;
3759 } else {
3760 /* default is config true */
3761 node->flags |= LYS_CONFIG_W;
3762 }
3763 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003764 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
3765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3766 "Configuration node cannot be child of any state data node.");
3767 goto error;
3768 }
Radek Krejci19a96102018-11-15 13:38:09 +01003769
Radek Krejcia6d57732018-11-29 13:40:37 +01003770 /* *list ordering */
3771 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
3772 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
3773 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing state data, "
3774 "RPC/action output parameters or notification content (%s).", ctx->path);
3775 node->flags &= ~LYS_ORDBY_MASK;
3776 node->flags |= LYS_ORDBY_SYSTEM;
3777 } else if (!(node->flags & LYS_ORDBY_MASK)) {
3778 /* default ordering is system */
3779 node->flags |= LYS_ORDBY_SYSTEM;
3780 }
3781 }
3782
Radek Krejci19a96102018-11-15 13:38:09 +01003783 /* status - it is not inherited by specification, but it does not make sense to have
3784 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01003785 if (!parent || parent->nodetype != LYS_CHOICE) {
3786 /* in case of choice/case's children, postpone the check to the moment we know if
3787 * the parent is choice (parent here) or some case (so we have to get its flags to check) */
Radek Krejcib1b59152019-01-07 13:21:56 +01003788 LY_CHECK_GOTO(lys_compile_status(ctx, node, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
Radek Krejci19a96102018-11-15 13:38:09 +01003789 }
3790
3791 if (!(options & LYSC_OPT_FREE_SP)) {
3792 node->sp = node_p;
3793 }
3794 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01003795 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
3796 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci9800fb82018-12-13 14:26:23 +01003797 COMPILE_MEMBER_GOTO(ctx, node_p->when, node->when, options, lys_compile_when, ret, error);
3798 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, options, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01003799 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
3800
3801 /* nodetype-specific part */
3802 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
3803
3804 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01003805 if (parent) {
3806 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01003807 cs = lys_compile_node_case(ctx, node_p->parent, options, (struct lysc_node_choice*)parent, node);
3808 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01003809 if (uses_status) {
3810
3811 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003812 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01003813 * it directly gets the same status flags as the choice;
3814 * uses_status cannot be applied here since uses cannot be child statement of choice */
3815 LY_CHECK_GOTO(lys_compile_status(ctx, node, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01003816 node->parent = (struct lysc_node*)cs;
3817 } else { /* other than choice */
3818 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01003819 }
Radek Krejci056d0a82018-12-06 16:57:25 +01003820 LY_CHECK_RET(lys_compile_node_connect(ctx, parent, node), LY_EVALID);
Radek Krejci19a96102018-11-15 13:38:09 +01003821 } else {
3822 /* top-level element */
3823 if (!ctx->mod->compiled->data) {
3824 ctx->mod->compiled->data = node;
3825 } else {
3826 /* insert at the end of the module's top-level nodes list */
3827 ctx->mod->compiled->data->prev->next = node;
3828 node->prev = ctx->mod->compiled->data->prev;
3829 ctx->mod->compiled->data->prev = node;
3830 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003831 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
3832 ctx->mod->compiled->notifs, node->name, node)) {
3833 return LY_EVALID;
3834 }
Radek Krejci19a96102018-11-15 13:38:09 +01003835 }
3836
3837 return LY_SUCCESS;
3838
3839error:
3840 lysc_node_free(ctx->ctx, node);
3841 return ret;
3842}
3843
Radek Krejcia3045382018-11-22 14:30:31 +01003844/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01003845 * @brief Compile the given YANG submodule into the main module.
3846 * @param[in] ctx Compile context
3847 * @param[in] inc Include structure from the main module defining the submodule.
3848 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3849 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3850 */
3851LY_ERR
3852lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc, int options)
3853{
3854 unsigned int u;
3855 LY_ERR ret = LY_SUCCESS;
3856 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003857 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01003858 struct lysc_module *mainmod = ctx->mod->compiled;
3859
3860 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->features, mainmod->features, options, u, lys_compile_feature, ret, error);
3861 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, options, u, lys_compile_identity, ret, error);
3862
3863error:
3864 return ret;
3865}
3866
3867/**
Radek Krejcia3045382018-11-22 14:30:31 +01003868 * @brief Compile the given YANG module.
3869 * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed.
3870 * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags).
3871 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3872 */
Radek Krejci19a96102018-11-15 13:38:09 +01003873LY_ERR
3874lys_compile(struct lys_module *mod, int options)
3875{
3876 struct lysc_ctx ctx = {0};
3877 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003878 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01003879 struct lysp_module *sp;
3880 struct lysp_node *node_p;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003881 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01003882 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01003883
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003884 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci19a96102018-11-15 13:38:09 +01003885 sp = mod->parsed;
3886
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003887 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01003888 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01003889 ctx.mod_def = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01003890
3891 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003892 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
3893 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01003894
Radek Krejci19a96102018-11-15 13:38:09 +01003895 if (sp->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003896 DUP_STRING(mod->ctx, sp->revs[0].date, mod_c->revision);
Radek Krejci19a96102018-11-15 13:38:09 +01003897 }
3898 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01003899 LY_ARRAY_FOR(sp->includes, u) {
3900 ret = lys_compile_submodule(&ctx, &sp->includes[u], options);
3901 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
3902 }
3903 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
3904 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01003905 if (sp->identities) {
3906 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
3907 }
3908
3909 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01003910 ret = lys_compile_node(&ctx, node_p, options, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01003911 LY_CHECK_GOTO(ret, error);
3912 }
3913 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
3914 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
3915
3916 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
3917
Radek Krejcia3045382018-11-22 14:30:31 +01003918 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01003919 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
3920 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
3921 * 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 +01003922 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003923 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01003924 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
3925 if (type->basetype == LY_TYPE_LEAFREF) {
3926 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01003927 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 +01003928 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003929 } else if (type->basetype == LY_TYPE_UNION) {
3930 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3931 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3932 /* validate the path */
3933 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
3934 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
3935 LY_CHECK_GOTO(ret, error);
3936 }
3937 }
Radek Krejcia3045382018-11-22 14:30:31 +01003938 }
3939 }
3940 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003941 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01003942 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01003943 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
3944 if (type->basetype == LY_TYPE_LEAFREF) {
3945 /* store pointer to the real type */
3946 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
3947 typeiter->basetype == LY_TYPE_LEAFREF;
3948 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3949 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003950 } else if (type->basetype == LY_TYPE_UNION) {
3951 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
3952 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
3953 /* store pointer to the real type */
3954 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
3955 typeiter->basetype == LY_TYPE_LEAFREF;
3956 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
3957 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
3958 }
3959 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01003960 }
3961 }
3962 }
Radek Krejcia3045382018-11-22 14:30:31 +01003963 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01003964 ly_set_erase(&ctx.groupings, NULL);
Radek Krejcia3045382018-11-22 14:30:31 +01003965
Radek Krejci19a96102018-11-15 13:38:09 +01003966 if (options & LYSC_OPT_FREE_SP) {
3967 lysp_module_free(mod->parsed);
3968 ((struct lys_module*)mod)->parsed = NULL;
3969 }
3970
3971 ((struct lys_module*)mod)->compiled = mod_c;
3972 return LY_SUCCESS;
3973
3974error:
Radek Krejcia3045382018-11-22 14:30:31 +01003975 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01003976 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01003977 lysc_module_free(mod_c, NULL);
3978 ((struct lys_module*)mod)->compiled = NULL;
3979 return ret;
3980}